* gnus.el: Fix copyright statements.
[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-article (article &optional group server buffer)
219   (nnultimate-possibly-change-server group server)
220   (let ((contents (cdr (assq article nnultimate-articles))))
221     (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
222     (when contents
223       (save-excursion
224         (set-buffer (or buffer nntp-server-buffer))
225         (erase-buffer)
226         (nnweb-insert-html (cons 'p (cons nil (list contents))))
227         (goto-char (point-min))
228         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
229         (let ((header (cdr (assq article nnultimate-headers))))
230           (mm-with-unibyte-current-buffer
231             (nnheader-insert-header header)))
232         (nnheader-report 'nnultimate "Fetched article %s" article)
233         (cons group article)))))
234
235 (deffoo nnultimate-request-list (&optional server)
236   (nnultimate-possibly-change-server nil server)
237   (mm-with-unibyte-buffer
238     (nnweb-insert
239      (if (string-match "/$" nnultimate-address)
240          (concat nnultimate-address "Ultimate.cgi")
241        nnultimate-address))
242     (let ((contents (nth 2 (car (nth 2
243                                      (nnultimate-find-forum-table
244                                       (w3-parse-buffer (current-buffer)))))))
245           sid elem description articles a href group forum
246           a1 a2)
247       (dolist (row contents)
248         (setq row (nth 2 row))
249         (when (setq a (nnweb-parse-find 'a row))
250           (setq group (car (last (nnweb-text a)))
251                 href (cdr (assq 'href (nth 1 a))))
252           (setq description (car (last (nnweb-text (nth 1 row)))))
253           (setq a1 (car (last (nnweb-text (nth 2 row)))))
254           (setq a2 (car (last (nnweb-text (nth 3 row)))))
255           (when (string-match "^[0-9]+$" a1)
256             (setq articles (string-to-number a1)))
257           (when (and a2 (string-match "^[0-9]+$" a2))
258             (setq articles (max articles (string-to-number a2))))
259           (when href
260             (string-match "number=\\([0-9]+\\)" href)
261             (setq forum (string-to-number (match-string 1 href)))
262             (if (setq elem (assoc group nnultimate-groups))
263                 (setcar (cdr elem) articles)
264               (push (list group articles forum description nil nil nil nil)
265                     nnultimate-groups))))))
266     (nnultimate-write-groups)
267     (nnultimate-generate-active)
268     t))
269
270 (deffoo nnultimate-request-newgroups (date &optional server)
271   (nnultimate-possibly-change-server nil server)
272   (nnultimate-generate-active)
273   t)
274
275 (nnoo-define-skeleton nnultimate)
276
277 ;;; Internal functions
278
279 (defun nnultimate-prune-days (group time)
280   "Compute the number of days to fetch info for."
281   (let ((old-time (nth 7 (assoc group nnultimate-groups))))
282     (if (null old-time)
283         1000
284       (- (time-to-days time) (time-to-days old-time)))))
285
286 (defun nnultimate-create-mapping (group)
287   (let* ((entry (assoc group nnultimate-groups))
288          (sid (nth 2 entry))
289          (topics (nth 4 entry))
290          (mapping (nth 5 entry))
291          (old-total (or (nth 6 entry) 1))
292          (current-time (current-time))
293          (furl
294           (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
295                   (number-to-string
296                    (nnultimate-prune-days group current-time))))
297          (furls (list (concat nnultimate-address (format furl sid))))
298          contents forum-contents furl-fetched a subject href
299          garticles topic tinfo old-max inc parse)
300     (mm-with-unibyte-buffer
301       (while furls
302         (erase-buffer)
303         (nnweb-insert (pop furls))
304         (goto-char (point-min))
305         (setq parse (w3-parse-buffer (current-buffer)))
306         (setq contents
307               (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
308                                        parse))))))
309         (setq forum-contents (nconc contents forum-contents))
310         (unless furl-fetched
311           (setq furl-fetched t)
312           ;; On the first time through this loop, we find all the
313           ;; forum URLs.
314           (dolist (a (nnweb-parse-find-all 'a parse))
315             (let ((href (cdr (assq 'href (nth 1 a)))))
316               (when (and href
317                          (string-match "forumdisplay.*startpoint" href))
318                 (push href furls))))
319           (setq furls (nreverse furls))))
320       ;; The main idea here is to map Gnus article numbers to
321       ;; nnultimate article numbers.  Say there are three topics in
322       ;; this forum, the first with 4 articles, the seconds with 2,
323       ;; and the third with 1.  Then this will translate into 7 Gnus
324       ;; article numbers, where 1-4 comes from the first topic, 5-6
325       ;; from the second and 7 from the third.  Now, then next time
326       ;; the group is entered, there's 2 new articles in topic one
327       ;; and 1 in topic three.  Then Gnus article number 8-9 be 5-6
328       ;; in topic one and 10 will be the 2 in topic three.
329       (dolist (row (reverse forum-contents))
330         (setq row (nth 2 row))
331         (when (setq a (nnweb-parse-find 'a row))
332           (setq subject (car (last (nnweb-text a)))
333                 href (cdr (assq 'href (nth 1 a))))
334           (let ((artlist (nreverse (nnweb-text row)))
335                 art)
336             (while (and (not art)
337                         artlist)
338               (when (string-match "^[0-9]+$" (car artlist))
339                 (setq art (1+ (string-to-number (car artlist)))))
340               (pop artlist))
341             (setq garticles art))
342           (when garticles
343             (string-match "/\\([0-9]+\\).html" href)
344             (setq topic (string-to-number (match-string 1 href)))
345             (if (setq tinfo (assq topic topics))
346                 (progn
347                   (setq old-max (cadr tinfo))
348                   (setcar (cdr tinfo) garticles))
349               (setq old-max 0)
350               (push (list topic garticles subject href) topics)
351               (setcar (nthcdr 4 entry) topics))
352             (when (not (= old-max garticles))
353               (setq inc (- garticles old-max))
354               (setq mapping (nconc mapping
355                                    (list
356                                     (list
357                                      old-total (1- (incf old-total inc))
358                                      topic (1+ old-max)))))
359               (incf old-max inc)
360               (setcar (nthcdr 5 entry) mapping)
361               (setcar (nthcdr 6 entry) old-total))))))
362     (setcar (nthcdr 7 entry) current-time)
363     (setcar (nthcdr 1 entry) (1- old-total))
364     (nnultimate-write-groups)
365     mapping))
366
367 (defun nnultimate-possibly-change-server (&optional group server)
368   (nnultimate-init server)
369   (when (and server
370              (not (nnultimate-server-opened server)))
371     (nnultimate-open-server server))
372   (unless nnultimate-groups-alist
373     (nnultimate-read-groups)
374     (setq nnultimate-groups (cdr (assoc nnultimate-address
375                                         nnultimate-groups-alist)))))
376
377 (deffoo nnultimate-open-server (server &optional defs connectionless)
378   (nnheader-init-server-buffer)
379   (if (nnultimate-server-opened server)
380       t
381     (unless (assq 'nnultimate-address defs)
382       (setq defs (append defs (list (list 'nnultimate-address server)))))
383     (nnoo-change-server 'nnultimate server defs)))
384
385 (defun nnultimate-read-groups ()
386   (setq nnultimate-groups-alist nil)
387   (let ((file (expand-file-name "groups" nnultimate-directory)))
388     (when (file-exists-p file)
389       (mm-with-unibyte-buffer
390         (insert-file-contents file)
391         (goto-char (point-min))
392         (setq nnultimate-groups-alist (read (current-buffer)))))))
393
394 (defun nnultimate-write-groups ()
395   (setq nnultimate-groups-alist
396         (delq (assoc nnultimate-address nnultimate-groups-alist)
397               nnultimate-groups-alist))
398   (push (cons nnultimate-address nnultimate-groups)
399         nnultimate-groups-alist)
400   (with-temp-file (expand-file-name "groups" nnultimate-directory)
401     (prin1 nnultimate-groups-alist (current-buffer))))
402     
403 (defun nnultimate-init (server)
404   "Initialize buffers and such."
405   (unless (file-exists-p nnultimate-directory)
406     (gnus-make-directory nnultimate-directory)))
407
408 (defun nnultimate-generate-active ()
409   (save-excursion
410     (set-buffer nntp-server-buffer)
411     (erase-buffer)
412     (dolist (elem nnultimate-groups)
413       (insert (prin1-to-string (car elem))
414               " " (number-to-string (cadr elem)) " 1 y\n"))))
415
416 (defun nnultimate-find-forum-table (contents)
417   (catch 'found
418     (nnultimate-find-forum-table-1 contents)))
419
420 (defun nnultimate-find-forum-table-1 (contents)
421   (dolist (element contents)
422     (unless (stringp element)
423       (when (and (eq (car element) 'table)
424                  (nnultimate-forum-table-p element))
425         (throw 'found element))
426       (when (nth 2 element)
427         (nnultimate-find-forum-table-1 (nth 2 element))))))
428
429 (defun nnultimate-forum-table-p (parse)
430   (when (not (apply 'gnus-or
431                     (mapcar
432                      (lambda (p)
433                        (nnweb-parse-find 'table p))
434                      (nth 2 parse))))
435     (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
436           case-fold-search)
437       (when (and href (string-match
438                        "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
439                        href))
440         t))))
441
442 (provide 'nnultimate)
443
444 ;; Local Variables:
445 ;; coding: iso-8859-1
446 ;; End:
447
448 ;;; nnultimate.el ends here