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 (nntp-server-buffer (or buffer nntp-server-buffer))
149 (with-current-buffer nntp-server-buffer
151 (goto-char (point-min))
152 (insert "Mime-Version: 1.0\nContent-Type: text/html\n")
154 (insert "Newsgroups: " group "\n"))
156 (insert "Subject: " (nnrss-format-string (nth 3 e)) "\n"))
158 (insert "From: " (nnrss-format-string (nth 4 e)) "\n"))
160 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
161 (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
164 (let ((point (point)))
165 (insert (nnrss-string-as-multibyte (nth 6 e)))
167 (while (re-search-forward "\n" nil t)
169 (goto-char (point-max))
171 (fill-region point (point))))
173 (insert "<p><a href='" (nth 2 e) "'>link</a></p>\n"))
174 (if nnrss-content-function
175 (funcall nnrss-content-function e group article)))))
178 (nnheader-report 'nnrss err))
180 (nnheader-report 'nnrss "no such id: %d" article))
182 (nnheader-report 'nnrss "article %s retrieved" (car e))
183 ;; we return the article number.
184 (cons nnrss-group (car e))))))
186 (deffoo nnrss-request-list (&optional server)
187 (nnrss-possibly-change-group nil server)
188 (nnrss-generate-active)
191 (deffoo nnrss-open-server (server &optional defs connectionless)
192 (nnrss-read-server-data server)
193 (nnoo-change-server 'nnrss server defs)
196 (deffoo nnrss-request-expire-articles
197 (articles group &optional server force)
198 (nnrss-possibly-change-group group server)
199 (let (e days not-expirable changed)
200 (dolist (art articles)
201 (if (and (setq e (assq art nnrss-group-data))
202 (nnmail-expired-article-p
204 (if (listp (setq days (nth 1 e))) days
205 (days-to-time (- days (time-to-days '(0 0)))))
207 (setq nnrss-group-data (delq e nnrss-group-data)
209 (push art not-expirable)))
211 (nnrss-save-group-data group server))
214 (deffoo nnrss-request-delete-group (group &optional force server)
215 (nnrss-possibly-change-group group server)
216 (setq nnrss-server-data
217 (delq (assoc group nnrss-server-data) nnrss-server-data))
218 (nnrss-save-server-data server)
219 (let ((file (expand-file-name
220 (nnrss-translate-file-chars
221 (concat group (and server
222 (not (equal server ""))
224 server ".el")) nnrss-directory)))
229 (deffoo nnrss-request-list-newsgroups (&optional server)
230 (nnrss-possibly-change-group nil server)
232 (set-buffer nntp-server-buffer)
234 (dolist (elem nnrss-group-alist)
236 (insert (car elem) "\t" (third elem) "\n"))))
239 (nnoo-define-skeleton nnrss)
241 ;;; Internal functions
242 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
243 (defun nnrss-fetch (url &optional local)
244 "Fetch the url and put it in a the expected lisp structure."
246 ;some CVS versions of url.el need this to close the connection quickly
247 (let* (xmlform htmlform)
248 ;; bit o' work necessary for w3 pre-cvs and post-cvs
250 (let ((coding-system-for-read 'binary))
251 (insert-file-contents url))
254 ;; Because xml-parse-region can't deal with anything that isn't
255 ;; xml and w3-parse-buffer can't deal with some xml, we have to
256 ;; parse with xml-parse-region first and, if that fails, parse
257 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
258 ;; why w3-parse-buffer fails to parse some well-formed xml and
262 (setq xmlform (xml-parse-region (point-min) (point-max)))
263 (error (if (fboundp 'w3-parse-buffer)
264 (setq htmlform (caddar (w3-parse-buffer
266 (message "nnrss: Not valid XML and w3 parse not available (%s)"
272 (defun nnrss-possibly-change-group (&optional group server)
274 (not (nnrss-server-opened server)))
275 (nnrss-open-server server))
276 (when (and group (not (equal group nnrss-group)))
277 (nnrss-read-group-data group server)
278 (setq nnrss-group group)))
280 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
282 (defun nnrss-generate-active ()
283 (if (y-or-n-p "fetch extra categories? ")
284 (dolist (func nnrss-extra-categories)
287 (set-buffer nntp-server-buffer)
289 (dolist (elem nnrss-group-alist)
290 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
291 (dolist (elem nnrss-server-data)
292 (unless (assoc (car elem) nnrss-group-alist)
293 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
297 (defun nnrss-read-server-data (server)
298 (setq nnrss-server-data nil)
299 (let ((file (expand-file-name
300 (nnrss-translate-file-chars
301 (concat "nnrss" (and server
302 (not (equal server ""))
307 (when (file-exists-p file)
309 (let ((coding-system-for-read 'binary)
310 (input-coding-system 'binary)
311 emacs-lisp-mode-hook)
312 (insert-file-contents file)
314 (goto-char (point-min))
317 (defun nnrss-save-server-data (server)
318 (gnus-make-directory nnrss-directory)
319 (let ((file (expand-file-name
320 (nnrss-translate-file-chars
321 (concat "nnrss" (and server
322 (not (equal server ""))
326 (let ((coding-system-for-write 'binary)
327 (output-coding-system 'binary)
328 print-level print-length)
330 (insert "(setq nnrss-group-alist '"
331 (prin1-to-string nnrss-group-alist)
333 (insert "(setq nnrss-server-data '"
334 (prin1-to-string nnrss-server-data)
337 (defun nnrss-read-group-data (group server)
338 (setq nnrss-group-data nil)
339 (setq nnrss-group-hashtb (gnus-make-hashtable))
340 (let ((pair (assoc group nnrss-server-data)))
341 (setq nnrss-group-max (or (cadr pair) 0))
342 (setq nnrss-group-min (+ nnrss-group-max 1)))
343 (let ((file (expand-file-name
344 (nnrss-translate-file-chars
345 (concat group (and server
346 (not (equal server ""))
350 (when (file-exists-p file)
352 (let ((coding-system-for-read 'binary)
353 (input-coding-system 'binary)
354 emacs-lisp-mode-hook)
355 (insert-file-contents file)
357 (goto-char (point-min))
359 (dolist (e nnrss-group-data)
360 (gnus-sethash (nth 2 e) e nnrss-group-hashtb)
361 (if (and (car e) (> nnrss-group-min (car e)))
362 (setq nnrss-group-min (car e)))
363 (if (and (car e) (< nnrss-group-max (car e)))
364 (setq nnrss-group-max (car e)))))))
366 (defun nnrss-save-group-data (group server)
367 (gnus-make-directory nnrss-directory)
368 (let ((file (expand-file-name
369 (nnrss-translate-file-chars
370 (concat group (and server
371 (not (equal server ""))
375 (let ((coding-system-for-write 'binary)
376 (output-coding-system 'binary)
377 print-level print-length)
379 (insert "(setq nnrss-group-data '"
380 (prin1-to-string nnrss-group-data)
385 (defun nnrss-no-cache (url)
388 (defun nnrss-insert-w3 (url)
389 (mm-with-unibyte-current-buffer
390 (mm-url-insert url)))
392 (defun nnrss-decode-entities-unibyte-string (string)
394 (mm-with-unibyte-buffer
396 (mm-url-decode-entities-nbsp)
399 (defalias 'nnrss-insert 'nnrss-insert-w3)
401 (if (featurep 'xemacs)
402 (defalias 'nnrss-string-as-multibyte 'identity)
403 (defalias 'nnrss-string-as-multibyte 'string-as-multibyte))
407 (defun nnrss-check-group (group server)
408 (let (file xml subject url extra changed author
409 date rss-ns rdf-ns content-ns dc-ns)
410 (if (and nnrss-use-local
411 (file-exists-p (setq file (expand-file-name
412 (nnrss-translate-file-chars
413 (concat group ".xml"))
416 (setq url (or (nth 2 (assoc group nnrss-server-data))
417 (second (assoc group nnrss-group-alist))))
422 (format "URL to search for %s: " group) "http://")))
423 (let ((pair (assoc group nnrss-server-data)))
425 (setcdr (cdr pair) (list url))
426 (push (list group nnrss-group-max url) nnrss-server-data)))
428 (setq xml (nnrss-fetch url)))
430 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
431 ;; for more RSS namespaces.
432 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
433 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
434 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
435 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
436 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
437 (when (and (listp item)
438 (eq (intern (concat rss-ns "item")) (car item))
439 (setq url (nnrss-decode-entities-unibyte-string
440 (nnrss-node-text rss-ns 'link (cddr item))))
441 (not (gnus-gethash url nnrss-group-hashtb)))
442 (setq subject (nnrss-node-text rss-ns 'title item))
443 (setq extra (or (nnrss-node-text content-ns 'encoded item)
444 (nnrss-node-text rss-ns 'description item)))
445 (setq author (or (nnrss-node-text rss-ns 'author item)
446 (nnrss-node-text dc-ns 'creator item)))
447 (setq date (or (nnrss-node-text dc-ns 'date item)
448 (nnrss-node-text rss-ns 'pubDate item)
449 (message-make-date)))
452 (incf nnrss-group-max)
455 (and subject (nnrss-decode-entities-unibyte-string subject))
456 (and author (nnrss-decode-entities-unibyte-string author))
458 (and extra (nnrss-decode-entities-unibyte-string extra)))
460 (gnus-sethash url (car nnrss-group-data) nnrss-group-hashtb)
463 (nnrss-save-group-data group server)
464 (let ((pair (assoc group nnrss-server-data)))
466 (setcar (cdr pair) nnrss-group-max)
467 (push (list group nnrss-group-max) nnrss-server-data)))
468 (nnrss-save-server-data server))))
470 (defun nnrss-generate-download-script ()
471 "Generate a download script in the current buffer.
472 It is useful when `(setq nnrss-use-local t)'."
474 (insert "#!/bin/sh\n")
475 (insert "WGET=wget\n")
476 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
477 (dolist (elem nnrss-server-data)
478 (let ((url (or (nth 2 elem)
479 (second (assoc (car elem) nnrss-group-alist)))))
480 (insert "$WGET -q -O \"$RSSDIR\"/'"
481 (nnrss-translate-file-chars (concat (car elem) ".xml"))
484 (defun nnrss-translate-file-chars (name)
485 (let ((nnheader-file-name-translation-alist
486 (append nnheader-file-name-translation-alist '((?' . ?_)))))
487 (nnheader-translate-file-chars name)))
489 (defvar nnrss-moreover-url
490 "http://w.moreover.com/categories/category_list_rss.html"
491 "The url of moreover.com categories.")
493 (defun nnrss-snarf-moreover-categories ()
494 "Snarf RSS links from moreover.com."
496 (let (category name url changed)
498 (nnrss-insert nnrss-moreover-url)
499 (goto-char (point-min))
500 (while (re-search-forward
501 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
503 (setq category (match-string 1))
504 (setq url (match-string 2)
505 name (mm-url-decode-entities-string
506 (rfc2231-decode-encoded-string
509 (setq name (concat category "." name)))
510 (unless (assoc name nnrss-server-data)
512 (push (list name 0 url) nnrss-server-data)))))
514 (nnrss-save-server-data ""))))
516 (defun nnrss-format-string (string)
517 (gnus-replace-in-string (nnrss-string-as-multibyte string) " *\n *" " "))
519 (defun nnrss-node-text (namespace local-name element)
520 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
522 (text (if (and node (listp node))
523 (nnrss-node-just-text node)
525 (cleaned-text (if text (gnus-replace-in-string
526 text "^[[:cntrl:]]+\\|^ +\\| +$" ""))))
527 (if (string-equal "" cleaned-text)
531 (defun nnrss-node-just-text (node)
532 (if (and node (listp node))
533 (mapconcat 'nnrss-node-just-text (cddr node) " ")
536 (defun nnrss-find-el (tag data &optional found-list)
537 "Find the all matching elements in the data. Careful with this on
540 (mapcar (lambda (bit)
542 (progn (if (equal tag (car bit))
546 (if (and (listp (car-safe (caddr bit)))
547 (not (stringp (caddr bit))))
555 tag (cddr bit))))))))
559 (defun nnrss-rsslink-p (el)
560 "Test if the element we are handed is an RSS autodiscovery link."
561 (and (eq (car-safe el) 'link)
562 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
563 (or (string-equal (cdr (assoc 'type (cadr el)))
564 "application/rss+xml")
565 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
567 (defun nnrss-get-rsslinks (data)
568 "Extract the <link> elements that are links to RSS from the parsed data."
571 (if (nnrss-rsslink-p el) el))
572 (nnrss-find-el 'link data))))
574 (defun nnrss-extract-hrefs (data)
575 "Recursively extract hrefs from a page's source. DATA should be
576 the output of xml-parse-region or w3-parse-buffer."
577 (mapcar (lambda (ahref)
578 (cdr (assoc 'href (cadr ahref))))
579 (nnrss-find-el 'a data)))
581 (defmacro nnrss-match-macro (base-uri item
582 onsite-list offsite-list)
583 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
584 (not (string-match "://" ,item)))
585 (setq ,onsite-list (append ,onsite-list (list ,item))))
586 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
588 (defun nnrss-order-hrefs (base-uri hrefs)
589 "Given a list of hrefs, sort them using the following priorities:
590 1. links ending in .rss
591 2. links ending in .rdf
592 3. links ending in .xml
593 4. links containing the above
596 BASE-URI is used to determine the location of the links and
597 whether they are `offsite' or `onsite'."
598 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
599 rss-onsite-in rdf-onsite-in xml-onsite-in
600 rss-offsite-end rdf-offsite-end xml-offsite-end
601 rss-offsite-in rdf-offsite-in xml-offsite-in)
602 (mapcar (lambda (href)
603 (if (not (null href))
604 (cond ((string-match "\\.rss$" href)
606 base-uri href rss-onsite-end rss-offsite-end))
607 ((string-match "\\.rdf$" href)
609 base-uri href rdf-onsite-end rdf-offsite-end))
610 ((string-match "\\.xml$" href)
612 base-uri href xml-onsite-end xml-offsite-end))
613 ((string-match "rss" href)
615 base-uri href rss-onsite-in rss-offsite-in))
616 ((string-match "rdf" href)
618 base-uri href rdf-onsite-in rdf-offsite-in))
619 ((string-match "xml" href)
621 base-uri href xml-onsite-in xml-offsite-in)))))
624 rss-onsite-end rdf-onsite-end xml-onsite-end
625 rss-onsite-in rdf-onsite-in xml-onsite-in
626 rss-offsite-end rdf-offsite-end xml-offsite-end
627 rss-offsite-in rdf-offsite-in xml-offsite-in)))
629 (defun nnrss-discover-feed (url)
630 "Given a page, find an RSS feed using Mark Pilgrim's
631 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
633 (let ((parsed-page (nnrss-fetch url)))
635 ;; 1. if this url is the rss, use it.
636 (if (nnrss-rss-p parsed-page)
637 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
638 (nnrss-rss-title-description rss-ns parsed-page url))
640 ;; 2. look for the <link rel="alternate"
641 ;; type="application/rss+xml" and use that if it is there.
642 (let ((links (nnrss-get-rsslinks parsed-page)))
644 (let* ((xml (nnrss-fetch
645 (cdr (assoc 'href (cadar links)))))
646 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
647 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
649 ;; 3. look for links on the site in the following order:
650 ;; - onsite links ending in .rss, .rdf, or .xml
651 ;; - onsite links containing any of the above
652 ;; - offsite links ending in .rss, .rdf, or .xml
653 ;; - offsite links containing any of the above
654 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
655 (match-string 0 url)))
656 (hrefs (nnrss-order-hrefs
657 base-uri (nnrss-extract-hrefs parsed-page)))
659 (while (and (eq rss-link nil) (not (eq hrefs nil)))
660 (let ((href-data (nnrss-fetch (car hrefs))))
661 (if (nnrss-rss-p href-data)
662 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
663 (setq rss-link (nnrss-rss-title-description
664 rss-ns href-data (car hrefs))))
665 (setq hrefs (cdr hrefs)))))
666 (if rss-link rss-link
669 (nnrss-find-rss-via-syndic8 url))))))))
671 (defun nnrss-find-rss-via-syndic8 (url)
672 "query syndic8 for the rss feeds it has for the url."
674 (progn (require 'xml-rpc)
675 (let ((feedid (xml-rpc-method-call
676 "http://www.syndic8.com/xmlrpc.php"
680 (let* ((feedinfo (xml-rpc-method-call
681 "http://www.syndic8.com/xmlrpc.php"
689 (cdr (assoc "status" listinfo))
692 (cdr (assoc "sitename" listinfo))
696 "sitename" listinfo)))
699 "dataurl" listinfo)))))))
701 (if (> (length urllist) 1)
702 (let ((completion-ignore-case t)
704 (mapcar (lambda (listinfo)
705 (cons (cdr (assoc "sitename" listinfo))
707 (cdr (assoc "feedid" listinfo)))))
711 "Multiple feeds found. Select one: "
712 selection nil t) urllist)))
714 (message "XML-RPC is not available... not checking Syndic8.")))
716 (defun nnrss-rss-p (data)
717 "Test if data is an RSS feed. Simply ensures that the first
718 element is rss or rdf."
719 (or (eq (caar data) 'rss)
720 (eq (caar data) 'rdf:RDF)))
722 (defun nnrss-rss-title-description (rss-namespace data url)
723 "Return the title of an RSS feed."
724 (if (nnrss-rss-p data)
725 (let ((description (intern (concat rss-namespace "description")))
726 (title (intern (concat rss-namespace "title")))
727 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
730 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
731 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
734 (defun nnrss-get-namespace-prefix (el uri)
735 "Given EL (containing a parsed element) and URI (containing a string
736 that gives the URI for which you want to retrieve the namespace
737 prefix), return the prefix."
738 (let* ((prefix (car (rassoc uri (cadar el))))
740 (split-string (symbol-name prefix) ":")))
741 (ns (cond ((eq (length nslist) 1) ; no prefix given
743 ((eq (length nslist) 2) ; extract prefix
745 (if (and ns (not (eq ns "")))
752 ;;; nnrss.el ends here