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))
32 (eval-when-compile (require 'gnus-clfns))
43 ;; Report failure to find w3 at load time if appropriate.
44 (eval '(require 'nnweb))
46 (nnoo-declare nnultimate)
48 (defvoo nnultimate-directory (nnheader-concat gnus-directory "ultimate/")
49 "Where nnultimate will save its files.")
51 (defvoo nnultimate-address ""
52 "The address of the Ultimate bulletin board.")
54 ;;; Internal variables
56 (defvar nnultimate-groups-alist nil)
57 (defvoo nnultimate-groups nil)
58 (defvoo nnultimate-headers nil)
59 (defvoo nnultimate-articles nil)
60 (defvar nnultimate-table-regexp
61 "postings.*editpost\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio")
63 ;;; Interface functions
65 (nnoo-define-basics nnultimate)
67 (deffoo nnultimate-retrieve-headers (articles &optional group server fetch-old)
68 (nnultimate-possibly-change-server group server)
69 (unless gnus-nov-is-evil
70 (let* ((last (car (last articles)))
73 (entry (assoc group nnultimate-groups))
75 (topics (nth 4 entry))
76 (mapping (nth 5 entry))
77 (old-total (or (nth 6 entry) 1))
78 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
79 (furls (list (concat nnultimate-address (format furl sid))))
80 (nnultimate-table-regexp
81 "postings.*editpost\\|forumdisplay\\|getbio")
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 current-page total-contents pages
85 farticles forum-contents parse furl-fetched mmap farticle)
87 (while (and (setq article (car articles))
90 (or (> article (caar map))
91 (< (cadar map) (caar map))))
93 (when (setq mmap (car map))
96 (<= article (nth 1 mmap)))
97 ;; Do we already have a fetcher for this topic?
98 (if (setq elem (assq (nth 2 mmap) fetchers))
99 ;; Yes, so we just add the spec to the end.
100 (nconc elem (list (cons article
101 (+ (nth 3 mmap) (incf farticle)))))
102 ;; No, so we add a new one.
103 (push (list (nth 2 mmap)
105 (+ (nth 3 mmap) (incf farticle))))
108 (setq article (car articles)))))
109 ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
110 ;; so we start fetching the topics that we need to satisfy the
114 (set-buffer nntp-server-buffer)
116 (setq nnultimate-articles nil)
117 (mm-with-unibyte-buffer
118 (dolist (elem fetchers)
122 (while (<= current-page pages)
124 (setq subject (nth 2 (assq (car elem) topics)))
125 (setq href (nth 3 (assq (car elem) topics)))
126 (if (= current-page 1)
128 (string-match "\\.html$" href)
129 (nnweb-insert (concat (substring href 0 (match-beginning 0))
130 "-" (number-to-string current-page)
131 (match-string 0 href))))
132 (goto-char (point-min))
134 (ignore-errors (w3-parse-buffer (current-buffer))))
135 (setq table (nnultimate-find-forum-table contents))
136 (goto-char (point-min))
137 (when (re-search-forward "topic is \\([0-9]+\\) pages" nil t)
138 (setq pages (string-to-number (match-string 1))))
139 (setq contents (cdr (nth 2 (car (nth 2 table)))))
140 (setq total-contents (nconc total-contents contents))
142 ;;(setq total-contents (nreverse total-contents))
143 (dolist (art (cdr elem))
144 (if (not (nth (1- (cdr art)) total-contents))
146 (push (list (car art)
147 (nth (1- (cdr art)) total-contents)
149 nnultimate-articles)))))
150 (setq nnultimate-articles
151 (sort nnultimate-articles 'car-less-than-car))
152 ;; Now we have all the articles, conveniently in an alist
153 ;; where the key is the Gnus article number.
154 (dolist (articlef nnultimate-articles)
155 (setq article (nth 0 articlef)
156 contents (nth 1 articlef)
157 subject (nth 2 articlef))
158 (setq from (mapconcat 'identity
159 (nnweb-text (car (nth 2 contents)))
161 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
163 (when (string-match "Posted" (car datel))
164 (setq date (substring (car datel) (match-end 0))
167 (setq date (delete "" (split-string
168 date "[-, \n\t\r
\81 \81 \81 ]")))
169 (if (or (member "AM" date)
174 (if (and (>= (length (nth 0 date)) 3)
176 (substring (nth 0 date) 0 3))
178 (substring (nth 0 date) 0 3)
179 (car (rassq (string-to-number (nth 0 date))
181 (nth 2 date) (nth 3 date)))
182 (setq date (format "%s %s %s %s"
183 (car (rassq (string-to-number (nth 1 date))
185 (nth 0 date) (nth 2 date) (nth 3 date))))
189 (make-full-mail-header
192 (concat "<" (number-to-string sid) "%"
193 (number-to-string article)
196 (/ (length (mapconcat
199 (cdr (nth 2 (nth 1 (nth 2 contents)))))
204 (setq nnultimate-headers (sort headers 'car-less-than-car))
206 (set-buffer nntp-server-buffer)
207 (mm-with-unibyte-current-buffer
209 (dolist (header nnultimate-headers)
210 (nnheader-insert-nov (cdr header))))))
213 (deffoo nnultimate-request-group (group &optional server dont-check)
214 (nnultimate-possibly-change-server nil server)
215 (when (not nnultimate-groups)
216 (nnultimate-request-list))
218 (nnultimate-create-mapping group))
219 (let ((elem (assoc group nnultimate-groups)))
222 (nnheader-report 'nnultimate "Group does not exist"))
224 (nnheader-report 'nnultimate "Opened group %s" group)
226 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
227 (prin1-to-string group))))))
229 (deffoo nnultimate-request-close ()
230 (setq nnultimate-groups-alist nil
231 nnultimate-groups nil))
233 (deffoo nnultimate-request-article (article &optional group server buffer)
234 (nnultimate-possibly-change-server group server)
235 (let ((contents (cdr (assq article nnultimate-articles))))
236 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
239 (set-buffer (or buffer nntp-server-buffer))
241 (nnweb-insert-html (cons 'p (cons nil (list contents))))
242 (goto-char (point-min))
243 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
244 (let ((header (cdr (assq article nnultimate-headers))))
245 (mm-with-unibyte-current-buffer
246 (nnheader-insert-header header)))
247 (nnheader-report 'nnultimate "Fetched article %s" article)
248 (cons group article)))))
250 (deffoo nnultimate-request-list (&optional server)
251 (nnultimate-possibly-change-server nil server)
252 (mm-with-unibyte-buffer
254 (if (string-match "/$" nnultimate-address)
255 (concat nnultimate-address "Ultimate.cgi")
257 (let ((contents (nth 2 (car (nth 2
258 (nnultimate-find-forum-table
259 (w3-parse-buffer (current-buffer)))))))
260 sid elem description articles a href group forum
262 (dolist (row contents)
263 (setq row (nth 2 row))
264 (when (setq a (nnweb-parse-find 'a row))
265 (setq group (car (last (nnweb-text a)))
266 href (cdr (assq 'href (nth 1 a))))
267 (setq description (car (last (nnweb-text (nth 1 row)))))
268 (setq a1 (car (last (nnweb-text (nth 2 row)))))
269 (setq a2 (car (last (nnweb-text (nth 3 row)))))
270 (when (string-match "^[0-9]+$" a1)
271 (setq articles (string-to-number a1)))
272 (when (and a2 (string-match "^[0-9]+$" a2))
273 (setq articles (max articles (string-to-number a2))))
275 (string-match "number=\\([0-9]+\\)" href)
276 (setq forum (string-to-number (match-string 1 href)))
277 (if (setq elem (assoc group nnultimate-groups))
278 (setcar (cdr elem) articles)
279 (push (list group articles forum description nil nil nil nil)
280 nnultimate-groups))))))
281 (nnultimate-write-groups)
282 (nnultimate-generate-active)
285 (deffoo nnultimate-request-newgroups (date &optional server)
286 (nnultimate-possibly-change-server nil server)
287 (nnultimate-generate-active)
290 (nnoo-define-skeleton nnultimate)
292 ;;; Internal functions
294 (defun nnultimate-prune-days (group time)
295 "Compute the number of days to fetch info for."
296 (let ((old-time (nth 7 (assoc group nnultimate-groups))))
299 (- (time-to-days time) (time-to-days old-time)))))
301 (defun nnultimate-create-mapping (group)
302 (let* ((entry (assoc group nnultimate-groups))
304 (topics (nth 4 entry))
305 (mapping (nth 5 entry))
306 (old-total (or (nth 6 entry) 1))
307 (current-time (current-time))
309 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
311 (nnultimate-prune-days group current-time))))
312 (furls (list (concat nnultimate-address (format furl sid))))
313 contents forum-contents furl-fetched a subject href
314 garticles topic tinfo old-max inc parse)
315 (mm-with-unibyte-buffer
318 (nnweb-insert (pop furls))
319 (goto-char (point-min))
320 (setq parse (w3-parse-buffer (current-buffer)))
322 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
324 (setq forum-contents (nconc contents forum-contents))
326 (setq furl-fetched t)
327 ;; On the first time through this loop, we find all the
329 (dolist (a (nnweb-parse-find-all 'a parse))
330 (let ((href (cdr (assq 'href (nth 1 a)))))
332 (string-match "forumdisplay.*startpoint" href))
334 (setq furls (nreverse furls))))
335 ;; The main idea here is to map Gnus article numbers to
336 ;; nnultimate article numbers. Say there are three topics in
337 ;; this forum, the first with 4 articles, the seconds with 2,
338 ;; and the third with 1. Then this will translate into 7 Gnus
339 ;; article numbers, where 1-4 comes from the first topic, 5-6
340 ;; from the second and 7 from the third. Now, then next time
341 ;; the group is entered, there's 2 new articles in topic one
342 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
343 ;; in topic one and 10 will be the 2 in topic three.
344 (dolist (row (nreverse forum-contents))
345 (setq row (nth 2 row))
346 (when (setq a (nnweb-parse-find 'a row))
347 (setq subject (car (last (nnweb-text a)))
348 href (cdr (assq 'href (nth 1 a))))
349 (let ((artlist (nreverse (nnweb-text row)))
351 (while (and (not art)
353 (when (string-match "^[0-9]+$" (car artlist))
354 (setq art (1+ (string-to-number (car artlist)))))
356 (setq garticles art))
358 (string-match "/\\([0-9]+\\).html" href)
359 (setq topic (string-to-number (match-string 1 href)))
360 (if (setq tinfo (assq topic topics))
362 (setq old-max (cadr tinfo))
363 (setcar (cdr tinfo) garticles))
365 (push (list topic garticles subject href) topics)
366 (setcar (nthcdr 4 entry) topics))
367 (when (not (= old-max garticles))
368 (setq inc (- garticles old-max))
369 (setq mapping (nconc mapping
372 old-total (1- (incf old-total inc))
373 topic (1+ old-max)))))
375 (setcar (nthcdr 5 entry) mapping)
376 (setcar (nthcdr 6 entry) old-total))))))
377 (setcar (nthcdr 7 entry) current-time)
378 (setcar (nthcdr 1 entry) (1- old-total))
379 (nnultimate-write-groups)
382 (defun nnultimate-possibly-change-server (&optional group server)
383 (nnultimate-init server)
385 (not (nnultimate-server-opened server)))
386 (nnultimate-open-server server))
387 (unless nnultimate-groups-alist
388 (nnultimate-read-groups)
389 (setq nnultimate-groups (cdr (assoc nnultimate-address
390 nnultimate-groups-alist)))))
392 (deffoo nnultimate-open-server (server &optional defs connectionless)
393 (nnheader-init-server-buffer)
394 (if (nnultimate-server-opened server)
396 (unless (assq 'nnultimate-address defs)
397 (setq defs (append defs (list (list 'nnultimate-address server)))))
398 (nnoo-change-server 'nnultimate server defs)))
400 (defun nnultimate-read-groups ()
401 (setq nnultimate-groups-alist nil)
402 (let ((file (expand-file-name "groups" nnultimate-directory)))
403 (when (file-exists-p file)
404 (mm-with-unibyte-buffer
405 (insert-file-contents file)
406 (goto-char (point-min))
407 (setq nnultimate-groups-alist (read (current-buffer)))))))
409 (defun nnultimate-write-groups ()
410 (setq nnultimate-groups-alist
411 (delq (assoc nnultimate-address nnultimate-groups-alist)
412 nnultimate-groups-alist))
413 (push (cons nnultimate-address nnultimate-groups)
414 nnultimate-groups-alist)
415 (with-temp-file (expand-file-name "groups" nnultimate-directory)
416 (prin1 nnultimate-groups-alist (current-buffer))))
418 (defun nnultimate-init (server)
419 "Initialize buffers and such."
420 (unless (file-exists-p nnultimate-directory)
421 (gnus-make-directory nnultimate-directory)))
423 (defun nnultimate-generate-active ()
425 (set-buffer nntp-server-buffer)
427 (dolist (elem nnultimate-groups)
428 (insert (prin1-to-string (car elem))
429 " " (number-to-string (cadr elem)) " 1 y\n"))))
431 (defun nnultimate-find-forum-table (contents)
433 (nnultimate-find-forum-table-1 contents)))
435 (defun nnultimate-find-forum-table-1 (contents)
436 (dolist (element contents)
437 (unless (stringp element)
438 (when (and (eq (car element) 'table)
439 (nnultimate-forum-table-p element))
440 (throw 'found element))
441 (when (nth 2 element)
442 (nnultimate-find-forum-table-1 (nth 2 element))))))
444 (defun nnultimate-forum-table-p (parse)
445 (when (not (apply 'gnus-or
448 (nnweb-parse-find 'table p))
450 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
452 (when (and href (string-match nnultimate-table-regexp href))
455 (provide 'nnultimate)
458 ;; coding: iso-8859-1
461 ;;; nnultimate.el ends here