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))
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)
61 ;;; Interface functions
63 (nnoo-define-basics nnultimate)
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)))
71 (entry (assoc group nnultimate-groups))
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)
83 (while (and (setq article (car articles))
86 (or (> article (caar map))
87 (< (cadar map) (caar map))))
89 (when (setq mmap (car map))
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)
101 (+ (nth 3 mmap) (incf farticle))))
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
110 (set-buffer nntp-server-buffer)
112 (setq nnultimate-articles nil)
113 (mm-with-unibyte-buffer
114 (dolist (elem fetchers)
118 (while (<= current-page pages)
120 (setq subject (nth 2 (assq (car elem) topics)))
121 (setq href (nth 3 (assq (car elem) topics)))
122 (if (= current-page 1)
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))
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)))
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))
140 ;;(setq total-contents (nreverse total-contents))
141 (dolist (art (cdr elem))
142 (if (not (nth (1- (cdr art)) total-contents))
144 (push (list (car art)
145 (nth (1- (cdr art)) total-contents)
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)))
159 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
161 (when (string-match "Posted" (car datel))
162 (setq date (substring (car datel) (match-end 0))
165 (setq date (delete "" (split-string date "[- \n\t\r ]")))
166 (if (or (member "AM" date)
168 (setq date (format "%s %s %s %s"
169 (car (rassq (string-to-number (nth 0 date))
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))
175 (nth 0 date) (nth 2 date) (nth 3 date))))
179 (make-full-mail-header
182 (concat "<" (number-to-string sid) "%"
183 (number-to-string article)
186 (/ (length (mapconcat
189 (cdr (nth 2 (nth 1 (nth 2 contents)))))
194 (setq nnultimate-headers (sort headers 'car-less-than-car))
196 (set-buffer nntp-server-buffer)
197 (mm-with-unibyte-current-buffer
199 (dolist (header nnultimate-headers)
200 (nnheader-insert-nov (cdr header))))))
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))
208 (nnultimate-create-mapping group))
209 (let ((elem (assoc group nnultimate-groups)))
212 (nnheader-report 'nnultimate "Group does not exist"))
214 (nnheader-report 'nnultimate "Opened group %s" group)
216 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
217 (prin1-to-string group))))))
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))))))
225 (set-buffer (or buffer nntp-server-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)))))
236 (deffoo nnultimate-request-list (&optional server)
237 (nnultimate-possibly-change-server nil server)
238 (mm-with-unibyte-buffer
240 (if (string-match "/$" nnultimate-address)
241 (concat nnultimate-address "Ultimate.cgi")
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
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))))
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)
271 (deffoo nnultimate-request-newgroups (date &optional server)
272 (nnultimate-possibly-change-server nil server)
273 (nnultimate-generate-active)
276 (nnoo-define-skeleton nnultimate)
278 ;;; Internal functions
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))))
285 (- (time-to-days time) (time-to-days old-time)))))
287 (defun nnultimate-create-mapping (group)
288 (let* ((entry (assoc group nnultimate-groups))
290 (topics (nth 4 entry))
291 (mapping (nth 5 entry))
292 (old-total (or (nth 6 entry) 1))
293 (current-time (current-time))
295 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
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
304 (nnweb-insert (pop furls))
305 (goto-char (point-min))
306 (setq parse (w3-parse-buffer (current-buffer)))
308 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
310 (setq forum-contents (nconc contents forum-contents))
312 (setq furl-fetched t)
313 ;; On the first time through this loop, we find all the
315 (dolist (a (nnweb-parse-find-all 'a parse))
316 (let ((href (cdr (assq 'href (nth 1 a)))))
318 (string-match "forumdisplay.*startpoint" href))
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)))
337 (while (and (not art)
339 (when (string-match "^[0-9]+$" (car artlist))
340 (setq art (1+ (string-to-number (car artlist)))))
342 (setq garticles art))
344 (string-match "/\\([0-9]+\\).html" href)
345 (setq topic (string-to-number (match-string 1 href)))
346 (if (setq tinfo (assq topic topics))
348 (setq old-max (cadr tinfo))
349 (setcar (cdr tinfo) garticles))
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
358 old-total (1- (incf old-total inc))
359 topic (1+ old-max)))))
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)
368 (defun nnultimate-possibly-change-server (&optional group server)
369 (nnultimate-init 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)))))
378 (deffoo nnultimate-open-server (server &optional defs connectionless)
379 (nnheader-init-server-buffer)
380 (if (nnultimate-server-opened server)
382 (unless (assq 'nnultimate-address defs)
383 (setq defs (append defs (list (list 'nnultimate-address server)))))
384 (nnoo-change-server 'nnultimate server defs)))
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)))))))
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))))
404 (defun nnultimate-init (server)
405 "Initialize buffers and such."
406 (unless (file-exists-p nnultimate-directory)
407 (gnus-make-directory nnultimate-directory)))
409 (defun nnultimate-generate-active ()
411 (set-buffer nntp-server-buffer)
413 (dolist (elem nnultimate-groups)
414 (insert (prin1-to-string (car elem))
415 " " (number-to-string (cadr elem)) " 1 y\n"))))
417 (defun nnultimate-find-forum-table (contents)
419 (nnultimate-find-forum-table-1 contents)))
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))))))
430 (defun nnultimate-forum-table-p (parse)
431 (when (not (apply 'gnus-or
434 (nnweb-parse-find 'table p))
436 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
438 (when (and href (string-match
439 "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
443 (provide 'nnultimate)
446 ;; coding: iso-8859-1
449 ;;; nnultimate.el ends here