Synch with `t-gnus-6_14' and Gnus.
[elisp/gnus.git-] / 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 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'nnoo)
35 (require 'message)
36 (require 'gnus-util)
37 (require 'gnus)
38 (require 'nnmail)
39 (require 'mm-util)
40 (eval-when-compile
41   (ignore-errors
42     (require 'nnweb)))
43 ;; Report failure to find w3 at load time if appropriate.
44 (eval '(require 'nnweb))
45
46 (nnoo-declare nnultimate)
47
48 (defvoo nnultimate-directory (nnheader-concat gnus-directory "ultimate/")
49   "Where nnultimate will save its files.")
50
51 (defvoo nnultimate-address ""
52   "The address of the Ultimate bulletin board.")
53
54 ;;; Internal variables
55
56 (defvar nnultimate-groups-alist nil)
57 (defvoo nnultimate-groups nil)
58 (defvoo nnultimate-headers nil)
59 (defvoo nnultimate-articles nil)
60
61 ;;; Interface functions
62
63 (nnoo-define-basics nnultimate)
64
65 (deffoo nnultimate-retrieve-headers (articles &optional group server fetch-old)
66   (nnultimate-possibly-change-server group server)
67   (unless gnus-nov-is-evil
68     (let* ((last (car (last articles)))
69            (did nil)
70            (start 1)
71            (entry (assoc group nnultimate-groups))
72            (sid (nth 2 entry))
73            (topics (nth 4 entry))
74            (mapping (nth 5 entry))
75            (old-total (or (nth 6 entry) 1))
76            (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
77            (furls (list (concat nnultimate-address (format furl sid))))
78            headers article subject score from date lines parent point
79            contents tinfo fetchers map elem a href garticles topic old-max
80            inc datel table string current-page total-contents pages
81            farticles forum-contents parse furl-fetched mmap farticle)
82       (setq map mapping)
83       (while (and (setq article (car articles))
84                   map)
85         (while (and map
86                     (or (> article (caar map))
87                         (< (cadar map) (caar map))))
88           (pop map))
89         (when (setq mmap (car map))
90           (setq farticle -1)
91           (while (and article
92                       (<= article (nth 1 mmap)))
93             ;; Do we already have a fetcher for this topic?
94             (if (setq elem (assq (nth 2 mmap) fetchers))
95                 ;; Yes, so we just add the spec to the end.
96                 (nconc elem (list (cons article
97                                         (+ (nth 3 mmap) (incf farticle)))))
98               ;; No, so we add a new one.
99               (push (list (nth 2 mmap)
100                           (cons article
101                                 (+ (nth 3 mmap) (incf farticle))))
102                     fetchers))
103             (pop articles)
104             (setq article (car articles)))))
105       ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
106       ;; so we start fetching the topics that we need to satisfy the
107       ;; request.
108       (if (not fetchers)
109           (save-excursion
110             (set-buffer nntp-server-buffer)
111             (erase-buffer))
112         (setq nnultimate-articles nil)
113         (mm-with-unibyte-buffer
114           (dolist (elem fetchers)
115             (setq pages 1
116                   current-page 1
117                   total-contents nil)
118             (while (<= current-page pages)
119               (erase-buffer)
120               (setq subject (nth 2 (assq (car elem) topics)))
121               (setq href (nth 3 (assq (car elem) topics)))
122               (if (= current-page 1)
123                   (nnweb-insert href)
124                 (string-match "\\.html$" href)
125                 (nnweb-insert (concat (substring href 0 (match-beginning 0))
126                                       "-" (number-to-string current-page)
127                                       (match-string 0 href))))
128               (goto-char (point-min))
129               (setq contents
130                     (ignore-errors (w3-parse-buffer (current-buffer))))
131               (setq table (nnultimate-find-forum-table contents))
132               (setq string (mapconcat 'identity (nnweb-text table) ""))
133               (when (string-match "topic is \\([0-9]\\) pages" string)
134                 (setq pages (string-to-number (match-string 1 string)))
135                 (setcdr table nil)
136                 (setq table (nnultimate-find-forum-table contents)))
137               (setq contents (cdr (nth 2 (car (nth 2 table)))))
138               (setq total-contents (nconc total-contents contents))
139               (incf current-page))
140             ;;(setq total-contents (nreverse total-contents))
141             (dolist (art (cdr elem))
142               (if (not (nth (1- (cdr art)) total-contents))
143                   ()                    ;(debug)
144                 (push (list (car art)
145                             (nth (1- (cdr art)) total-contents)
146                             subject)
147                       nnultimate-articles)))))
148         (setq nnultimate-articles
149               (sort nnultimate-articles 'car-less-than-car))
150         ;; Now we have all the articles, conveniently in an alist
151         ;; where the key is the Gnus article number.
152         (dolist (articlef nnultimate-articles)
153           (setq article (nth 0 articlef)
154                 contents (nth 1 articlef)
155                 subject (nth 2 articlef))
156           (setq from (mapconcat 'identity
157                                 (nnweb-text (car (nth 2 contents)))
158                                 " ")
159                 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
160           (while datel
161             (when (string-match "Posted" (car datel))
162               (setq date (substring (car datel) (match-end 0))
163                     datel nil))
164             (pop datel))
165           (setq date (delete "" (split-string date "[- \n\t\r    ]")))
166           (if (or (member "AM" date)
167                   (member "PM" date))
168               (setq date (format "%s %s %s %s"
169                                  (car (rassq (string-to-number (nth 0 date))
170                                              parse-time-months))
171                                  (nth 1 date) (nth 2 date) (nth 3 date)))
172             (setq date (format "%s %s %s %s"
173                                (car (rassq (string-to-number (nth 1 date))
174                                            parse-time-months))
175                                (nth 0 date) (nth 2 date) (nth 3 date))))
176           (push
177            (cons
178             article
179             (make-full-mail-header
180              article subject
181              from (or date "")
182              (concat "<" (number-to-string sid) "%"
183                      (number-to-string article)
184                      "@ultimate>")
185              "" 0
186              (/ (length (mapconcat
187                          'identity
188                          (nnweb-text
189                           (cdr (nth 2 (nth 1 (nth 2 contents)))))
190                          ""))
191                 70)
192              nil nil))
193            headers))
194         (setq nnultimate-headers (sort headers 'car-less-than-car))
195         (save-excursion
196           (set-buffer nntp-server-buffer)
197           (mm-with-unibyte-current-buffer
198             (erase-buffer)
199             (dolist (header nnultimate-headers)
200               (nnheader-insert-nov (cdr header))))))
201       'nov)))
202
203 (deffoo nnultimate-request-group (group &optional server dont-check)
204   (nnultimate-possibly-change-server nil server)
205   (when (not nnultimate-groups)
206     (nnultimate-request-list))
207   (unless dont-check
208     (nnultimate-create-mapping group))
209   (let ((elem (assoc group nnultimate-groups)))
210     (cond
211      ((not elem)
212       (nnheader-report 'nnultimate "Group does not exist"))
213      (t
214       (nnheader-report 'nnultimate "Opened group %s" group)
215       (nnheader-insert
216        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
217        (prin1-to-string group))))))
218
219 (deffoo nnultimate-request-close ()
220   (setq nnultimate-groups-alist nil
221         nnultimate-groups nil))
222
223 (deffoo nnultimate-request-article (article &optional group server buffer)
224   (nnultimate-possibly-change-server group server)
225   (let ((contents (cdr (assq article nnultimate-articles))))
226     (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
227     (when contents
228       (save-excursion
229         (set-buffer (or buffer nntp-server-buffer))
230         (erase-buffer)
231         (nnweb-insert-html (cons 'p (cons nil (list contents))))
232         (goto-char (point-min))
233         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
234         (let ((header (cdr (assq article nnultimate-headers))))
235           (mm-with-unibyte-current-buffer
236             (nnheader-insert-header header)))
237         (nnheader-report 'nnultimate "Fetched article %s" article)
238         (cons group article)))))
239
240 (deffoo nnultimate-request-list (&optional server)
241   (nnultimate-possibly-change-server nil server)
242   (mm-with-unibyte-buffer
243     (nnweb-insert
244      (if (string-match "/$" nnultimate-address)
245          (concat nnultimate-address "Ultimate.cgi")
246        nnultimate-address))
247     (let ((contents (nth 2 (car (nth 2
248                                      (nnultimate-find-forum-table
249                                       (w3-parse-buffer (current-buffer)))))))
250           sid elem description articles a href group forum
251           a1 a2)
252       (dolist (row contents)
253         (setq row (nth 2 row))
254         (when (setq a (nnweb-parse-find 'a row))
255           (setq group (car (last (nnweb-text a)))
256                 href (cdr (assq 'href (nth 1 a))))
257           (setq description (car (last (nnweb-text (nth 1 row)))))
258           (setq a1 (car (last (nnweb-text (nth 2 row)))))
259           (setq a2 (car (last (nnweb-text (nth 3 row)))))
260           (when (string-match "^[0-9]+$" a1)
261             (setq articles (string-to-number a1)))
262           (when (and a2 (string-match "^[0-9]+$" a2))
263             (setq articles (max articles (string-to-number a2))))
264           (when href
265             (string-match "number=\\([0-9]+\\)" href)
266             (setq forum (string-to-number (match-string 1 href)))
267             (if (setq elem (assoc group nnultimate-groups))
268                 (setcar (cdr elem) articles)
269               (push (list group articles forum description nil nil nil nil)
270                     nnultimate-groups))))))
271     (nnultimate-write-groups)
272     (nnultimate-generate-active)
273     t))
274
275 (deffoo nnultimate-request-newgroups (date &optional server)
276   (nnultimate-possibly-change-server nil server)
277   (nnultimate-generate-active)
278   t)
279
280 (nnoo-define-skeleton nnultimate)
281
282 ;;; Internal functions
283
284 (defun nnultimate-prune-days (group time)
285   "Compute the number of days to fetch info for."
286   (let ((old-time (nth 7 (assoc group nnultimate-groups))))
287     (if (null old-time)
288         1000
289       (- (time-to-days time) (time-to-days old-time)))))
290
291 (defun nnultimate-create-mapping (group)
292   (let* ((entry (assoc group nnultimate-groups))
293          (sid (nth 2 entry))
294          (topics (nth 4 entry))
295          (mapping (nth 5 entry))
296          (old-total (or (nth 6 entry) 1))
297          (current-time (current-time))
298          (furl
299           (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
300                   (number-to-string
301                    (nnultimate-prune-days group current-time))))
302          (furls (list (concat nnultimate-address (format furl sid))))
303          contents forum-contents furl-fetched a subject href
304          garticles topic tinfo old-max inc parse)
305     (mm-with-unibyte-buffer
306       (while furls
307         (erase-buffer)
308         (nnweb-insert (pop furls))
309         (goto-char (point-min))
310         (setq parse (w3-parse-buffer (current-buffer)))
311         (setq contents
312               (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
313                                        parse))))))
314         (setq forum-contents (nconc contents forum-contents))
315         (unless furl-fetched
316           (setq furl-fetched t)
317           ;; On the first time through this loop, we find all the
318           ;; forum URLs.
319           (dolist (a (nnweb-parse-find-all 'a parse))
320             (let ((href (cdr (assq 'href (nth 1 a)))))
321               (when (and href
322                          (string-match "forumdisplay.*startpoint" href))
323                 (push href furls))))
324           (setq furls (nreverse furls))))
325       ;; The main idea here is to map Gnus article numbers to
326       ;; nnultimate article numbers.  Say there are three topics in
327       ;; this forum, the first with 4 articles, the seconds with 2,
328       ;; and the third with 1.  Then this will translate into 7 Gnus
329       ;; article numbers, where 1-4 comes from the first topic, 5-6
330       ;; from the second and 7 from the third.  Now, then next time
331       ;; the group is entered, there's 2 new articles in topic one
332       ;; and 1 in topic three.  Then Gnus article number 8-9 be 5-6
333       ;; in topic one and 10 will be the 2 in topic three.
334       (dolist (row (reverse forum-contents))
335         (setq row (nth 2 row))
336         (when (setq a (nnweb-parse-find 'a row))
337           (setq subject (car (last (nnweb-text a)))
338                 href (cdr (assq 'href (nth 1 a))))
339           (let ((artlist (nreverse (nnweb-text row)))
340                 art)
341             (while (and (not art)
342                         artlist)
343               (when (string-match "^[0-9]+$" (car artlist))
344                 (setq art (1+ (string-to-number (car artlist)))))
345               (pop artlist))
346             (setq garticles art))
347           (when garticles
348             (string-match "/\\([0-9]+\\).html" href)
349             (setq topic (string-to-number (match-string 1 href)))
350             (if (setq tinfo (assq topic topics))
351                 (progn
352                   (setq old-max (cadr tinfo))
353                   (setcar (cdr tinfo) garticles))
354               (setq old-max 0)
355               (push (list topic garticles subject href) topics)
356               (setcar (nthcdr 4 entry) topics))
357             (when (not (= old-max garticles))
358               (setq inc (- garticles old-max))
359               (setq mapping (nconc mapping
360                                    (list
361                                     (list
362                                      old-total (1- (incf old-total inc))
363                                      topic (1+ old-max)))))
364               (incf old-max inc)
365               (setcar (nthcdr 5 entry) mapping)
366               (setcar (nthcdr 6 entry) old-total))))))
367     (setcar (nthcdr 7 entry) current-time)
368     (setcar (nthcdr 1 entry) (1- old-total))
369     (nnultimate-write-groups)
370     mapping))
371
372 (defun nnultimate-possibly-change-server (&optional group server)
373   (nnultimate-init server)
374   (when (and server
375              (not (nnultimate-server-opened server)))
376     (nnultimate-open-server server))
377   (unless nnultimate-groups-alist
378     (nnultimate-read-groups)
379     (setq nnultimate-groups (cdr (assoc nnultimate-address
380                                         nnultimate-groups-alist)))))
381
382 (deffoo nnultimate-open-server (server &optional defs connectionless)
383   (nnheader-init-server-buffer)
384   (if (nnultimate-server-opened server)
385       t
386     (unless (assq 'nnultimate-address defs)
387       (setq defs (append defs (list (list 'nnultimate-address server)))))
388     (nnoo-change-server 'nnultimate server defs)))
389
390 (defun nnultimate-read-groups ()
391   (setq nnultimate-groups-alist nil)
392   (let ((file (expand-file-name "groups" nnultimate-directory)))
393     (when (file-exists-p file)
394       (mm-with-unibyte-buffer
395         (insert-file-contents file)
396         (goto-char (point-min))
397         (setq nnultimate-groups-alist (read (current-buffer)))))))
398
399 (defun nnultimate-write-groups ()
400   (setq nnultimate-groups-alist
401         (delq (assoc nnultimate-address nnultimate-groups-alist)
402               nnultimate-groups-alist))
403   (push (cons nnultimate-address nnultimate-groups)
404         nnultimate-groups-alist)
405   (with-temp-file (expand-file-name "groups" nnultimate-directory)
406     (prin1 nnultimate-groups-alist (current-buffer))))
407     
408 (defun nnultimate-init (server)
409   "Initialize buffers and such."
410   (unless (file-exists-p nnultimate-directory)
411     (gnus-make-directory nnultimate-directory)))
412
413 (defun nnultimate-generate-active ()
414   (save-excursion
415     (set-buffer nntp-server-buffer)
416     (erase-buffer)
417     (dolist (elem nnultimate-groups)
418       (insert (prin1-to-string (car elem))
419               " " (number-to-string (cadr elem)) " 1 y\n"))))
420
421 (defun nnultimate-find-forum-table (contents)
422   (catch 'found
423     (nnultimate-find-forum-table-1 contents)))
424
425 (defun nnultimate-find-forum-table-1 (contents)
426   (dolist (element contents)
427     (unless (stringp element)
428       (when (and (eq (car element) 'table)
429                  (nnultimate-forum-table-p element))
430         (throw 'found element))
431       (when (nth 2 element)
432         (nnultimate-find-forum-table-1 (nth 2 element))))))
433
434 (defun nnultimate-forum-table-p (parse)
435   (when (not (apply 'gnus-or
436                     (mapcar
437                      (lambda (p)
438                        (nnweb-parse-find 'table p))
439                      (nth 2 parse))))
440     (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
441           case-fold-search)
442       (when (and href (string-match
443                        "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
444                        href))
445         t))))
446
447 (provide 'nnultimate)
448
449 ;; Local Variables:
450 ;; coding: iso-8859-1
451 ;; End:
452
453 ;;; nnultimate.el ends here