1 ;;; nnultimate.el --- interfacing with the Ultimate Bulletin Board system -*- coding: iso-latin-1 -*-
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)
59 (defvar nnultimate-table-regexp
60 "postings.*editpost\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio")
62 ;;; Interface functions
64 (nnoo-define-basics nnultimate)
66 (deffoo nnultimate-retrieve-headers (articles &optional group server fetch-old)
67 (nnultimate-possibly-change-server group server)
68 (unless gnus-nov-is-evil
69 (let* ((last (car (last articles)))
72 (entry (assoc group nnultimate-groups))
74 (topics (nth 4 entry))
75 (mapping (nth 5 entry))
76 (old-total (or (nth 6 entry) 1))
77 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
78 (furls (list (concat nnultimate-address (format furl sid))))
79 (nnultimate-table-regexp
80 "postings.*editpost\\|forumdisplay\\|getbio")
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 table string current-page total-contents pages
84 farticles forum-contents parse furl-fetched mmap farticle)
86 (while (and (setq article (car articles))
89 (or (> article (caar map))
90 (< (cadar map) (caar map))))
92 (when (setq mmap (car map))
95 (<= article (nth 1 mmap)))
96 ;; Do we already have a fetcher for this topic?
97 (if (setq elem (assq (nth 2 mmap) fetchers))
98 ;; Yes, so we just add the spec to the end.
99 (nconc elem (list (cons article
100 (+ (nth 3 mmap) (incf farticle)))))
101 ;; No, so we add a new one.
102 (push (list (nth 2 mmap)
104 (+ (nth 3 mmap) (incf farticle))))
107 (setq article (car articles)))))
108 ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
109 ;; so we start fetching the topics that we need to satisfy the
113 (set-buffer nntp-server-buffer)
115 (setq nnultimate-articles nil)
116 (mm-with-unibyte-buffer
117 (dolist (elem fetchers)
121 (while (<= current-page pages)
123 (setq subject (nth 2 (assq (car elem) topics)))
124 (setq href (nth 3 (assq (car elem) topics)))
125 (if (= current-page 1)
127 (string-match "\\.html$" href)
128 (nnweb-insert (concat (substring href 0 (match-beginning 0))
129 "-" (number-to-string current-page)
130 (match-string 0 href))))
131 (goto-char (point-min))
133 (ignore-errors (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)))
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))
143 ;;(setq total-contents (nreverse total-contents))
144 (dolist (art (cdr elem))
145 (if (not (nth (1- (cdr art)) total-contents))
147 (push (list (car art)
148 (nth (1- (cdr art)) total-contents)
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)))
162 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
164 (when (string-match "Posted" (car datel))
165 (setq date (substring (car datel) (match-end 0))
168 (setq date (delete "" (split-string
169 date "[-, \n\t\r
\81 \81 \81 ]")))
170 (if (or (member "AM" date)
175 (if (and (>= (length (nth 0 date)) 3)
177 (substring (nth 0 date) 0 3))
179 (substring (nth 0 date) 0 3)
180 (car (rassq (string-to-number (nth 0 date))
182 (nth 2 date) (nth 3 date)))
183 (setq date (format "%s %s %s %s"
184 (car (rassq (string-to-number (nth 1 date))
186 (nth 0 date) (nth 2 date) (nth 3 date))))
190 (make-full-mail-header
193 (concat "<" (number-to-string sid) "%"
194 (number-to-string article)
197 (/ (length (mapconcat
200 (cdr (nth 2 (nth 1 (nth 2 contents)))))
205 (setq nnultimate-headers (sort headers 'car-less-than-car))
207 (set-buffer nntp-server-buffer)
208 (mm-with-unibyte-current-buffer
210 (dolist (header nnultimate-headers)
211 (nnheader-insert-nov (cdr header))))))
214 (deffoo nnultimate-request-group (group &optional server dont-check)
215 (nnultimate-possibly-change-server nil server)
216 (when (not nnultimate-groups)
217 (nnultimate-request-list))
219 (nnultimate-create-mapping group))
220 (let ((elem (assoc group nnultimate-groups)))
223 (nnheader-report 'nnultimate "Group does not exist"))
225 (nnheader-report 'nnultimate "Opened group %s" group)
227 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
228 (prin1-to-string group))))))
230 (deffoo nnultimate-request-close ()
231 (setq nnultimate-groups-alist nil
232 nnultimate-groups nil))
234 (deffoo nnultimate-request-article (article &optional group server buffer)
235 (nnultimate-possibly-change-server group server)
236 (let ((contents (cdr (assq article nnultimate-articles))))
237 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
240 (set-buffer (or buffer nntp-server-buffer))
242 (nnweb-insert-html (cons 'p (cons nil (list contents))))
243 (goto-char (point-min))
244 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
245 (let ((header (cdr (assq article nnultimate-headers))))
246 (mm-with-unibyte-current-buffer
247 (nnheader-insert-header header)))
248 (nnheader-report 'nnultimate "Fetched article %s" article)
249 (cons group article)))))
251 (deffoo nnultimate-request-list (&optional server)
252 (nnultimate-possibly-change-server nil server)
253 (mm-with-unibyte-buffer
255 (if (string-match "/$" nnultimate-address)
256 (concat nnultimate-address "Ultimate.cgi")
258 (let ((contents (nth 2 (car (nth 2
259 (nnultimate-find-forum-table
260 (w3-parse-buffer (current-buffer)))))))
261 sid elem description articles a href group forum
263 (dolist (row contents)
264 (setq row (nth 2 row))
265 (when (setq a (nnweb-parse-find 'a row))
266 (setq group (car (last (nnweb-text a)))
267 href (cdr (assq 'href (nth 1 a))))
268 (setq description (car (last (nnweb-text (nth 1 row)))))
269 (setq a1 (car (last (nnweb-text (nth 2 row)))))
270 (setq a2 (car (last (nnweb-text (nth 3 row)))))
271 (when (string-match "^[0-9]+$" a1)
272 (setq articles (string-to-number a1)))
273 (when (and a2 (string-match "^[0-9]+$" a2))
274 (setq articles (max articles (string-to-number a2))))
276 (string-match "number=\\([0-9]+\\)" href)
277 (setq forum (string-to-number (match-string 1 href)))
278 (if (setq elem (assoc group nnultimate-groups))
279 (setcar (cdr elem) articles)
280 (push (list group articles forum description nil nil nil nil)
281 nnultimate-groups))))))
282 (nnultimate-write-groups)
283 (nnultimate-generate-active)
286 (deffoo nnultimate-request-newgroups (date &optional server)
287 (nnultimate-possibly-change-server nil server)
288 (nnultimate-generate-active)
291 (nnoo-define-skeleton nnultimate)
293 ;;; Internal functions
295 (defun nnultimate-prune-days (group time)
296 "Compute the number of days to fetch info for."
297 (let ((old-time (nth 7 (assoc group nnultimate-groups))))
300 (- (time-to-days time) (time-to-days old-time)))))
302 (defun nnultimate-create-mapping (group)
303 (let* ((entry (assoc group nnultimate-groups))
305 (topics (nth 4 entry))
306 (mapping (nth 5 entry))
307 (old-total (or (nth 6 entry) 1))
308 (current-time (current-time))
310 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
312 (nnultimate-prune-days group current-time))))
313 (furls (list (concat nnultimate-address (format furl sid))))
314 contents forum-contents furl-fetched a subject href
315 garticles topic tinfo old-max inc parse)
316 (mm-with-unibyte-buffer
319 (nnweb-insert (pop furls))
320 (goto-char (point-min))
321 (setq parse (w3-parse-buffer (current-buffer)))
323 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
325 (setq forum-contents (nconc contents forum-contents))
327 (setq furl-fetched t)
328 ;; On the first time through this loop, we find all the
330 (dolist (a (nnweb-parse-find-all 'a parse))
331 (let ((href (cdr (assq 'href (nth 1 a)))))
333 (string-match "forumdisplay.*startpoint" href))
335 (setq furls (nreverse furls))))
336 ;; The main idea here is to map Gnus article numbers to
337 ;; nnultimate article numbers. Say there are three topics in
338 ;; this forum, the first with 4 articles, the seconds with 2,
339 ;; and the third with 1. Then this will translate into 7 Gnus
340 ;; article numbers, where 1-4 comes from the first topic, 5-6
341 ;; from the second and 7 from the third. Now, then next time
342 ;; the group is entered, there's 2 new articles in topic one
343 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
344 ;; in topic one and 10 will be the 2 in topic three.
345 (dolist (row (nreverse forum-contents))
346 (setq row (nth 2 row))
347 (when (setq a (nnweb-parse-find 'a row))
348 (setq subject (car (last (nnweb-text a)))
349 href (cdr (assq 'href (nth 1 a))))
350 (let ((artlist (nreverse (nnweb-text row)))
352 (while (and (not art)
354 (when (string-match "^[0-9]+$" (car artlist))
355 (setq art (1+ (string-to-number (car artlist)))))
357 (setq garticles art))
359 (string-match "/\\([0-9]+\\).html" href)
360 (setq topic (string-to-number (match-string 1 href)))
361 (if (setq tinfo (assq topic topics))
363 (setq old-max (cadr tinfo))
364 (setcar (cdr tinfo) garticles))
366 (push (list topic garticles subject href) topics)
367 (setcar (nthcdr 4 entry) topics))
368 (when (not (= old-max garticles))
369 (setq inc (- garticles old-max))
370 (setq mapping (nconc mapping
373 old-total (1- (incf old-total inc))
374 topic (1+ old-max)))))
376 (setcar (nthcdr 5 entry) mapping)
377 (setcar (nthcdr 6 entry) old-total))))))
378 (setcar (nthcdr 7 entry) current-time)
379 (setcar (nthcdr 1 entry) (1- old-total))
380 (nnultimate-write-groups)
383 (defun nnultimate-possibly-change-server (&optional group server)
384 (nnultimate-init server)
386 (not (nnultimate-server-opened server)))
387 (nnultimate-open-server server))
388 (unless nnultimate-groups-alist
389 (nnultimate-read-groups)
390 (setq nnultimate-groups (cdr (assoc nnultimate-address
391 nnultimate-groups-alist)))))
393 (deffoo nnultimate-open-server (server &optional defs connectionless)
394 (nnheader-init-server-buffer)
395 (if (nnultimate-server-opened server)
397 (unless (assq 'nnultimate-address defs)
398 (setq defs (append defs (list (list 'nnultimate-address server)))))
399 (nnoo-change-server 'nnultimate server defs)))
401 (defun nnultimate-read-groups ()
402 (setq nnultimate-groups-alist nil)
403 (let ((file (expand-file-name "groups" nnultimate-directory)))
404 (when (file-exists-p file)
405 (mm-with-unibyte-buffer
406 (insert-file-contents file)
407 (goto-char (point-min))
408 (setq nnultimate-groups-alist (read (current-buffer)))))))
410 (defun nnultimate-write-groups ()
411 (setq nnultimate-groups-alist
412 (delq (assoc nnultimate-address nnultimate-groups-alist)
413 nnultimate-groups-alist))
414 (push (cons nnultimate-address nnultimate-groups)
415 nnultimate-groups-alist)
416 (with-temp-file (expand-file-name "groups" nnultimate-directory)
417 (prin1 nnultimate-groups-alist (current-buffer))))
419 (defun nnultimate-init (server)
420 "Initialize buffers and such."
421 (unless (file-exists-p nnultimate-directory)
422 (gnus-make-directory nnultimate-directory)))
424 (defun nnultimate-generate-active ()
426 (set-buffer nntp-server-buffer)
428 (dolist (elem nnultimate-groups)
429 (insert (prin1-to-string (car elem))
430 " " (number-to-string (cadr elem)) " 1 y\n"))))
432 (defun nnultimate-find-forum-table (contents)
434 (nnultimate-find-forum-table-1 contents)))
436 (defun nnultimate-find-forum-table-1 (contents)
437 (dolist (element contents)
438 (unless (stringp element)
439 (when (and (eq (car element) 'table)
440 (nnultimate-forum-table-p element))
441 (throw 'found element))
442 (when (nth 2 element)
443 (nnultimate-find-forum-table-1 (nth 2 element))))))
445 (defun nnultimate-forum-table-p (parse)
446 (when (not (apply 'gnus-or
449 (nnweb-parse-find 'table p))
451 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
453 (when (and href (string-match nnultimate-table-regexp href))
456 (provide 'nnultimate)
459 ;; coding: iso-8859-1
462 ;;; nnultimate.el ends here