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))
45 ;; Report failure to find w3 at load time if appropriate.
51 (nnoo-declare nnultimate)
53 (defvoo nnultimate-directory (nnheader-concat gnus-directory "ultimate/")
54 "Where nnultimate will save its files.")
56 (defvoo nnultimate-address ""
57 "The address of the Ultimate bulletin board.")
59 ;;; Internal variables
61 (defvar nnultimate-groups-alist nil)
62 (defvoo nnultimate-groups nil)
63 (defvoo nnultimate-headers nil)
64 (defvoo nnultimate-articles nil)
66 ;;; Interface functions
68 (nnoo-define-basics nnultimate)
70 (deffoo nnultimate-retrieve-headers (articles &optional group server fetch-old)
71 (nnultimate-possibly-change-server group server)
72 (unless gnus-nov-is-evil
73 (let* ((last (car (last articles)))
76 (entry (assoc group nnultimate-groups))
78 (topics (nth 4 entry))
79 (mapping (nth 5 entry))
80 (old-total (or (nth 6 entry) 1))
81 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
82 (furls (list (concat nnultimate-address (format furl sid))))
83 headers article subject score from date lines parent point
84 contents tinfo fetchers map elem a href garticles topic old-max
85 inc datel table string current-page total-contents pages
86 farticles forum-contents parse furl-fetched)
88 (while (and (setq article (car articles))
91 (> article (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)
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))
133 (setq contents (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 contents total-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 date "[- \n\t\r ]")))
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)
195 (dolist (header nnultimate-headers)
196 (nnheader-insert-nov (cdr header)))))
197 (setcar (nthcdr 6 entry) (nth 1 entry))
198 (nnultimate-write-groups)
201 (deffoo nnultimate-request-group (group &optional server dont-check)
202 (nnultimate-possibly-change-server nil server)
203 (when (or (not dont-check)
204 (not nnultimate-groups))
205 (nnultimate-request-list))
207 (nnultimate-create-mapping group))
208 (let ((elem (assoc group nnultimate-groups)))
211 (nnheader-report 'nnultimate "Group does not exist"))
213 (nnheader-report 'nnultimate "Opened group %s" group)
215 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
216 (prin1-to-string group))))))
218 (deffoo nnultimate-request-article (article &optional group server buffer)
219 (nnultimate-possibly-change-server group server)
220 (let ((contents (cdr (assq article nnultimate-articles))))
221 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
224 (set-buffer (or buffer nntp-server-buffer))
226 (nnweb-insert-html (cons 'p (cons nil (list contents))))
227 (goto-char (point-min))
228 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
229 (let ((header (cdr (assq article nnultimate-headers))))
230 (nnheader-insert-header header))
231 (nnheader-report 'nnultimate "Fetched article %s" article)
232 (cons group article)))))
234 (deffoo nnultimate-request-list (&optional server)
235 (nnultimate-possibly-change-server nil server)
237 (nnweb-insert (concat nnultimate-address "Ultimate.cgi"))
238 (let ((contents (nth 2 (car (nth 2
239 (nnultimate-find-forum-table
240 (w3-parse-buffer (current-buffer)))))))
241 sid elem description articles a href group forum
243 (dolist (row contents)
244 (setq row (nth 2 row))
245 (when (setq a (nnweb-parse-find 'a row))
246 (setq group (car (last (nnweb-text a)))
247 href (cdr (assq 'href (nth 1 a))))
248 (setq description (car (last (nnweb-text (nth 1 row)))))
249 (setq a1 (car (last (nnweb-text (nth 2 row)))))
250 (setq a2 (car (last (nnweb-text (nth 3 row)))))
251 (when (string-match "^[0-9]+$" a1)
252 (setq articles (string-to-number a1)))
253 (when (and a2 (string-match "^[0-9]+$" a2))
254 (setq articles (max articles (string-to-number a2))))
256 (string-match "number=\\([0-9]+\\)" href)
257 (setq forum (string-to-number (match-string 1 href)))
258 (if (setq elem (assoc group nnultimate-groups))
259 (setcar (cdr elem) articles)
260 (push (list group articles forum description nil nil nil)
261 nnultimate-groups))))))
262 (nnultimate-write-groups)
263 (nnultimate-generate-active)
266 (deffoo nnultimate-request-newgroups (date &optional server)
267 (nnultimate-possibly-change-server nil server)
268 (nnultimate-generate-active)
271 (nnoo-define-skeleton nnultimate)
273 ;;; Internal functions
276 (defun nnultimate-create-mapping (group)
277 (let* ((entry (assoc group nnultimate-groups))
279 (topics (nth 4 entry))
280 (mapping (nth 5 entry))
281 (old-total (or (nth 6 entry) 1))
282 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
283 (furls (list (concat nnultimate-address (format furl sid))))
284 contents forum-contents furl-fetched a subject href
285 garticles topic tinfo old-max inc)
289 (nnweb-insert (pop furls))
290 (goto-char (point-min))
291 (setq parse (w3-parse-buffer (current-buffer)))
293 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
295 (setq forum-contents (nconc contents forum-contents))
296 (when (and (not mapping)
298 (setq furl-fetched t)
299 ;; On the first mapping, we fetch all the forum URLs.
300 (dolist (a (nnweb-parse-find-all 'a parse))
301 (let ((href (cdr (assq 'href (nth 1 a)))))
303 (string-match "forumdisplay.*startpoint" href))
305 (setq furls (nreverse furls))))
306 ;; The main idea here is to map Gnus article numbers to
307 ;; nnultimate article numbers. Say there are three topics in
308 ;; this forum, the first with 4 articles, the seconds with 2,
309 ;; and the third with 1. Then this will translate into 7 Gnus
310 ;; article numbers, where 1-4 comes from the first topic, 5-6
311 ;; from the second and 7 from the third. Now, then next time
312 ;; the group is entered, there's 2 new articles in topic one
313 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
314 ;; in topic one and 10 will be the 2 in topic three.
315 (dolist (row (reverse forum-contents))
316 (setq row (nth 2 row))
317 (when (setq a (nnweb-parse-find 'a row))
318 (setq subject (car (last (nnweb-text a)))
319 href (cdr (assq 'href (nth 1 a))))
320 (let ((artlist (nreverse (nnweb-text row)))
322 (while (and (not art)
324 (when (string-match "^[0-9]+$" (car artlist))
325 (setq art (1+ (string-to-number (car artlist)))))
327 (setq garticles art))
328 (string-match "/\\([0-9]+\\).html" href)
329 (setq topic (string-to-number (match-string 1 href)))
330 (if (setq tinfo (assq topic topics))
332 (setq old-max (cadr tinfo))
333 (setcar (cdr tinfo) garticles))
335 (push (list topic garticles subject href) topics)
336 (setcar (nthcdr 4 entry) topics))
337 (when (not (= old-max garticles))
338 (setq inc (- garticles old-max))
339 (setq mapping (nconc mapping
342 old-total (1- (incf old-total inc))
343 topic (1+ old-max)))))
345 (setcar (nthcdr 5 entry) mapping)
346 (setcar (nthcdr 6 entry) old-total)))))
347 (nnultimate-write-groups)
350 (defun nnultimate-possibly-change-server (&optional group server)
351 (nnultimate-init server)
353 (not (nnultimate-server-opened server)))
354 (nnultimate-open-server server))
355 ; (unless nnultimate-groups-alist
356 (nnultimate-read-groups)
357 (setq nnultimate-groups (cdr (assoc nnultimate-address
358 nnultimate-groups-alist))))
360 (deffoo nnultimate-open-server (server &optional defs connectionless)
361 (nnheader-init-server-buffer)
362 (if (nnultimate-server-opened server)
364 (unless (assq 'nnultimate-address defs)
365 (setq defs (append defs (list (list 'nnultimate-address server)))))
366 (nnoo-change-server 'nnultimate server defs)))
368 (defun nnultimate-read-groups ()
369 (setq nnultimate-groups-alist nil)
370 (let ((file (expand-file-name "groups" nnultimate-directory)))
371 (when (file-exists-p file)
373 (insert-file-contents file)
374 (goto-char (point-min))
375 (setq nnultimate-groups-alist (read (current-buffer)))))))
377 (defun nnultimate-write-groups ()
378 (setq nnultimate-groups-alist
379 (delq (assoc nnultimate-address nnultimate-groups-alist)
380 nnultimate-groups-alist))
381 (push (cons nnultimate-address nnultimate-groups)
382 nnultimate-groups-alist)
383 (with-temp-file (expand-file-name "groups" nnultimate-directory)
384 (prin1 nnultimate-groups-alist (current-buffer))))
386 (defun nnultimate-init (server)
387 "Initialize buffers and such."
388 (unless (file-exists-p nnultimate-directory)
389 (gnus-make-directory nnultimate-directory)))
391 (defun nnultimate-date-to-date (sdate)
392 (let ((elem (split-string sdate)))
393 (concat (substring (nth 0 elem) 0 3) " "
394 (substring (nth 1 elem) 0 3) " "
395 (substring (nth 2 elem) 0 2) " "
396 (substring (nth 3 elem) 1 6) " "
397 (format-time-string "%Y") " "
400 (defun nnultimate-generate-active ()
402 (set-buffer nntp-server-buffer)
404 (dolist (elem nnultimate-groups)
405 (insert (prin1-to-string (car elem))
406 " " (number-to-string (cadr elem)) " 1 y\n"))))
408 (defun nnultimate-find-forum-table (contents)
410 (nnultimate-find-forum-table-1 contents)))
412 (defun nnultimate-find-forum-table-1 (contents)
413 (dolist (element contents)
414 (unless (stringp element)
415 (when (and (eq (car element) 'table)
416 (nnultimate-forum-table-p element))
417 (throw 'found element))
418 (when (nth 2 element)
419 (nnultimate-find-forum-table-1 (nth 2 element))))))
421 (defun nnultimate-forum-table-p (parse)
422 (when (not (apply 'gnus-or
425 (nnweb-parse-find 'table p))
427 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
429 (when (and href (string-match
430 "postings\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
434 (provide 'nnultimate)
436 ;;; nnultimate.el ends here