1 ;;; nnultimate.el --- interfacing with the Ultimate Bulletin Board system -*- coding: iso-latin-1 -*-
2 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Note: You need to have `url' and `w3' installed for this
31 (eval-when-compile (require 'cl
))
42 ;; Report failure to find w3 at load time if appropriate.
43 (eval '(require 'nnweb
))
45 (nnoo-declare nnultimate
)
47 (defvoo nnultimate-directory
(nnheader-concat gnus-directory
"ultimate/")
48 "Where nnultimate will save its files.")
50 (defvoo nnultimate-address
""
51 "The address of the Ultimate bulletin board.")
53 ;;; Internal variables
55 (defvar nnultimate-groups-alist nil
)
56 (defvoo nnultimate-groups nil
)
57 (defvoo nnultimate-headers nil
)
58 (defvoo nnultimate-articles nil
)
60 ;;; Interface functions
62 (nnoo-define-basics nnultimate
)
64 (deffoo nnultimate-retrieve-headers
(articles &optional group server fetch-old
)
65 (nnultimate-possibly-change-server group server
)
66 (unless gnus-nov-is-evil
67 (let* ((last (car (last articles
)))
70 (entry (assoc group nnultimate-groups
))
72 (topics (nth 4 entry
))
73 (mapping (nth 5 entry
))
74 (old-total (or (nth 6 entry
) 1))
75 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
76 (furls (list (concat nnultimate-address
(format furl sid
))))
77 headers article subject score from date lines parent point
78 contents tinfo fetchers map elem a href garticles topic old-max
79 inc datel table string current-page total-contents pages
80 farticles forum-contents parse furl-fetched mmap farticle
)
82 (while (and (setq article
(car articles
))
85 (or (> article
(caar map
))
86 (< (cadar map
) (caar map
))))
88 (when (setq mmap
(car map
))
91 (<= article
(nth 1 mmap
)))
92 ;; Do we already have a fetcher for this topic?
93 (if (setq elem
(assq (nth 2 mmap
) fetchers
))
94 ;; Yes, so we just add the spec to the end.
95 (nconc elem
(list (cons article
96 (+ (nth 3 mmap
) (incf farticle
)))))
97 ;; No, so we add a new one.
98 (push (list (nth 2 mmap
)
100 (+ (nth 3 mmap
) (incf farticle
))))
103 (setq article
(car articles
)))))
104 ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
105 ;; so we start fetching the topics that we need to satisfy the
109 (set-buffer nntp-server-buffer
)
111 (setq nnultimate-articles nil
)
112 (mm-with-unibyte-buffer
113 (dolist (elem fetchers
)
117 (while (<= current-page pages
)
119 (setq subject
(nth 2 (assq (car elem
) topics
)))
120 (setq href
(nth 3 (assq (car elem
) topics
)))
121 (if (= current-page
1)
123 (string-match "\\.html$" href
)
124 (nnweb-insert (concat (substring href
0 (match-beginning 0))
125 "-" (number-to-string current-page
)
126 (match-string 0 href
))))
127 (goto-char (point-min))
129 (ignore-errors (w3-parse-buffer (current-buffer))))
130 (setq table
(nnultimate-find-forum-table contents
))
131 (setq string
(mapconcat 'identity
(nnweb-text table
) ""))
132 (when (string-match "topic is \\([0-9]\\) pages" string
)
133 (setq pages
(string-to-number (match-string 1 string
)))
135 (setq table
(nnultimate-find-forum-table contents
)))
136 (setq contents
(cdr (nth 2 (car (nth 2 table
)))))
137 (setq total-contents
(nconc total-contents contents
))
139 ;;(setq total-contents (nreverse total-contents))
140 (dolist (art (cdr elem
))
141 (if (not (nth (1- (cdr art
)) total-contents
))
143 (push (list (car art
)
144 (nth (1- (cdr art
)) total-contents
)
146 nnultimate-articles
)))))
147 (setq nnultimate-articles
148 (sort nnultimate-articles
'car-less-than-car
))
149 ;; Now we have all the articles, conveniently in an alist
150 ;; where the key is the Gnus article number.
151 (dolist (articlef nnultimate-articles
)
152 (setq article
(nth 0 articlef
)
153 contents
(nth 1 articlef
)
154 subject
(nth 2 articlef
))
155 (setq from
(mapconcat 'identity
156 (nnweb-text (car (nth 2 contents
)))
158 datel
(nnweb-text (nth 2 (car (cdr (nth 2 contents
))))))
160 (when (string-match "Posted" (car datel
))
161 (setq date
(substring (car datel
) (match-end 0))
164 (setq date
(delete "" (split-string date
"[- \n\t\r ]")))
165 (if (or (member "AM" date
)
167 (setq date
(format "%s %s %s %s"
168 (car (rassq (string-to-number (nth 0 date
))
170 (nth 1 date
) (nth 2 date
) (nth 3 date
)))
171 (setq date
(format "%s %s %s %s"
172 (car (rassq (string-to-number (nth 1 date
))
174 (nth 0 date
) (nth 2 date
) (nth 3 date
))))
178 (make-full-mail-header
181 (concat "<" (number-to-string sid
) "%"
182 (number-to-string article
)
185 (/ (length (mapconcat
188 (cdr (nth 2 (nth 1 (nth 2 contents
)))))
193 (setq nnultimate-headers
(sort headers
'car-less-than-car
))
195 (set-buffer nntp-server-buffer
)
196 (mm-with-unibyte-current-buffer
198 (dolist (header nnultimate-headers
)
199 (nnheader-insert-nov (cdr header
))))))
202 (deffoo nnultimate-request-group
(group &optional server dont-check
)
203 (nnultimate-possibly-change-server nil server
)
204 (when (not nnultimate-groups
)
205 (nnultimate-request-list))
207 (nnultimate-create-mapping group
))
208 (let ((elem (assoc group nnultimate-groups
)))
211 (nnheader-report 'nnultimate
"Group does not exist"))
213 (nnheader-report 'nnultimate
"Opened group %s" group
)
215 "211 %d %d %d %s\n" (cadr elem
) 1 (cadr elem
)
216 (prin1-to-string group
))))))
218 (deffoo nnultimate-request-close
()
219 (setq nnultimate-groups-alist nil
220 nnultimate-groups nil
))
222 (deffoo nnultimate-request-article
(article &optional group server buffer
)
223 (nnultimate-possibly-change-server group server
)
224 (let ((contents (cdr (assq article nnultimate-articles
))))
225 (setq contents
(cddr (nth 2 (nth 1 (nth 2 (car contents
))))))
228 (set-buffer (or buffer nntp-server-buffer
))
230 (nnweb-insert-html (cons 'p
(cons nil
(list contents
))))
231 (goto-char (point-min))
232 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
233 (let ((header (cdr (assq article nnultimate-headers
))))
234 (mm-with-unibyte-current-buffer
235 (nnheader-insert-header header
)))
236 (nnheader-report 'nnultimate
"Fetched article %s" article
)
237 (cons group article
)))))
239 (deffoo nnultimate-request-list
(&optional server
)
240 (nnultimate-possibly-change-server nil server
)
241 (mm-with-unibyte-buffer
243 (if (string-match "/$" nnultimate-address
)
244 (concat nnultimate-address
"Ultimate.cgi")
246 (let ((contents (nth 2 (car (nth 2
247 (nnultimate-find-forum-table
248 (w3-parse-buffer (current-buffer)))))))
249 sid elem description articles a href group forum
251 (dolist (row contents
)
252 (setq row
(nth 2 row
))
253 (when (setq a
(nnweb-parse-find 'a row
))
254 (setq group
(car (last (nnweb-text a
)))
255 href
(cdr (assq 'href
(nth 1 a
))))
256 (setq description
(car (last (nnweb-text (nth 1 row
)))))
257 (setq a1
(car (last (nnweb-text (nth 2 row
)))))
258 (setq a2
(car (last (nnweb-text (nth 3 row
)))))
259 (when (string-match "^[0-9]+$" a1
)
260 (setq articles
(string-to-number a1
)))
261 (when (and a2
(string-match "^[0-9]+$" a2
))
262 (setq articles
(max articles
(string-to-number a2
))))
264 (string-match "number=\\([0-9]+\\)" href
)
265 (setq forum
(string-to-number (match-string 1 href
)))
266 (if (setq elem
(assoc group nnultimate-groups
))
267 (setcar (cdr elem
) articles
)
268 (push (list group articles forum description nil nil nil nil
)
269 nnultimate-groups
))))))
270 (nnultimate-write-groups)
271 (nnultimate-generate-active)
274 (deffoo nnultimate-request-newgroups
(date &optional server
)
275 (nnultimate-possibly-change-server nil server
)
276 (nnultimate-generate-active)
279 (nnoo-define-skeleton nnultimate
)
281 ;;; Internal functions
283 (defun nnultimate-prune-days (group time
)
284 "Compute the number of days to fetch info for."
285 (let ((old-time (nth 7 (assoc group nnultimate-groups
))))
288 (- (time-to-days time
) (time-to-days old-time
)))))
290 (defun nnultimate-create-mapping (group)
291 (let* ((entry (assoc group nnultimate-groups
))
293 (topics (nth 4 entry
))
294 (mapping (nth 5 entry
))
295 (old-total (or (nth 6 entry
) 1))
296 (current-time (current-time))
298 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
300 (nnultimate-prune-days group current-time
))))
301 (furls (list (concat nnultimate-address
(format furl sid
))))
302 contents forum-contents furl-fetched a subject href
303 garticles topic tinfo old-max inc parse
)
304 (mm-with-unibyte-buffer
307 (nnweb-insert (pop furls
))
308 (goto-char (point-min))
309 (setq parse
(w3-parse-buffer (current-buffer)))
311 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
313 (setq forum-contents
(nconc contents forum-contents
))
315 (setq furl-fetched t
)
316 ;; On the first time through this loop, we find all the
318 (dolist (a (nnweb-parse-find-all 'a parse
))
319 (let ((href (cdr (assq 'href
(nth 1 a
)))))
321 (string-match "forumdisplay.*startpoint" href
))
323 (setq furls
(nreverse furls
))))
324 ;; The main idea here is to map Gnus article numbers to
325 ;; nnultimate article numbers. Say there are three topics in
326 ;; this forum, the first with 4 articles, the seconds with 2,
327 ;; and the third with 1. Then this will translate into 7 Gnus
328 ;; article numbers, where 1-4 comes from the first topic, 5-6
329 ;; from the second and 7 from the third. Now, then next time
330 ;; the group is entered, there's 2 new articles in topic one
331 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
332 ;; in topic one and 10 will be the 2 in topic three.
333 (dolist (row (reverse forum-contents
))
334 (setq row
(nth 2 row
))
335 (when (setq a
(nnweb-parse-find 'a row
))
336 (setq subject
(car (last (nnweb-text a
)))
337 href
(cdr (assq 'href
(nth 1 a
))))
338 (let ((artlist (nreverse (nnweb-text row
)))
340 (while (and (not art
)
342 (when (string-match "^[0-9]+$" (car artlist
))
343 (setq art
(1+ (string-to-number (car artlist
)))))
345 (setq garticles art
))
347 (string-match "/\\([0-9]+\\).html" href
)
348 (setq topic
(string-to-number (match-string 1 href
)))
349 (if (setq tinfo
(assq topic topics
))
351 (setq old-max
(cadr tinfo
))
352 (setcar (cdr tinfo
) garticles
))
354 (push (list topic garticles subject href
) topics
)
355 (setcar (nthcdr 4 entry
) topics
))
356 (when (not (= old-max garticles
))
357 (setq inc
(- garticles old-max
))
358 (setq mapping
(nconc mapping
361 old-total
(1- (incf old-total inc
))
362 topic
(1+ old-max
)))))
364 (setcar (nthcdr 5 entry
) mapping
)
365 (setcar (nthcdr 6 entry
) old-total
))))))
366 (setcar (nthcdr 7 entry
) current-time
)
367 (setcar (nthcdr 1 entry
) (1- old-total
))
368 (nnultimate-write-groups)
371 (defun nnultimate-possibly-change-server (&optional group server
)
372 (nnultimate-init server
)
374 (not (nnultimate-server-opened server
)))
375 (nnultimate-open-server server
))
376 (unless nnultimate-groups-alist
377 (nnultimate-read-groups)
378 (setq nnultimate-groups
(cdr (assoc nnultimate-address
379 nnultimate-groups-alist
)))))
381 (deffoo nnultimate-open-server
(server &optional defs connectionless
)
382 (nnheader-init-server-buffer)
383 (if (nnultimate-server-opened server
)
385 (unless (assq 'nnultimate-address defs
)
386 (setq defs
(append defs
(list (list 'nnultimate-address server
)))))
387 (nnoo-change-server 'nnultimate server defs
)))
389 (defun nnultimate-read-groups ()
390 (setq nnultimate-groups-alist nil
)
391 (let ((file (expand-file-name "groups" nnultimate-directory
)))
392 (when (file-exists-p file
)
393 (mm-with-unibyte-buffer
394 (insert-file-contents file
)
395 (goto-char (point-min))
396 (setq nnultimate-groups-alist
(read (current-buffer)))))))
398 (defun nnultimate-write-groups ()
399 (setq nnultimate-groups-alist
400 (delq (assoc nnultimate-address nnultimate-groups-alist
)
401 nnultimate-groups-alist
))
402 (push (cons nnultimate-address nnultimate-groups
)
403 nnultimate-groups-alist
)
404 (with-temp-file (expand-file-name "groups" nnultimate-directory
)
405 (prin1 nnultimate-groups-alist
(current-buffer))))
407 (defun nnultimate-init (server)
408 "Initialize buffers and such."
409 (unless (file-exists-p nnultimate-directory
)
410 (gnus-make-directory nnultimate-directory
)))
412 (defun nnultimate-generate-active ()
414 (set-buffer nntp-server-buffer
)
416 (dolist (elem nnultimate-groups
)
417 (insert (prin1-to-string (car elem
))
418 " " (number-to-string (cadr elem
)) " 1 y\n"))))
420 (defun nnultimate-find-forum-table (contents)
422 (nnultimate-find-forum-table-1 contents
)))
424 (defun nnultimate-find-forum-table-1 (contents)
425 (dolist (element contents
)
426 (unless (stringp element
)
427 (when (and (eq (car element
) 'table
)
428 (nnultimate-forum-table-p element
))
429 (throw 'found element
))
430 (when (nth 2 element
)
431 (nnultimate-find-forum-table-1 (nth 2 element
))))))
433 (defun nnultimate-forum-table-p (parse)
434 (when (not (apply 'gnus-or
437 (nnweb-parse-find 'table p
))
439 (let ((href (cdr (assq 'href
(nth 1 (nnweb-parse-find 'a parse
20)))))
441 (when (and href
(string-match
442 "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
446 (provide 'nnultimate
)
449 ;; coding: iso-8859-1
452 ;;; nnultimate.el ends here