1 ;;; nnultimate.el --- interfacing with the Ultimate Bulletin Board system
2 ;; Copyright (C) 1999 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 (> article (caar map)))
87 (when (setq mmap (car map))
90 (<= article (nth 1 mmap)))
91 ;; Do we already have a fetcher for this topic?
92 (if (setq elem (assq (nth 2 mmap) fetchers))
93 ;; Yes, so we just add the spec to the end.
94 (nconc elem (list (cons article
95 (+ (nth 3 mmap) (incf farticle)))))
96 ;; No, so we add a new one.
97 (push (list (nth 2 mmap)
99 (+ (nth 3 mmap) (incf farticle))))
102 (setq article (car articles)))))
103 ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
104 ;; so we start fetching the topics that we need to satisfy the
108 (set-buffer nntp-server-buffer)
110 (setq nnultimate-articles nil)
111 (mm-with-unibyte-buffer
112 (dolist (elem fetchers)
116 (while (<= current-page pages)
118 (setq subject (nth 2 (assq (car elem) topics)))
119 (setq href (nth 3 (assq (car elem) topics)))
120 (if (= current-page 1)
122 (string-match "\\.html$" href)
123 (nnweb-insert (concat (substring href 0 (match-beginning 0))
124 "-" (number-to-string current-page)
125 (match-string 0 href))))
126 (goto-char (point-min))
127 (setq contents (w3-parse-buffer (current-buffer)))
128 (setq table (nnultimate-find-forum-table contents))
129 (setq string (mapconcat 'identity (nnweb-text table) ""))
130 (when (string-match "topic is \\([0-9]\\) pages" string)
131 (setq pages (string-to-number (match-string 1 string)))
133 (setq table (nnultimate-find-forum-table contents)))
134 (setq contents (cdr (nth 2 (car (nth 2 table)))))
135 (setq total-contents (nconc total-contents contents))
137 ;;(setq total-contents (nreverse total-contents))
138 (dolist (art (cdr elem))
139 (if (not (nth (1- (cdr art)) total-contents))
141 (push (list (car art)
142 (nth (1- (cdr art)) total-contents)
144 nnultimate-articles)))))
145 (setq nnultimate-articles
146 (sort nnultimate-articles 'car-less-than-car))
147 ;; Now we have all the articles, conveniently in an alist
148 ;; where the key is the Gnus article number.
149 (dolist (articlef nnultimate-articles)
150 (setq article (nth 0 articlef)
151 contents (nth 1 articlef)
152 subject (nth 2 articlef))
153 (setq from (mapconcat 'identity
154 (nnweb-text (car (nth 2 contents)))
156 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
158 (when (string-match "Posted" (car datel))
159 (setq date (substring (car datel) (match-end 0))
162 (setq date (delete "" (split-string date "[- \n\t\r ]")))
163 (if (or (member "AM" date)
165 (setq date (format "%s %s %s %s"
166 (car (rassq (string-to-number (nth 0 date))
168 (nth 1 date) (nth 2 date) (nth 3 date)))
169 (setq date (format "%s %s %s %s"
170 (car (rassq (string-to-number (nth 1 date))
172 (nth 0 date) (nth 2 date) (nth 3 date))))
176 (make-full-mail-header
179 (concat "<" (number-to-string sid) "%"
180 (number-to-string article)
183 (/ (length (mapconcat
186 (cdr (nth 2 (nth 1 (nth 2 contents)))))
191 (setq nnultimate-headers (sort headers 'car-less-than-car))
193 (set-buffer nntp-server-buffer)
194 (mm-with-unibyte-current-buffer
196 (dolist (header nnultimate-headers)
197 (nnheader-insert-nov (cdr header))))))
200 (deffoo nnultimate-request-group (group &optional server dont-check)
201 (nnultimate-possibly-change-server nil server)
202 (when (not nnultimate-groups)
203 (nnultimate-request-list))
205 (nnultimate-create-mapping group))
206 (let ((elem (assoc group nnultimate-groups)))
209 (nnheader-report 'nnultimate "Group does not exist"))
211 (nnheader-report 'nnultimate "Opened group %s" group)
213 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
214 (prin1-to-string group))))))
216 (deffoo nnultimate-request-article (article &optional group server buffer)
217 (nnultimate-possibly-change-server group server)
218 (let ((contents (cdr (assq article nnultimate-articles))))
219 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
222 (set-buffer (or buffer nntp-server-buffer))
224 (nnweb-insert-html (cons 'p (cons nil (list contents))))
225 (goto-char (point-min))
226 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
227 (let ((header (cdr (assq article nnultimate-headers))))
228 (mm-with-unibyte-current-buffer
229 (nnheader-insert-header header)))
230 (nnheader-report 'nnultimate "Fetched article %s" article)
231 (cons group article)))))
233 (deffoo nnultimate-request-list (&optional server)
234 (nnultimate-possibly-change-server nil server)
235 (mm-with-unibyte-buffer
237 (if (string-match "/$" nnultimate-address)
238 (concat nnultimate-address "Ultimate.cgi")
240 (let ((contents (nth 2 (car (nth 2
241 (nnultimate-find-forum-table
242 (w3-parse-buffer (current-buffer)))))))
243 sid elem description articles a href group forum
245 (dolist (row contents)
246 (setq row (nth 2 row))
247 (when (setq a (nnweb-parse-find 'a row))
248 (setq group (car (last (nnweb-text a)))
249 href (cdr (assq 'href (nth 1 a))))
250 (setq description (car (last (nnweb-text (nth 1 row)))))
251 (setq a1 (car (last (nnweb-text (nth 2 row)))))
252 (setq a2 (car (last (nnweb-text (nth 3 row)))))
253 (when (string-match "^[0-9]+$" a1)
254 (setq articles (string-to-number a1)))
255 (when (and a2 (string-match "^[0-9]+$" a2))
256 (setq articles (max articles (string-to-number a2))))
258 (string-match "number=\\([0-9]+\\)" href)
259 (setq forum (string-to-number (match-string 1 href)))
260 (if (setq elem (assoc group nnultimate-groups))
261 (setcar (cdr elem) articles)
262 (push (list group articles forum description nil nil nil nil)
263 nnultimate-groups))))))
264 (nnultimate-write-groups)
265 (nnultimate-generate-active)
268 (deffoo nnultimate-request-newgroups (date &optional server)
269 (nnultimate-possibly-change-server nil server)
270 (nnultimate-generate-active)
273 (nnoo-define-skeleton nnultimate)
275 ;;; Internal functions
277 (defun nnultimate-prune-days (group time)
278 "Compute the number of days to fetch info for."
279 (let ((old-time (nth 7 (assoc group nnultimate-groups))))
282 (- (time-to-days time) (time-to-days old-time)))))
284 (defun nnultimate-create-mapping (group)
285 (let* ((entry (assoc group nnultimate-groups))
287 (topics (nth 4 entry))
288 (mapping (nth 5 entry))
289 (old-total (or (nth 6 entry) 1))
290 (current-time (current-time))
292 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
294 (nnultimate-prune-days group current-time))))
295 (furls (list (concat nnultimate-address (format furl sid))))
296 contents forum-contents furl-fetched a subject href
297 garticles topic tinfo old-max inc parse)
298 (mm-with-unibyte-buffer
301 (nnweb-insert (pop furls))
302 (goto-char (point-min))
303 (setq parse (w3-parse-buffer (current-buffer)))
305 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
307 (setq forum-contents (nconc contents forum-contents))
309 (setq furl-fetched t)
310 ;; On the first time through this loop, we find all the
312 (dolist (a (nnweb-parse-find-all 'a parse))
313 (let ((href (cdr (assq 'href (nth 1 a)))))
315 (string-match "forumdisplay.*startpoint" href))
317 (setq furls (nreverse furls))))
318 ;; The main idea here is to map Gnus article numbers to
319 ;; nnultimate article numbers. Say there are three topics in
320 ;; this forum, the first with 4 articles, the seconds with 2,
321 ;; and the third with 1. Then this will translate into 7 Gnus
322 ;; article numbers, where 1-4 comes from the first topic, 5-6
323 ;; from the second and 7 from the third. Now, then next time
324 ;; the group is entered, there's 2 new articles in topic one
325 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
326 ;; in topic one and 10 will be the 2 in topic three.
327 (dolist (row (reverse forum-contents))
328 (setq row (nth 2 row))
329 (when (setq a (nnweb-parse-find 'a row))
330 (setq subject (car (last (nnweb-text a)))
331 href (cdr (assq 'href (nth 1 a))))
332 (let ((artlist (nreverse (nnweb-text row)))
334 (while (and (not art)
336 (when (string-match "^[0-9]+$" (car artlist))
337 (setq art (1+ (string-to-number (car artlist)))))
339 (setq garticles art))
340 (string-match "/\\([0-9]+\\).html" href)
341 (setq topic (string-to-number (match-string 1 href)))
342 (if (setq tinfo (assq topic topics))
344 (setq old-max (cadr tinfo))
345 (setcar (cdr tinfo) garticles))
347 (push (list topic garticles subject href) topics)
348 (setcar (nthcdr 4 entry) topics))
349 (when (not (= old-max garticles))
350 (setq inc (- garticles old-max))
351 (setq mapping (nconc mapping
354 old-total (1- (incf old-total inc))
355 topic (1+ old-max)))))
357 (setcar (nthcdr 5 entry) mapping)
358 (setcar (nthcdr 6 entry) old-total)))))
359 (setcar (nthcdr 7 entry) current-time)
360 (setcar (nthcdr 1 entry) (1- old-total))
361 (nnultimate-write-groups)
364 (defun nnultimate-possibly-change-server (&optional group server)
365 (nnultimate-init server)
367 (not (nnultimate-server-opened server)))
368 (nnultimate-open-server server))
369 (unless nnultimate-groups-alist
370 (nnultimate-read-groups)
371 (setq nnultimate-groups (cdr (assoc nnultimate-address
372 nnultimate-groups-alist)))))
374 (deffoo nnultimate-open-server (server &optional defs connectionless)
375 (nnheader-init-server-buffer)
376 (if (nnultimate-server-opened server)
378 (unless (assq 'nnultimate-address defs)
379 (setq defs (append defs (list (list 'nnultimate-address server)))))
380 (nnoo-change-server 'nnultimate server defs)))
382 (defun nnultimate-read-groups ()
383 (setq nnultimate-groups-alist nil)
384 (let ((file (expand-file-name "groups" nnultimate-directory)))
385 (when (file-exists-p file)
386 (mm-with-unibyte-buffer
387 (insert-file-contents file)
388 (goto-char (point-min))
389 (setq nnultimate-groups-alist (read (current-buffer)))))))
391 (defun nnultimate-write-groups ()
392 (setq nnultimate-groups-alist
393 (delq (assoc nnultimate-address nnultimate-groups-alist)
394 nnultimate-groups-alist))
395 (push (cons nnultimate-address nnultimate-groups)
396 nnultimate-groups-alist)
397 (with-temp-file (expand-file-name "groups" nnultimate-directory)
398 (prin1 nnultimate-groups-alist (current-buffer))))
400 (defun nnultimate-init (server)
401 "Initialize buffers and such."
402 (unless (file-exists-p nnultimate-directory)
403 (gnus-make-directory nnultimate-directory)))
405 (defun nnultimate-generate-active ()
407 (set-buffer nntp-server-buffer)
409 (dolist (elem nnultimate-groups)
410 (insert (prin1-to-string (car elem))
411 " " (number-to-string (cadr elem)) " 1 y\n"))))
413 (defun nnultimate-find-forum-table (contents)
415 (nnultimate-find-forum-table-1 contents)))
417 (defun nnultimate-find-forum-table-1 (contents)
418 (dolist (element contents)
419 (unless (stringp element)
420 (when (and (eq (car element) 'table)
421 (nnultimate-forum-table-p element))
422 (throw 'found element))
423 (when (nth 2 element)
424 (nnultimate-find-forum-table-1 (nth 2 element))))))
426 (defun nnultimate-forum-table-p (parse)
427 (when (not (apply 'gnus-or
430 (nnweb-parse-find 'table p))
432 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
434 (when (and href (string-match
435 "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
439 (provide 'nnultimate)
441 ;;; nnultimate.el ends here