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