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