1 ;;; nnultimate.el --- interfacing with the Ultimate Bulletin Board system
2 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; Note: You need to have `url' and `w3' installed for this
31 (eval-when-compile (require 'cl))
42 ;; Report failure to find w3 at load time if appropriate.
43 (eval '(require 'nnweb))
45 (nnoo-declare nnultimate)
47 (defvoo nnultimate-directory (nnheader-concat gnus-directory "ultimate/")
48 "Where nnultimate will save its files.")
50 (defvoo nnultimate-address ""
51 "The address of the Ultimate bulletin board.")
53 ;;; Internal variables
55 (defvar nnultimate-groups-alist nil)
56 (defvoo nnultimate-groups nil)
57 (defvoo nnultimate-headers nil)
58 (defvoo nnultimate-articles nil)
60 ;;; Interface functions
62 (nnoo-define-basics nnultimate)
64 (deffoo nnultimate-retrieve-headers (articles &optional group server fetch-old)
65 (nnultimate-possibly-change-server group server)
66 (unless gnus-nov-is-evil
67 (let* ((last (car (last articles)))
70 (entry (assoc group nnultimate-groups))
72 (topics (nth 4 entry))
73 (mapping (nth 5 entry))
74 (old-total (or (nth 6 entry) 1))
75 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
76 (furls (list (concat nnultimate-address (format furl sid))))
77 headers article subject score from date lines parent point
78 contents tinfo fetchers map elem a href garticles topic old-max
79 inc datel table string current-page total-contents pages
80 farticles forum-contents parse furl-fetched mmap farticle)
82 (while (and (setq article (car articles))
85 (or (> article (caar map))
86 (< (cadar map) (caar map))))
88 (when (setq mmap (car map))
91 (<= article (nth 1 mmap)))
92 ;; Do we already have a fetcher for this topic?
93 (if (setq elem (assq (nth 2 mmap) fetchers))
94 ;; Yes, so we just add the spec to the end.
95 (nconc elem (list (cons article
96 (+ (nth 3 mmap) (incf farticle)))))
97 ;; No, so we add a new one.
98 (push (list (nth 2 mmap)
100 (+ (nth 3 mmap) (incf farticle))))
103 (setq article (car articles)))))
104 ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
105 ;; so we start fetching the topics that we need to satisfy the
109 (set-buffer nntp-server-buffer)
111 (setq nnultimate-articles nil)
112 (mm-with-unibyte-buffer
113 (dolist (elem fetchers)
117 (while (<= current-page pages)
119 (setq subject (nth 2 (assq (car elem) topics)))
120 (setq href (nth 3 (assq (car elem) topics)))
121 (if (= current-page 1)
123 (string-match "\\.html$" href)
124 (nnweb-insert (concat (substring href 0 (match-beginning 0))
125 "-" (number-to-string current-page)
126 (match-string 0 href))))
127 (goto-char (point-min))
129 (ignore-errors (w3-parse-buffer (current-buffer))))
130 (setq table (nnultimate-find-forum-table contents))
131 (setq string (mapconcat 'identity (nnweb-text table) ""))
132 (when (string-match "topic is \\([0-9]\\) pages" string)
133 (setq pages (string-to-number (match-string 1 string)))
135 (setq table (nnultimate-find-forum-table contents)))
136 (setq contents (cdr (nth 2 (car (nth 2 table)))))
137 (setq total-contents (nconc total-contents contents))
139 ;;(setq total-contents (nreverse total-contents))
140 (dolist (art (cdr elem))
141 (if (not (nth (1- (cdr art)) total-contents))
143 (push (list (car art)
144 (nth (1- (cdr art)) total-contents)
146 nnultimate-articles)))))
147 (setq nnultimate-articles
148 (sort nnultimate-articles 'car-less-than-car))
149 ;; Now we have all the articles, conveniently in an alist
150 ;; where the key is the Gnus article number.
151 (dolist (articlef nnultimate-articles)
152 (setq article (nth 0 articlef)
153 contents (nth 1 articlef)
154 subject (nth 2 articlef))
155 (setq from (mapconcat 'identity
156 (nnweb-text (car (nth 2 contents)))
158 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
160 (when (string-match "Posted" (car datel))
161 (setq date (substring (car datel) (match-end 0))
164 (setq date (delete "" (split-string date "[- \n\t\r ]")))
165 (if (or (member "AM" date)
167 (setq date (format "%s %s %s %s"
168 (car (rassq (string-to-number (nth 0 date))
170 (nth 1 date) (nth 2 date) (nth 3 date)))
171 (setq date (format "%s %s %s %s"
172 (car (rassq (string-to-number (nth 1 date))
174 (nth 0 date) (nth 2 date) (nth 3 date))))
178 (make-full-mail-header
181 (concat "<" (number-to-string sid) "%"
182 (number-to-string article)
185 (/ (length (mapconcat
188 (cdr (nth 2 (nth 1 (nth 2 contents)))))
193 (setq nnultimate-headers (sort headers 'car-less-than-car))
195 (set-buffer nntp-server-buffer)
196 (mm-with-unibyte-current-buffer
198 (dolist (header nnultimate-headers)
199 (nnheader-insert-nov (cdr header))))))
202 (deffoo nnultimate-request-group (group &optional server dont-check)
203 (nnultimate-possibly-change-server nil server)
204 (when (not nnultimate-groups)
205 (nnultimate-request-list))
207 (nnultimate-create-mapping group))
208 (let ((elem (assoc group nnultimate-groups)))
211 (nnheader-report 'nnultimate "Group does not exist"))
213 (nnheader-report 'nnultimate "Opened group %s" group)
215 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
216 (prin1-to-string group))))))
218 (deffoo nnultimate-request-article (article &optional group server buffer)
219 (nnultimate-possibly-change-server group server)
220 (let ((contents (cdr (assq article nnultimate-articles))))
221 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
224 (set-buffer (or buffer nntp-server-buffer))
226 (nnweb-insert-html (cons 'p (cons nil (list contents))))
227 (goto-char (point-min))
228 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
229 (let ((header (cdr (assq article nnultimate-headers))))
230 (mm-with-unibyte-current-buffer
231 (nnheader-insert-header header)))
232 (nnheader-report 'nnultimate "Fetched article %s" article)
233 (cons group article)))))
235 (deffoo nnultimate-request-list (&optional server)
236 (nnultimate-possibly-change-server nil server)
237 (mm-with-unibyte-buffer
239 (if (string-match "/$" nnultimate-address)
240 (concat nnultimate-address "Ultimate.cgi")
242 (let ((contents (nth 2 (car (nth 2
243 (nnultimate-find-forum-table
244 (w3-parse-buffer (current-buffer)))))))
245 sid elem description articles a href group forum
247 (dolist (row contents)
248 (setq row (nth 2 row))
249 (when (setq a (nnweb-parse-find 'a row))
250 (setq group (car (last (nnweb-text a)))
251 href (cdr (assq 'href (nth 1 a))))
252 (setq description (car (last (nnweb-text (nth 1 row)))))
253 (setq a1 (car (last (nnweb-text (nth 2 row)))))
254 (setq a2 (car (last (nnweb-text (nth 3 row)))))
255 (when (string-match "^[0-9]+$" a1)
256 (setq articles (string-to-number a1)))
257 (when (and a2 (string-match "^[0-9]+$" a2))
258 (setq articles (max articles (string-to-number a2))))
260 (string-match "number=\\([0-9]+\\)" href)
261 (setq forum (string-to-number (match-string 1 href)))
262 (if (setq elem (assoc group nnultimate-groups))
263 (setcar (cdr elem) articles)
264 (push (list group articles forum description nil nil nil nil)
265 nnultimate-groups))))))
266 (nnultimate-write-groups)
267 (nnultimate-generate-active)
270 (deffoo nnultimate-request-newgroups (date &optional server)
271 (nnultimate-possibly-change-server nil server)
272 (nnultimate-generate-active)
275 (nnoo-define-skeleton nnultimate)
277 ;;; Internal functions
279 (defun nnultimate-prune-days (group time)
280 "Compute the number of days to fetch info for."
281 (let ((old-time (nth 7 (assoc group nnultimate-groups))))
284 (- (time-to-days time) (time-to-days old-time)))))
286 (defun nnultimate-create-mapping (group)
287 (let* ((entry (assoc group nnultimate-groups))
289 (topics (nth 4 entry))
290 (mapping (nth 5 entry))
291 (old-total (or (nth 6 entry) 1))
292 (current-time (current-time))
294 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
296 (nnultimate-prune-days group current-time))))
297 (furls (list (concat nnultimate-address (format furl sid))))
298 contents forum-contents furl-fetched a subject href
299 garticles topic tinfo old-max inc parse)
300 (mm-with-unibyte-buffer
303 (nnweb-insert (pop furls))
304 (goto-char (point-min))
305 (setq parse (w3-parse-buffer (current-buffer)))
307 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
309 (setq forum-contents (nconc contents forum-contents))
311 (setq furl-fetched t)
312 ;; On the first time through this loop, we find all the
314 (dolist (a (nnweb-parse-find-all 'a parse))
315 (let ((href (cdr (assq 'href (nth 1 a)))))
317 (string-match "forumdisplay.*startpoint" href))
319 (setq furls (nreverse furls))))
320 ;; The main idea here is to map Gnus article numbers to
321 ;; nnultimate article numbers. Say there are three topics in
322 ;; this forum, the first with 4 articles, the seconds with 2,
323 ;; and the third with 1. Then this will translate into 7 Gnus
324 ;; article numbers, where 1-4 comes from the first topic, 5-6
325 ;; from the second and 7 from the third. Now, then next time
326 ;; the group is entered, there's 2 new articles in topic one
327 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
328 ;; in topic one and 10 will be the 2 in topic three.
329 (dolist (row (reverse forum-contents))
330 (setq row (nth 2 row))
331 (when (setq a (nnweb-parse-find 'a row))
332 (setq subject (car (last (nnweb-text a)))
333 href (cdr (assq 'href (nth 1 a))))
334 (let ((artlist (nreverse (nnweb-text row)))
336 (while (and (not art)
338 (when (string-match "^[0-9]+$" (car artlist))
339 (setq art (1+ (string-to-number (car artlist)))))
341 (setq garticles art))
343 (string-match "/\\([0-9]+\\).html" href)
344 (setq topic (string-to-number (match-string 1 href)))
345 (if (setq tinfo (assq topic topics))
347 (setq old-max (cadr tinfo))
348 (setcar (cdr tinfo) garticles))
350 (push (list topic garticles subject href) topics)
351 (setcar (nthcdr 4 entry) topics))
352 (when (not (= old-max garticles))
353 (setq inc (- garticles old-max))
354 (setq mapping (nconc mapping
357 old-total (1- (incf old-total inc))
358 topic (1+ old-max)))))
360 (setcar (nthcdr 5 entry) mapping)
361 (setcar (nthcdr 6 entry) old-total))))))
362 (setcar (nthcdr 7 entry) current-time)
363 (setcar (nthcdr 1 entry) (1- old-total))
364 (nnultimate-write-groups)
367 (defun nnultimate-possibly-change-server (&optional group server)
368 (nnultimate-init server)
370 (not (nnultimate-server-opened server)))
371 (nnultimate-open-server server))
372 (unless nnultimate-groups-alist
373 (nnultimate-read-groups)
374 (setq nnultimate-groups (cdr (assoc nnultimate-address
375 nnultimate-groups-alist)))))
377 (deffoo nnultimate-open-server (server &optional defs connectionless)
378 (nnheader-init-server-buffer)
379 (if (nnultimate-server-opened server)
381 (unless (assq 'nnultimate-address defs)
382 (setq defs (append defs (list (list 'nnultimate-address server)))))
383 (nnoo-change-server 'nnultimate server defs)))
385 (defun nnultimate-read-groups ()
386 (setq nnultimate-groups-alist nil)
387 (let ((file (expand-file-name "groups" nnultimate-directory)))
388 (when (file-exists-p file)
389 (mm-with-unibyte-buffer
390 (insert-file-contents file)
391 (goto-char (point-min))
392 (setq nnultimate-groups-alist (read (current-buffer)))))))
394 (defun nnultimate-write-groups ()
395 (setq nnultimate-groups-alist
396 (delq (assoc nnultimate-address nnultimate-groups-alist)
397 nnultimate-groups-alist))
398 (push (cons nnultimate-address nnultimate-groups)
399 nnultimate-groups-alist)
400 (with-temp-file (expand-file-name "groups" nnultimate-directory)
401 (prin1 nnultimate-groups-alist (current-buffer))))
403 (defun nnultimate-init (server)
404 "Initialize buffers and such."
405 (unless (file-exists-p nnultimate-directory)
406 (gnus-make-directory nnultimate-directory)))
408 (defun nnultimate-generate-active ()
410 (set-buffer nntp-server-buffer)
412 (dolist (elem nnultimate-groups)
413 (insert (prin1-to-string (car elem))
414 " " (number-to-string (cadr elem)) " 1 y\n"))))
416 (defun nnultimate-find-forum-table (contents)
418 (nnultimate-find-forum-table-1 contents)))
420 (defun nnultimate-find-forum-table-1 (contents)
421 (dolist (element contents)
422 (unless (stringp element)
423 (when (and (eq (car element) 'table)
424 (nnultimate-forum-table-p element))
425 (throw 'found element))
426 (when (nth 2 element)
427 (nnultimate-find-forum-table-1 (nth 2 element))))))
429 (defun nnultimate-forum-table-p (parse)
430 (when (not (apply 'gnus-or
433 (nnweb-parse-find 'table p))
435 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
437 (when (and href (string-match
438 "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
442 (provide 'nnultimate)
445 ;; coding: iso-8859-1
448 ;;; nnultimate.el ends here