1 ;;; nnultimate.el --- interfacing with the Ultimate Bulletin Board system
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Note: You need to have `url' and `w3' installed for this
31 (eval-when-compile (require 'cl
))
42 (autoload 'w3-parse-buffer
"w3-parse")
44 (nnoo-declare nnultimate
)
46 (defvoo nnultimate-directory
(nnheader-concat gnus-directory
"ultimate/")
47 "Where nnultimate will save its files.")
49 (defvoo nnultimate-address
""
50 "The address of the Ultimate bulletin board.")
52 ;;; Internal variables
54 (defvar nnultimate-groups-alist nil
)
55 (defvoo nnultimate-groups nil
)
56 (defvoo nnultimate-headers nil
)
57 (defvoo nnultimate-articles nil
)
58 (defvar nnultimate-table-regexp
59 "postings.*editpost\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio")
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 (nnultimate-table-regexp
79 "postings.*editpost\\|forumdisplay\\|getbio")
80 headers article subject score from date lines parent point
81 contents tinfo fetchers map elem a href garticles topic old-max
82 inc datel table current-page total-contents pages
83 farticles forum-contents parse furl-fetched mmap farticle
)
85 (while (and (setq article
(car articles
))
87 ;; Skip past the articles in the map until we reach the
88 ;; article we're looking for.
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 (mm-url-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 (goto-char (point-min))
137 (when (re-search-forward "topic is \\([0-9]+\\) pages" nil t
)
138 (setq pages
(string-to-number (match-string 1))))
139 (setq contents
(cdr (nth 2 (car (nth 2 table
)))))
140 (setq total-contents
(nconc total-contents contents
))
144 (dolist (co total-contents
)
145 (push (list (or (nnultimate-topic-article-to-article
146 group
(car elem
) (incf i
))
149 nnultimate-articles
))))
151 (dolist (art (cdr elem
))
152 (when (nth (1- (cdr art
)) total-contents
)
153 (push (list (car art
)
154 (nth (1- (cdr art
)) total-contents
)
156 nnultimate-articles
))))))
157 (setq nnultimate-articles
158 (sort nnultimate-articles
'car-less-than-car
))
159 ;; Now we have all the articles, conveniently in an alist
160 ;; where the key is the Gnus article number.
161 (dolist (articlef nnultimate-articles
)
162 (setq article
(nth 0 articlef
)
163 contents
(nth 1 articlef
)
164 subject
(nth 2 articlef
))
165 (setq from
(mapconcat 'identity
166 (nnweb-text (car (nth 2 contents
)))
168 datel
(nnweb-text (nth 2 (car (cdr (nth 2 contents
))))))
170 (when (string-match "Posted" (car datel
))
171 (setq date
(substring (car datel
) (match-end 0))
175 (setq date
(delete "" (split-string date
"[-, \n\t\r ]")))
177 (if (or (member "AM" date
)
182 (if (and (>= (length (nth 0 date
)) 3)
184 (substring (nth 0 date
) 0 3))
186 (substring (nth 0 date
) 0 3)
187 (car (rassq (string-to-number (nth 0 date
))
189 (nth 2 date
) (nth 3 date
))
190 (format "%s %s %s %s"
191 (car (rassq (string-to-number (nth 1 date
))
193 (nth 0 date
) (nth 2 date
) (nth 3 date
)))))
197 (make-full-mail-header
200 (concat "<" (number-to-string sid
) "%"
201 (number-to-string article
)
202 "@ultimate." server
">")
204 (/ (length (mapconcat
207 (cdr (nth 2 (nth 1 (nth 2 contents
)))))
212 (setq nnultimate-headers
(sort headers
'car-less-than-car
))
214 (set-buffer nntp-server-buffer
)
215 (mm-with-unibyte-current-buffer
217 (dolist (header nnultimate-headers
)
218 (nnheader-insert-nov (cdr header
))))))
221 (defun nnultimate-topic-article-to-article (group topic article
)
223 (dolist (elem (nth 5 (assoc group nnultimate-groups
)))
224 (when (and (= topic
(nth 2 elem
))
225 (>= article
(nth 3 elem
))
226 (< article
(+ (- (nth 1 elem
) (nth 0 elem
)) 1
229 (+ (nth 0 elem
) (- article
(nth 3 elem
))))))))
231 (deffoo nnultimate-request-group
(group &optional server dont-check
)
232 (nnultimate-possibly-change-server nil server
)
233 (when (not nnultimate-groups
)
234 (nnultimate-request-list))
236 (nnultimate-create-mapping group
))
237 (let ((elem (assoc group nnultimate-groups
)))
240 (nnheader-report 'nnultimate
"Group does not exist"))
242 (nnheader-report 'nnultimate
"Opened group %s" group
)
244 "211 %d %d %d %s\n" (cadr elem
) 1 (cadr elem
)
245 (prin1-to-string group
))))))
247 (deffoo nnultimate-request-close
()
248 (setq nnultimate-groups-alist nil
249 nnultimate-groups nil
))
251 (deffoo nnultimate-request-article
(article &optional group server buffer
)
252 (nnultimate-possibly-change-server group server
)
253 (let ((contents (cdr (assq article nnultimate-articles
))))
254 (setq contents
(cddr (nth 2 (nth 1 (nth 2 (car contents
))))))
257 (set-buffer (or buffer nntp-server-buffer
))
259 (nnweb-insert-html (cons 'p
(cons nil
(list contents
))))
260 (goto-char (point-min))
261 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
262 (let ((header (cdr (assq article nnultimate-headers
))))
263 (mm-with-unibyte-current-buffer
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
)
270 (mm-with-unibyte-buffer
272 (if (string-match "/$" nnultimate-address
)
273 (concat nnultimate-address
"Ultimate.cgi")
275 (let ((contents (nth 2 (car (nth 2
276 (nnultimate-find-forum-table
277 (w3-parse-buffer (current-buffer)))))))
278 sid elem description articles a href group forum
280 (dolist (row contents
)
281 (setq row
(nth 2 row
))
282 (when (setq a
(nnweb-parse-find 'a row
))
283 (setq group
(car (last (nnweb-text a
)))
284 href
(cdr (assq 'href
(nth 1 a
))))
285 (setq description
(car (last (nnweb-text (nth 1 row
)))))
286 (setq a1
(car (last (nnweb-text (nth 2 row
)))))
287 (setq a2
(car (last (nnweb-text (nth 3 row
)))))
288 (when (string-match "^[0-9]+$" a1
)
289 (setq articles
(string-to-number a1
)))
290 (when (and a2
(string-match "^[0-9]+$" a2
))
291 (setq articles
(max articles
(string-to-number a2
))))
293 (string-match "number=\\([0-9]+\\)" href
)
294 (setq forum
(string-to-number (match-string 1 href
)))
295 (if (setq elem
(assoc group nnultimate-groups
))
296 (setcar (cdr elem
) articles
)
297 (push (list group articles forum description nil nil nil nil
)
298 nnultimate-groups
))))))
299 (nnultimate-write-groups)
300 (nnultimate-generate-active)
303 (deffoo nnultimate-request-newgroups
(date &optional server
)
304 (nnultimate-possibly-change-server nil server
)
305 (nnultimate-generate-active)
308 (nnoo-define-skeleton nnultimate
)
310 ;;; Internal functions
312 (defun nnultimate-prune-days (group time
)
313 "Compute the number of days to fetch info for."
314 (let ((old-time (nth 7 (assoc group nnultimate-groups
))))
317 (- (time-to-days time
) (time-to-days old-time
)))))
319 (defun nnultimate-create-mapping (group)
320 (let* ((entry (assoc group nnultimate-groups
))
322 (topics (nth 4 entry
))
323 (mapping (nth 5 entry
))
324 (old-total (or (nth 6 entry
) 1))
325 (current-time (current-time))
327 (concat "forumdisplay.cgi?action=topics&number=%d&DaysPrune="
329 (nnultimate-prune-days group current-time
))))
330 (furls (list (concat nnultimate-address
(format furl sid
))))
331 contents forum-contents furl-fetched a subject href
332 garticles topic tinfo old-max inc parse
)
333 (mm-with-unibyte-buffer
336 (mm-url-insert (pop furls
))
337 (goto-char (point-min))
338 (setq parse
(w3-parse-buffer (current-buffer)))
340 (cdr (nth 2 (car (nth 2 (nnultimate-find-forum-table
342 (setq forum-contents
(nconc contents forum-contents
))
344 (setq furl-fetched t
)
345 ;; On the first time through this loop, we find all the
347 (dolist (a (nnweb-parse-find-all 'a parse
))
348 (let ((href (cdr (assq 'href
(nth 1 a
)))))
350 (string-match "forumdisplay.*startpoint" href
))
352 (setq furls
(nreverse furls
))))
353 ;; The main idea here is to map Gnus article numbers to
354 ;; nnultimate article numbers. Say there are three topics in
355 ;; this forum, the first with 4 articles, the seconds with 2,
356 ;; and the third with 1. Then this will translate into 7 Gnus
357 ;; article numbers, where 1-4 comes from the first topic, 5-6
358 ;; from the second and 7 from the third. Now, then next time
359 ;; the group is entered, there's 2 new articles in topic one
360 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
361 ;; in topic one and 10 will be the 2 in topic three.
362 (dolist (row (nreverse forum-contents
))
363 (setq row
(nth 2 row
))
364 (when (setq a
(nnweb-parse-find 'a row
))
365 (setq subject
(car (last (nnweb-text a
)))
366 href
(cdr (assq 'href
(nth 1 a
))))
367 (let ((artlist (nreverse (nnweb-text row
)))
369 (while (and (not art
)
371 (when (string-match "^[0-9]+$" (car artlist
))
372 (setq art
(1+ (string-to-number (car artlist
)))))
374 (setq garticles art
))
376 (string-match "/\\([0-9]+\\).html" href
)
377 (setq topic
(string-to-number (match-string 1 href
)))
378 (if (setq tinfo
(assq topic topics
))
380 (setq old-max
(cadr tinfo
))
381 (setcar (cdr tinfo
) garticles
))
383 (push (list topic garticles subject href
) topics
)
384 (setcar (nthcdr 4 entry
) topics
))
385 (when (not (= old-max garticles
))
386 (setq inc
(- garticles old-max
))
387 (setq mapping
(nconc mapping
390 old-total
(1- (incf old-total inc
))
391 topic
(1+ old-max
)))))
393 (setcar (nthcdr 5 entry
) mapping
)
394 (setcar (nthcdr 6 entry
) old-total
))))))
395 (setcar (nthcdr 7 entry
) current-time
)
396 (setcar (nthcdr 1 entry
) (1- old-total
))
397 (nnultimate-write-groups)
400 (defun nnultimate-possibly-change-server (&optional group server
)
401 (nnultimate-init server
)
403 (not (nnultimate-server-opened server
)))
404 (nnultimate-open-server server
))
405 (unless nnultimate-groups-alist
406 (nnultimate-read-groups)
407 (setq nnultimate-groups
(cdr (assoc nnultimate-address
408 nnultimate-groups-alist
)))))
410 (deffoo nnultimate-open-server
(server &optional defs connectionless
)
411 (nnheader-init-server-buffer)
412 (if (nnultimate-server-opened server
)
414 (unless (assq 'nnultimate-address defs
)
415 (setq defs
(append defs
(list (list 'nnultimate-address server
)))))
416 (nnoo-change-server 'nnultimate server defs
)))
418 (defun nnultimate-read-groups ()
419 (setq nnultimate-groups-alist nil
)
420 (let ((file (expand-file-name "groups" nnultimate-directory
)))
421 (when (file-exists-p file
)
422 (mm-with-unibyte-buffer
423 (insert-file-contents file
)
424 (goto-char (point-min))
425 (setq nnultimate-groups-alist
(read (current-buffer)))))))
427 (defun nnultimate-write-groups ()
428 (setq nnultimate-groups-alist
429 (delq (assoc nnultimate-address nnultimate-groups-alist
)
430 nnultimate-groups-alist
))
431 (push (cons nnultimate-address nnultimate-groups
)
432 nnultimate-groups-alist
)
433 (with-temp-file (expand-file-name "groups" nnultimate-directory
)
434 (prin1 nnultimate-groups-alist
(current-buffer))))
436 (defun nnultimate-init (server)
437 "Initialize buffers and such."
438 (unless (file-exists-p nnultimate-directory
)
439 (gnus-make-directory nnultimate-directory
)))
441 (defun nnultimate-generate-active ()
443 (set-buffer nntp-server-buffer
)
445 (dolist (elem nnultimate-groups
)
446 (insert (prin1-to-string (car elem
))
447 " " (number-to-string (cadr elem
)) " 1 y\n"))))
449 (defun nnultimate-find-forum-table (contents)
451 (nnultimate-find-forum-table-1 contents
)))
453 (defun nnultimate-find-forum-table-1 (contents)
454 (dolist (element contents
)
455 (unless (stringp element
)
456 (when (and (eq (car element
) 'table
)
457 (nnultimate-forum-table-p element
))
458 (throw 'found element
))
459 (when (nth 2 element
)
460 (nnultimate-find-forum-table-1 (nth 2 element
))))))
462 (defun nnultimate-forum-table-p (parse)
463 (when (not (apply 'gnus-or
466 (nnweb-parse-find 'table p
))
468 (let ((href (cdr (assq 'href
(nth 1 (nnweb-parse-find 'a parse
20)))))
470 (when (and href
(string-match nnultimate-table-regexp href
))
473 (provide 'nnultimate
)
476 ;; coding: iso-8859-1
479 ;; arch-tag: ab6bfc45-8fe1-4647-9c78-41050eb152b8
480 ;;; nnultimate.el ends here