*** 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 (and (stringp article)
300                  (string-match "%\\([0-9]+\\)@" article))
301         (setq article (string-to-number (match-string 1 article))))
302       (when (numberp article)
303         (if (= article 1)
304             (progn
305               (search-forward " on ")
306               (forward-line 1)
307               (setq contents
308                     (buffer-substring
309                      (point)
310                      (search-forward
311                                   "A href=http://slashdot.org/article.pl"))))
312           (search-forward (format "<a name=\"%d\">" (1- article)))
313           (setq contents
314                 (buffer-substring
315                  (re-search-forward "<td[^>]+>")
316                  (search-forward "</td>"))))))
317     (when contents
318       (save-excursion
319         (set-buffer (or buffer nntp-server-buffer))
320         (erase-buffer)
321         (insert contents)
322         ;;(nnweb-remove-markup)
323         ;;(nnweb-decode-entities)
324         (goto-char (point-min))
325         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
326         (let ((header (cdr (assq article nnslashdot-headers))))
327           (nnheader-insert-header header))
328         (nnheader-report 'nnslashdot "Fetched article %s" article)
329         (cons group article)))))
330
331 (deffoo nnslashdot-close-server (&optional server)
332   (when (and (nnslashdot-server-opened server)
333              (gnus-buffer-live-p nnslashdot-buffer))
334     (save-excursion
335       (set-buffer nnslashdot-buffer)
336       (kill-buffer nnslashdot-buffer)))
337   (nnoo-close-server 'nnslashdot server))
338
339 (deffoo nnslashdot-request-list (&optional server)
340   (nnslashdot-possibly-change-server nil server)
341   (let ((case-fold-search t)
342         (number 0)
343         sid elem description articles gname)
344     (while (> (- nnslashdot-group-number number) 0)
345       (with-temp-buffer
346         (url-insert-file-contents (format nnslashdot-active-url number))
347         (setq buffer-file-name nil)
348         (goto-char (point-min))
349         (while (re-search-forward
350                 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
351           (setq sid (match-string 1)
352                 description (match-string 2))
353           (forward-line 1)
354           (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
355             (setq articles (string-to-number (match-string 1))))
356           (setq gname (concat description " (" sid ")"))
357           (if (setq elem (assoc gname nnslashdot-groups))
358               (setcar (cdr elem) articles)
359             (push (list gname articles sid) nnslashdot-groups))))
360       (incf number 30))
361     (nnslashdot-write-groups)
362     (nnslashdot-generate-active)
363     t))
364
365 (deffoo nnslashdot-request-newgroups (date &optional server)
366   (nnslashdot-possibly-change-server nil server)
367   (nnslashdot-generate-active)
368   t)
369
370 (deffoo nnslashdot-asynchronous-p ()
371   nil)
372
373 (nnoo-define-skeleton nnslashdot)
374
375 ;;; Internal functions
376
377 (defun nnslashdot-possibly-change-server (&optional group server)
378   (nnslashdot-init server)
379   (when (and server
380              (not (nnslashdot-server-opened server)))
381     (nnslashdot-open-server server))
382   (unless nnslashdot-groups
383     (nnslashdot-read-groups)))
384
385 (defun nnslashdot-read-groups ()
386   (let ((file (expand-file-name "groups" nnslashdot-directory)))
387     (when (file-exists-p file)
388       (with-temp-buffer
389         (insert-file-contents file)
390         (goto-char (point-min))
391         (setq nnslashdot-groups (read (current-buffer)))))))
392
393 (defun nnslashdot-write-groups ()
394   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
395     (prin1 nnslashdot-groups (current-buffer))))
396     
397 (defun nnslashdot-init (server)
398   "Initialize buffers and such."
399   (unless (file-exists-p nnslashdot-directory)
400     (gnus-make-directory nnslashdot-directory))
401   (unless (gnus-buffer-live-p nnslashdot-buffer)
402     (setq nnslashdot-buffer
403           (save-excursion
404             (nnheader-set-temp-buffer
405              (format " *nnslashdot %s*" server))))))
406
407 (defun nnslashdot-encode-www-form-urlencoded (pairs)
408   "Return PAIRS encoded for forms."
409   (mapconcat
410    (function
411     (lambda (data)
412       (concat (w3-form-encode-xwfu (car data)) "="
413               (w3-form-encode-xwfu (cdr data)))))
414    pairs "&"))
415
416 (defun nnslashdot-fetch-form (url pairs)
417   (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
418         (url-request-method "POST")
419         (url-request-extra-headers
420          '(("Content-type" . "application/x-www-form-urlencoded"))))
421     (url-insert-file-contents url)
422     (setq buffer-file-name nil))
423   t)
424
425 (defun nnslashdot-decode-entities ()
426   (goto-char (point-min))
427   (while (re-search-forward "&\\([a-z]+\\);" nil t)
428     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
429                                                   w3-html-entities))
430                                        ?#))
431                    t t)))
432
433 (defun nnslashdot-remove-markup ()
434   (goto-char (point-min))
435   (while (search-forward "<!--" nil t)
436     (delete-region (match-beginning 0)
437                    (or (search-forward "-->" nil t)
438                        (point-max))))
439   (goto-char (point-min))
440   (while (re-search-forward "<[^>]+>" nil t)
441     (replace-match "" t t)))
442
443 (defun nnslashdot-date-to-date (sdate)
444   (let ((elem (split-string sdate)))
445     (concat (substring (nth 0 elem) 0 3) " "
446             (substring (nth 1 elem) 0 3) " "
447             (substring (nth 2 elem) 0 2) " "
448             (substring (nth 3 elem) 1 6) " "
449             (format-time-string "%Y") " "
450             (nth 4 elem))))
451
452 (defun nnslashdot-generate-active ()
453   (save-excursion
454     (set-buffer nntp-server-buffer)
455     (erase-buffer)
456     (dolist (elem nnslashdot-groups)
457       (insert (prin1-to-string (car elem))
458               " " (number-to-string (cadr elem)) " 1 y\n"))))
459
460 (provide 'nnslashdot)
461
462 ;;; nnslashdot.el ends here