*** empty log message ***
[gnus] / lisp / nnslashdot.el
1 ;;; nnslashdot.el --- interfacing with Slashdot
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `url' and `w3' installed for this
27 ;; backend to work.
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'nnoo)
34 (require 'message)
35 (require 'gnus-util)
36 (require 'gnus)
37 (require 'nnmail)
38 (require 'mm-util)
39 (require 'nnweb)
40 (eval-when-compile
41   (ignore-errors
42     (require 'w3)
43     (require 'url)
44     (require 'w3-forms)))
45 ;; Report failure to find w3 at load time if appropriate.
46 (eval '(progn
47          (require 'w3)
48          (require 'url)
49          (require 'w3-forms)))
50
51 (nnoo-declare nnslashdot)
52
53 (defvoo nnslashdot-directory (nnheader-concat gnus-directory "slashdot/")
54   "Where nnslashdot will save its files.")
55
56 (defvoo nnslashdot-active-url "http://slashdot.org/search.pl?section=&min=%d"
57   "Where nnslashdot will fetch the active file from.")
58
59 (defvoo nnslashdot-comments-url "http://slashdot.org/comments.pl?sid=%s&threshold=%d&commentsort=0&mode=flat&startat=%d"
60   "Where nnslashdot will fetch comments from.")
61
62 (defvoo nnslashdot-article-url
63     "http://slashdot.org/article.pl?sid=%s&mode=nocomment"
64   "Where nnslashdot will fetch the article from.")
65
66 (defvoo nnslashdot-threshold 0
67   "The article threshold.")
68
69 (defvoo nnslashdot-group-number 30
70   "The number of groups to keep updated.")
71
72 ;;; Internal variables
73
74 (defvar nnslashdot-groups nil)
75 (defvar nnslashdot-buffer nil)
76 (defvar nnslashdot-headers nil)
77
78 ;;; Interface functions
79
80 (nnoo-define-basics nnslashdot)
81
82 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
83   (nnslashdot-possibly-change-server group server)
84   (let ((last (car (last articles)))
85         (did nil)
86         (start 1)
87         (sid (caddr (assoc group nnslashdot-groups)))
88         headers article subject score from date lines parent point
89         startats s)
90     (save-excursion
91       (set-buffer nnslashdot-buffer)
92       (erase-buffer)
93       (when (= start 1)
94         (url-insert-file-contents (format nnslashdot-article-url sid))
95         (setq buffer-file-name nil)
96         (goto-char (point-min))
97         (search-forward "Posted by ")
98         (when (looking-at "<a[^>]+>\\([^<]+\\)")
99           (setq from (match-string 1)))
100         (search-forward " on ")
101         (setq date (nnslashdot-date-to-date
102                     (buffer-substring (point) (1- (search-forward "<")))))
103         (forward-line 2)
104         (setq lines (count-lines (point)
105                                  (search-forward
106                                   "A href=http://slashdot.org/article.pl")))
107         (push
108          (cons
109           1
110           (make-full-mail-header
111            1 group from date (concat "<" sid "%1@slashdot>")
112            "" 0 lines nil nil))
113          headers)
114         (goto-char (point-max))
115         (while (re-search-backward "startat=\\([0-9]+\\)" nil t)
116           (setq s (match-string 1))
117           (unless (memq s startats)
118             (push s startats)))
119         (unless startats
120           (push 1 startats)))
121       (setq startats (sort startats '<))
122       (while (and (setq start (pop startats))
123                   (< start last))
124         (setq point (goto-char (point-max)))
125         (url-insert-file-contents
126          (format nnslashdot-comments-url sid nnslashdot-threshold start))
127         (setq buffer-file-name nil)
128         (goto-char point)
129         (while (re-search-forward
130                 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score:\\([^)]+\\))"
131                 nil t)
132           (setq article (string-to-number (match-string 1))
133                 subject (match-string 2)
134                 score (match-string 3))
135           (when (string-match "^Re: *" subject)
136             (setq subject (concat "Re: " (substring subject (match-end 0)))))
137           (forward-line 1)
138           (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
139               (setq from (concat (match-string 1) " <" (match-string 2) ">"))
140             (looking-at "by \\(.+\\) on ")
141             (setq from (match-string 1)))
142           (goto-char (- (match-end 0) 5))
143           (search-forward " on ")
144           (setq date
145                 (nnslashdot-date-to-date
146                  (buffer-substring (point) (progn (end-of-line) (point)))))
147           (setq lines (/ (abs (- (search-forward "<td ")
148                                  (search-forward "</td>")))
149                          70))
150           (forward-line 2)
151           (setq parent
152                 (if (looking-at ".*cid=\\([0-9]+\\)")
153                     (match-string 1)
154                   nil))
155           (setq did t)
156           (push
157            (cons
158             (1+ article)
159             (make-full-mail-header
160              (1+ article) subject
161              from date
162              (concat "<" sid "%"
163                      (number-to-string (1+ article)) 
164                      "@slashdot>")
165              (if parent
166                  (concat "<" sid "%"
167                          (number-to-string (1+ (string-to-number parent)))
168                          "@slashdot>")
169                "")
170              0 (string-to-number score) nil nil))
171            headers))))
172     (setq nnslashdot-headers
173           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
174     (save-excursion
175       (set-buffer nntp-server-buffer)
176       (erase-buffer)
177       (dolist (header nnslashdot-headers)
178         (nnheader-insert-nov (cdr header))))
179     'nov))
180
181 (deffoo nnslashdot-sane-retrieve-headers (articles &optional group
182                                                    server fetch-old)
183   (nnslashdot-possibly-change-server group server)
184   (let ((last (car (last articles)))
185         (did nil)
186         (start (car articles))
187         (sid (caddr (assoc group nnslashdot-groups)))
188         headers article subject score from date lines parent point)
189     (save-excursion
190       (set-buffer nnslashdot-buffer)
191       (erase-buffer)
192       (when (= start 1)
193         (url-insert-file-contents (format nnslashdot-article-url sid))
194         (setq buffer-file-name nil)
195         (goto-char (point-min))
196         (search-forward "Posted by ")
197         (when (looking-at "<a[^>]+>\\([^<]+\\)")
198           (setq from (match-string 1)))
199         (search-forward " on ")
200         (setq date (nnslashdot-date-to-date
201                     (buffer-substring (point) (1- (search-forward "<")))))
202         (forward-line 2)
203         (setq lines (count-lines (point)
204                                  (search-forward
205                                   "A href=http://slashdot.org/article.pl")))
206         (push
207          (cons
208           1
209           (make-full-mail-header
210            1 group from date (concat "<" sid "%1@slashdot>")
211            "" 0 lines nil nil))
212          headers))
213       (while (or (not article)
214                  (and did
215                       (< article last)))
216         (when article
217           (setq start (1+ article)))
218         (setq point (goto-char (point-max)))
219         (url-insert-file-contents
220          (format nnslashdot-comments-url sid nnslashdot-threshold start))
221         (setq buffer-file-name nil)
222         (goto-char point)
223         (while (re-search-forward
224                 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
225                 nil t)
226           (setq article (string-to-number (match-string 1))
227                 subject (match-string 2)
228                 score (match-string 3))
229           (forward-line 1)
230           (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
231               (setq from (concat (match-string 1) " <" (match-string 2) ">"))
232             (looking-at "by \\(.+\\) on ")
233             (setq from (match-string 1)))
234           (goto-char (- (match-end 0) 5))
235           (search-forward " on ")
236           (setq date
237                 (nnslashdot-date-to-date
238                  (buffer-substring (point) (progn (end-of-line) (point)))))
239           (setq lines (/ (abs (- (search-forward "<td ")
240                                  (search-forward "</td>")))
241                          70))
242           (forward-line 2)
243           (setq parent
244                 (if (looking-at ".*cid=\\([0-9]+\\)")
245                     (match-string 1)
246                   nil))
247           (setq did t)
248           (push
249            (cons
250             (1+ article)
251             (make-full-mail-header
252              (1+ article) (concat subject " (" score ")")
253              from date
254              (concat "<" sid "%"
255                      (number-to-string (1+ article)) 
256                      "@slashdot>")
257              (if parent
258                  (concat "<" sid "%"
259                          (number-to-string (1+ (string-to-number parent)))
260                          "@slashdot>")
261                "")
262              0 lines nil nil))
263            headers))))
264     (setq nnslashdot-headers
265           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
266     (save-excursion
267       (set-buffer nntp-server-buffer)
268       (erase-buffer)
269       (dolist (header nnslashdot-headers)
270         (nnheader-insert-nov (cdr header))))
271     'nov))
272
273 (deffoo nnslashdot-request-group (group &optional server dont-check)
274   (nnslashdot-possibly-change-server nil server)
275   (let ((elem (assoc group nnslashdot-groups)))
276     (cond
277      ((not elem)
278       (nnheader-report 'nnslashdot "Group does not exist"))
279      (t
280       (nnheader-report 'nnslashdot "Opened group %s" group)
281       (nnheader-insert
282        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
283        (prin1-to-string group))))))
284
285 (deffoo nnslashdot-close-group (group &optional server)
286   (nnslashdot-possibly-change-server group server)
287   (when (gnus-buffer-live-p nnslashdot-buffer)
288     (save-excursion
289       (set-buffer nnslashdot-buffer)
290       (kill-buffer nnslashdot-buffer)))
291   t)
292
293 (deffoo nnslashdot-request-article (article &optional group server buffer)
294   (nnslashdot-possibly-change-server group server)
295   (let (contents)
296     (save-excursion
297       (set-buffer nnslashdot-buffer)
298       (goto-char (point-min))
299       (when (numberp article)
300         (if (= article 1)
301             (progn
302               (search-forward " on ")
303               (forward-line 1)
304               (setq contents
305                     (buffer-substring
306                      (point)
307                      (search-forward
308                                   "A href=http://slashdot.org/article.pl"))))
309           (search-forward (format "<a name=\"%d\">" (1- article)))
310           (setq contents
311                 (buffer-substring
312                  (re-search-forward "<td[^>]+>")
313                  (search-forward "</td>"))))))
314     (when contents
315       (save-excursion
316         (set-buffer (or buffer nntp-server-buffer))
317         (erase-buffer)
318         (insert contents)
319         ;;(nnweb-remove-markup)
320         ;;(nnweb-decode-entities)
321         (goto-char (point-min))
322         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
323         (let ((header (cdr (assq article nnslashdot-headers))))
324           (nnheader-insert-header header))
325         (nnheader-report 'nnslashdot "Fetched article %s" article)
326         t))))
327
328 (deffoo nnslashdot-close-server (&optional server)
329   (when (and (nnslashdot-server-opened server)
330              (gnus-buffer-live-p nnslashdot-buffer))
331     (save-excursion
332       (set-buffer nnslashdot-buffer)
333       (kill-buffer nnslashdot-buffer)))
334   (nnoo-close-server 'nnslashdot server))
335
336 (deffoo nnslashdot-request-list (&optional server)
337   (nnslashdot-possibly-change-server nil server)
338   (let ((case-fold-search t)
339         (number 0)
340         sid elem description articles gname)
341     (while (> (- nnslashdot-group-number number) 0)
342       (with-temp-buffer
343         (url-insert-file-contents (format nnslashdot-active-url number))
344         (setq buffer-file-name nil)
345         (goto-char (point-min))
346         (while (re-search-forward
347                 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
348           (setq sid (match-string 1)
349                 description (match-string 2))
350           (forward-line 1)
351           (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
352             (setq articles (string-to-number (match-string 1))))
353           (setq gname (concat description " (" sid ")"))
354           (if (setq elem (assoc gname nnslashdot-groups))
355               (setcar (cdr elem) articles)
356             (push (list gname articles sid) nnslashdot-groups))))
357       (incf number 30))
358     (nnslashdot-write-groups)
359     (nnslashdot-generate-active)
360     t))
361
362 (deffoo nnslashdot-request-newgroups (date &optional server)
363   (nnslashdot-possibly-change-server nil server)
364   (nnslashdot-generate-active))
365
366 (deffoo nnslashdot-asynchronous-p ()
367   nil)
368
369 (nnoo-define-skeleton nnslashdot)
370
371 ;;; Internal functions
372
373 (defun nnslashdot-possibly-change-server (&optional group server)
374   (nnslashdot-init server)
375   (when (and server
376              (not (nnslashdot-server-opened server)))
377     (nnslashdot-open-server server))
378   (unless nnslashdot-groups
379     (nnslashdot-read-groups)))
380
381 (defun nnslashdot-read-groups ()
382   (let ((file (expand-file-name "groups" nnslashdot-directory)))
383     (when (file-exists-p file)
384       (with-temp-buffer
385         (insert-file-contents file)
386         (goto-char (point-min))
387         (setq nnslashdot-groups (read (current-buffer)))))))
388
389 (defun nnslashdot-write-groups ()
390   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
391     (prin1 nnslashdot-groups (current-buffer))))
392     
393 (defun nnslashdot-init (server)
394   "Initialize buffers and such."
395   (unless (file-exists-p nnslashdot-directory)
396     (gnus-make-directory nnslashdot-directory))
397   (unless (gnus-buffer-live-p nnslashdot-buffer)
398     (setq nnslashdot-buffer
399           (save-excursion
400             (nnheader-set-temp-buffer
401              (format " *nnslashdot %s*" server))))))
402
403 (defun nnslashdot-encode-www-form-urlencoded (pairs)
404   "Return PAIRS encoded for forms."
405   (mapconcat
406    (function
407     (lambda (data)
408       (concat (w3-form-encode-xwfu (car data)) "="
409               (w3-form-encode-xwfu (cdr data)))))
410    pairs "&"))
411
412 (defun nnslashdot-fetch-form (url pairs)
413   (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
414         (url-request-method "POST")
415         (url-request-extra-headers
416          '(("Content-type" . "application/x-www-form-urlencoded"))))
417     (url-insert-file-contents url)
418     (setq buffer-file-name nil))
419   t)
420
421 (defun nnslashdot-decode-entities ()
422   (goto-char (point-min))
423   (while (re-search-forward "&\\([a-z]+\\);" nil t)
424     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
425                                                   w3-html-entities))
426                                        ?#))
427                    t t)))
428
429 (defun nnslashdot-remove-markup ()
430   (goto-char (point-min))
431   (while (search-forward "<!--" nil t)
432     (delete-region (match-beginning 0)
433                    (or (search-forward "-->" nil t)
434                        (point-max))))
435   (goto-char (point-min))
436   (while (re-search-forward "<[^>]+>" nil t)
437     (replace-match "" t t)))
438
439 (defun nnslashdot-date-to-date (sdate)
440   (let ((elem (split-string sdate)))
441     (concat (substring (nth 0 elem) 0 3) " "
442             (substring (nth 1 elem) 0 3) " "
443             (substring (nth 2 elem) 0 2) " "
444             (substring (nth 3 elem) 1 6) " "
445             (format-time-string "%Y") " "
446             (nth 4 elem))))
447
448 (defun nnslashdot-generate-active ()
449   (save-excursion
450     (set-buffer nntp-server-buffer)
451     (erase-buffer)
452     (dolist (elem nnslashdot-groups)
453       (insert (prin1-to-string (car elem))
454               " " (number-to-string (cadr elem)) " 1 y\n"))))
455
456 (provide 'nnslashdot)
457
458 ;;; nnslashdot.el ends here