*** empty log message ***
[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   (let* ((last (car (last articles)))
73          (did nil)
74          (start 1)
75          (entry (gnus-copy-sequence (assoc group nnultimate-groups)))
76          (sid (nth 2 entry))
77          (topics (nth 4 entry))
78          (mapping (nth 5 entry))
79          (old-total (or (nth 6 entry) 0))
80          (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
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)
84     (with-temp-buffer
85       (nnweb-insert (concat nnultimate-address (format furl sid)))
86       (goto-char (point-min))
87       (setq contents (nth 2 (car (nth 2
88                                       (nnultimate-find-forum-table
89                                        (w3-parse-buffer (current-buffer)))))))
90       ;; The main idea here is to map Gnus article numbers to
91       ;; nnultimate article numbers.  Say there are three topics in
92       ;; this forum, the first with 4 articles, the seconds with 2,
93       ;; and the third with 1.  Then this will translate into 7 Gnus
94       ;; article numbers, where 1-4 comes from the first topic, 5-6
95       ;; from the second and 7 from the third.  Now, then next time
96       ;; the group is entered, there's 2 new articles in topic one and
97       ;; 1 in topic three.  Then Gnus article number 8-9 be 5-6 in
98       ;; topic one and 10 will be the 2 in topic three.
99       (dolist (row (cdr contents))
100         (setq row (nth 2 row))
101         (when (setq a (nnweb-parse-find 'a row))
102           (setq subject (car (last (nnweb-text a)))
103                 href (cdr (assq 'href (nth 1 a))))
104           (let ((artlist (nreverse (nnweb-text row)))
105                 art)
106             (while (and (not art)
107                         artlist)
108               (when (string-match "^[0-9]+$" (car artlist))
109                 (setq art (1+ (string-to-number (car artlist)))))
110               (pop artlist))
111             (setq garticles art))
112           (string-match "/\\([0-9]+\\).html" href)
113           (setq topic (string-to-number (match-string 1 href)))
114           (if (setq tinfo (assq topic topics))
115               (progn
116                 (setq old-max (cadr tinfo))
117                 (setcar (cdr tinfo) garticles))
118             (setq old-max 0)
119             (push (list topic garticles subject href) topics)
120             (setcar (nthcdr 4 entry) topics))
121           (when (not (= old-max garticles))
122             (setq inc (- garticles old-max))
123             (setq mapping (nconc mapping
124                                  (list
125                                   (list
126                                    (setq old-total (+ old-total inc))
127                                    topic (1+ old-max)))))
128             (incf old-max inc)
129             (setcar (nthcdr 5 entry) mapping))))
130       (setq map mapping)
131       (while (and (setq article (car articles))
132                   map)
133         (while (and map
134                     (> article (caar map)))
135           (pop map))
136         (while (and article
137                     map
138                     (<= article (caar map)))
139           (if (setq elem (assq (cadar map) fetchers))
140               (nconc elem (list (cons article
141                                       (+ (caddar map)
142                                          (- (caar map) article)))))
143             (push (list (cadar map) (cons article
144                                           (+ (caddar map)
145                                              (- (caar map) article))))
146                   fetchers))
147           (setq article (car (setq articles (cdr articles))))))
148       ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
149       ;; so we start fetching the topics that we need to satisfy the
150       ;; request.
151       (if (not fetchers)
152           (save-excursion
153             (set-buffer nntp-server-buffer)
154             (erase-buffer))
155         (setq nnultimate-articles nil)
156         (with-temp-buffer
157           (dolist (elem fetchers)
158             (erase-buffer)
159             (setq subject (nth 2 (assq (car elem) topics)))
160             (nnweb-insert (nth 3 (assq (car elem) topics)))
161             (goto-char (point-min))
162             (setq a (w3-parse-buffer (current-buffer)))
163             (setq contents
164                   (cdr
165                    (nth 2 (car (nth 2
166                                     (nnultimate-find-forum-table
167                                      (w3-parse-buffer (current-buffer))))))))
168             (dolist (art (cdr elem))
169               (push (list (car art)
170                           (nth (1- (cdr art)) contents)
171                           subject)
172                     nnultimate-articles))))
173         (setq nnultimate-articles
174               (sort nnultimate-articles 'car-less-than-car))
175         ;; Now we have all the articles, conveniently in an alist
176         ;; where the key is the Gnus article number.
177         (dolist (articlef nnultimate-articles)
178           (setq article (nth 0 articlef)
179                 contents (nth 1 articlef)
180                 subject (nth 2 articlef))
181           (setq from (mapconcat 'identity
182                                 (nnweb-text (car (nth 2 contents)))
183                                 " ")
184                 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
185           (while datel
186             (when (string-match "Posted" (car datel))
187               (setq date (substring (car datel) (match-end 0))
188                     datel nil))
189             (pop datel))
190           (setq date (delete "" (split-string date "[- \n\t\r    ]")))
191           (setq date (format "%s %s %s %s"
192                              (car (rassq (string-to-number (nth 1 date))
193                                          parse-time-months))
194                              (nth 0 date) (nth 2 date) (nth 3 date)))
195           (push
196            (cons
197             article
198             (make-full-mail-header
199              article subject
200              from (or date "")
201              (concat "<" (number-to-string sid) "%"
202                      (number-to-string article) 
203                      "@ultimate>")
204              "" 0
205              (/ (length (mapconcat
206                          'identity
207                          (nnweb-text
208                           (cdr (nth 2 (nth 1 (nth 2 contents)))))
209                          ""))
210                 70)
211              nil nil))
212            headers))
213         (setq nnultimate-headers (sort headers 'car-less-than-car))
214         (save-excursion
215           (set-buffer nntp-server-buffer)
216           (erase-buffer)
217           (dolist (header nnultimate-headers)
218             (nnheader-insert-nov (cdr header))))))
219     'nov))
220
221 (deffoo nnultimate-request-group (group &optional server dont-check)
222   (nnultimate-possibly-change-server nil server)
223   (when (or (not dont-check)
224             (not nnultimate-groups))
225     (nnultimate-request-list))
226   (let ((elem (assoc group nnultimate-groups)))
227     (cond
228      ((not elem)
229       (nnheader-report 'nnultimate "Group does not exist"))
230      (t
231       (nnheader-report 'nnultimate "Opened group %s" group)
232       (nnheader-insert
233        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
234        (prin1-to-string group))))))
235
236 (deffoo nnultimate-request-article (article &optional group server buffer)
237   (nnultimate-possibly-change-server group server)
238   (let ((contents (cdr (assq article nnultimate-articles))))
239     (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
240     (when contents
241       (save-excursion
242         (set-buffer (or buffer nntp-server-buffer))
243         (erase-buffer)
244         (nnweb-insert-html (cons 'p (cons nil (list contents))))
245         (goto-char (point-min))
246         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
247         (let ((header (cdr (assq article nnultimate-headers))))
248           (nnheader-insert-header header))
249         (nnheader-report 'nnultimate "Fetched article %s" article)
250         (cons group article)))))
251
252 (deffoo nnultimate-request-list (&optional server)
253   (nnultimate-possibly-change-server nil server)
254   (with-temp-buffer
255     (nnweb-insert (concat nnultimate-address "Ultimate.cgi"))
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)
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-possibly-change-server (&optional group server)
294   (nnultimate-init server)
295   (when (and server
296              (not (nnultimate-server-opened server)))
297     (nnultimate-open-server server))
298   (unless nnultimate-groups-alist
299     (nnultimate-read-groups))
300   (setq nnultimate-groups (cdr (assoc nnultimate-address
301                                       nnultimate-groups-alist))))
302
303 (deffoo nnultimate-open-server (server &optional defs connectionless)
304   (nnheader-init-server-buffer)
305   (if (nnultimate-server-opened server)
306       t
307     (unless (assq 'nnultimate-address defs)
308       (setq defs (append defs (list (list 'nnultimate-address server)))))
309     (nnoo-change-server 'nnultimate server defs)))
310
311 (defun nnultimate-read-groups ()
312   (let ((file (expand-file-name "groups" nnultimate-directory)))
313     (when (file-exists-p file)
314       (with-temp-buffer
315         (insert-file-contents file)
316         (goto-char (point-min))
317         (setq nnultimate-groups-alist (read (current-buffer)))))))
318
319 (defun nnultimate-write-groups ()
320   (setq nnultimate-groups-alist
321         (delq (assoc nnultimate-address nnultimate-groups-alist)
322               nnultimate-groups-alist))
323   (push (cons nnultimate-address nnultimate-groups)
324         nnultimate-groups-alist)
325   (with-temp-file (expand-file-name "groups" nnultimate-directory)
326     (prin1 nnultimate-groups-alist (current-buffer))))
327     
328 (defun nnultimate-init (server)
329   "Initialize buffers and such."
330   (unless (file-exists-p nnultimate-directory)
331     (gnus-make-directory nnultimate-directory)))
332
333 (defun nnultimate-date-to-date (sdate)
334   (let ((elem (split-string sdate)))
335     (concat (substring (nth 0 elem) 0 3) " "
336             (substring (nth 1 elem) 0 3) " "
337             (substring (nth 2 elem) 0 2) " "
338             (substring (nth 3 elem) 1 6) " "
339             (format-time-string "%Y") " "
340             (nth 4 elem))))
341
342 (defun nnultimate-generate-active ()
343   (save-excursion
344     (set-buffer nntp-server-buffer)
345     (erase-buffer)
346     (dolist (elem nnultimate-groups)
347       (insert (prin1-to-string (car elem))
348               " " (number-to-string (cadr elem)) " 1 y\n"))))
349
350 (defun nnultimate-find-forum-table (contents)
351   (catch 'found
352     (nnultimate-find-forum-table-1 contents)))
353
354 (defun nnultimate-find-forum-table-1 (contents)
355   (dolist (element contents)
356     (unless (stringp element)
357       (when (and (eq (car element) 'table)
358                  (nnultimate-forum-table-p element))
359         (throw 'found element))
360       (when (nth 2 element)
361         (nnultimate-find-forum-table-1 (nth 2 element))))))
362
363 (defun nnultimate-forum-table-p (parse)
364   (when (not (apply 'gnus-or
365                     (mapcar
366                      (lambda (p)
367                        (nnweb-parse-find 'table p))
368                      (nth 2 parse))))
369     (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
370           case-fold-search)
371       (when (and href (string-match "forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
372                                     href))
373         t))))
374
375 (provide 'nnultimate)
376
377 ;;; nnultimate.el ends here