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