2001-05-05 15: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 (require 'rfc2231)
37 (eval-when-compile
38   (ignore-errors
39     (require 'xml)
40     (require 'w3)
41     (require 'w3-forms)
42     (require 'nnweb)))
43 ;; Report failure to find w3 at load time if appropriate.
44 (eval '(progn
45          (require 'xml)
46          (require 'w3)
47          (require 'w3-forms)
48          (require 'nnweb)))
49
50 (nnoo-declare nnrss)
51
52 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
53   "Where nnrss will save its files.")
54
55 ;; (group max rss-url)
56 (defvoo nnrss-server-data nil)
57
58 ;; (num timestamp url subject author date extra)
59 (defvoo nnrss-group-data nil)
60 (defvoo nnrss-group-max 0)
61 (defvoo nnrss-group-min 1)
62 (defvoo nnrss-group nil)
63 (defvoo nnrss-group-hashtb nil)
64 (defvoo nnrss-status-string "")
65
66 (defconst nnrss-version "nnrss 1.0")
67
68 (defvar nnrss-group-alist
69   '(("MacWeek"
70      "http://macweek.zdnet.com/macweek.xml"
71      "The Macintosh news authority.")
72     ("Linux.Weekly.News"
73      "http://lwn.net/headlines/rss")
74     ("Motley.Fool"
75      "http://www.fool.com/About/headlines/rss_headlines.asp")
76     ("NewsForge.rdf"
77      "http://www.newsforge.com/newsforge.rdf")
78     ("Slashdot"
79      "http://www.slashdot.com/slashdot.rdf")
80     ("CNN"
81      "http://www.cnn.com/cnn.rss"
82      "The world's news leader.")
83     ("FreshMeat"
84      "http://freshmeat.net/backend/fm.rdf"
85      "The one-stop-shop for all your Linux software needs.")
86     ("The.Guardian.newspaper"
87      "http://www.guardianunlimited.co.uk/rss/1,,,00.xml"
88      "Intelligent news and comment throughout the day from The Guardian newspaper.")
89     ("MonkeyFist.rdf"
90      "http://monkeyfist.com/rdf.php3"
91      "News and opinion on politics, technology, and eclectic miscellany.")
92     ("NewsForge"
93      "http://www.newsforge.com/newsforge.rss")
94     ("Reuters.Health"
95      "http://www.reutershealth.com/eline.rss"
96      "Consumer-oriented health-related news stories.")
97     ("Salon"
98      "http://www.salon.com/feed/RDF/salon_use.rdf")
99     ("Wired"
100      "http://www.wired.com/news_drop/netcenter/netcenter.rdf")
101     ("ITN"
102      "http://www.itn.co.uk/itn.rdf")
103     ("Meerkat"
104      "http://www.oreillynet.com/meerkat/?_fl=rss10"
105      "An Open Wire Service")
106     ("MonkeyFist"
107      "http://monkeyfist.com/rss1.php3"
108      "News and opinion on politics, technology, and eclectic miscellany.")
109     ("Reuters.Health.rdf"
110      "http://www.reutershealth.com/eline.rdf"
111      "Consumer-oriented health-related news stories.")
112     ;;("4xt" "http://4xt.org/news/general.rss10" "Resources for XT users.")
113     ("Aaronland" "http://aaronland.net/xml/abhb.rdf" "A boy and his basement.")
114     ("Art of the Mix" "http://www.artofthemix.org/xml/rss.asp" "A website devoted to the art of making mixed tapes and cds.")
115     ("Dave Beckett's RDF Resource Guide" "http://www.ilrt.bristol.ac.uk/discovery/rdf/resources/rss.rdf" "A comprehensive guide to resources about RDF.")
116     ("David Chess" "http://www.davidchess.com/words/log.rss" "Mostly-daily musings on philosophy, children, culture, technology, the emergence of life from matter, chocolate, Nomic, and all that sort of thing.")
117     ;;("Dublin Core Metadata Intitiative" "http://www.dublincore.org/news.rss" "Latest news from DCMI.")
118     ("Figby Articles" "http://www.figby.com/index-rss.php" "A weblog with daily stories about technology, books and publishing, privacy, science, and occasional humor.")
119     ;;("Figby News" "http://www.figby.com/news.php" "Categorized RSS feeds from various sources.")
120     ("Figby Quickies" "http://www.figby.com/quickies-rss.php" "Quick commented links to other sites from Figby.com.")
121     ("Flutterby!" "http://www.flutterby.com/main.rdf" "News and views from Dan Lyke.")
122     ("Groovelog" "http://groovelog.agora.co.uk/groove+log/groovelog.nsf/today.rss.xml" "The open-access groove users' weblog.")
123     ;;("Groovelog.rss10" "http://groovelog.agora.co.uk/groove+log/groovelog.nsf/today.rss10.xml" "The open-access groove users' weblog.")
124     ("Hit or Miss" "http://hit-or-miss.org/rss/" "Daily weblog and journal.")
125     ;;("Internet.com Feeds" "http://www.webreference.com/services/news/" "News from ")
126     ("Larkfarm News" "http://www.larkfarm.com/Larkfarm.rdf" "Mike Gunderloy's web site.")
127     ("Latest RFCs" "http://x42.com/rss/rfc.rss")
128     ("Linux Today" "http://linuxtoday.com/backend/biglt.rss")
129     ("Linux Today.rdf" "http://linuxtoday.com/backend/my-netscape10.rdf")
130     ("More Like This WebLog" "http://www.whump.com/moreLikeThis/RSS" "Because the more you know, the more jokes you get.")
131     ("Motivational Quotes of the Day" "http://www.quotationspage.com/data/mqotd.rss" "Four motivational quotations each day from the Quotations Page.")
132     ;;("My Netscape Network" "http://www.dmoz.org/Netscape/My_Netscape_Network/")
133     ;;("My UserLand" "http://my.userland.com/choose")
134     ("Network World Fusion NetFlash" "http://www.nwfusion.com/netflash.rss" "Daily breaking news about networking products, technologies and services.")
135     ;;("News Feeds" "http://newsfeeds.manilasites.com/" "Jeff Barr highlights high quality RSS feeds.")
136     ;;("News Is Free Export" "http://www.newsisfree.com/export.php3")
137     ("News Is Free" "http://www.newsisfree.com/news.rdf.php3")
138     ;;("News is Free XML Export" "http://www.newsisfree.com/ocs/directory.xml")
139     ("O'Reilly Network Articles" "http://www.oreillynet.com/cs/rss/query/q/260?x-ver=1.0")
140     ("Quotes of the Day" "http://www.quotationspage.com/data/qotd.rss" "Four humorous quotations each day from the Quotations Page.")
141     ("RDF Interest Group" "http://ilrt.org/discovery/rdf-dev/roads/cgi-bin/desire/ig2rss?list=www-rdf-interest" "An experimental channel scraped from the RDF Interest Group mail archives.")
142     ("RDF Logic List" "http://ilrt.org/discovery/rdf-dev/roads/cgi-bin/desire/ig2rss?list=www-rdf-logic" "An experimental channel scraped from the RDF Logic mail archives.")
143     ("RSS Info" "http://www.blogspace.com/rss/rss10" "News and information on the RSS format")
144     ;;("RSS-DEV listing" "http://www.egroups.com/links/rss-dev/Feeds_000966335046/" "A listing of RSS files from the RSS-DEV list.")
145     ("Semantic Web List" "http://ilrt.org/discovery/rdf-dev/roads/cgi-bin/desire/ig2rss?list=semantic-web" "An experimental channel scraped from the W3C's Semantic Web mail archives.")
146     ;;("Sherch!" "http://www.sherch.com/~pldms/cgi-bin/sherch.pl" "Sherlock for the rest of us.")
147     ;;("Street Fusion Archived Financial Webcasts" "http://partners.streetfusion.com/rdf/archive.rdf")
148     ;;("Street Fusion Upcoming Financial Webcasts" "http://partners.streetfusion.com/rdf/live.rdf")
149     ;;("TNL.net newsletter" "http://www.tnl.net/newsletter/channel100.asp" "A newsletter about Internet technology and issues.")
150     ("W3C" "http://www.w3.org/2000/08/w3c-synd/home.rss" "The latest news at the World Wide Web Consortium.")
151     ;;("XML News: RSS Live Content" "http://www.xmlnews.org/RSS/content.html" "A listing of well-known RSS feeds.")
152     ("XMLfr" "http://xmlfr.org/actualites/general.rss10" "French speaking portal site dedicated to XML.")
153     ("XMLhack" "http://xmlhack.com/rss10.php" "Developer news from the XML community.")))
154
155 (defvar nnrss-use-local nil)
156
157 (nnoo-define-basics nnrss)
158
159 ;;; Interface functions
160
161 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
162   (nnrss-possibly-change-group group server)
163   (let (e)
164     (save-excursion
165       (set-buffer nntp-server-buffer)
166       (erase-buffer)
167       (dolist (article articles)
168         (if (setq e (assq article nnrss-group-data))
169             (insert (number-to-string (car e)) "\t" ;; number
170                     (if (nth 3 e)
171                         (nnrss-string-as-multibyte (nth 3 e)) "")
172                     "\t" ;; subject
173                     (if (nth 4 e)
174                         (nnrss-string-as-multibyte (nth 4 e)) "")
175                     "\t" ;;from
176                     (or (nth 5 e) "")
177                     "\t" ;; date
178                     (format "<%d@%s.nnrss>" (car e) group)
179                     "\t" ;; id
180                     "\t" ;; refs
181                     "0" "\t" ;; chars
182                     "0" "\t" ;; lines
183                     "\n")))))
184   'nov)
185
186 (deffoo nnrss-request-group (group &optional server dont-check)
187   (nnrss-possibly-change-group group server)
188   (if dont-check
189       t
190     (nnrss-check-group group server)
191     (nnheader-report 'nnrss "Opened group %s" group)
192     (nnheader-insert
193      "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
194      (prin1-to-string group)
195      t)))
196
197 (deffoo nnrss-close-group (group &optional server)
198   t)
199
200 (deffoo nnrss-request-article (article &optional group server buffer)
201   (nnrss-possibly-change-group group server)
202   (let ((e (assq article nnrss-group-data))
203         (nntp-server-buffer (or buffer nntp-server-buffer))
204         post err)
205     (when e
206       (catch 'error
207         (with-current-buffer nntp-server-buffer
208           (erase-buffer)
209           (goto-char (point-min))
210           (if (nth 3 e)
211               (insert "Subject: " (nnrss-string-as-multibyte (nth 3 e)) "\n"))
212           (if (nth 4 e)
213               (insert "From: " (nnrss-string-as-multibyte (nth 4 e)) "\n"))
214           (if (nth 5 e)
215               (insert "Date: " (nnrss-string-as-multibyte (nth 5 e)) "\n"))
216           (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
217           (insert "\n")
218           (if (nth 6 e)
219               (let ((point (point)))
220                 (insert (nnrss-string-as-multibyte (nth 6 e)) "\n\n")
221                 (fill-region point (point))))
222           (if (nth 2 e)
223               (insert (nth 2 e) "\n")))))
224     (cond
225      (err
226       (nnheader-report 'nnrss err))
227      ((not e)
228       (nnheader-report 'nnrss "No such id: %d" article))
229      (t
230       (nnheader-report 'nnrss "Article %s retrieved" (car e))
231       ;; We return the article number.
232       (cons nnrss-group (car e))))))
233
234 (deffoo nnrss-request-list (&optional server)
235   (nnrss-possibly-change-group nil server)
236   (nnrss-generate-active)
237   t)
238
239 (deffoo nnrss-open-server (server &optional defs connectionless)
240   (nnrss-read-server-data server)
241   (nnoo-change-server 'nnrss server defs)
242   t)
243
244 (deffoo nnrss-request-expire-articles
245     (articles group &optional server force)
246   (nnrss-possibly-change-group group server)
247   (let (e days not-expirable changed)
248     (dolist (art articles)
249       (if (and (setq e (assq art nnrss-group-data))
250                (nnmail-expired-article-p
251                 group
252                 (if (listp (setq days (nth 1 e))) days 
253                   (days-to-time (- days (time-to-days '(0 0)))))
254                 force))
255           (setq nnrss-group-data (delq e nnrss-group-data)
256                 changed t)
257         (push art not-expirable)))
258     (if changed
259         (nnrss-save-group-data group server))
260     not-expirable))
261
262 (deffoo nnrss-request-delete-group (group &optional force server)
263   (nnrss-possibly-change-group group server)
264   (setq nnrss-server-data
265         (delq (assoc group nnrss-server-data) nnrss-server-data))
266   (nnrss-save-server-data server)
267   (let ((file (expand-file-name 
268                (nnrss-translate-file-chars
269                 (concat group (and server
270                                    (not (equal server ""))
271                                    "-")
272                         server ".el")) nnrss-directory)))
273     (ignore-errors
274       (delete-file file)))
275   t)
276
277 (deffoo nnrss-request-list-newsgroups (&optional server)
278   (nnrss-possibly-change-group nil server)
279   (save-excursion
280     (set-buffer nntp-server-buffer)
281     (erase-buffer)
282     (dolist (elem nnrss-group-alist)
283       (if (third elem)
284           (insert (car elem) "\t" (third elem) "\n"))))
285   t)
286
287 (nnoo-define-skeleton nnrss)
288
289 ;;; Internal functions
290
291 (defun nnrss-possibly-change-group (&optional group server)
292   (when (and server
293              (not (nnrss-server-opened server)))
294     (nnrss-open-server server))
295   (when (and group (not (equal group nnrss-group)))
296     (nnrss-read-group-data group server)
297     (setq nnrss-group group)))
298
299 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
300
301 (defun nnrss-generate-active ()
302   (if (y-or-n-p "Fetch extra categories?")
303       (dolist (func nnrss-extra-categories)
304         (funcall func)))
305   (save-excursion
306     (set-buffer nntp-server-buffer)
307     (erase-buffer)
308     (dolist (elem nnrss-group-alist)
309       (insert (prin1-to-string (car elem)) " 0 1 y\n"))
310     (dolist (elem nnrss-server-data)
311       (unless (assoc (car elem) nnrss-group-alist)
312         (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
313
314 ;;; Data functions
315
316 (defun nnrss-read-server-data (server)
317   (setq nnrss-server-data nil)
318   (let ((file (expand-file-name 
319                (nnrss-translate-file-chars
320                 (concat "nnrss" (and server
321                                      (not (equal server ""))
322                                      "-")
323                         server
324                         ".el"))
325                nnrss-directory)))
326     (when (file-exists-p file)
327       (with-temp-buffer
328         (let ((coding-system-for-read 'binary))
329           (insert-file-contents file))
330         (emacs-lisp-mode)
331         (goto-char (point-min))
332         (eval-buffer)))))
333
334 (defun nnrss-save-server-data (server)
335   (gnus-make-directory nnrss-directory)
336   (let ((file (expand-file-name 
337                (nnrss-translate-file-chars
338                 (concat "nnrss" (and server
339                                      (not (equal server ""))
340                                      "-")
341                         server ".el"))
342                nnrss-directory)))
343     (let ((coding-system-for-write 'binary))
344       (with-temp-file file
345         (insert "(setq nnrss-server-data '"
346                 (prin1-to-string nnrss-server-data)
347                 ")\n")))))
348
349 (defun nnrss-read-group-data (group server)
350   (setq nnrss-group-data nil)
351   (setq nnrss-group-hashtb (gnus-make-hashtable))
352   (let ((pair (assoc group nnrss-server-data)))
353     (setq nnrss-group-max (or (cadr pair) 0))
354     (setq nnrss-group-min (+ nnrss-group-max 1)))
355   (let ((file (expand-file-name 
356                (nnrss-translate-file-chars
357                 (concat group (and server
358                                    (not (equal server ""))
359                                    "-")
360                         server ".el"))
361                nnrss-directory)))
362     (when (file-exists-p file)
363       (with-temp-buffer
364         (let ((coding-system-for-read 'binary))
365           (insert-file-contents file))
366         (emacs-lisp-mode)
367         (goto-char (point-min))
368         (eval-buffer))
369       (dolist (e nnrss-group-data)
370         (gnus-sethash (nth 2 e) e nnrss-group-hashtb)
371         (if (and (car e) (> nnrss-group-min (car e)))
372             (setq nnrss-group-min (car e)))
373         (if (and (car e) (< nnrss-group-max (car e)))
374             (setq nnrss-group-max (car e)))))))
375
376 (defun nnrss-save-group-data (group server)
377   (gnus-make-directory nnrss-directory)
378   (let ((file (expand-file-name 
379                (nnrss-translate-file-chars
380                 (concat group (and server
381                                    (not (equal server ""))
382                                    "-")
383                         server ".el"))
384                nnrss-directory)))
385     (let ((coding-system-for-write 'binary))
386       (with-temp-file file
387         (insert "(setq nnrss-group-data '"
388                 (prin1-to-string nnrss-group-data)
389                 ")\n")))))
390
391 ;;; URL interface
392
393 (defun nnrss-no-cache (url)
394   "")
395
396 ;; TODO:: disable cache.
397 ;;
398 ;; (defun nnrss-insert-w3 (url)
399 ;;   (require 'url)
400 ;;   (require 'url-cache)
401 ;;   (let ((url-cache-creation-function 'nnrss-no-cache))
402 ;;     (mm-with-unibyte-current-buffer
403 ;;       (nnweb-insert url))))
404
405 (defun nnrss-insert-w3 (url)
406   (mm-with-unibyte-current-buffer
407     (nnweb-insert url)))
408
409 (defun nnrss-decode-entities-unibyte-string (string)
410   (mm-with-unibyte-buffer
411     (insert string)
412     (nnweb-decode-entities)
413     (buffer-substring (point-min) (point-max))))
414
415 (defalias 'nnrss-insert 'nnrss-insert-w3)
416
417 (if (featurep 'xemacs)
418     (defalias 'nnrss-string-as-multibyte 'identity)
419   (defalias 'nnrss-string-as-multibyte 'string-as-multibyte))
420
421 ;;; Snarf functions
422
423 (defun nnrss-check-group (group server)
424   (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities))
425         file xml subject url extra changed author date)
426     (condition-case err
427         (mm-with-unibyte-buffer
428           (if (and nnrss-use-local
429                    (file-exists-p (setq file (expand-file-name
430                                           (nnrss-translate-file-chars
431                                            (concat group ".xml"))
432                                           nnrss-directory))))
433               (insert-file-contents file)
434             (setq url (or (nth 2 (assoc group nnrss-server-data))
435                           (second (assoc group nnrss-group-alist))))
436             (unless url
437               (setq url
438                 (read-string (format "RSS url of %s: " group "http://")))
439               (let ((pair (assoc group nnrss-server-data)))
440                 (if pair
441                     (setcdr (cdr pair) (list url))
442                   (push (list group nnrss-group-max url) nnrss-server-data)))
443               (setq changed t))
444             (nnrss-insert url))
445           (goto-char (point-min))
446           (while (re-search-forward "\r\n?" nil t)
447             (replace-match "\n"))
448           (goto-char (point-min))
449           (if (re-search-forward "<rdf\\|<rss" nil t)
450               (goto-char (match-beginning 0)))
451           (setq xml (xml-parse-region (point) (point-max))))
452       (error 
453        (nnheader-message 1 "Error in group %s: %s" group (cadr err))))
454     (while (and xml (not (assq 'item xml)))
455       (unless (listp (car (setq xml (cddar xml))))
456         (setq xml nil)))
457     (dolist (item xml)
458        (when (and (listp item)
459                   (eq 'item (car item))
460                   (setq url (caddr (assq 'link (cddr item))))
461                   (setq url (nnrss-decode-entities-unibyte-string url))
462                   (not (gnus-gethash url nnrss-group-hashtb)))
463          (setq subject (caddr (assq 'title (cddr item))))
464          (setq extra (or (caddr (assq 'description (cddr item)))
465                          (caddr (assq 'dc:description (cddr item)))))
466          (setq author (caddr (assq 'dc:creator (cddr item))))
467          (setq date (or (caddr (assq 'dc:date (cddr item)))
468                         (message-make-date)))
469          (push
470           (list
471            (incf nnrss-group-max)
472            (current-time)
473            url
474            (and subject (nnrss-decode-entities-unibyte-string subject))
475            (and author (nnrss-decode-entities-unibyte-string author))
476            date
477            (and extra (nnrss-decode-entities-unibyte-string extra)))
478           nnrss-group-data)
479          (gnus-sethash url (car nnrss-group-data) nnrss-group-hashtb)
480          (setq changed t)))
481     (when changed
482         (nnrss-save-group-data group server)
483         (let ((pair (assoc group nnrss-server-data)))
484           (if pair
485               (setcar (cdr pair) nnrss-group-max)
486             (push (list group nnrss-group-max) nnrss-server-data)))
487         (nnrss-save-server-data server))))
488
489 (defun nnrss-generate-download-script ()
490   "Generate a download script in the current buffer.
491 It is useful when `(setq nnrss-use-local t)'."
492   (interactive)
493   (insert "#!/bin/sh\n")
494   (insert "WGET=wget\n")
495   (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
496   (dolist (elem nnrss-server-data)
497     (let ((url (or (nth 2 elem)
498                    (second (assoc (car elem) nnrss-group-alist)))))
499     (insert "$WGET -q -O \"$RSSDIR\"/'" 
500             (nnrss-translate-file-chars (concat (car elem) ".xml"))
501             "' '" url "'\n"))))
502
503 (defun nnrss-translate-file-chars (name)
504   (let ((nnheader-file-name-translation-alist
505          (append nnheader-file-name-translation-alist '((?' . ?_)))))
506     (nnheader-translate-file-chars name)))
507
508 (defvar nnrss-moreover-url 
509   "http://w.moreover.com/categories/category_list_rss.html"
510   "The url of moreover.com categories.")
511
512 (defun nnrss-snarf-moreover-categories ()
513   "Snarf RSS links from moreover.com."
514   (interactive)
515   (let (category name url changed)
516     (with-temp-buffer
517       (nnrss-insert nnrss-moreover-url)
518       (goto-char (point-min))
519       (while (re-search-forward 
520               "<A NAME=\"\\([^\"]+\\)\">\\|<A HREF=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
521         (if (match-string 1)
522             (setq category (match-string 1))
523           (setq url (match-string 2)
524                 name (nnweb-decode-entities-string
525                       (rfc2231-decode-encoded-string 
526                        (match-string 3))))
527           (if category
528               (setq name (concat category "." name)))
529           (unless (assoc name nnrss-server-data)
530             (setq changed t)
531             (push (list name 0 url) nnrss-server-data)))))
532     (if changed
533         (nnrss-save-server-data ""))))
534
535 (provide 'nnrss)
536
537 ;;; nnrss.el ends here