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 '()
63 "List of RSS addresses.")
65 (defvar nnrss-use-local nil)
67 (defvar nnrss-description-field 'X-Gnus-Description
68 "Field name used for DESCRIPTION.
69 To use the description in headers, put this name into `nnmail-extra-headers'.")
71 (defvar nnrss-url-field 'X-Gnus-Url
72 "Field name used for URL.
73 To use the description in headers, put this name into `nnmail-extra-headers'.")
75 (defvar nnrss-content-function nil
76 "A function which is called in `nnrss-request-article'.
77 The arguments are (ENTRY GROUP ARTICLE).
78 ENTRY is the record of the current headline. GROUP is the group name.
79 ARTICLE is the article number of the current headline.")
81 (nnoo-define-basics nnrss)
83 ;;; Interface functions
85 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
86 (nnrss-possibly-change-group group server)
89 (set-buffer nntp-server-buffer)
91 (dolist (article articles)
92 (if (setq e (assq article nnrss-group-data))
93 (insert (number-to-string (car e)) "\t" ;; number
95 (nnrss-format-string (nth 3 e)) "")
98 (nnrss-format-string (nth 4 e))
103 (format "<%d@%s.nnrss>" (car e) group)
110 (memq nnrss-description-field
111 nnmail-extra-headers))
112 (concat (symbol-name nnrss-description-field)
114 (nnrss-format-string (nth 6 e))
118 (memq nnrss-url-field
119 nnmail-extra-headers))
120 (concat (symbol-name nnrss-url-field)
122 (nnrss-format-string (nth 2 e))
128 (deffoo nnrss-request-group (group &optional server dont-check)
129 (nnrss-possibly-change-group group server)
132 (nnrss-check-group group server)
133 (nnheader-report 'nnrss "Opened group %s" group)
135 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
136 (prin1-to-string group)
139 (deffoo nnrss-close-group (group &optional server)
142 (deffoo nnrss-request-article (article &optional group server buffer)
143 (nnrss-possibly-change-group group server)
144 (let ((e (assq article nnrss-group-data))
145 (boundary "=-=-=-=-=-=-=-=-=-")
146 (nntp-server-buffer (or buffer nntp-server-buffer))
150 (with-current-buffer nntp-server-buffer
152 (goto-char (point-min))
153 (insert "Mime-Version: 1.0\nContent-Type: multipart/alternative; boundary=\"" boundary "\"\n")
155 (insert "Newsgroups: " group "\n"))
157 (insert "Subject: " (nnrss-format-string (nth 3 e)) "\n"))
159 (insert "From: " (nnrss-format-string (nth 4 e)) "\n"))
161 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
162 (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
164 (let ((text (if (nth 6 e)
165 (nnrss-string-as-multibyte (nth 6 e))))
168 (insert "\n\n--" boundary "\nContent-Type: text/plain\n\n")
169 (let ((point (point)))
173 (while (re-search-forward "\n" nil t)
175 (goto-char (point-max))
179 (insert "\n\n--" boundary "\nContent-Type: text/html\n\n")
180 (let ((point (point)))
182 (progn (insert "<html><head></head><body>\n" text "\n</body></html>")
184 (while (re-search-forward "\n" nil t)
186 (goto-char (point-max))
189 (insert "<p><a href=\"" link "\">link</a></p>\n"))))
190 (if nnrss-content-function
191 (funcall nnrss-content-function e group article)))))
194 (nnheader-report 'nnrss err))
196 (nnheader-report 'nnrss "no such id: %d" article))
198 (nnheader-report 'nnrss "article %s retrieved" (car e))
199 ;; we return the article number.
200 (cons nnrss-group (car e))))))
202 (deffoo nnrss-request-list (&optional server)
203 (nnrss-possibly-change-group nil server)
204 (nnrss-generate-active)
207 (deffoo nnrss-open-server (server &optional defs connectionless)
208 (nnrss-read-server-data server)
209 (nnoo-change-server 'nnrss server defs)
212 (deffoo nnrss-request-expire-articles
213 (articles group &optional server force)
214 (nnrss-possibly-change-group group server)
215 (let (e days not-expirable changed)
216 (dolist (art articles)
217 (if (and (setq e (assq art nnrss-group-data))
218 (nnmail-expired-article-p
220 (if (listp (setq days (nth 1 e))) days
221 (days-to-time (- days (time-to-days '(0 0)))))
223 (setq nnrss-group-data (delq e nnrss-group-data)
225 (push art not-expirable)))
227 (nnrss-save-group-data group server))
230 (deffoo nnrss-request-delete-group (group &optional force server)
231 (nnrss-possibly-change-group group server)
232 (setq nnrss-server-data
233 (delq (assoc group nnrss-server-data) nnrss-server-data))
234 (nnrss-save-server-data server)
235 (let ((file (expand-file-name
236 (nnrss-translate-file-chars
237 (concat group (and server
238 (not (equal server ""))
240 server ".el")) nnrss-directory)))
245 (deffoo nnrss-request-list-newsgroups (&optional server)
246 (nnrss-possibly-change-group nil server)
248 (set-buffer nntp-server-buffer)
250 (dolist (elem nnrss-group-alist)
252 (insert (car elem) "\t" (third elem) "\n"))))
255 (nnoo-define-skeleton nnrss)
257 ;;; Internal functions
258 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
259 (defun nnrss-fetch (url &optional local)
260 "Fetch the url and put it in a the expected lisp structure."
262 ;some CVS versions of url.el need this to close the connection quickly
263 (let* (xmlform htmlform)
264 ;; bit o' work necessary for w3 pre-cvs and post-cvs
266 (let ((coding-system-for-read 'binary))
267 (insert-file-contents url))
270 ;; Because xml-parse-region can't deal with anything that isn't
271 ;; xml and w3-parse-buffer can't deal with some xml, we have to
272 ;; parse with xml-parse-region first and, if that fails, parse
273 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
274 ;; why w3-parse-buffer fails to parse some well-formed xml and
278 (setq xmlform (xml-parse-region (point-min) (point-max)))
279 (error (if (fboundp 'w3-parse-buffer)
280 (setq htmlform (caddar (w3-parse-buffer
282 (message "nnrss: Not valid XML and w3 parse not available (%s)"
288 (defun nnrss-possibly-change-group (&optional group server)
290 (not (nnrss-server-opened server)))
291 (nnrss-open-server server))
292 (when (and group (not (equal group nnrss-group)))
293 (nnrss-read-group-data group server)
294 (setq nnrss-group group)))
296 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
298 (defun nnrss-generate-active ()
299 (if (y-or-n-p "fetch extra categories? ")
300 (dolist (func nnrss-extra-categories)
303 (set-buffer nntp-server-buffer)
305 (dolist (elem nnrss-group-alist)
306 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
307 (dolist (elem nnrss-server-data)
308 (unless (assoc (car elem) nnrss-group-alist)
309 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
313 (defun nnrss-read-server-data (server)
314 (setq nnrss-server-data nil)
315 (let ((file (expand-file-name
316 (nnrss-translate-file-chars
317 (concat "nnrss" (and server
318 (not (equal server ""))
323 (when (file-exists-p file)
325 (let ((coding-system-for-read 'binary)
326 emacs-lisp-mode-hook)
327 (insert-file-contents file)
329 (goto-char (point-min))
332 (defun nnrss-save-server-data (server)
333 (gnus-make-directory nnrss-directory)
334 (let ((file (expand-file-name
335 (nnrss-translate-file-chars
336 (concat "nnrss" (and server
337 (not (equal server ""))
341 (let ((coding-system-for-write 'binary)
342 print-level print-length)
344 (insert "(setq nnrss-group-alist '"
345 (prin1-to-string nnrss-group-alist)
347 (insert "(setq nnrss-server-data '"
348 (prin1-to-string nnrss-server-data)
351 (defun nnrss-read-group-data (group server)
352 (setq nnrss-group-data nil)
353 (setq nnrss-group-hashtb (gnus-make-hashtable))
354 (let ((pair (assoc group nnrss-server-data)))
355 (setq nnrss-group-max (or (cadr pair) 0))
356 (setq nnrss-group-min (+ nnrss-group-max 1)))
357 (let ((file (expand-file-name
358 (nnrss-translate-file-chars
359 (concat group (and server
360 (not (equal server ""))
364 (when (file-exists-p file)
366 (let ((coding-system-for-read 'binary)
367 emacs-lisp-mode-hook)
368 (insert-file-contents file)
370 (goto-char (point-min))
372 (dolist (e nnrss-group-data)
373 (gnus-sethash (nth 2 e) e nnrss-group-hashtb)
374 (if (and (car e) (> nnrss-group-min (car e)))
375 (setq nnrss-group-min (car e)))
376 (if (and (car e) (< nnrss-group-max (car e)))
377 (setq nnrss-group-max (car e)))))))
379 (defun nnrss-save-group-data (group server)
380 (gnus-make-directory nnrss-directory)
381 (let ((file (expand-file-name
382 (nnrss-translate-file-chars
383 (concat group (and server
384 (not (equal server ""))
388 (let ((coding-system-for-write 'binary)
389 print-level print-length)
391 (insert "(setq nnrss-group-data '"
392 (prin1-to-string nnrss-group-data)
397 (defun nnrss-no-cache (url)
400 (defun nnrss-insert-w3 (url)
401 (mm-with-unibyte-current-buffer
402 (mm-url-insert url)))
404 (defun nnrss-decode-entities-unibyte-string (string)
406 (mm-with-unibyte-buffer
408 (mm-url-decode-entities-nbsp)
411 (defalias 'nnrss-insert 'nnrss-insert-w3)
413 (if (featurep 'xemacs)
414 (defalias 'nnrss-string-as-multibyte 'identity)
415 (defalias 'nnrss-string-as-multibyte 'string-as-multibyte))
419 (defun nnrss-check-group (group server)
420 (let (file xml subject url extra changed author
421 date rss-ns rdf-ns content-ns dc-ns)
422 (if (and nnrss-use-local
423 (file-exists-p (setq file (expand-file-name
424 (nnrss-translate-file-chars
425 (concat group ".xml"))
427 (setq xml (nnrss-fetch file t))
428 (setq url (or (nth 2 (assoc group nnrss-server-data))
429 (second (assoc group nnrss-group-alist))))
436 (format "URL to search for %s: " group) "http://")))))
437 (let ((pair (assoc group nnrss-server-data)))
439 (setcdr (cdr pair) (list url))
440 (push (list group nnrss-group-max url) nnrss-server-data)))
442 (setq xml (nnrss-fetch url)))
444 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
445 ;; for more RSS namespaces.
446 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
447 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
448 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
449 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
450 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
451 (when (and (listp item)
452 (eq (intern (concat rss-ns "item")) (car item))
453 (setq url (nnrss-decode-entities-unibyte-string
454 (nnrss-node-text rss-ns 'link (cddr item))))
455 (not (gnus-gethash url nnrss-group-hashtb)))
456 (setq subject (nnrss-node-text rss-ns 'title item))
457 (setq extra (or (nnrss-node-text content-ns 'encoded item)
458 (nnrss-node-text rss-ns 'description item)))
459 (setq author (or (nnrss-node-text rss-ns 'author item)
460 (nnrss-node-text dc-ns 'creator item)
461 (nnrss-node-text dc-ns 'contributor item)))
462 (setq date (or (nnrss-node-text dc-ns 'date item)
463 (nnrss-node-text rss-ns 'pubDate item)
464 (message-make-date)))
467 (incf nnrss-group-max)
470 (and subject (nnrss-decode-entities-unibyte-string subject))
471 (and author (nnrss-decode-entities-unibyte-string author))
473 (and extra (nnrss-decode-entities-unibyte-string extra)))
475 (gnus-sethash url (car nnrss-group-data) nnrss-group-hashtb)
478 (nnrss-save-group-data group server)
479 (let ((pair (assoc group nnrss-server-data)))
481 (setcar (cdr pair) nnrss-group-max)
482 (push (list group nnrss-group-max) nnrss-server-data)))
483 (nnrss-save-server-data server))))
485 (defun nnrss-generate-download-script ()
486 "Generate a download script in the current buffer.
487 It is useful when `(setq nnrss-use-local t)'."
489 (insert "#!/bin/sh\n")
490 (insert "WGET=wget\n")
491 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
492 (dolist (elem nnrss-server-data)
493 (let ((url (or (nth 2 elem)
494 (second (assoc (car elem) nnrss-group-alist)))))
495 (insert "$WGET -q -O \"$RSSDIR\"/'"
496 (nnrss-translate-file-chars (concat (car elem) ".xml"))
499 (defun nnrss-translate-file-chars (name)
500 (let ((nnheader-file-name-translation-alist
501 (append nnheader-file-name-translation-alist '((?' . ?_)))))
502 (nnheader-translate-file-chars name)))
504 (defvar nnrss-moreover-url
505 "http://w.moreover.com/categories/category_list_rss.html"
506 "The url of moreover.com categories.")
508 (defun nnrss-snarf-moreover-categories ()
509 "Snarf RSS links from moreover.com."
511 (let (category name url changed)
513 (nnrss-insert nnrss-moreover-url)
514 (goto-char (point-min))
515 (while (re-search-forward
516 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
518 (setq category (match-string 1))
519 (setq url (match-string 2)
520 name (mm-url-decode-entities-string
521 (rfc2231-decode-encoded-string
524 (setq name (concat category "." name)))
525 (unless (assoc name nnrss-server-data)
527 (push (list name 0 url) nnrss-server-data)))))
529 (nnrss-save-server-data ""))))
531 (defun nnrss-format-string (string)
532 (gnus-replace-in-string (nnrss-string-as-multibyte string) " *\n *" " "))
534 (defun nnrss-node-text (namespace local-name element)
535 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
537 (text (if (and node (listp node))
538 (nnrss-node-just-text node)
540 (cleaned-text (if text (gnus-replace-in-string
541 text "^[\000-\037\177]+\\|^ +\\| +$" ""))))
542 (if (string-equal "" cleaned-text)
546 (defun nnrss-node-just-text (node)
547 (if (and node (listp node))
548 (mapconcat 'nnrss-node-just-text (cddr node) " ")
551 (defun nnrss-find-el (tag data &optional found-list)
552 "Find the all matching elements in the data. Careful with this on
555 (mapcar (lambda (bit)
557 (progn (if (equal tag (car bit))
561 (if (and (listp (car-safe (caddr bit)))
562 (not (stringp (caddr bit))))
570 tag (cddr bit))))))))
574 (defun nnrss-rsslink-p (el)
575 "Test if the element we are handed is an RSS autodiscovery link."
576 (and (eq (car-safe el) 'link)
577 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
578 (or (string-equal (cdr (assoc 'type (cadr el)))
579 "application/rss+xml")
580 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
582 (defun nnrss-get-rsslinks (data)
583 "Extract the <link> elements that are links to RSS from the parsed data."
586 (if (nnrss-rsslink-p el) el))
587 (nnrss-find-el 'link data))))
589 (defun nnrss-extract-hrefs (data)
590 "Recursively extract hrefs from a page's source. DATA should be
591 the output of xml-parse-region or w3-parse-buffer."
592 (mapcar (lambda (ahref)
593 (cdr (assoc 'href (cadr ahref))))
594 (nnrss-find-el 'a data)))
596 (defmacro nnrss-match-macro (base-uri item
597 onsite-list offsite-list)
598 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
599 (not (string-match "://" ,item)))
600 (setq ,onsite-list (append ,onsite-list (list ,item))))
601 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
603 (defun nnrss-order-hrefs (base-uri hrefs)
604 "Given a list of hrefs, sort them using the following priorities:
605 1. links ending in .rss
606 2. links ending in .rdf
607 3. links ending in .xml
608 4. links containing the above
611 BASE-URI is used to determine the location of the links and
612 whether they are `offsite' or `onsite'."
613 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
614 rss-onsite-in rdf-onsite-in xml-onsite-in
615 rss-offsite-end rdf-offsite-end xml-offsite-end
616 rss-offsite-in rdf-offsite-in xml-offsite-in)
617 (mapcar (lambda (href)
618 (if (not (null href))
619 (cond ((string-match "\\.rss$" href)
621 base-uri href rss-onsite-end rss-offsite-end))
622 ((string-match "\\.rdf$" href)
624 base-uri href rdf-onsite-end rdf-offsite-end))
625 ((string-match "\\.xml$" href)
627 base-uri href xml-onsite-end xml-offsite-end))
628 ((string-match "rss" href)
630 base-uri href rss-onsite-in rss-offsite-in))
631 ((string-match "rdf" href)
633 base-uri href rdf-onsite-in rdf-offsite-in))
634 ((string-match "xml" href)
636 base-uri href xml-onsite-in xml-offsite-in)))))
639 rss-onsite-end rdf-onsite-end xml-onsite-end
640 rss-onsite-in rdf-onsite-in xml-onsite-in
641 rss-offsite-end rdf-offsite-end xml-offsite-end
642 rss-offsite-in rdf-offsite-in xml-offsite-in)))
644 (defun nnrss-discover-feed (url)
645 "Given a page, find an RSS feed using Mark Pilgrim's
646 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
648 (let ((parsed-page (nnrss-fetch url)))
650 ;; 1. if this url is the rss, use it.
651 (if (nnrss-rss-p parsed-page)
652 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
653 (nnrss-rss-title-description rss-ns parsed-page url))
655 ;; 2. look for the <link rel="alternate"
656 ;; type="application/rss+xml" and use that if it is there.
657 (let ((links (nnrss-get-rsslinks parsed-page)))
659 (let* ((xml (nnrss-fetch
660 (cdr (assoc 'href (cadar links)))))
661 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
662 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
664 ;; 3. look for links on the site in the following order:
665 ;; - onsite links ending in .rss, .rdf, or .xml
666 ;; - onsite links containing any of the above
667 ;; - offsite links ending in .rss, .rdf, or .xml
668 ;; - offsite links containing any of the above
669 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
670 (match-string 0 url)))
671 (hrefs (nnrss-order-hrefs
672 base-uri (nnrss-extract-hrefs parsed-page)))
674 (while (and (eq rss-link nil) (not (eq hrefs nil)))
675 (let ((href-data (nnrss-fetch (car hrefs))))
676 (if (nnrss-rss-p href-data)
677 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
678 (setq rss-link (nnrss-rss-title-description
679 rss-ns href-data (car hrefs))))
680 (setq hrefs (cdr hrefs)))))
681 (if rss-link rss-link
684 (nnrss-find-rss-via-syndic8 url))))))))
686 (defun nnrss-find-rss-via-syndic8 (url)
687 "query syndic8 for the rss feeds it has for the url."
688 (if (not (locate-library "xml-rpc"))
690 (message "XML-RPC is not available... not checking Syndic8.")
693 (let ((feedid (xml-rpc-method-call
694 "http://www.syndic8.com/xmlrpc.php"
698 (let* ((feedinfo (xml-rpc-method-call
699 "http://www.syndic8.com/xmlrpc.php"
707 (cdr (assoc "status" listinfo))
710 (cdr (assoc "sitename" listinfo))
714 "sitename" listinfo)))
717 "dataurl" listinfo)))))))
719 (if (not (> (length urllist) 1))
721 (let ((completion-ignore-case t)
723 (mapcar (lambda (listinfo)
724 (cons (cdr (assoc "sitename" listinfo))
726 (cdr (assoc "feedid" listinfo)))))
730 "Multiple feeds found. Select one: "
731 selection nil t) urllist)))))))))
733 (defun nnrss-rss-p (data)
734 "Test if data is an RSS feed. Simply ensures that the first
735 element is rss or rdf."
736 (or (eq (caar data) 'rss)
737 (eq (caar data) 'rdf:RDF)))
739 (defun nnrss-rss-title-description (rss-namespace data url)
740 "Return the title of an RSS feed."
741 (if (nnrss-rss-p data)
742 (let ((description (intern (concat rss-namespace "description")))
743 (title (intern (concat rss-namespace "title")))
744 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
747 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
748 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
751 (defun nnrss-get-namespace-prefix (el uri)
752 "Given EL (containing a parsed element) and URI (containing a string
753 that gives the URI for which you want to retrieve the namespace
754 prefix), return the prefix."
755 (let* ((prefix (car (rassoc uri (cadar el))))
757 (split-string (symbol-name prefix) ":")))
758 (ns (cond ((eq (length nslist) 1) ; no prefix given
760 ((eq (length nslist) 2) ; extract prefix
762 (if (and ns (not (eq ns "")))
769 ;;; nnrss.el ends here