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)
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 string 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 (setq string (mapconcat 'identity (nnweb-text table) ""))
137 (when (string-match "topic is \\([0-9]\\) pages" string)
138 (setq pages (string-to-number (match-string 1 string)))
140 (setq table (nnultimate-find-forum-table contents)))
141 (setq contents (cdr (nth 2 (car (nth 2 table)))))
142 (setq total-contents (nconc total-contents contents))
144 ;;(setq total-contents (nreverse total-contents))
145 (dolist (art (cdr elem))
146 (if (not (nth (1- (cdr art)) total-contents))
148 (push (list (car art)
149 (nth (1- (cdr art)) total-contents)
151 nnultimate-articles)))))
152 (setq nnultimate-articles
153 (sort nnultimate-articles 'car-less-than-car))
154 ;; Now we have all the articles, conveniently in an alist
155 ;; where the key is the Gnus article number.
156 (dolist (articlef nnultimate-articles)
157 (setq article (nth 0 articlef)
158 contents (nth 1 articlef)
159 subject (nth 2 articlef))
160 (setq from (mapconcat 'identity
161 (nnweb-text (car (nth 2 contents)))
163 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
165 (when (string-match "Posted" (car datel))
166 (setq date (substring (car datel) (match-end 0))
169 (setq date (delete "" (split-string date "[- \n\t\r
\81 \81 \81 ]")))
170 (if (or (member "AM" date)
172 (setq date (format "%s %s %s %s"
173 (car (rassq (string-to-number (nth 0 date))
175 (nth 1 date) (nth 2 date) (nth 3 date)))
176 (setq date (format "%s %s %s %s"
177 (car (rassq (string-to-number (nth 1 date))
179 (nth 0 date) (nth 2 date) (nth 3 date))))
183 (make-full-mail-header
186 (concat "<" (number-to-string sid) "%"
187 (number-to-string article)
190 (/ (length (mapconcat
193 (cdr (nth 2 (nth 1 (nth 2 contents)))))
198 (setq nnultimate-headers (sort headers 'car-less-than-car))
200 (set-buffer nntp-server-buffer)
201 (mm-with-unibyte-current-buffer
203 (dolist (header nnultimate-headers)
204 (nnheader-insert-nov (cdr header))))))
207 (deffoo nnultimate-request-group (group &optional server dont-check)
208 (nnultimate-possibly-change-server nil server)
209 (when (not nnultimate-groups)
210 (nnultimate-request-list))
212 (nnultimate-create-mapping group))
213 (let ((elem (assoc group nnultimate-groups)))
216 (nnheader-report 'nnultimate "Group does not exist"))
218 (nnheader-report 'nnultimate "Opened group %s" group)
220 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
221 (prin1-to-string group))))))
223 (deffoo nnultimate-request-close ()
224 (setq nnultimate-groups-alist nil
225 nnultimate-groups nil))
227 (deffoo nnultimate-request-article (article &optional group server buffer)
228 (nnultimate-possibly-change-server group server)
229 (let ((contents (cdr (assq article nnultimate-articles))))
230 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
233 (set-buffer (or buffer nntp-server-buffer))
235 (nnweb-insert-html (cons 'p (cons nil (list contents))))
236 (goto-char (point-min))
237 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
238 (let ((header (cdr (assq article nnultimate-headers))))
239 (mm-with-unibyte-current-buffer
240 (nnheader-insert-header header)))
241 (nnheader-report 'nnultimate "Fetched article %s" article)
242 (cons group article)))))
244 (deffoo nnultimate-request-list (&optional server)
245 (nnultimate-possibly-change-server nil server)
246 (mm-with-unibyte-buffer
248 (if (string-match "/$" nnultimate-address)
249 (concat nnultimate-address "Ultimate.cgi")
251 (let ((contents (nth 2 (car (nth 2
252 (nnultimate-find-forum-table
253 (w3-parse-buffer (current-buffer)))))))
254 sid elem description articles a href group forum
256 (dolist (row contents)
257 (setq row (nth 2 row))
258 (when (setq a (nnweb-parse-find 'a row))
259 (setq group (car (last (nnweb-text a)))
260 href (cdr (assq 'href (nth 1 a))))
261 (setq description (car (last (nnweb-text (nth 1 row)))))
262 (setq a1 (car (last (nnweb-text (nth 2 row)))))
263 (setq a2 (car (last (nnweb-text (nth 3 row)))))
264 (when (string-match "^[0-9]+$" a1)
265 (setq articles (string-to-number a1)))
266 (when (and a2 (string-match "^[0-9]+$" a2))
267 (setq articles (max articles (string-to-number a2))))
269 (string-match "number=\\([0-9]+\\)" href)
270 (setq forum (string-to-number (match-string 1 href)))
271 (if (setq elem (assoc group nnultimate-groups))
272 (setcar (cdr elem) articles)
273 (push (list group articles forum description nil nil nil nil)
274 nnultimate-groups))))))
275 (nnultimate-write-groups)
276 (nnultimate-generate-active)
279 (deffoo nnultimate-request-newgroups (date &optional server)
280 (nnultimate-possibly-change-server nil server)
281 (nnultimate-generate-active)
284 (nnoo-define-skeleton nnultimate)
286 ;;; Internal functions
288 (defun nnultimate-prune-days (group time)
289 "Compute the number of days to fetch info for."
290 (let ((old-time (nth 7 (assoc group nnultimate-groups))))
293 (- (time-to-days time) (time-to-days old-time)))))
295 (defun nnultimate-create-mapping (group)
296 (let* ((entry (assoc group nnultimate-groups))
298 (topics (nth 4 entry))
299 (mapping (nth 5 entry))
300 (old-total (or (nth 6 entry) 1))
301 (current-time (current-time))
303 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
305 (nnultimate-prune-days group current-time))))
306 (furls (list (concat nnultimate-address (format furl sid))))
307 contents forum-contents furl-fetched a subject href
308 garticles topic tinfo old-max inc parse)
309 (mm-with-unibyte-buffer
312 (nnweb-insert (pop furls))
313 (goto-char (point-min))
314 (setq parse (w3-parse-buffer (current-buffer)))
316 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
318 (setq forum-contents (nconc contents forum-contents))
320 (setq furl-fetched t)
321 ;; On the first time through this loop, we find all the
323 (dolist (a (nnweb-parse-find-all 'a parse))
324 (let ((href (cdr (assq 'href (nth 1 a)))))
326 (string-match "forumdisplay.*startpoint" href))
328 (setq furls (nreverse furls))))
329 ;; The main idea here is to map Gnus article numbers to
330 ;; nnultimate article numbers. Say there are three topics in
331 ;; this forum, the first with 4 articles, the seconds with 2,
332 ;; and the third with 1. Then this will translate into 7 Gnus
333 ;; article numbers, where 1-4 comes from the first topic, 5-6
334 ;; from the second and 7 from the third. Now, then next time
335 ;; the group is entered, there's 2 new articles in topic one
336 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
337 ;; in topic one and 10 will be the 2 in topic three.
338 (dolist (row (nreverse forum-contents))
339 (setq row (nth 2 row))
340 (when (setq a (nnweb-parse-find 'a row))
341 (setq subject (car (last (nnweb-text a)))
342 href (cdr (assq 'href (nth 1 a))))
343 (let ((artlist (nreverse (nnweb-text row)))
345 (while (and (not art)
347 (when (string-match "^[0-9]+$" (car artlist))
348 (setq art (1+ (string-to-number (car artlist)))))
350 (setq garticles art))
352 (string-match "/\\([0-9]+\\).html" href)
353 (setq topic (string-to-number (match-string 1 href)))
354 (if (setq tinfo (assq topic topics))
356 (setq old-max (cadr tinfo))
357 (setcar (cdr tinfo) garticles))
359 (push (list topic garticles subject href) topics)
360 (setcar (nthcdr 4 entry) topics))
361 (when (not (= old-max garticles))
362 (setq inc (- garticles old-max))
363 (setq mapping (nconc mapping
366 old-total (1- (incf old-total inc))
367 topic (1+ old-max)))))
369 (setcar (nthcdr 5 entry) mapping)
370 (setcar (nthcdr 6 entry) old-total))))))
371 (setcar (nthcdr 7 entry) current-time)
372 (setcar (nthcdr 1 entry) (1- old-total))
373 (nnultimate-write-groups)
376 (defun nnultimate-possibly-change-server (&optional group server)
377 (nnultimate-init server)
379 (not (nnultimate-server-opened server)))
380 (nnultimate-open-server server))
381 (unless nnultimate-groups-alist
382 (nnultimate-read-groups)
383 (setq nnultimate-groups (cdr (assoc nnultimate-address
384 nnultimate-groups-alist)))))
386 (deffoo nnultimate-open-server (server &optional defs connectionless)
387 (nnheader-init-server-buffer)
388 (if (nnultimate-server-opened server)
390 (unless (assq 'nnultimate-address defs)
391 (setq defs (append defs (list (list 'nnultimate-address server)))))
392 (nnoo-change-server 'nnultimate server defs)))
394 (defun nnultimate-read-groups ()
395 (setq nnultimate-groups-alist nil)
396 (let ((file (expand-file-name "groups" nnultimate-directory)))
397 (when (file-exists-p file)
398 (mm-with-unibyte-buffer
399 (insert-file-contents file)
400 (goto-char (point-min))
401 (setq nnultimate-groups-alist (read (current-buffer)))))))
403 (defun nnultimate-write-groups ()
404 (setq nnultimate-groups-alist
405 (delq (assoc nnultimate-address nnultimate-groups-alist)
406 nnultimate-groups-alist))
407 (push (cons nnultimate-address nnultimate-groups)
408 nnultimate-groups-alist)
409 (with-temp-file (expand-file-name "groups" nnultimate-directory)
410 (prin1 nnultimate-groups-alist (current-buffer))))
412 (defun nnultimate-init (server)
413 "Initialize buffers and such."
414 (unless (file-exists-p nnultimate-directory)
415 (gnus-make-directory nnultimate-directory)))
417 (defun nnultimate-generate-active ()
419 (set-buffer nntp-server-buffer)
421 (dolist (elem nnultimate-groups)
422 (insert (prin1-to-string (car elem))
423 " " (number-to-string (cadr elem)) " 1 y\n"))))
425 (defun nnultimate-find-forum-table (contents)
427 (nnultimate-find-forum-table-1 contents)))
429 (defun nnultimate-find-forum-table-1 (contents)
430 (dolist (element contents)
431 (unless (stringp element)
432 (when (and (eq (car element) 'table)
433 (nnultimate-forum-table-p element))
434 (throw 'found element))
435 (when (nth 2 element)
436 (nnultimate-find-forum-table-1 (nth 2 element))))))
438 (defun nnultimate-forum-table-p (parse)
439 (when (not (apply 'gnus-or
442 (nnweb-parse-find 'table p))
444 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
446 (when (and href (string-match nnultimate-table-regexp href))
449 (provide 'nnultimate)
452 ;; coding: iso-8859-1
455 ;;; nnultimate.el ends here