See ChangeLog for the log entries
[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         (with-temp-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           (erase-buffer)
201           (dolist (header nnultimate-headers)
202             (nnheader-insert-nov (cdr header)))))
203       (setcar (nthcdr 6 entry) (nth 1 entry))
204       (nnultimate-write-groups)
205       'nov)))
206
207 (deffoo nnultimate-request-group (group &optional server dont-check)
208   (nnultimate-possibly-change-server nil server)
209   (when (or (not dont-check)
210             (not nnultimate-groups))
211     (nnultimate-request-list))
212   (unless dont-check
213     (nnultimate-create-mapping group))
214   (let ((elem (assoc group nnultimate-groups)))
215     (cond
216      ((not elem)
217       (nnheader-report 'nnultimate "Group does not exist"))
218      (t
219       (nnheader-report 'nnultimate "Opened group %s" group)
220       (nnheader-insert
221        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
222        (prin1-to-string group))))))
223
224 (deffoo nnultimate-request-article (article &optional group server buffer)
225   (nnultimate-possibly-change-server group server)
226   (let ((contents (cdr (assq article nnultimate-articles))))
227     (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
228     (when contents
229       (save-excursion
230         (set-buffer (or buffer nntp-server-buffer))
231         (erase-buffer)
232         (nnweb-insert-html (cons 'p (cons nil (list contents))))
233         (goto-char (point-min))
234         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
235         (let ((header (cdr (assq article nnultimate-headers))))
236           (nnheader-insert-header header))
237         (nnheader-report 'nnultimate "Fetched article %s" article)
238         (cons group article)))))
239
240 (deffoo nnultimate-request-list (&optional server)
241   (nnultimate-possibly-change-server nil server)
242   (with-temp-buffer
243     (nnweb-insert (concat nnultimate-address "Ultimate.cgi"))
244     (let ((contents (nth 2 (car (nth 2
245                                      (nnultimate-find-forum-table
246                                       (w3-parse-buffer (current-buffer)))))))
247           sid elem description articles a href group forum
248           a1 a2)
249       (dolist (row contents)
250         (setq row (nth 2 row))
251         (when (setq a (nnweb-parse-find 'a row))
252           (setq group (car (last (nnweb-text a)))
253                 href (cdr (assq 'href (nth 1 a))))
254           (setq description (car (last (nnweb-text (nth 1 row)))))
255           (setq a1 (car (last (nnweb-text (nth 2 row)))))
256           (setq a2 (car (last (nnweb-text (nth 3 row)))))
257           (when (string-match "^[0-9]+$" a1)
258             (setq articles (string-to-number a1)))
259           (when (and a2 (string-match "^[0-9]+$" a2))
260             (setq articles (max articles (string-to-number a2))))
261           (when href
262             (string-match "number=\\([0-9]+\\)" href)
263             (setq forum (string-to-number (match-string 1 href)))
264             (if (setq elem (assoc group nnultimate-groups))
265                 (setcar (cdr elem) articles)
266               (push (list group articles forum description nil nil nil)
267                     nnultimate-groups))))))
268     (nnultimate-write-groups)
269     (nnultimate-generate-active)
270     t))
271
272 (deffoo nnultimate-request-newgroups (date &optional server)
273   (nnultimate-possibly-change-server nil server)
274   (nnultimate-generate-active)
275   t)
276
277 (nnoo-define-skeleton nnultimate)
278
279 ;;; Internal functions
280
281
282 (defun nnultimate-create-mapping (group)
283   (let* ((entry (assoc group nnultimate-groups))
284          (sid (nth 2 entry))
285          (topics (nth 4 entry))
286          (mapping (nth 5 entry))
287          (old-total (or (nth 6 entry) 1))
288          (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
289          (furls (list (concat nnultimate-address (format furl sid))))
290          contents forum-contents furl-fetched a subject href
291          garticles topic tinfo old-max inc parse)
292     (with-temp-buffer
293       (while furls
294         (erase-buffer)
295         (nnweb-insert (pop furls))
296         (goto-char (point-min))
297         (setq parse (w3-parse-buffer (current-buffer)))
298         (setq contents
299               (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
300                                        parse))))))
301         (setq forum-contents (nconc contents forum-contents))
302         (when (and (not mapping)
303                    (not furl-fetched))
304           (setq furl-fetched t)
305           ;; On the first mapping, we fetch all the forum URLs.
306           (dolist (a (nnweb-parse-find-all 'a parse))
307             (let ((href (cdr (assq 'href (nth 1 a)))))
308               (when (and href
309                          (string-match "forumdisplay.*startpoint" href))
310                 (push href furls))))
311           (setq furls (nreverse furls))))
312       ;; The main idea here is to map Gnus article numbers to
313       ;; nnultimate article numbers.  Say there are three topics in
314       ;; this forum, the first with 4 articles, the seconds with 2,
315       ;; and the third with 1.  Then this will translate into 7 Gnus
316       ;; article numbers, where 1-4 comes from the first topic, 5-6
317       ;; from the second and 7 from the third.  Now, then next time
318       ;; the group is entered, there's 2 new articles in topic one
319       ;; and 1 in topic three.  Then Gnus article number 8-9 be 5-6
320       ;; in topic one and 10 will be the 2 in topic three.
321       (dolist (row (reverse forum-contents))
322         (setq row (nth 2 row))
323         (when (setq a (nnweb-parse-find 'a row))
324           (setq subject (car (last (nnweb-text a)))
325                 href (cdr (assq 'href (nth 1 a))))
326           (let ((artlist (nreverse (nnweb-text row)))
327                 art)
328             (while (and (not art)
329                         artlist)
330               (when (string-match "^[0-9]+$" (car artlist))
331                 (setq art (1+ (string-to-number (car artlist)))))
332               (pop artlist))
333             (setq garticles art))
334           (string-match "/\\([0-9]+\\).html" href)
335           (setq topic (string-to-number (match-string 1 href)))
336           (if (setq tinfo (assq topic topics))
337               (progn
338                 (setq old-max (cadr tinfo))
339                 (setcar (cdr tinfo) garticles))
340             (setq old-max 0)
341             (push (list topic garticles subject href) topics)
342             (setcar (nthcdr 4 entry) topics))
343           (when (not (= old-max garticles))
344             (setq inc (- garticles old-max))
345             (setq mapping (nconc mapping
346                                  (list
347                                   (list
348                                    old-total (1- (incf old-total inc))
349                                    topic (1+ old-max)))))
350             (incf old-max inc)
351             (setcar (nthcdr 5 entry) mapping)
352             (setcar (nthcdr 6 entry) old-total)))))
353     (nnultimate-write-groups)
354     mapping))
355
356 (defun nnultimate-possibly-change-server (&optional group server)
357   (nnultimate-init server)
358   (when (and server
359              (not (nnultimate-server-opened server)))
360     (nnultimate-open-server server))
361 ;  (unless nnultimate-groups-alist
362     (nnultimate-read-groups)
363   (setq nnultimate-groups (cdr (assoc nnultimate-address
364                                       nnultimate-groups-alist))))
365
366 (deffoo nnultimate-open-server (server &optional defs connectionless)
367   (nnheader-init-server-buffer)
368   (if (nnultimate-server-opened server)
369       t
370     (unless (assq 'nnultimate-address defs)
371       (setq defs (append defs (list (list 'nnultimate-address server)))))
372     (nnoo-change-server 'nnultimate server defs)))
373
374 (defun nnultimate-read-groups ()
375   (setq nnultimate-groups-alist nil)
376   (let ((file (expand-file-name "groups" nnultimate-directory)))
377     (when (file-exists-p file)
378       (with-temp-buffer
379         (insert-file-contents file)
380         (goto-char (point-min))
381         (setq nnultimate-groups-alist (read (current-buffer)))))))
382
383 (defun nnultimate-write-groups ()
384   (setq nnultimate-groups-alist
385         (delq (assoc nnultimate-address nnultimate-groups-alist)
386               nnultimate-groups-alist))
387   (push (cons nnultimate-address nnultimate-groups)
388         nnultimate-groups-alist)
389   (with-temp-file (expand-file-name "groups" nnultimate-directory)
390     (prin1 nnultimate-groups-alist (current-buffer))))
391     
392 (defun nnultimate-init (server)
393   "Initialize buffers and such."
394   (unless (file-exists-p nnultimate-directory)
395     (gnus-make-directory nnultimate-directory)))
396
397 (defun nnultimate-generate-active ()
398   (save-excursion
399     (set-buffer nntp-server-buffer)
400     (erase-buffer)
401     (dolist (elem nnultimate-groups)
402       (insert (prin1-to-string (car elem))
403               " " (number-to-string (cadr elem)) " 1 y\n"))))
404
405 (defun nnultimate-find-forum-table (contents)
406   (catch 'found
407     (nnultimate-find-forum-table-1 contents)))
408
409 (defun nnultimate-find-forum-table-1 (contents)
410   (dolist (element contents)
411     (unless (stringp element)
412       (when (and (eq (car element) 'table)
413                  (nnultimate-forum-table-p element))
414         (throw 'found element))
415       (when (nth 2 element)
416         (nnultimate-find-forum-table-1 (nth 2 element))))))
417
418 (defun nnultimate-forum-table-p (parse)
419   (when (not (apply 'gnus-or
420                     (mapcar
421                      (lambda (p)
422                        (nnweb-parse-find 'table p))
423                      (nth 2 parse))))
424     (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
425           case-fold-search)
426       (when (and href (string-match
427                        "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
428                                     href))
429         t))))
430
431 (provide 'nnultimate)
432
433 ;;; nnultimate.el ends here