1 ;;; nnwfm.el --- interfacing with a web forum
3 ;; Copyright (C) 2000, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 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
))
41 (autoload 'w3-parse-buffer
"w3-parse")
45 (defvoo nnwfm-directory
(nnheader-concat gnus-directory
"wfm/")
46 "Where nnwfm will save its files.")
48 (defvoo nnwfm-address
""
49 "The address of the Ultimate bulletin board.")
51 ;;; Internal variables
53 (defvar nnwfm-groups-alist nil
)
54 (defvoo nnwfm-groups nil
)
55 (defvoo nnwfm-headers nil
)
56 (defvoo nnwfm-articles nil
)
57 (defvar nnwfm-table-regexp
58 "postings.*editpost\\|forumdisplay\\|Forum[0-9]+/HTML\\|getbio")
60 ;;; Interface functions
62 (nnoo-define-basics nnwfm
)
64 (deffoo nnwfm-retrieve-headers
(articles &optional group server fetch-old
)
65 (nnwfm-possibly-change-server group server
)
66 (unless gnus-nov-is-evil
67 (let* ((last (car (last articles
)))
70 (entry (assoc group nnwfm-groups
))
72 (topics (nth 4 entry
))
73 (mapping (nth 5 entry
))
74 (old-total (or (nth 6 entry
) 1))
75 (nnwfm-table-regexp "Thread.asp")
76 headers article subject score from date lines parent point
77 contents tinfo fetchers map elem a href garticles topic old-max
78 inc datel table string current-page total-contents pages
79 farticles forum-contents parse furl-fetched mmap farticle
80 thread-id tables hstuff bstuff time
)
82 (while (and (setq article
(car articles
))
85 (or (> article
(caar map
))
86 (< (cadar map
) (caar map
))))
88 (when (setq mmap
(car map
))
91 (<= article
(nth 1 mmap
)))
92 ;; Do we already have a fetcher for this topic?
93 (if (setq elem
(assq (nth 2 mmap
) fetchers
))
94 ;; Yes, so we just add the spec to the end.
95 (nconc elem
(list (cons article
96 (+ (nth 3 mmap
) (incf farticle
)))))
97 ;; No, so we add a new one.
98 (push (list (nth 2 mmap
)
100 (+ (nth 3 mmap
) (incf farticle
))))
103 (setq article
(car articles
)))))
104 ;; Now we have the mapping from/to Gnus/nnwfm article numbers,
105 ;; so we start fetching the topics that we need to satisfy the
109 (set-buffer nntp-server-buffer
)
111 (setq nnwfm-articles nil
)
112 (mm-with-unibyte-buffer
113 (dolist (elem fetchers
)
115 (setq subject
(nth 2 (assq (car elem
) topics
))
116 thread-id
(nth 0 (assq (car elem
) topics
)))
118 (concat nnwfm-address
119 (format "Item.asp?GroupID=%d&ThreadID=%d" sid
121 (goto-char (point-min))
127 (w3-parse-buffer (current-buffer)))))))))
128 (setq tables
(cdr (caddar (memq (assq 'div tables
) tables
))))
130 (dolist (table tables
)
131 (when (eq (car table
) 'table
)
132 (setq table
(caddar (caddar (caddr table
)))
133 hstuff
(delete ":link" (nnweb-text (car table
)))
134 bstuff
(car (caddar (cdr table
)))
137 (setq time
(nnwfm-date-to-time (nth 2 hstuff
)))
138 (push (list from time bstuff
) contents
))))
139 (setq contents
(nreverse contents
))
140 (dolist (art (cdr elem
))
141 (push (list (car art
)
142 (nth (1- (cdr art
)) contents
)
146 (sort nnwfm-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 nnwfm-articles
)
150 (setq article
(nth 0 articlef
)
151 contents
(nth 1 articlef
)
152 subject
(nth 2 articlef
))
153 (setq from
(nth 0 contents
)
154 date
(message-make-date (nth 1 contents
)))
158 (make-full-mail-header
161 (concat "<" (number-to-string sid
) "%"
162 (number-to-string article
)
165 (/ (length (mapconcat 'identity
(nnweb-text (nth 2 contents
)) ""))
169 (setq nnwfm-headers
(sort headers
'car-less-than-car
))
171 (set-buffer nntp-server-buffer
)
172 (mm-with-unibyte-current-buffer
174 (dolist (header nnwfm-headers
)
175 (nnheader-insert-nov (cdr header
))))))
178 (deffoo nnwfm-request-group
(group &optional server dont-check
)
179 (nnwfm-possibly-change-server nil server
)
180 (when (not nnwfm-groups
)
181 (nnwfm-request-list))
183 (nnwfm-create-mapping group
))
184 (let ((elem (assoc group nnwfm-groups
)))
187 (nnheader-report 'nnwfm
"Group does not exist"))
189 (nnheader-report 'nnwfm
"Opened group %s" group
)
191 "211 %d %d %d %s\n" (cadr elem
) 1 (cadr elem
)
192 (prin1-to-string group
))))))
194 (deffoo nnwfm-request-close
()
195 (setq nnwfm-groups-alist nil
198 (deffoo nnwfm-request-article
(article &optional group server buffer
)
199 (nnwfm-possibly-change-server group server
)
200 (let ((contents (cdr (assq article nnwfm-articles
))))
201 (when (setq contents
(nth 2 (car contents
)))
203 (set-buffer (or buffer nntp-server-buffer
))
205 (nnweb-insert-html contents
)
206 (goto-char (point-min))
207 (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
208 (let ((header (cdr (assq article nnwfm-headers
))))
209 (mm-with-unibyte-current-buffer
210 (nnheader-insert-header header
)))
211 (nnheader-report 'nnwfm
"Fetched article %s" article
)
212 (cons group article
)))))
214 (deffoo nnwfm-request-list
(&optional server
)
215 (nnwfm-possibly-change-server nil server
)
216 (mm-with-unibyte-buffer
218 (if (string-match "/$" nnwfm-address
)
219 (concat nnwfm-address
"Group.asp")
221 (let* ((nnwfm-table-regexp "Thread.asp")
222 (contents (w3-parse-buffer (current-buffer)))
223 sid elem description articles a href group forum
225 (dolist (row (cdr (nth 2 (car (nth 2 (nnwfm-find-forum-table
227 (setq row
(nth 2 row
))
228 (when (setq a
(nnweb-parse-find 'a row
))
229 (setq group
(car (last (nnweb-text a
)))
230 href
(cdr (assq 'href
(nth 1 a
))))
231 (setq description
(car (last (nnweb-text (nth 1 row
)))))
234 (gnus-replace-in-string
235 (car (last (nnweb-text (nth 3 row
)))) "," "")))
237 (string-match "GroupId=\\([0-9]+\\)" href
))
238 (setq forum
(string-to-number (match-string 1 href
)))
239 (if (setq elem
(assoc group nnwfm-groups
))
240 (setcar (cdr elem
) articles
)
241 (push (list group articles forum description nil nil nil nil
)
244 (nnwfm-generate-active)
247 (deffoo nnwfm-request-newgroups
(date &optional server
)
248 (nnwfm-possibly-change-server nil server
)
249 (nnwfm-generate-active)
252 (nnoo-define-skeleton nnwfm
)
254 ;;; Internal functions
256 (defun nnwfm-new-threads-p (group time
)
257 "See whether we want to fetch the threads for GROUP written before TIME."
258 (let ((old-time (nth 7 (assoc group nnwfm-groups
))))
260 (time-less-p old-time time
))))
262 (defun nnwfm-create-mapping (group)
263 (let* ((entry (assoc group nnwfm-groups
))
265 (topics (nth 4 entry
))
266 (mapping (nth 5 entry
))
267 (old-total (or (nth 6 entry
) 1))
268 (current-time (current-time))
269 (nnwfm-table-regexp "Thread.asp")
270 (furls (list (concat nnwfm-address
271 (format "Thread.asp?GroupId=%d" sid
))))
273 contents forum-contents a subject href
274 garticles topic tinfo old-max inc parse elem date
276 (mm-with-unibyte-buffer
279 (push (car furls
) fetched-urls
)
280 (mm-url-insert (pop furls
))
281 (goto-char (point-min))
282 (while (re-search-forward " wr(" nil t
)
284 (setq elem
(message-tokenize-header
285 (gnus-replace-in-string
293 (string-to-number (nth 1 elem
))
294 (gnus-replace-in-string (nth 2 elem
) "\"" "")
295 (string-to-number (nth 5 elem
)))
297 (when (re-search-forward "href=\"\\(Thread.*DateLast=\\([^\"]+\\)\\)"
299 (setq url
(match-string 1)
300 time
(nnwfm-date-to-time (gnus-url-unhex-string
302 (when (and (nnwfm-new-threads-p group time
)
306 (mm-url-decode-entities-string url
)))
309 ;; The main idea here is to map Gnus article numbers to
310 ;; nnwfm article numbers. Say there are three topics in
311 ;; this forum, the first with 4 articles, the seconds with 2,
312 ;; and the third with 1. Then this will translate into 7 Gnus
313 ;; article numbers, where 1-4 comes from the first topic, 5-6
314 ;; from the second and 7 from the third. Now, then next time
315 ;; the group is entered, there's 2 new articles in topic one
316 ;; and 1 in topic three. Then Gnus article number 8-9 be 5-6
317 ;; in topic one and 10 will be the 2 in topic three.
318 (dolist (elem (nreverse forum-contents
))
319 (setq subject
(nth 1 elem
)
321 garticles
(nth 2 elem
))
322 (if (setq tinfo
(assq topic topics
))
324 (setq old-max
(cadr tinfo
))
325 (setcar (cdr tinfo
) garticles
))
327 (push (list topic garticles subject
) topics
)
328 (setcar (nthcdr 4 entry
) topics
))
329 (when (not (= old-max garticles
))
330 (setq inc
(- garticles old-max
))
331 (setq mapping
(nconc mapping
334 old-total
(1- (incf old-total inc
))
335 topic
(1+ old-max
)))))
337 (setcar (nthcdr 5 entry
) mapping
)
338 (setcar (nthcdr 6 entry
) old-total
))))
339 (setcar (nthcdr 7 entry
) current-time
)
340 (setcar (nthcdr 1 entry
) (1- old-total
))
344 (defun nnwfm-possibly-change-server (&optional group server
)
347 (not (nnwfm-server-opened server
)))
348 (nnwfm-open-server server
))
349 (unless nnwfm-groups-alist
351 (setq nnwfm-groups
(cdr (assoc nnwfm-address
352 nnwfm-groups-alist
)))))
354 (deffoo nnwfm-open-server
(server &optional defs connectionless
)
355 (nnheader-init-server-buffer)
356 (if (nnwfm-server-opened server
)
358 (unless (assq 'nnwfm-address defs
)
359 (setq defs
(append defs
(list (list 'nnwfm-address server
)))))
360 (nnoo-change-server 'nnwfm server defs
)))
362 (defun nnwfm-read-groups ()
363 (setq nnwfm-groups-alist nil
)
364 (let ((file (expand-file-name "groups" nnwfm-directory
)))
365 (when (file-exists-p file
)
366 (mm-with-unibyte-buffer
367 (insert-file-contents file
)
368 (goto-char (point-min))
369 (setq nnwfm-groups-alist
(read (current-buffer)))))))
371 (defun nnwfm-write-groups ()
372 (setq nnwfm-groups-alist
373 (delq (assoc nnwfm-address nnwfm-groups-alist
)
375 (push (cons nnwfm-address nnwfm-groups
)
377 (with-temp-file (expand-file-name "groups" nnwfm-directory
)
378 (prin1 nnwfm-groups-alist
(current-buffer))))
380 (defun nnwfm-init (server)
381 "Initialize buffers and such."
382 (unless (file-exists-p nnwfm-directory
)
383 (gnus-make-directory nnwfm-directory
)))
385 (defun nnwfm-generate-active ()
387 (set-buffer nntp-server-buffer
)
389 (dolist (elem nnwfm-groups
)
390 (insert (prin1-to-string (car elem
))
391 " " (number-to-string (cadr elem
)) " 1 y\n"))))
393 (defun nnwfm-find-forum-table (contents)
395 (nnwfm-find-forum-table-1 contents
)))
397 (defun nnwfm-find-forum-table-1 (contents)
398 (dolist (element contents
)
399 (unless (stringp element
)
400 (when (and (eq (car element
) 'table
)
401 (nnwfm-forum-table-p element
))
402 (throw 'found element
))
403 (when (nth 2 element
)
404 (nnwfm-find-forum-table-1 (nth 2 element
))))))
406 (defun nnwfm-forum-table-p (parse)
407 (when (not (apply 'gnus-or
410 (nnweb-parse-find 'table p
))
412 (let ((href (cdr (assq 'href
(nth 1 (nnweb-parse-find 'a parse
20)))))
414 (when (and href
(string-match nnwfm-table-regexp href
))
417 (defun nnwfm-date-to-time (date)
418 (let ((time (mapcar #'string-to-number
(split-string date
"[\\.\\+ :]"))))
419 (encode-time 0 (nth 4 time
) (nth 3 time
)
420 (nth 0 time
) (nth 1 time
)
421 (if (< (nth 2 time
) 70)
422 (+ 2000 (nth 2 time
))
423 (+ 1900 (nth 2 time
))))))
428 ;; coding: iso-8859-1
431 ;; arch-tag: d813966a-4211-4557-ad11-d1ac2bc86536
432 ;;; nnwfm.el ends here