Expand anti-spam. Fixes.
[gnus] / lisp / nnrss.el
1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002  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 'gnus)
31 (require 'nnoo)
32 (require 'nnmail)
33 (require 'message)
34 (require 'mm-util)
35 (require 'gnus-util)
36 (require 'time-date)
37 (require 'rfc2231)
38 (require 'mm-url)
39 (eval-when-compile
40   (ignore-errors
41     (require 'xml)))
42 (eval '(require 'xml))
43
44 (nnoo-declare nnrss)
45
46 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
47   "Where nnrss will save its files.")
48
49 ;; (group max rss-url)
50 (defvoo nnrss-server-data nil)
51
52 ;; (num timestamp url subject author date extra)
53 (defvoo nnrss-group-data nil)
54 (defvoo nnrss-group-max 0)
55 (defvoo nnrss-group-min 1)
56 (defvoo nnrss-group nil)
57 (defvoo nnrss-group-hashtb nil)
58 (defvoo nnrss-status-string "")
59
60 (defconst nnrss-version "nnrss 1.0")
61
62 (defvar nnrss-group-alist
63   '(("MacWeek"
64      "http://macweek.zdnet.com/macweek.xml"
65      "The Macintosh news authority.")
66     ("Linux.Weekly.News"
67      "http://lwn.net/headlines/rss")
68     ("Motley.Fool"
69      "http://www.fool.com/About/headlines/rss_headlines.asp")
70     ("NewsForge.rdf"
71      "http://www.newsforge.com/newsforge.rdf")
72     ("Slashdot"
73      "http://www.slashdot.com/slashdot.rdf")
74     ("CNN"
75      "http://www.cnn.com/cnn.rss"
76      "The world's news leader.")
77     ("FreshMeat"
78      "http://freshmeat.net/backend/fm-releases.rdf"
79      "The one-stop-shop for all your Linux software needs.")
80     ("The.Guardian.newspaper"
81      "http://www.guardianunlimited.co.uk/rss/1,,,00.xml"
82      "Intelligent news and comment throughout the day from The Guardian newspaper.")
83     ("MonkeyFist.rdf"
84      "http://monkeyfist.com/rdf.php3"
85      "News and opinion on politics, technology, and eclectic miscellany.")
86     ("NewsForge"
87      "http://www.newsforge.com/newsforge.rss")
88     ("Reuters.Health"
89      "http://www.reutershealth.com/eline.rss"
90      "Consumer-oriented health-related news stories.")
91     ("Salon"
92      "http://www.salon.com/feed/RDF/salon_use.rdf")
93     ("Wired"
94      "http://www.wired.com/news_drop/netcenter/netcenter.rdf")
95     ("ITN"
96      "http://www.itn.co.uk/itn.rdf")
97     ("Meerkat"
98      "http://www.oreillynet.com/meerkat/?_fl=rss10"
99      "An Open Wire Service")
100     ("MonkeyFist"
101      "http://monkeyfist.com/rss1.php3"
102      "News and opinion on politics, technology, and eclectic miscellany.")
103     ("Reuters.Health.rdf"
104      "http://www.reutershealth.com/eline.rdf"
105      "Consumer-oriented health-related news stories.")
106     ("Aaronland" "http://aaronland.net/xml/abhb.rdf" "A boy and his basement.")
107     ("Art of the Mix" "http://www.artofthemix.org/xml/rss.asp" "A website devoted to the art of making mixed tapes and cds.")
108     ("Dave Beckett's RDF Resource Guide" "http://www.ilrt.bristol.ac.uk/discovery/rdf/resources/rss.rdf" "A comprehensive guide to resources about RDF.")
109     ("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.")
110     ("Figby Articles" "http://www.figby.com/index-rss.php" "A weblog with daily stories about technology, books and publishing, privacy, science, and occasional humor.")
111     ("Figby Quickies" "http://www.figby.com/quickies-rss.php" "Quick commented links to other sites from Figby.com.")
112     ("Flutterby!" "http://www.flutterby.com/main.rdf" "News and views from Dan Lyke.")
113     ("Groovelog" 
114      "http://groovelog.agora.co.uk/groove+log/groovelog.nsf/today.rss.xml"
115      "The open-access groove users' weblog.")
116     ("Hit or Miss" "http://hit-or-miss.org/rss/" "Daily weblog and journal.")
117     ("Larkfarm News" "http://www.larkfarm.com/Larkfarm.rdf" "Mike Gunderloy's web site.")
118     ("Latest RFCs" "http://x42.com/rss/rfc.rss")
119     ("Linux Today" "http://linuxtoday.com/backend/biglt.rss")
120     ("Linux Today.rdf" "http://linuxtoday.com/backend/my-netscape10.rdf")
121     ("More Like This WebLog" "http://www.whump.com/moreLikeThis/RSS" "Because the more you know, the more jokes you get.")
122     ("Motivational Quotes of the Day" "http://www.quotationspage.com/data/mqotd.rss" "Four motivational quotations each day from the Quotations Page.")
123     ("Network World Fusion NetFlash" "http://www.nwfusion.com/netflash.rss" "Daily breaking news about networking products, technologies and services.")
124     ("News Is Free" "http://www.newsisfree.com/news.rdf.php3")
125     ("O'Reilly Network Articles" "http://www.oreillynet.com/cs/rss/query/q/260?x-ver=1.0")
126     ("Quotes of the Day" "http://www.quotationspage.com/data/qotd.rss" "Four humorous quotations each day from the Quotations Page.")
127     ("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.")
128     ("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.")
129     ("RSS Info" "http://www.blogspace.com/rss/rss10" "News and information on the RSS format")
130     ("Semantic Web List"
131      "http://ilrt.org/discovery/rdf-dev/roads/cgi-bin/desire/ig2rss?list=semantic-web"
132      "An experimental channel scraped from the W3C's Semantic Web mail archives.")
133     ("W3C"
134      "http://www.w3.org/2000/08/w3c-synd/home.rss"
135      "The latest news at the World Wide Web Consortium.")
136     ("|fr| XMLfr" "http://xmlfr.org/actualites/general.rss10"
137      "French speaking portal site dedicated to XML.")
138     ("XMLhack" "http://xmlhack.com/rss10.php"
139      "Developer news from the XML community.")
140     ("The Register"
141      "http://www.theregister.co.uk/tonys/slashdot.rdf"
142      "The Register -- Biting the hand that feeds IT.")
143     ("|de| Heise-Ticker"
144      "http://www.heise.de/newsticker/heise.rdf"
145      "German news ticker about technology.")
146     ("|de| Telepolis News"
147      "http://www.heise.de/tp/news.rdf"
148      "German background news about technology.")
149     ("Kuro5hin"
150      "http://www.kuro5hin.org/backend.rdf"
151      "Technology and culture, from the trenches.")
152     ("Jabber Software Foundation News"
153      "http://www.jabber.org/news/rss.xml"
154      "News and announcements from the Jabber Software Foundation.")
155     ("MacRumors"
156      "http://www.macrumors.com/macrumors.xml"
157      "The mac news you care about.")
158     ("Mac OS X Hints"
159      "http://www.macosxhints.com/backend/geeklog.rdf"
160      "Mac OS X Hits.")
161     )
162   "List of RSS addresses.")
163
164 (defvar nnrss-use-local nil)
165
166 (defvar nnrss-description-field 'X-Gnus-Description
167   "Field name used for DESCRIPTION.
168 To use the description in headers, put this name into `nnmail-extra-headers'.")
169
170 (defvar nnrss-url-field 'X-Gnus-Url
171   "Field name used for URL.
172 To use the description in headers, put this name into `nnmail-extra-headers'.")
173
174 (defvar nnrss-content-function nil
175   "A function which is called in `nnrss-request-article'.
176 The arguments are (ENTRY GROUP ARTICLE).
177 ENTRY is the record of the current headline. GROUP is the group name.
178 ARTICLE is the article number of the current headline.")
179
180 (nnoo-define-basics nnrss)
181
182 ;;; Interface functions
183
184 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
185   (nnrss-possibly-change-group group server)
186   (let (e)
187     (save-excursion
188       (set-buffer nntp-server-buffer)
189       (erase-buffer)
190       (dolist (article articles)
191         (if (setq e (assq article nnrss-group-data))
192             (insert (number-to-string (car e)) "\t" ;; number
193                     (if (nth 3 e)
194                         (nnrss-format-string (nth 3 e)) "")
195                     "\t" ;; subject
196                     (if (nth 4 e)
197                         (nnrss-format-string (nth 4 e))
198                       "(nobody)")
199                     "\t" ;;from
200                     (or (nth 5 e) "")
201                     "\t" ;; date
202                     (format "<%d@%s.nnrss>" (car e) group)
203                     "\t" ;; id
204                     "\t" ;; refs
205                     "-1" "\t" ;; chars
206                     "-1" "\t" ;; lines
207                     "" "\t" ;; Xref
208                     (if (and (nth 6 e)
209                              (memq nnrss-description-field
210                                    nnmail-extra-headers))
211                         (concat (symbol-name nnrss-description-field)
212                                 ": "
213                                 (nnrss-format-string (nth 6 e))
214                                 "\t")
215                       "")
216                     (if (and (nth 2 e)
217                              (memq nnrss-url-field
218                                    nnmail-extra-headers))
219                         (concat (symbol-name nnrss-url-field)
220                                 ": "
221                                 (nnrss-format-string (nth 2 e))
222                                 "\t")
223                       "")
224                     "\n")))))
225   'nov)
226
227 (deffoo nnrss-request-group (group &optional server dont-check)
228   (nnrss-possibly-change-group group server)
229   (if dont-check
230       t
231     (nnrss-check-group group server)
232     (nnheader-report 'nnrss "Opened group %s" group)
233     (nnheader-insert
234      "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
235      (prin1-to-string group)
236      t)))
237
238 (deffoo nnrss-close-group (group &optional server)
239   t)
240
241 (deffoo nnrss-request-article (article &optional group server buffer)
242   (nnrss-possibly-change-group group server)
243   (let ((e (assq article nnrss-group-data))
244         (nntp-server-buffer (or buffer nntp-server-buffer))
245         post err)
246     (when e
247       (catch 'error
248         (with-current-buffer nntp-server-buffer
249           (erase-buffer)
250           (goto-char (point-min))
251           (if group
252               (insert "Newsgroups: " group "\n"))
253           (if (nth 3 e)
254               (insert "Subject: " (nnrss-format-string (nth 3 e)) "\n"))
255           (if (nth 4 e)
256               (insert "From: " (nnrss-format-string (nth 4 e)) "\n"))
257           (if (nth 5 e)
258               (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
259           (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
260           (insert "\n")
261           (if (nth 6 e)
262               (let ((point (point)))
263                 (insert (nnrss-string-as-multibyte (nth 6 e)))
264                 (goto-char point)
265                 (while (search-forward "\n" nil t)
266                   (delete-char -1))
267                 (goto-char (point-max))
268                 (insert "\n\n")
269                 (fill-region point (point))))
270           (if (nth 2 e)
271               (insert (nth 2 e) "\n"))
272           (if nnrss-content-function
273               (funcall nnrss-content-function e group article)))))
274     (cond
275      (err
276       (nnheader-report 'nnrss err))
277      ((not e)
278       (nnheader-report 'nnrss "No such id: %d" article))
279      (t
280       (nnheader-report 'nnrss "Article %s retrieved" (car e))
281       ;; We return the article number.
282       (cons nnrss-group (car e))))))
283
284 (deffoo nnrss-request-list (&optional server)
285   (nnrss-possibly-change-group nil server)
286   (nnrss-generate-active)
287   t)
288
289 (deffoo nnrss-open-server (server &optional defs connectionless)
290   (nnrss-read-server-data server)
291   (nnoo-change-server 'nnrss server defs)
292   t)
293
294 (deffoo nnrss-request-expire-articles
295     (articles group &optional server force)
296   (nnrss-possibly-change-group group server)
297   (let (e days not-expirable changed)
298     (dolist (art articles)
299       (if (and (setq e (assq art nnrss-group-data))
300                (nnmail-expired-article-p
301                 group
302                 (if (listp (setq days (nth 1 e))) days
303                   (days-to-time (- days (time-to-days '(0 0)))))
304                 force))
305           (setq nnrss-group-data (delq e nnrss-group-data)
306                 changed t)
307         (push art not-expirable)))
308     (if changed
309         (nnrss-save-group-data group server))
310     not-expirable))
311
312 (deffoo nnrss-request-delete-group (group &optional force server)
313   (nnrss-possibly-change-group group server)
314   (setq nnrss-server-data
315         (delq (assoc group nnrss-server-data) nnrss-server-data))
316   (nnrss-save-server-data server)
317   (let ((file (expand-file-name
318                (nnrss-translate-file-chars
319                 (concat group (and server
320                                    (not (equal server ""))
321                                    "-")
322                         server ".el")) nnrss-directory)))
323     (ignore-errors
324       (delete-file file)))
325   t)
326
327 (deffoo nnrss-request-list-newsgroups (&optional server)
328   (nnrss-possibly-change-group nil server)
329   (save-excursion
330     (set-buffer nntp-server-buffer)
331     (erase-buffer)
332     (dolist (elem nnrss-group-alist)
333       (if (third elem)
334           (insert (car elem) "\t" (third elem) "\n"))))
335   t)
336
337 (nnoo-define-skeleton nnrss)
338
339 ;;; Internal functions
340
341 (defun nnrss-possibly-change-group (&optional group server)
342   (when (and server
343              (not (nnrss-server-opened server)))
344     (nnrss-open-server server))
345   (when (and group (not (equal group nnrss-group)))
346     (nnrss-read-group-data group server)
347     (setq nnrss-group group)))
348
349 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
350
351 (defun nnrss-generate-active ()
352   (if (y-or-n-p "Fetch extra categories? ")
353       (dolist (func nnrss-extra-categories)
354         (funcall func)))
355   (save-excursion
356     (set-buffer nntp-server-buffer)
357     (erase-buffer)
358     (dolist (elem nnrss-group-alist)
359       (insert (prin1-to-string (car elem)) " 0 1 y\n"))
360     (dolist (elem nnrss-server-data)
361       (unless (assoc (car elem) nnrss-group-alist)
362         (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
363
364 ;;; Data functions
365
366 (defun nnrss-read-server-data (server)
367   (setq nnrss-server-data nil)
368   (let ((file (expand-file-name
369                (nnrss-translate-file-chars
370                 (concat "nnrss" (and server
371                                      (not (equal server ""))
372                                      "-")
373                         server
374                         ".el"))
375                nnrss-directory)))