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