2001-05-04 00:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / nnrss.el
1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001  Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: RSS
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
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; 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 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'nnoo)
31 (require 'nnmail)
32 (require 'message)
33 (require 'mm-util)
34 (require 'gnus-util)
35 (require 'time-date)
36 (eval-when-compile
37   (ignore-errors
38     (require 'xml)
39     (require 'w3)
40     (require 'w3-forms)
41     (require 'nnweb)))
42 ;; Report failure to find w3 at load time if appropriate.
43 (eval '(progn
44          (require 'xml)
45          (require 'w3)
46          (require 'w3-forms)
47          (require 'nnweb)))
48
49 (nnoo-declare nnrss)
50
51 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
52   "Where nnrss will save its files.")
53
54 ;; (group max rss-url)
55 (defvoo nnrss-server-data nil)
56
57 ;; (num timestamp url subject author date extra)
58 (defvoo nnrss-group-data nil)
59 (defvoo nnrss-group-max 0)
60 (defvoo nnrss-group-min 1)
61 (defvoo nnrss-group nil)
62 (defvoo nnrss-group-hashtb nil)
63 (defvoo nnrss-status-string "")
64
65 (defconst nnrss-version "nnrss 1.0")
66
67 (defvar nnrss-group-alist
68   '(("MacWeek"
69      "http://macweek.zdnet.com/macweek.xml")
70     ("Linux.Weekly.News"
71      "http://lwn.net/headlines/rss")
72     ("Motley.Fool"
73      "http://www.fool.com/About/headlines/rss_headlines.asp")
74     ("NewsForge.rdf"
75      "http://www.newsforge.com/newsforge.rdf")
76     ("Slashdot"
77      "http://www.slashdot.com/slashdot.rdf")
78     ("CNN"
79      "http://www.cnn.com/cnn.rss")
80     ("FreshMeat"
81      "http://freshmeat.net/backend/fm.rdf")
82     ("The.Guardian.newspaper"
83      "http://www.guardianunlimited.co.uk/rss/1,,,00.xml")
84     ("MonkeyFist.rdf"
85      "http://monkeyfist.com/rdf.php3")
86     ("NewsForge"
87      "http://www.newsforge.com/newsforge.rss")
88     ("Reuters.Health"
89      "http://www.reutershealth.com/eline.rss")
90     ("Salon"
91      "http://www.salon.com/feed/RDF/salon_use.rdf")
92     ("Wired"
93      "http://www.wired.com/news_drop/netcenter/netcenter.rdf")
94     ("ITN"
95      "http://www.itn.co.uk/itn.rdf")
96     ("Meerkat"
97      "http://www.oreillynet.com/meerkat/?_fl=rss10")
98     ("MonkeyFist"
99      "http://monkeyfist.com/rss1.php3")
100     ("Reuters.Health.rdf"
101      "http://www.reutershealth.com/eline.rdf")))
102
103 (defvar nnrss-use-local nil)
104
105 (nnoo-define-basics nnrss)
106
107 ;;; Interface functions
108
109 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
110   (nnrss-possibly-change-group group server)
111   (let (e)
112     (save-excursion
113       (set-buffer nntp-server-buffer)
114       (erase-buffer)
115       (dolist (article articles)
116         (if (setq e (assq article nnrss-group-data))
117             (insert (number-to-string (car e)) "\t" ;; number
118                     (if (nth 3 e)
119                         (nnrss-string-as-multibyte (nth 3 e)) "")
120                     "\t" ;; subject
121                     (if (nth 4 e)
122                         (nnrss-string-as-multibyte (nth 4 e)) "")
123                     "\t" ;;from
124                     (or (nth 5 e) "")
125                     "\t" ;; date
126                     (format "<%d@%s.nnrss>" (car e) group)
127                     "\t" ;; id
128                     "\t" ;; refs
129                     "0" "\t" ;; chars
130                     "0" "\t" ;; lines
131                     "\n")))))
132   'nov)
133
134 (deffoo nnrss-request-group (group &optional server dont-check)
135   (nnrss-possibly-change-group group server)
136   (if dont-check
137       t
138     (nnrss-check-group group server)
139     (nnheader-report 'nnrss "Opened group %s" group)
140     (nnheader-insert
141      "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
142      (prin1-to-string group)
143      t)))
144
145 (deffoo nnrss-close-group (group &optional server)
146   t)
147
148 (deffoo nnrss-request-article (article &optional group server buffer)
149   (nnrss-possibly-change-group group server)
150   (let ((e (assq article nnrss-group-data))
151         (nntp-server-buffer (or buffer nntp-server-buffer))
152         post err)
153     (when e
154       (catch 'error
155         (with-current-buffer nntp-server-buffer
156           (erase-buffer)
157           (goto-char (point-min))
158           (if (nth 3 e)
159               (insert "Subject: " (nnrss-string-as-multibyte (nth 3 e)) "\n"))
160           (if (nth 4 e)
161               (insert "From: " (nnrss-string-as-multibyte (nth 4 e)) "\n"))
162           (if (nth 5 e)
163               (insert "Date: " (nnrss-string-as-multibyte (nth 5 e)) "\n"))
164           (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
165           (insert "\n")
166           (if (nth 6 e)
167               (let ((point (point)))
168                 (insert (nnrss-string-as-multibyte (nth 6 e)) "\n\n")
169                 (fill-region point (point))))
170           (if (nth 2 e)
171               (insert (nth 2 e) "\n")))))
172     (cond
173      (err
174       (nnheader-report 'nnrss err))
175      ((not e)
176       (nnheader-report 'nnrss "No such id: %d" article))
177      (t
178       (nnheader-report 'nnrss "Article %s retrieved" (car e))
179       ;; We return the article number.
180       (cons nnrss-group (car e))))))
181
182 (deffoo nnrss-request-list (&optional server)
183   (nnrss-possibly-change-group nil server)
184   (nnrss-generate-active)
185   t)
186
187 (deffoo nnrss-open-server (server &optional defs connectionless)
188   (nnoo-change-server 'nnrss server defs)
189   t)
190
191 (deffoo nnrss-request-expire-articles
192     (articles group &optional server force)
193   (nnrss-possibly-change-group group server)
194   (let (e days expirable)
195     (dolist (art articles)
196       (when (setq e (assq art nnrss-group-data))
197       (if (nnmail-expired-article-p
198            group
199            (if (listp (setq days (nth 1 e))) days 
200              (days-to-time (- days (time-to-days '(0 0)))))
201            force)
202           (push art expirable)
203           (setq nnrss-group-data (delq e nnrss-group-data)))))
204     (if expirable
205         (nnrss-save-group-data group server))
206     expirable))
207
208 (deffoo nnrss-request-delete-group (group &optional force server)
209   (nnrss-possibly-change-group group server)
210   (setq nnrss-server-data
211         (delq (assoc group nnrss-server-data) nnrss-server-data))
212   (nnrss-save-server-data server)
213   (let ((file (expand-file-name (concat group (and server
214                                                    (not (equal server ""))
215                                                    "-")
216                                         server ".el") nnrss-directory)))
217     (delete-file file))
218   t)
219
220 (nnoo-define-skeleton nnrss)
221
222 ;;; Internal functions
223
224 (defun nnrss-possibly-change-group (&optional group server)
225   (when (and server
226              (not (nnrss-server-opened server)))
227     (nnrss-read-server-data server)
228     (nnrss-open-server server))
229   (when (and group (not (equal group nnrss-group)))
230     (nnrss-read-group-data group server)
231     (setq nnrss-group group)))
232
233 (defun nnrss-generate-active ()
234   (save-excursion
235     (set-buffer nntp-server-buffer)
236     (erase-buffer)
237     (dolist (elem nnrss-group-alist)
238       (insert (prin1-to-string (car elem)) " 0 1 y\n"))
239     (dolist (elem nnrss-server-data)
240       (unless (assoc (car elem) nnrss-group-alist)
241         (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
242
243 ;;; Data functions
244
245 (defun nnrss-read-server-data (server)
246   (setq nnrss-server-data nil)
247   (let ((file (expand-file-name (concat "nnrss" (and server
248                                                      (not (equal server ""))
249                                                      "-")
250                                         server
251                                         ".el")
252                                 nnrss-directory)))
253     (when (file-exists-p file)
254       (with-temp-buffer
255         (let ((coding-system-for-read 'binary))
256           (insert-file-contents file))
257         (goto-char (point-min))
258         (eval-buffer)))))
259
260 (defun nnrss-save-server-data (server)
261   (gnus-make-directory nnrss-directory)
262   (let ((file (expand-file-name (concat "nnrss" (and server
263                                                      (not (equal server ""))
264                                                      "-")
265                                         server ".el")
266                                 nnrss-directory)))
267     (let ((coding-system-for-write 'binary))
268       (with-temp-file file
269         (insert "(setq nnrss-server-data '"
270                 (prin1-to-string nnrss-server-data)
271                 ")\n")))))
272
273 (defun nnrss-read-group-data (group server)
274   (setq nnrss-group-data nil)
275   (setq nnrss-group-hashtb (gnus-make-hashtable))
276   (let ((pair (assoc group nnrss-server-data)))
277     (setq nnrss-group-max (or (cadr pair) 0))
278     (setq nnrss-group-min (+ nnrss-group-max 1)))
279   (let ((file (expand-file-name (concat group (and server
280                                                    (not (equal server ""))
281                                                    "-")
282                                         server ".el")
283                                 nnrss-directory)))
284     (when (file-exists-p file)
285       (with-temp-buffer
286         (let ((coding-system-for-read 'binary))
287           (insert-file-contents file))
288         (goto-char (point-min))
289         (eval-buffer))
290       (dolist (e nnrss-group-data)
291         (gnus-sethash (nth 2 e) e nnrss-group-hashtb)
292         (if (and (car e) (> nnrss-group-min (car e)))
293             (setq nnrss-group-min (car e)))
294         (if (and (car e) (< nnrss-group-max (car e)))
295             (setq nnrss-group-max (car e)))))))
296
297 (defun nnrss-save-group-data (group server)
298   (gnus-make-directory nnrss-directory)
299   (let ((file (expand-file-name (concat group (and server
300                                                    (not (equal server ""))
301                                                    "-")
302                                         server ".el")
303                                 nnrss-directory)))
304     (let ((coding-system-for-write 'binary))
305       (with-temp-file file
306         (insert "(setq nnrss-group-data '"
307                 (prin1-to-string nnrss-group-data)
308                 ")\n")))))
309
310 ;;; URL interface
311
312 (defun nnrss-no-cache (url)
313   "")
314
315 ;; TODO:: disable cache.
316 ;;
317 ;; (defun nnrss-insert-w3 (url)
318 ;;   (require 'url)
319 ;;   (require 'url-cache)
320 ;;   (let ((url-cache-creation-function 'nnrss-no-cache))
321 ;;     (mm-with-unibyte-current-buffer
322 ;;       (nnweb-insert url))))
323
324 (defun nnrss-insert-w3 (url)
325   (mm-with-unibyte-current-buffer
326     (nnweb-insert url)))
327
328 (defun nnrss-decode-entities-unibyte-string (string)
329   (mm-with-unibyte-buffer
330     (insert string)
331     (nnweb-decode-entities)
332     (buffer-substring (point-min) (point-max))))
333
334 (defalias 'nnrss-insert 'nnrss-insert-w3)
335
336 (if (featurep 'xemacs)
337     (defalias 'nnrss-string-as-multibyte 'identity)
338   (defalias 'nnrss-string-as-multibyte 'string-as-multibyte))
339
340 ;;; Snarf functions
341
342 (defun nnrss-check-group (group server)
343   (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities))
344         file xml subject url extra changed author date)
345     (mm-with-unibyte-buffer
346       (if (and nnrss-use-local
347                (file-exists-p (setq file (expand-file-name
348                                           (concat group ".xml")
349                                           nnrss-directory))))
350           (insert-file-contents file)
351         (setq url (or (nth 2 (assoc group nnrss-server-data))
352                       (second (assoc group nnrss-group-alist))))
353         (unless url
354           (setq url
355                 (read-string (format "RSS url of %s: " group "http://")))
356           (let ((pair (assoc group nnrss-server-data)))
357             (if pair
358                 (setcdr (cdr pair) (list url))
359               (push (list group nnrss-group-max url) nnrss-server-data)))
360           (setq changed t))
361         (nnrss-insert url))
362       (goto-char (point-min))
363       (while (re-search-forward "\r\n?" nil t)
364         (replace-match "\n"))
365       (goto-char (point-min))
366       (if (re-search-forward "<rdf\\|<rss" nil t)
367           (goto-char (match-beginning 0)))
368       (setq xml (xml-parse-region (point) (point-max))))
369     (while (and xml (not (assq 'item xml)))
370       (unless (listp (car (setq xml (cddar xml))))
371         (setq xml nil)))
372     (dolist (item xml)
373        (when (and (listp item)
374                   (eq 'item (car item))
375                   (setq url (caddr (assq 'link (cddr item))))
376                   (setq url (nnrss-decode-entities-unibyte-string url))
377                   (not (gnus-gethash url nnrss-group-hashtb)))
378          (setq subject (caddr (assq 'title (cddr item))))
379          (setq extra (or (caddr (assq 'description (cddr item)))
380                          (caddr (assq 'dc:description (cddr item)))))
381          (setq author (caddr (assq 'dc:creator (cddr item))))
382          (setq date (or (caddr (assq 'dc:date (cddr item)))
383                         (message-make-date)))
384          (push
385           (list
386            (incf nnrss-group-max)
387            (current-time)
388            url
389            (and subject (nnrss-decode-entities-unibyte-string subject))
390            (and author (nnrss-decode-entities-unibyte-string author))
391            date
392            (and extra (nnrss-decode-entities-unibyte-string extra)))
393           nnrss-group-data)
394          (gnus-sethash url (car nnrss-group-data) nnrss-group-hashtb)
395          (setq changed t)))
396     (when changed
397         (nnrss-save-group-data group server)
398         (let ((pair (assoc group nnrss-server-data)))
399           (if pair
400               (setcar (cdr pair) nnrss-group-max)
401             (push (list group nnrss-group-max) nnrss-server-data)))
402         (nnrss-save-server-data server))))
403
404 (provide 'nnrss)
405
406 ;;; nnrss.el ends here