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