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