Fix my last change.
[gnus] / lisp / nnultimate.el
1 ;;; nnultimate.el --- interfacing with the Ultimate Bulletin Board system
2 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `url' and `w3' installed for this
27 ;; backend to work.
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'nnoo)
34 (require 'message)
35 (require 'gnus-util)
36 (require 'gnus)
37 (require 'nnmail)
38 (require 'mm-util)
39 (eval-when-compile
40   (ignore-errors
41     (require 'nnweb)))
42 ;; Report failure to find w3 at load time if appropriate.
43 (eval '(require 'nnweb))
44
45 (nnoo-declare nnultimate)
46
47 (defvoo nnultimate-directory (nnheader-concat gnus-directory "ultimate/")
48   "Where nnultimate will save its files.")
49
50 (defvoo nnultimate-address ""
51   "The address of the Ultimate bulletin board.")
52
53 ;;; Internal variables
54
55 (defvar nnultimate-groups-alist nil)
56 (defvoo nnultimate-groups nil)
57 (defvoo nnultimate-headers nil)
58 (defvoo nnultimate-articles nil)
59
60 ;;; Interface functions
61
62 (nnoo-define-basics nnultimate)
63
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)))
68            (did nil)
69            (start 1)
70            (entry (assoc group nnultimate-groups))
71            (sid (nth 2 entry))
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)
81       (setq map mapping)
82       (while (and (setq article (car articles))
83                   map)
84         (while (and map
85                     (or (> article (caar map))
86                         (< (cadar map) (caar map))))
87           (pop map))
88         (when (setq mmap (car map))
89           (setq farticle -1)
90           (while (and article
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)
99                           (cons article
100                                 (+ (nth 3 mmap) (incf farticle))))
101                     fetchers))
102             (pop articles)
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
106       ;; request.
107       (if (not fetchers)
108           (save-excursion
109             (set-buffer nntp-server-buffer)
110             (erase-buffer))
111         (setq nnultimate-articles nil)
112         (mm-with-unibyte-buffer
113           (dolist (elem fetchers)
114             (setq pages 1
115                   current-page 1
116                   total-contents nil)
117             (while (<= current-page pages)
118               (erase-buffer)
119               (setq subject (nth 2 (assq (car elem) topics)))
120               (setq href (nth 3 (assq (car elem) topics)))
121               (if (= current-page 1)
122                   (nnweb-insert href)
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))
128               (setq contents
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)))
134                 (setcdr table nil)
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))
138               (incf current-page))
139             ;;(setq total-contents (nreverse total-contents))
140             (dolist (art (cdr elem))
141               (if (not (nth (1- (cdr art)) total-contents))
142                   ()                    ;(debug)
143                 (push (list (car art)
144                             (nth (1- (cdr art)) total-contents)
145                             subject)
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)))
157                                 " ")
158                 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
159           (while datel
160             (when (string-match "Posted" (car datel))
161               (setq date (substring (car datel) (match-end 0))
162                     datel nil))
163             (pop datel))
164           (setq date (delete "" (split-string date "[- \n\t\r    ]")))
165           (if (or (member "AM" date)
166                   (member "PM" date))
167               (setq date (format "%s %s %s %s"
168                                  (car (rassq (string-to-number (nth 0 date))
169                                              parse-time-months))
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))
173                                            parse-time-months))
174                                (nth 0 date) (nth 2 date) (nth 3 date))))
175           (push
176            (cons
177             article
178             (make-full-mail-header
179              article subject
180              from (or date "")
181              (concat "<" (number-to-string sid) "%"
182                      (number-to-string article)
183                      "@ultimate>")
184              "" 0
185              (/ (length (mapconcat
186                          'identity
187                          (nnweb-text
188                           (cdr (nth 2 (nth 1 (nth 2 contents)))))
189                          ""))
190                 70)
191              nil nil))
192            headers))
193         (setq nnultimate-headers (sort headers 'car-less-than-car))
194         (save-excursion
195           (set-buffer nntp-server-buffer)
196           (mm-with-unibyte-current-buffer
197             (erase-buffer)
198             (dolist (header nnultimate-headers)
199               (nnheader-insert-nov (cdr header))))))
200       'nov)))
201
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))
206   (unless dont-check
207     (nnultimate-create-mapping group))
208   (let ((elem (assoc group nnultimate-groups)))
209     (cond
210      ((not elem)
211       (nnheader-report 'nnultimate "Group does not exist"))
212      (t
213       (nnheader-report 'nnultimate "Opened group %s" group)
214       (nnheader-insert
215        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
216        (prin1-to-string group))))))
217
218 (deffoo nnultimate-request-close ()
219   (setq nnultimate-groups-alist nil
220         nnultimate-groups nil))
221
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))))))
226     (when contents
227       (save-excursion
228         (set-buffer (or buffer nntp-server-buffer))
229         (erase-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)))))
238
239 (deffoo nnultimate-request-list (&optional server)
240   (nnultimate-possibly-change-server nil server)
241   (mm-with-unibyte-buffer
242     (nnweb-insert
243      (if (string-match "/$" nnultimate-address)
244          (concat nnultimate-address "Ultimate.cgi")
245        nnultimate-address))
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
250           a1 a2)
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))))
263           (when href
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)
272     t))
273
274 (deffoo nnultimate-request-newgroups (date &optional server)
275   (nnultimate-possibly-change-server nil server)
276   (nnultimate-generate-active)
277   t)
278
279 (nnoo-define-skeleton nnultimate)
280
281 ;;; Internal functions
282
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))))
286     (if (null old-time)
287         1000
288       (- (time-to-days time) (time-to-days old-time)))))
289
290 (defun nnultimate-create-mapping (group)
291   (let* ((entry (assoc group nnultimate-groups))
292          (sid (nth 2 entry))
293          (topics (nth 4 entry))
294          (mapping (nth 5 entry))
295          (old-total (or (nth 6 entry) 1))
296          (current-time (current-time))
297          (furl
298           (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
299                   (number-to-string
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
305       (while furls
306         (erase-buffer)
307         (nnweb-insert (pop furls))
308         (goto-char (point-min))
309         (setq parse (w3-parse-buffer (current-buffer)))
310         (setq contents
311               (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
312                                        parse))))))
313         (setq forum-contents (nconc contents forum-contents))
314         (unless furl-fetched
315           (setq furl-fetched t)
316           ;; On the first time through this loop, we find all the
317           ;; forum URLs.
318           (dolist (a (nnweb-parse-find-all 'a parse))
319             (let ((href (cdr (assq 'href (nth 1 a)))))
320               (when (and href
321                          (string-match "forumdisplay.*startpoint" href))
322                 (push href furls))))
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)))
339                 art)
340             (while (and (not art)
341                         artlist)
342               (when (string-match "^[0-9]+$" (car artlist))
343                 (setq art (1+ (string-to-number (car artlist)))))
344               (pop artlist))
345             (setq garticles art))
346           (when garticles
347             (string-match "/\\([0-9]+\\).html" href)
348             (setq topic (string-to-number (match-string 1 href)))
349             (if (setq tinfo (assq topic topics))
350                 (progn
351                   (setq old-max (cadr tinfo))
352                   (setcar (cdr tinfo) garticles))
353               (setq old-max 0)
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
359                                    (list
360                                     (list
361                                      old-total (1- (incf old-total inc))
362                                      topic (1+ old-max)))))
363               (incf old-max inc)
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)
369     mapping))
370
371 (defun nnultimate-possibly-change-server (&optional group server)
372   (nnultimate-init server)
373   (when (and 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)))))
380
381 (deffoo nnultimate-open-server (server &optional defs connectionless)
382   (nnheader-init-server-buffer)
383   (if (nnultimate-server-opened server)
384       t
385     (unless (assq 'nnultimate-address defs)
386       (setq defs (append defs (list (list 'nnultimate-address server)))))
387     (nnoo-change-server 'nnultimate server defs)))
388
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)))))))
397
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))))
406     
407 (defun nnultimate-init (server)
408   "Initialize buffers and such."
409   (unless (file-exists-p nnultimate-directory)
410     (gnus-make-directory nnultimate-directory)))
411
412 (defun nnultimate-generate-active ()
413   (save-excursion
414     (set-buffer nntp-server-buffer)
415     (erase-buffer)
416     (dolist (elem nnultimate-groups)
417       (insert (prin1-to-string (car elem))
418               " " (number-to-string (cadr elem)) " 1 y\n"))))
419
420 (defun nnultimate-find-forum-table (contents)
421   (catch 'found
422     (nnultimate-find-forum-table-1 contents)))
423
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))))))
432
433 (defun nnultimate-forum-table-p (parse)
434   (when (not (apply 'gnus-or
435                     (mapcar
436                      (lambda (p)
437                        (nnweb-parse-find 'table p))
438                      (nth 2 parse))))
439     (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
440           case-fold-search)
441       (when (and href (string-match
442                        "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
443                        href))
444         t))))
445
446 (provide 'nnultimate)
447
448 ;; Local Variables:
449 ;; coding: iso-8859-1
450 ;; End:
451
452 ;;; nnultimate.el ends here