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