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 (let* ((last (car (last articles)))
75 (entry (assoc group nnultimate-groups))
77 (topics (nth 4 entry))
78 (mapping (nth 5 entry))
79 (old-total (or (nth 6 entry) 0))
80 (furl "forumdisplay.cgi?action=topics&number=%d&DaysPrune=1000")
81 headers article subject score from date lines parent point
82 contents tinfo fetchers map elem a href garticles topic old-max
83 inc datel table string current-page total-contents)
85 (nnweb-insert (concat nnultimate-address (format furl sid)))
86 (goto-char (point-min))
87 (setq contents (nth 2 (car (nth 2
88 (nnultimate-find-forum-table
89 (w3-parse-buffer (current-buffer)))))))
90 ;; The main idea here is to map Gnus article numbers to
91 ;; nnultimate article numbers. Say there are three topics in
92 ;; this forum, the first with 4 articles, the seconds with 2,
93 ;; and the third with 1. Then this will translate into 7 Gnus
94 ;; article numbers, where 1-4 comes from the first topic, 5-6
95 ;; from the second and 7 from the third. Now, then next time
96 ;; the group is entered, there's 2 new articles in topic one and
97 ;; 1 in topic three. Then Gnus article number 8-9 be 5-6 in
98 ;; topic one and 10 will be the 2 in topic three.
99 (dolist (row (cdr contents))
100 (setq row (nth 2 row))
101 (when (setq a (nnweb-parse-find 'a row))
102 (setq subject (car (last (nnweb-text a)))
103 href (cdr (assq 'href (nth 1 a))))
104 (let ((artlist (nreverse (nnweb-text row)))
106 (while (and (not art)
108 (when (string-match "^[0-9]+$" (car artlist))
109 (setq art (1+ (string-to-number (car artlist)))))
111 (setq garticles art))
112 (string-match "/\\([0-9]+\\).html" href)
113 (setq topic (string-to-number (match-string 1 href)))
114 (if (setq tinfo (assq topic topics))
116 (setq old-max (cadr tinfo))
117 (setcar (cdr tinfo) garticles))
119 (push (list topic garticles subject href) topics)
120 (setcar (nthcdr 4 entry) topics))
121 (when (not (= old-max garticles))
122 (setq inc (- garticles old-max))
123 (setq mapping (nconc mapping
126 (setq old-total (+ old-total inc))
127 topic (1+ old-max)))))
129 (setcar (nthcdr 5 entry) mapping))))
131 (while (and (setq article (car articles))
134 (> article (caar map)))
138 (<= article (caar map)))
139 (if (setq elem (assq (cadar map) fetchers))
140 (nconc elem (list (cons article
142 (- (caar map) article)))))
143 (push (list (cadar map) (cons article
145 (- (caar map) article))))
147 (setq article (car (setq articles (cdr articles))))))
148 ;; Now we have the mapping from/to Gnus/nnultimate article numbers,
149 ;; so we start fetching the topics that we need to satisfy the
153 (set-buffer nntp-server-buffer)
155 (setq nnultimate-articles nil)
157 (dolist (elem fetchers)
161 (while (<= current-page pages)
163 (setq subject (nth 2 (assq (car elem) topics)))
164 (setq href (nth 3 (assq (car elem) topics)))
165 (if (= current-page 1)
167 (string-match "\\.html$" href)
168 (nnweb-insert (concat (substring href 0 (match-beginning 0))
169 "-" (number-to-string current-page)
170 (match-string 0 href))))
171 (goto-char (point-min))
172 (setq contents (w3-parse-buffer (current-buffer)))
173 (setq table (nnultimate-find-forum-table contents))
174 (setq string (mapconcat 'identity (nnweb-text table) ""))
175 (when (string-match "topic is \\([0-9]\\) pages" string)
176 (setq pages (string-to-number (match-string 1 string)))
178 (setq table (nnultimate-find-forum-table contents)))
179 (setq contents (cdr (nth 2 (car (nth 2 table)))))
180 (setq total-contents (nconc total-contents contents))
182 (setq total-contents (nreverse total-contents))
183 (dolist (art (cdr elem))
184 (push (list (car art)
185 (nth (1- (cdr art)) total-contents)
187 nnultimate-articles))))
188 (setq nnultimate-articles
189 (sort nnultimate-articles 'car-less-than-car))
190 ;; Now we have all the articles, conveniently in an alist
191 ;; where the key is the Gnus article number.
192 (dolist (articlef nnultimate-articles)
193 (setq article (nth 0 articlef)
194 contents (nth 1 articlef)
195 subject (nth 2 articlef))
196 (setq from (mapconcat 'identity
197 (nnweb-text (car (nth 2 contents)))
199 datel (nnweb-text (nth 2 (car (cdr (nth 2 contents))))))
201 (when (string-match "Posted" (car datel))
202 (setq date (substring (car datel) (match-end 0))
205 (setq date (delete "" (split-string date "[- \n\t\r ]")))
206 (setq date (format "%s %s %s %s"
207 (car (rassq (string-to-number (nth 1 date))
209 (nth 0 date) (nth 2 date) (nth 3 date)))
213 (make-full-mail-header
216 (concat "<" (number-to-string sid) "%"
217 (number-to-string article)
220 (/ (length (mapconcat
223 (cdr (nth 2 (nth 1 (nth 2 contents)))))
228 (setq nnultimate-headers (sort headers 'car-less-than-car))
230 (set-buffer nntp-server-buffer)
232 (dolist (header nnultimate-headers)
233 (nnheader-insert-nov (cdr header))))))
234 (nnultimate-write-groups)
237 (deffoo nnultimate-request-group (group &optional server dont-check)
238 (nnultimate-possibly-change-server nil server)
239 (when (or (not dont-check)
240 (not nnultimate-groups))
241 (nnultimate-request-list))
242 (let ((elem (assoc group nnultimate-groups)))
245 (nnheader-report 'nnultimate "Group does not exist"))
247 (nnheader-report 'nnultimate "Opened group %s" group)
249 "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
250 (prin1-to-string group))))))
252 (deffoo nnultimate-request-article (article &optional group server buffer)
253 (nnultimate-possibly-change-server group server)
254 (let ((contents (cdr (assq article nnultimate-articles))))
255 (setq contents (cddr (nth 2 (nth 1 (nth 2 (car contents))))))
258 (set-buffer (or buffer nntp-server-buffer))
260 (nnweb-insert-html (cons 'p (cons nil (list contents))))
261 (goto-char (point-min))
262 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
263 (let ((header (cdr (assq article nnultimate-headers))))
264 (nnheader-insert-header header))
265 (nnheader-report 'nnultimate "Fetched article %s" article)
266 (cons group article)))))
268 (deffoo nnultimate-request-list (&optional server)
269 (nnultimate-possibly-change-server nil server)
271 (nnweb-insert (concat nnultimate-address "Ultimate.cgi"))
272 (let ((contents (nth 2 (car (nth 2
273 (nnultimate-find-forum-table
274 (w3-parse-buffer (current-buffer)))))))
275 sid elem description articles a href group forum
277 (dolist (row contents)
278 (setq row (nth 2 row))
279 (when (setq a (nnweb-parse-find 'a row))
280 (setq group (car (last (nnweb-text a)))
281 href (cdr (assq 'href (nth 1 a))))
282 (setq description (car (last (nnweb-text (nth 1 row)))))
283 (setq a1 (car (last (nnweb-text (nth 2 row)))))
284 (setq a2 (car (last (nnweb-text (nth 3 row)))))
285 (when (string-match "^[0-9]+$" a1)
286 (setq articles (string-to-number a1)))
287 (when (and a2 (string-match "^[0-9]+$" a2))
288 (setq articles (max articles (string-to-number a2))))
290 (string-match "number=\\([0-9]+\\)" href)
291 (setq forum (string-to-number (match-string 1 href)))
292 (if (setq elem (assoc group nnultimate-groups))
293 (setcar (cdr elem) articles)
294 (push (list group articles forum description nil nil nil)
295 nnultimate-groups))))))
296 (nnultimate-write-groups)
297 (nnultimate-generate-active)
300 (deffoo nnultimate-request-newgroups (date &optional server)
301 (nnultimate-possibly-change-server nil server)
302 (nnultimate-generate-active)
305 (nnoo-define-skeleton nnultimate)
307 ;;; Internal functions
309 (defun nnultimate-possibly-change-server (&optional group server)
310 (nnultimate-init server)
312 (not (nnultimate-server-opened server)))
313 (nnultimate-open-server server))
314 (unless nnultimate-groups-alist
315 (nnultimate-read-groups))
316 (setq nnultimate-groups (cdr (assoc nnultimate-address
317 nnultimate-groups-alist))))
319 (deffoo nnultimate-open-server (server &optional defs connectionless)
320 (nnheader-init-server-buffer)
321 (if (nnultimate-server-opened server)
323 (unless (assq 'nnultimate-address defs)
324 (setq defs (append defs (list (list 'nnultimate-address server)))))
325 (nnoo-change-server 'nnultimate server defs)))
327 (defun nnultimate-read-groups ()
328 (let ((file (expand-file-name "groups" nnultimate-directory)))
329 (when (file-exists-p file)
331 (insert-file-contents file)
332 (goto-char (point-min))
333 (setq nnultimate-groups-alist (read (current-buffer)))))))
335 (defun nnultimate-write-groups ()
336 (setq nnultimate-groups-alist
337 (delq (assoc nnultimate-address nnultimate-groups-alist)
338 nnultimate-groups-alist))
339 (push (cons nnultimate-address nnultimate-groups)
340 nnultimate-groups-alist)
341 (with-temp-file (expand-file-name "groups" nnultimate-directory)
342 (prin1 nnultimate-groups-alist (current-buffer))))
344 (defun nnultimate-init (server)
345 "Initialize buffers and such."
346 (unless (file-exists-p nnultimate-directory)
347 (gnus-make-directory nnultimate-directory)))
349 (defun nnultimate-date-to-date (sdate)
350 (let ((elem (split-string sdate)))
351 (concat (substring (nth 0 elem) 0 3) " "
352 (substring (nth 1 elem) 0 3) " "
353 (substring (nth 2 elem) 0 2) " "
354 (substring (nth 3 elem) 1 6) " "
355 (format-time-string "%Y") " "
358 (defun nnultimate-generate-active ()
360 (set-buffer nntp-server-buffer)
362 (dolist (elem nnultimate-groups)
363 (insert (prin1-to-string (car elem))
364 " " (number-to-string (cadr elem)) " 1 y\n"))))
366 (defun nnultimate-find-forum-table (contents)
368 (nnultimate-find-forum-table-1 contents)))
370 (defun nnultimate-find-forum-table-1 (contents)
371 (dolist (element contents)
372 (unless (stringp element)
373 (when (and (eq (car element) 'table)
374 (nnultimate-forum-table-p element))
375 (throw 'found element))
376 (when (nth 2 element)
377 (nnultimate-find-forum-table-1 (nth 2 element))))))
379 (defun nnultimate-forum-table-p (parse)
380 (when (not (apply 'gnus-or
383 (nnweb-parse-find 'table p))
385 (let ((href (cdr (assq 'href (nth 1 (nnweb-parse-find 'a parse 20)))))
387 (when (and href (string-match "forumdisplay\\|Forum[0-9]+/HTML\\|getbio"
391 (provide 'nnultimate)
393 ;;; nnultimate.el ends here