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