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