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