1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
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.
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.
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.
28 (eval-when-compile (require 'cl))
42 (eval '(require 'xml))
46 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
47 "Where nnrss will save its files.")
49 ;; (group max rss-url)
50 (defvoo nnrss-server-data nil)
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 "")
60 (defconst nnrss-version "nnrss 1.0")
62 (defvar nnrss-group-alist
64 "http://macweek.zdnet.com/macweek.xml"
65 "The Macintosh news authority.")
67 "http://lwn.net/headlines/rss")
69 "http://www.fool.com/About/headlines/rss_headlines.asp")
71 "http://www.newsforge.com/newsforge.rdf")
73 "http://www.slashdot.com/slashdot.rdf")
75 "http://www.cnn.com/cnn.rss"
76 "The world's news leader.")
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.")
84 "http://monkeyfist.com/rdf.php3"
85 "News and opinion on politics, technology, and eclectic miscellany.")
87 "http://www.newsforge.com/newsforge.rss")
89 "http://www.reutershealth.com/eline.rss"
90 "Consumer-oriented health-related news stories.")
92 "http://www.salon.com/feed/RDF/salon_use.rdf")
94 "http://www.wired.com/news_drop/netcenter/netcenter.rdf")
96 "http://www.itn.co.uk/itn.rdf")
98 "http://www.oreillynet.com/meerkat/?_fl=rss10"
99 "An Open Wire Service")
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.")
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")
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.")
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.")
141 "http://www.theregister.co.uk/tonys/slashdot.rdf"
142 "The Register -- Biting the hand that feeds IT.")
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.")
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.")
156 "http://www.macrumors.com/macrumors.xml"
157 "The mac news you care about.")
159 "http://www.macosxhints.com/backend/geeklog.rdf"
162 "List of RSS addresses.")
164 (defvar nnrss-use-local nil)
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'.")
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'.")
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.")
180 (nnoo-define-basics nnrss)
182 ;;; Interface functions
184 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
185 (nnrss-possibly-change-group group server)
188 (set-buffer nntp-server-buffer)
190 (dolist (article articles)
191 (if (setq e (assq article nnrss-group-data))
192 (insert (number-to-string (car e)) "\t" ;; number
194 (nnrss-format-string (nth 3 e)) "")
197 (nnrss-format-string (nth 4 e))
202 (format "<%d@%s.nnrss>" (car e) group)
209 (memq nnrss-description-field
210 nnmail-extra-headers))
211 (concat (symbol-name nnrss-description-field)
213 (nnrss-format-string (nth 6 e))
217 (memq nnrss-url-field
218 nnmail-extra-headers))
219 (concat (symbol-name nnrss-url-field)
221 (nnrss-format-string (nth 2 e))
227 (deffoo nnrss-request-group (group &optional server dont-check)
228 (nnrss-possibly-change-group group server)
231 (nnrss-check-group group server)
232 (nnheader-report 'nnrss "Opened group %s" group)
234 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
235 (prin1-to-string group)
238 (deffoo nnrss-close-group (group &optional server)
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))
248 (with-current-buffer nntp-server-buffer
250 (goto-char (point-min))
252 (insert "Newsgroups: " group "\n"))
254 (insert "Subject: " (nnrss-format-string (nth 3 e)) "\n"))
256 (insert "From: " (nnrss-format-string (nth 4 e)) "\n"))
258 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
259 (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
262 (let ((point (point)))
263 (insert (nnrss-string-as-multibyte (nth 6 e)))
265 (while (search-forward "\n" nil t)
267 (goto-char (point-max))
269 (fill-region point (point))))
271 (insert (nth 2 e) "\n"))
272 (if nnrss-content-function
273 (funcall nnrss-content-function e group article)))))
276 (nnheader-report 'nnrss err))
278 (nnheader-report 'nnrss "No such id: %d" article))
280 (nnheader-report 'nnrss "Article %s retrieved" (car e))
281 ;; We return the article number.
282 (cons nnrss-group (car e))))))
284 (deffoo nnrss-request-list (&optional server)
285 (nnrss-possibly-change-group nil server)
286 (nnrss-generate-active)
289 (deffoo nnrss-open-server (server &optional defs connectionless)
290 (nnrss-read-server-data server)
291 (nnoo-change-server 'nnrss server defs)
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
302 (if (listp (setq days (nth 1 e))) days
303 (days-to-time (- days (time-to-days '(0 0)))))
305 (setq nnrss-group-data (delq e nnrss-group-data)
307 (push art not-expirable)))
309 (nnrss-save-group-data group server))
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 ""))
322 server ".el")) nnrss-directory)))
327 (deffoo nnrss-request-list-newsgroups (&optional server)
328 (nnrss-possibly-change-group nil server)
330 (set-buffer nntp-server-buffer)
332 (dolist (elem nnrss-group-alist)
334 (insert (car elem) "\t" (third elem) "\n"))))
337 (nnoo-define-skeleton nnrss)
339 ;;; Internal functions
341 (defun nnrss-possibly-change-group (&optional group 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)))
349 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
351 (defun nnrss-generate-active ()
352 (if (y-or-n-p "Fetch extra categories? ")
353 (dolist (func nnrss-extra-categories)
356 (set-buffer nntp-server-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")))))
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 ""))
376 (when (file-exists-p file)
378 (let ((coding-system-for-read 'binary)
379 emacs-lisp-mode-hook)
380 (insert-file-contents file)
382 (goto-char (point-min))
385 (defun nnrss-save-server-data (server)
386 (gnus-make-directory nnrss-directory)
387 (let ((file (expand-file-name
388 (nnrss-translate-file-chars
389 (concat "nnrss" (and server
390 (not (equal server ""))
394 (let ((coding-system-for-write 'binary)
395 print-level print-length)
397 (insert "(setq nnrss-group-alist '"
398 (prin1-to-string nnrss-group-alist)
400 (insert "(setq nnrss-server-data '"
401 (prin1-to-string nnrss-server-data)
404 (defun nnrss-read-group-data (group server)
405 (setq nnrss-group-data nil)
406 (setq nnrss-group-hashtb (gnus-make-hashtable))
407 (let ((pair (assoc group nnrss-server-data)))
408 (setq nnrss-group-max (or (cadr pair) 0))
409 (setq nnrss-group-min (+ nnrss-group-max 1)))
410 (let ((file (expand-file-name
411 (nnrss-translate-file-chars
412 (concat group (and server
413 (not (equal server ""))
417 (when (file-exists-p file)
419 (let ((coding-system-for-read 'binary)
420 emacs-lisp-mode-hook)
421 (insert-file-contents file)
423 (goto-char (point-min))
425 (dolist (e nnrss-group-data)
426 (gnus-sethash (nth 2 e) e nnrss-group-hashtb)
427 (if (and (car e) (> nnrss-group-min (car e)))
428 (setq nnrss-group-min (car e)))
429 (if (and (car e) (< nnrss-group-max (car e)))
430 (setq nnrss-group-max (car e)))))))
432 (defun nnrss-save-group-data (group server)
433 (gnus-make-directory nnrss-directory)
434 (let ((file (expand-file-name
435 (nnrss-translate-file-chars
436 (concat group (and server
437 (not (equal server ""))
441 (let ((coding-system-for-write 'binary)
442 print-level print-length)
444 (insert "(setq nnrss-group-data '"
445 (prin1-to-string nnrss-group-data)
450 (defun nnrss-no-cache (url)
453 (defun nnrss-insert-w3 (url)
454 (mm-with-unibyte-current-buffer
455 (mm-url-insert url)))
457 (defun nnrss-decode-entities-unibyte-string (string)
458 (mm-with-unibyte-buffer
460 (mm-url-decode-entities-nbsp)
463 (defalias 'nnrss-insert 'nnrss-insert-w3)
465 (if (featurep 'xemacs)
466 (defalias 'nnrss-string-as-multibyte 'identity)
467 (defalias 'nnrss-string-as-multibyte 'string-as-multibyte))
471 (defun nnrss-check-group (group server)
472 (let (file xml subject url extra changed author date)
474 (mm-with-unibyte-buffer
475 (if (and nnrss-use-local
476 (file-exists-p (setq file (expand-file-name
477 (nnrss-translate-file-chars
478 (concat group ".xml"))
480 (insert-file-contents file)
481 (setq url (or (nth 2 (assoc group nnrss-server-data))
482 (second (assoc group nnrss-group-alist))))
485 (read-string (format "RSS url of %s: " group "http://")))
486 (let ((pair (assoc group nnrss-server-data)))
488 (setcdr (cdr pair) (list url))
489 (push (list group nnrss-group-max url) nnrss-server-data)))
492 (goto-char (point-min))
493 (while (re-search-forward "\r\n?" nil t)
494 (replace-match "\n"))
495 (goto-char (point-min))
496 (if (re-search-forward "<rdf\\|<rss" nil t)
497 (goto-char (match-beginning 0)))
498 (setq xml (xml-parse-region (point) (point-max))))
500 (nnheader-message 1 "Error in group %s: %s" group (cadr err))))
501 (while (and xml (not (assq 'item xml)))
502 (setq xml (cddar xml))
503 (while (not (listp (car xml)))
504 (setq xml (cdr xml))))
505 (dolist (item (nreverse xml))
506 (when (and (listp item)
507 (eq 'item (car item))
508 (setq url (nnrss-node-text (assq 'link (cddr item))))
509 (setq url (nnrss-decode-entities-unibyte-string url))
510 (not (gnus-gethash url nnrss-group-hashtb)))
511 (setq subject (nnrss-node-text (assq 'title (cddr item))))
512 (setq extra (or (nnrss-node-text (assq 'description (cddr item)))
513 (nnrss-node-text (assq 'dc:description (cddr item)))))
514 (setq author (nnrss-node-text (assq 'dc:creator (cddr item))))
515 (setq date (or (nnrss-node-text (assq 'dc:date (cddr item)))
516 (message-make-date)))
519 (incf nnrss-group-max)
522 (and subject (nnrss-decode-entities-unibyte-string subject))
523 (and author (nnrss-decode-entities-unibyte-string author))
525 (and extra (nnrss-decode-entities-unibyte-string extra)))
527 (gnus-sethash url (car nnrss-group-data) nnrss-group-hashtb)
530 (nnrss-save-group-data group server)
531 (let ((pair (assoc group nnrss-server-data)))
533 (setcar (cdr pair) nnrss-group-max)
534 (push (list group nnrss-group-max) nnrss-server-data)))
535 (nnrss-save-server-data server))))
537 (defun nnrss-generate-download-script ()
538 "Generate a download script in the current buffer.
539 It is useful when `(setq nnrss-use-local t)'."
541 (insert "#!/bin/sh\n")
542 (insert "WGET=wget\n")
543 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
544 (dolist (elem nnrss-server-data)
545 (let ((url (or (nth 2 elem)
546 (second (assoc (car elem) nnrss-group-alist)))))
547 (insert "$WGET -q -O \"$RSSDIR\"/'"
548 (nnrss-translate-file-chars (concat (car elem) ".xml"))
551 (defun nnrss-translate-file-chars (name)
552 (let ((nnheader-file-name-translation-alist
553 (append nnheader-file-name-translation-alist '((?' . ?_)))))
554 (nnheader-translate-file-chars name)))
556 (defvar nnrss-moreover-url
557 "http://w.moreover.com/categories/category_list_rss.html"
558 "The url of moreover.com categories.")
560 (defun nnrss-snarf-moreover-categories ()
561 "Snarf RSS links from moreover.com."
563 (let (category name url changed)
565 (nnrss-insert nnrss-moreover-url)
566 (goto-char (point-min))
567 (while (re-search-forward
568 "<A NAME=\"\\([^\"]+\\)\">\\|<A HREF=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
570 (setq category (match-string 1))
571 (setq url (match-string 2)
572 name (mm-url-decode-entities-string
573 (rfc2231-decode-encoded-string
576 (setq name (concat category "." name)))
577 (unless (assoc name nnrss-server-data)
579 (push (list name 0 url) nnrss-server-data)))))
581 (nnrss-save-server-data ""))))
583 (defun nnrss-format-string (string)
584 (gnus-replace-in-string (nnrss-string-as-multibyte string) " *\n *" " "))
586 (defun nnrss-node-text (node)
587 (if (and node (listp node))
588 (mapconcat 'nnrss-node-text (cddr node) "")
593 ;;; nnrss.el ends here