456fcac9a79d109fb185b29613d52468745bf9a7
[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-articles-url "http://slashdot.org/article.pl?sid=%s&threshold=%d&commentsort=0&mode=flat&startat=%d"
60   "Where nnslashdot will fetch articles from.")
61
62 (defvoo nnslashdot-threshold 0
63   "The article threshold.")
64
65 (defvoo nnslashdot-group-number 30
66   "The number of groups to keep updated.")
67
68 ;;; Internal variables
69
70 (defvar nnslashdot-groups nil)
71 (defvar nnslashdot-buffer nil)
72 (defvar nnslashdot-headers nil)
73
74 ;;; Interface functions
75
76 (nnoo-define-basics nnslashdot)
77
78 (deffoo nnslashdot-retrieve-headers (articles &optional group server fetch-old)
79   (nnslashdot-possibly-change-server group server)
80   (let ((last (car (last articles)))
81         (did nil)
82         (start 1)
83         headers article subject score from date lines parent point)
84     (save-excursion
85       (set-buffer nnslashdot-buffer)
86       (erase-buffer)
87       (while (or (not article)
88                  (and did
89                       (< (string-to-number article) last)))
90         (when article
91           (setq start (1+ (string-to-number article))))
92         (setq point (goto-char (point-max)))
93         (url-insert-file-contents
94          (format nnslashdot-articles-url
95                  (caddr (assoc group nnslashdot-groups))
96                  nnslashdot-threshold start))
97         (setq buffer-file-name nil)
98         (goto-char point)
99         (while (re-search-forward
100                 "<a name=\"\\([0-9]+\\)\"><b>\\([^<]+\\)</b>.*score\\([^)]+\\))"
101                 nil t)
102           (setq article (match-string 1)
103                 subject (match-string 2)
104                 score (match-string 3))
105           (forward-line 1)
106           (if (looking-at "by <a[^>]+>\\([^<]+\\)</a>[ \t\n]*.*(\\([^)]+\\))")
107               (setq from (concat (match-string 1) " <" (match-string 2) ">"))
108             (looking-at "by \\([^ ]+\\) ")
109             (setq from (match-string 1)))
110           (goto-char (match-end 0))
111           (search-forward "on ")
112           (setq date
113                 (nnslashdot-date-to-date
114                  (buffer-substring (point) (progn (end-of-line) (point)))))
115           (setq lines (count-lines (search-forward "<td")
116                                    (search-forward "</td>")))
117           (forward-line 2)
118           (setq parent
119                 (if (looking-at ".*cid=\\([0-9]+\\)")
120                     (match-string 1)
121                   nil))
122           (setq did t)
123           (push
124            (cons
125             (string-to-number article)
126             (make-full-mail-header
127              (string-to-number article) (concat subject " (" score ")")
128              from date (concat "<" group "%" article "@slashdot>")
129              (if parent (concat "<" group "%" parent "@slashdot>") "")
130              0 lines nil nil))
131            headers))))
132     (setq nnslashdot-headers
133           (sort headers (lambda (s1 s2) (< (car s1) (car s2)))))
134     (save-excursion
135       (set-buffer nntp-server-buffer)
136       (erase-buffer)
137       (dolist (header nnslashdot-headers)
138         (nnheader-insert-nov (cdr header))))
139     'nov))
140
141 (deffoo nnslashdot-request-group (group &optional server dont-check)
142   (nnslashdot-possibly-change-server nil server)
143   (let ((elem (assoc group nnslashdot-groups)))
144     (cond
145      ((not elem)
146       (nnheader-report 'nnslashdot "Group does not exist"))
147      (t
148       (nnheader-report 'nnslashdot "Opened group %s" group)
149       (nnheader-insert
150        "211 %d %d %d %s\n" (cadr elem) 1 (cadr elem)
151        (prin1-to-string group))))))
152
153 (deffoo nnslashdot-close-group (group &optional server)
154   (nnslashdot-possibly-change-server group server)
155   (when (gnus-buffer-live-p nnslashdot-buffer)
156     (save-excursion
157       (set-buffer nnslashdot-buffer)
158       (kill-buffer nnslashdot-buffer)))
159   t)
160
161 (deffoo nnslashdot-request-article (article &optional group server buffer)
162   (nnslashdot-possibly-change-server group server)
163   (let (contents)
164     (save-excursion
165       (set-buffer nnslashdot-buffer)
166       (goto-char (point-min))
167       (when (and (numberp article)
168                (search-forward (format "<a name=\"%d\">" article)))
169         (setq contents
170               (buffer-substring
171                (re-search-forward "<td[^>]+>")
172                (search-forward "</td>")))))
173     (when contents
174       (save-excursion
175         (set-buffer (or buffer nntp-server-buffer))
176         (erase-buffer)
177         (insert contents)
178         ;;(nnweb-remove-markup)
179         ;;(nnweb-decode-entities)
180         (goto-char (point-min))
181         (insert "Content-Type: text/html\nMIME-Version: 1.0\n")
182         (let ((header (cdr (assq article nnslashdot-headers))))
183           (nnheader-insert-header header))
184         (nnheader-report 'nnslashdot "Fetched article %s" article)
185         t))))
186
187 (deffoo nnslashdot-close-server (&optional server)
188   (when (and (nnslashdot-server-opened server)
189              (gnus-buffer-live-p nnslashdot-buffer))
190     (save-excursion
191       (set-buffer nnslashdot-buffer)
192       (kill-buffer nnslashdot-buffer)))
193   (nnoo-close-server 'nnslashdot server))
194
195 (deffoo nnslashdot-request-list (&optional server)
196   (nnslashdot-possibly-change-server nil server)
197   (let ((case-fold-search t)
198         (number 0)
199         sid elem description articles gname)
200     (while (> (- nnslashdot-group-number number) 0)
201       (with-temp-buffer
202         (url-insert-file-contents (format nnslashdot-active-url number))
203         (setq buffer-file-name nil)
204         (goto-char (point-min))
205         (while (re-search-forward
206                 "article.pl\\?sid=\\([^&]+\\).*<b>\\([^<]+\\)</b>" nil t)
207           (setq sid (match-string 1)
208                 description (match-string 2))
209           (forward-line 1)
210           (when (re-search-forward "<b>\\([0-9]+\\)</b>" nil t)
211             (setq articles (string-to-number (match-string 1))))
212           (setq gname (concat description " (" sid ")"))
213           (if (setq elem (assoc gname nnslashdot-groups))
214               (setcar (cdr elem) articles)
215             (push (list gname articles sid) nnslashdot-groups))))
216       (incf number 30))
217     (nnslashdot-write-groups)
218     (save-excursion
219       (set-buffer nntp-server-buffer)
220       (erase-buffer)
221       (dolist (elem nnslashdot-groups)
222         (insert (prin1-to-string (car elem))
223                 " " (number-to-string (cadr elem)) " 1 m\n")))
224     t))
225
226 (deffoo nnslashdot-asynchronous-p ()
227   nil)
228
229 (nnoo-define-skeleton nnslashdot)
230
231 ;;; Internal functions
232
233 (defun nnslashdot-possibly-change-server (&optional group server)
234   (nnslashdot-init server)
235   (when (and server
236              (not (nnslashdot-server-opened server)))
237     (nnslashdot-open-server server))
238   (unless nnslashdot-groups
239     (nnslashdot-read-groups)))
240
241 (defun nnslashdot-read-groups ()
242   (let ((file (expand-file-name "groups" nnslashdot-directory)))
243     (when (file-exists-p file)
244       (with-temp-buffer
245         (insert-file-contents file)
246         (goto-char (point-min))
247         (setq nnslashdot-groups (read (current-buffer)))))))
248
249 (defun nnslashdot-write-groups ()
250   (with-temp-file (expand-file-name "groups" nnslashdot-directory)
251     (prin1 nnslashdot-groups (current-buffer))))
252     
253 (defun nnslashdot-init (server)
254   "Initialize buffers and such."
255   (unless (file-exists-p nnslashdot-directory)
256     (gnus-make-directory nnslashdot-directory))
257   (unless (gnus-buffer-live-p nnslashdot-buffer)
258     (setq nnslashdot-buffer
259           (save-excursion
260             (nnheader-set-temp-buffer
261              (format " *nnslashdot %s*" server))))))
262
263 (defun nnslashdot-encode-www-form-urlencoded (pairs)
264   "Return PAIRS encoded for forms."
265   (mapconcat
266    (function
267     (lambda (data)
268       (concat (w3-form-encode-xwfu (car data)) "="
269               (w3-form-encode-xwfu (cdr data)))))
270    pairs "&"))
271
272 (defun nnslashdot-fetch-form (url pairs)
273   (let ((url-request-data (nnslashdot-encode-www-form-urlencoded pairs))
274         (url-request-method "POST")
275         (url-request-extra-headers
276          '(("Content-type" . "application/x-www-form-urlencoded"))))
277     (url-insert-file-contents url)
278     (setq buffer-file-name nil))
279   t)
280
281 (defun nnslashdot-decode-entities ()
282   (goto-char (point-min))
283   (while (re-search-forward "&\\([a-z]+\\);" nil t)
284     (replace-match (char-to-string (or (cdr (assq (intern (match-string 1))
285                                                   w3-html-entities))
286                                        ?#))
287                    t t)))
288
289 (defun nnslashdot-remove-markup ()
290   (goto-char (point-min))
291   (while (search-forward "<!--" nil t)
292     (delete-region (match-beginning 0)
293                    (or (search-forward "-->" nil t)
294                        (point-max))))
295   (goto-char (point-min))
296   (while (re-search-forward "<[^>]+>" nil t)
297     (replace-match "" t t)))
298
299 (defun nnslashdot-date-to-date (sdate)
300   (let ((elem (split-string sdate)))
301     (concat (substring (nth 0 elem) 0 3) " "
302             (substring (nth 1 elem) 0 3) " "
303             (substring (nth 2 elem) 0 2) " "
304             (substring (nth 3 elem) 1 6) " "
305             (nth 4 elem))))
306
307 (provide 'nnslashdot)
308
309 ;;; nnslashdot.el ends here