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