1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 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))
39 (require 'eword-encode)
45 (eval '(require 'xml))
47 ;; Reload mm-util emulating macros for compiling.
49 (let ((features (delq 'mm-util (copy-sequence features))))
50 (load "nnheader" nil t)))
54 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
55 "Where nnrss will save its files.")
57 ;; (group max rss-url)
58 (defvoo nnrss-server-data nil)
60 ;; (num timestamp url subject author date extra)
61 (defvoo nnrss-group-data nil)
62 (defvoo nnrss-group-max 0)
63 (defvoo nnrss-group-min 1)
64 (defvoo nnrss-group nil)
65 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
66 (defvoo nnrss-status-string "")
68 (defconst nnrss-version "nnrss 1.0")
70 (defvar nnrss-group-alist '()
71 "List of RSS addresses.")
73 (defvar nnrss-use-local nil)
75 (defvar nnrss-description-field 'X-Gnus-Description
76 "Field name used for DESCRIPTION.
77 To use the description in headers, put this name into `nnmail-extra-headers'.")
79 (defvar nnrss-url-field 'X-Gnus-Url
80 "Field name used for URL.
81 To use the description in headers, put this name into `nnmail-extra-headers'.")
83 (defvar nnrss-content-function nil
84 "A function which is called in `nnrss-request-article'.
85 The arguments are (ENTRY GROUP ARTICLE).
86 ENTRY is the record of the current headline. GROUP is the group name.
87 ARTICLE is the article number of the current headline.")
89 (defvar nnrss-file-coding-system nnheader-auto-save-coding-system
90 "Coding system used when reading and writing files.")
92 (nnoo-define-basics nnrss)
94 ;;; Interface functions
96 (defsubst nnrss-format-string (string)
97 (gnus-replace-in-string string " *\n *" " "))
99 (defun nnrss-decode-group-name (group)
100 (if (and group (mm-coding-system-p 'utf-8))
101 (setq group (mm-decode-coding-string group 'utf-8))
104 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
105 (setq group (nnrss-decode-group-name group))
106 (nnrss-possibly-change-group group server)
109 (set-buffer nntp-server-buffer)
111 (dolist (article articles)
112 (if (setq e (assq article nnrss-group-data))
113 (insert (number-to-string (car e)) "\t" ;; number
118 (or (nth 4 e) "(nobody)")
124 (format "<%d@%s.nnrss>" (car e) group)
135 (memq nnrss-description-field
136 nnmail-extra-headers))
137 (concat (symbol-name nnrss-description-field)
139 (nnrss-format-string (nth 6 e))
143 (memq nnrss-url-field
144 nnmail-extra-headers))
145 (concat (symbol-name nnrss-url-field)
147 (nnrss-format-string (nth 2 e))
153 (deffoo nnrss-request-group (group &optional server dont-check)
154 (nnheader-message 6 (concat "nnrss: Requesting " group "..."))
155 (setq group (nnrss-decode-group-name group))
156 (nnrss-possibly-change-group group server)
160 (nnrss-check-group group server)
161 (nnheader-report 'nnrss "Opened group %s" group)
163 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
164 (prin1-to-string group)
166 (nnheader-message 6 (concat "nnrss: Requesting " group "...done"))))
168 (deffoo nnrss-close-group (group &optional server)
171 (defun nnrss-body-presentation-method ()
172 "Return a body presentation method used with MIME-View.
173 The return value will be `html' or `text'."
174 (in-calist-package 'mime-view)
175 (let ((default (cdr (assq 'body-presentation-method
176 (car (ctree-find-calist
177 mime-preview-condition
178 '((type . text) (subtype . t)))))))
179 (html (cdr (assq 'body-presentation-method
180 (car (ctree-find-calist
181 mime-preview-condition
182 '((type . text) (subtype . html))))))))
183 (if (or (not default)
189 (deffoo nnrss-request-article (article &optional group server buffer)
190 (setq group (nnrss-decode-group-name group))
191 (when (stringp article)
192 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
193 (string-to-number (match-string 1 article))
195 (nnrss-possibly-change-group group server)
196 (let ((e (assq article nnrss-group-data))
197 (nntp-server-buffer (or buffer nntp-server-buffer))
201 (with-current-buffer nntp-server-buffer
204 (mm-with-unibyte-current-buffer
205 (insert "Newsgroups: "
206 (if (mm-coding-system-p 'utf-8)
207 (mm-encode-coding-string group 'utf-8)
211 (insert "Subject: " (nth 3 e) "\n"))
213 (insert "From: " (nth 4 e) "\n"))
215 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
216 (insert (format "Message-ID: <%d@%s.nnrss>\n"
218 (gnus-replace-in-string group "[\t\n ]+" "_")))
220 (let ((text (if (nth 6 e)
222 (delete "" (split-string (nth 6 e) "\n+"))
225 (mail-header-separator "")
226 mime-edit-insert-user-agent-field)
228 (if (eq 'html (nnrss-body-presentation-method))
230 (mime-edit-insert-text "html")
231 (insert "<html><head></head><body>\n")
235 (insert "<p><a href=\"" link "\">link</a></p>\n"))
236 (insert "</body></html>\n"))
237 (mime-edit-insert-text "plain")
242 (insert "\n" link "\n")))
244 (insert link "\n"))))
245 (mime-edit-translate-body)))
246 (when nnrss-content-function
247 (funcall nnrss-content-function e group article)))))
250 (nnheader-report 'nnrss err))
252 (nnheader-report 'nnrss "no such id: %d" article))
254 (nnheader-report 'nnrss "article %s retrieved" (car e))
255 ;; we return the article number.
256 (cons nnrss-group (car e))))))
258 (deffoo nnrss-request-list (&optional server)
259 (nnrss-possibly-change-group nil server)
260 (nnrss-generate-active)
263 (deffoo nnrss-open-server (server &optional defs connectionless)
264 (nnrss-read-server-data server)
265 (nnoo-change-server 'nnrss server defs)
268 (deffoo nnrss-request-expire-articles
269 (articles group &optional server force)
270 (setq group (nnrss-decode-group-name group))
271 (nnrss-possibly-change-group group server)
272 (let (e days not-expirable changed)
273 (dolist (art articles)
274 (if (and (setq e (assq art nnrss-group-data))
275 (nnmail-expired-article-p
277 (if (listp (setq days (nth 1 e))) days
278 (days-to-time (- days (time-to-days '(0 0)))))
280 (setq nnrss-group-data (delq e nnrss-group-data)
282 (push art not-expirable)))
284 (nnrss-save-group-data group server))
287 (deffoo nnrss-request-delete-group (group &optional force server)
288 (setq group (nnrss-decode-group-name group))
289 (nnrss-possibly-change-group group server)
291 ;; There may be two or more entries in `nnrss-group-alist' since
292 ;; this function didn't delete them formerly.
293 (while (setq elem (assoc group nnrss-group-alist))
294 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
295 (setq nnrss-server-data
296 (delq (assoc group nnrss-server-data) nnrss-server-data))
297 (nnrss-save-server-data server)
299 (delete-file (nnrss-make-filename group server)))
302 (deffoo nnrss-request-list-newsgroups (&optional server)
303 (nnrss-possibly-change-group nil server)
305 (set-buffer nntp-server-buffer)
307 (dolist (elem nnrss-group-alist)
309 (insert (car elem) "\t" (third elem) "\n"))))
312 (nnoo-define-skeleton nnrss)
314 ;;; Internal functions
315 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
317 (defun nnrss-get-encoding ()
318 "Return an encoding attribute specified in the current xml contents."
319 (goto-char (point-min))
321 (if (re-search-forward
322 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
324 (intern-soft (downcase (or (match-string-no-properties 1)
325 (match-string-no-properties 2))))
326 ;; The default encoding for xml.
329 (defun nnrss-fetch (url &optional local)
330 "Fetch URL and put it in a the expected Lisp structure."
331 (mm-with-unibyte-buffer
332 ;;some CVS versions of url.el need this to close the connection quickly
333 (let (cs xmlform htmlform)
334 ;; bit o' work necessary for w3 pre-cvs and post-cvs
336 (let ((coding-system-for-read 'binary))
337 (insert-file-contents url))
338 (let (;; FIXME: shouldn't binding `coding-system-for-read' be
339 ;; moved to `mm-url-insert'?
340 (coding-system-for-read 'binary)
341 ;; mm-url will load mm-util. d-e-m-c should be bound to
342 ;; t then, because of `mm-emacs-mule'.
343 (default-enable-multibyte-characters t))
344 (mm-url-insert url)))
345 (nnheader-remove-cr-followed-by-lf)
346 ;; Decode text according to the encoding attribute.
347 (when (setq cs (nnrss-get-encoding))
348 (mm-decode-coding-region (point-min) (point-max) cs)
349 (mm-enable-multibyte))
350 (goto-char (point-min))
352 ;; Because xml-parse-region can't deal with anything that isn't
353 ;; xml and w3-parse-buffer can't deal with some xml, we have to
354 ;; parse with xml-parse-region first and, if that fails, parse
355 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
356 ;; why w3-parse-buffer fails to parse some well-formed xml and
360 (setq xmlform (xml-parse-region (point-min) (point-max)))
363 (setq htmlform (caddar (w3-parse-buffer
367 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
373 (defun nnrss-possibly-change-group (&optional group server)
375 (not (nnrss-server-opened server)))
376 (nnrss-open-server server))
377 (when (and group (not (equal group nnrss-group)))
378 (nnrss-read-group-data group server)
379 (setq nnrss-group group)))
381 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
383 (defun nnrss-generate-active ()
384 (when (y-or-n-p "Fetch extra categories? ")
385 (mapc 'funcall nnrss-extra-categories))
387 (set-buffer nntp-server-buffer)
389 (dolist (elem nnrss-group-alist)
390 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
391 (dolist (elem nnrss-server-data)
392 (unless (assoc (car elem) nnrss-group-alist)
393 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
397 (defun nnrss-read-server-data (server)
398 (setq nnrss-server-data nil)
399 (let ((file (nnrss-make-filename "nnrss" server)))
400 (when (file-exists-p file)
401 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
402 ;; file names. So, we use `insert-file-contents' instead.
403 (mm-with-multibyte-buffer
404 (let ((coding-system-for-read nnrss-file-coding-system)
405 (file-name-coding-system nnmail-pathname-coding-system))
406 (insert-file-contents file)
407 (eval-region (point-min) (point-max)))))))
409 (defun nnrss-save-server-data (server)
410 (gnus-make-directory nnrss-directory)
411 (let ((coding-system-for-write nnrss-file-coding-system)
412 (file-name-coding-system nnmail-pathname-coding-system))
413 (with-temp-file (nnrss-make-filename "nnrss" server)
414 (insert (format ";; -*- coding: %s; -*-\n"
415 nnrss-file-coding-system))
416 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
418 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
420 (defun nnrss-read-group-data (group server)
421 (setq nnrss-group-data nil)
422 (if (hash-table-p nnrss-group-hashtb)
423 (clrhash nnrss-group-hashtb)
424 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
425 (let ((pair (assoc group nnrss-server-data)))
426 (setq nnrss-group-max (or (cadr pair) 0))
427 (setq nnrss-group-min (+ nnrss-group-max 1)))
428 (let ((file (nnrss-make-filename group server)))
429 (when (file-exists-p file)
430 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
431 ;; file names. So, we use `insert-file-contents' instead.
432 (mm-with-multibyte-buffer
433 (let ((coding-system-for-read nnrss-file-coding-system)
434 (file-name-coding-system nnmail-pathname-coding-system))
435 (insert-file-contents file)
436 (eval-region (point-min) (point-max))))
437 (dolist (e nnrss-group-data)
438 (puthash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
439 (when (and (car e) (> nnrss-group-min (car e)))
440 (setq nnrss-group-min (car e)))
441 (when (and (car e) (< nnrss-group-max (car e)))
442 (setq nnrss-group-max (car e)))))))
444 (defun nnrss-save-group-data (group server)
445 (gnus-make-directory nnrss-directory)
446 (let ((coding-system-for-write nnrss-file-coding-system)
447 (file-name-coding-system nnmail-pathname-coding-system))
448 (with-temp-file (nnrss-make-filename group server)
449 (insert (format ";; -*- coding: %s; -*-\n"
450 nnrss-file-coding-system))
451 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
453 (defun nnrss-make-filename (name server)
455 (nnrss-translate-file-chars
458 (not (equal server ""))
464 (gnus-add-shutdown 'nnrss-close 'gnus)
466 (defun nnrss-close ()
467 "Clear internal nnrss variables."
468 (setq nnrss-group-data nil
469 nnrss-server-data nil
470 nnrss-group-hashtb nil
471 nnrss-group-alist nil))
475 (defun nnrss-no-cache (url)
478 (defun nnrss-insert-w3 (url)
479 (mm-with-unibyte-current-buffer
480 (mm-url-insert url)))
482 (defun nnrss-decode-entities-string (string)
484 (mm-with-multibyte-buffer
486 (mm-url-decode-entities-nbsp)
489 (defalias 'nnrss-insert 'nnrss-insert-w3)
491 (defun nnrss-mime-encode-string (string)
492 (mm-with-multibyte-buffer
494 (mm-url-decode-entities-nbsp)
495 (goto-char (point-min))
496 (while (re-search-forward "[\t\n ]+" nil t)
498 (goto-char (point-min))
499 (skip-chars-forward " ")
500 (delete-region (point-min) (point))
501 (goto-char (point-max))
502 (skip-chars-forward " ")
503 (delete-region (point) (point-max))
504 (eword-encode-string (buffer-string) (eval '(- -1 (lsh -1 -1))))))
508 (defun nnrss-check-group (group server)
509 (let (file xml subject url extra changed author
510 date rss-ns rdf-ns content-ns dc-ns)
511 (if (and nnrss-use-local
512 (file-exists-p (setq file (expand-file-name
513 (nnrss-translate-file-chars
514 (concat group ".xml"))
516 (setq xml (nnrss-fetch file t))
517 (setq url (or (nth 2 (assoc group nnrss-server-data))
518 (second (assoc group nnrss-group-alist))))
525 (format "URL to search for %s: " group) "http://")))))
526 (let ((pair (assoc group nnrss-server-data)))
528 (setcdr (cdr pair) (list url))
529 (push (list group nnrss-group-max url) nnrss-server-data)))
531 (setq xml (nnrss-fetch url)))
533 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
534 ;; for more RSS namespaces.
535 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
536 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
537 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
538 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
539 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
540 (when (and (listp item)
541 (string= (concat rss-ns "item") (car item))
542 (if (setq url (nnrss-decode-entities-string
543 (nnrss-node-text rss-ns 'link (cddr item))))
544 (not (gethash url nnrss-group-hashtb))
545 (setq extra (or (nnrss-node-text content-ns 'encoded item)
546 (nnrss-node-text rss-ns 'description item)))
547 (not (gethash extra nnrss-group-hashtb))))
548 (setq subject (nnrss-node-text rss-ns 'title item))
549 (setq extra (or extra
550 (nnrss-node-text content-ns 'encoded item)
551 (nnrss-node-text rss-ns 'description item)))
552 (setq author (or (nnrss-node-text rss-ns 'author item)
553 (nnrss-node-text dc-ns 'creator item)
554 (nnrss-node-text dc-ns 'contributor item)))
555 (setq date (or (nnrss-node-text dc-ns 'date item)
556 (nnrss-node-text rss-ns 'pubDate item)
557 (message-make-date)))
560 (incf nnrss-group-max)
563 (and subject (nnrss-mime-encode-string subject))
564 (and author (nnrss-mime-encode-string author))
566 (and extra (nnrss-decode-entities-string extra)))
568 (puthash (or url extra) t nnrss-group-hashtb)
572 (nnrss-save-group-data group server)
573 (let ((pair (assoc group nnrss-server-data)))
575 (setcar (cdr pair) nnrss-group-max)
576 (push (list group nnrss-group-max) nnrss-server-data)))
577 (nnrss-save-server-data server))))
579 (defun nnrss-opml-import (opml-file)
580 "OPML subscriptions import.
581 Read the file and attempt to subscribe to each Feed in the file."
582 (interactive "fImport file: ")
584 (lambda (node) (gnus-group-make-rss-group
585 (cdr (assq 'xmlUrl (cadr node)))))
586 (nnrss-find-el 'outline
588 (find-file opml-file)
589 (xml-parse-region (point-min)
592 (defun nnrss-opml-export ()
593 "OPML subscription export.
594 Export subscriptions to a buffer in OPML Format."
596 (with-current-buffer (get-buffer-create "*OPML Export*")
597 (mm-set-buffer-file-coding-system 'utf-8)
599 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
600 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
601 "<opml version=\"1.1\">\n"
603 " <title>mySubscriptions</title>\n"
604 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
606 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
607 " <ownerName>" (user-full-name) "</ownerName>\n"
612 " <outline text=\"" (car sub) "\" xmlUrl=\""
613 (cadr sub) "\"/>\n")))
618 (pop-to-buffer "*OPML Export*")
619 (when (fboundp 'sgml-mode)
622 (defun nnrss-generate-download-script ()
623 "Generate a download script in the current buffer.
624 It is useful when `(setq nnrss-use-local t)'."
626 (insert "#!/bin/sh\n")
627 (insert "WGET=wget\n")
628 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
629 (dolist (elem nnrss-server-data)
630 (let ((url (or (nth 2 elem)
631 (second (assoc (car elem) nnrss-group-alist)))))
632 (insert "$WGET -q -O \"$RSSDIR\"/'"
633 (nnrss-translate-file-chars (concat (car elem) ".xml"))
636 (defun nnrss-translate-file-chars (name)
637 (let ((nnheader-file-name-translation-alist
638 (append nnheader-file-name-translation-alist '((?' . ?_)))))
639 (nnheader-translate-file-chars name)))
641 (defvar nnrss-moreover-url
642 "http://w.moreover.com/categories/category_list_rss.html"
643 "The url of moreover.com categories.")
645 (defun nnrss-snarf-moreover-categories ()
646 "Snarf RSS links from moreover.com."
648 (let (category name url changed)
650 (nnrss-insert nnrss-moreover-url)
651 (goto-char (point-min))
652 (while (re-search-forward
653 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
655 (setq category (match-string 1))
656 (setq url (match-string 2)
657 name (mm-url-decode-entities-string
658 (cadr (mime-decode-parameters
659 (list "c*" (match-string 3))))))
661 (setq name (concat category "." name)))
662 (unless (assoc name nnrss-server-data)
664 (push (list name 0 url) nnrss-server-data)))))
666 (nnrss-save-server-data ""))))
668 (defun nnrss-node-text (namespace local-name element)
669 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
671 (text (if (and node (listp node))
672 (nnrss-node-just-text node)
674 (cleaned-text (if text (gnus-replace-in-string
675 text "^[\000-\037\177]+\\|^ +\\| +$" ""))))
676 (if (string-equal "" cleaned-text)
680 (defun nnrss-node-just-text (node)
681 (if (and node (listp node))
682 (mapconcat 'nnrss-node-just-text (cddr node) " ")
685 (defun nnrss-find-el (tag data &optional found-list)
686 "Find the all matching elements in the data.
687 Careful with this on large documents!"
691 (when (equal tag (car bit))
692 ;; Old xml.el may return a list of string.
693 (when (and (consp (caddr bit))
694 (stringp (caaddr bit)))
695 (setcar (cddr bit) (caaddr bit)))
699 (if (and (listp (car-safe (caddr bit)))
700 (not (stringp (caddr bit))))
712 (defun nnrss-rsslink-p (el)
713 "Test if the element we are handed is an RSS autodiscovery link."
714 (and (eq (car-safe el) 'link)
715 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
716 (or (string-equal (cdr (assoc 'type (cadr el)))
717 "application/rss+xml")
718 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
720 (defun nnrss-get-rsslinks (data)
721 "Extract the <link> elements that are links to RSS from the parsed data."
724 (if (nnrss-rsslink-p el) el))
725 (nnrss-find-el 'link data))))
727 (defun nnrss-extract-hrefs (data)
728 "Recursively extract hrefs from a page's source.
729 DATA should be the output of `xml-parse-region' or
731 (mapcar (lambda (ahref)
732 (cdr (assoc 'href (cadr ahref))))
733 (nnrss-find-el 'a data)))
735 (defmacro nnrss-match-macro (base-uri item
736 onsite-list offsite-list)
737 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
738 (not (string-match "://" ,item)))
739 (setq ,onsite-list (append ,onsite-list (list ,item))))
740 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
742 (defun nnrss-order-hrefs (base-uri hrefs)
743 "Given a list of hrefs, sort them using the following priorities:
744 1. links ending in .rss
745 2. links ending in .rdf
746 3. links ending in .xml
747 4. links containing the above
750 BASE-URI is used to determine the location of the links and
751 whether they are `offsite' or `onsite'."
752 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
753 rss-onsite-in rdf-onsite-in xml-onsite-in
754 rss-offsite-end rdf-offsite-end xml-offsite-end
755 rss-offsite-in rdf-offsite-in xml-offsite-in)
757 (if (not (null href))
758 (cond ((string-match "\\.rss$" href)
760 base-uri href rss-onsite-end rss-offsite-end))
761 ((string-match "\\.rdf$" href)
763 base-uri href rdf-onsite-end rdf-offsite-end))
764 ((string-match "\\.xml$" href)
766 base-uri href xml-onsite-end xml-offsite-end))
767 ((string-match "rss" href)
769 base-uri href rss-onsite-in rss-offsite-in))
770 ((string-match "rdf" href)
772 base-uri href rdf-onsite-in rdf-offsite-in))
773 ((string-match "xml" href)
775 base-uri href xml-onsite-in xml-offsite-in)))))
778 rss-onsite-end rdf-onsite-end xml-onsite-end
779 rss-onsite-in rdf-onsite-in xml-onsite-in
780 rss-offsite-end rdf-offsite-end xml-offsite-end
781 rss-offsite-in rdf-offsite-in xml-offsite-in)))
783 (defun nnrss-discover-feed (url)
784 "Given a page, find an RSS feed using Mark Pilgrim's
785 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
787 (let ((parsed-page (nnrss-fetch url)))
789 ;; 1. if this url is the rss, use it.
790 (if (nnrss-rss-p parsed-page)
791 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
792 (nnrss-rss-title-description rss-ns parsed-page url))
794 ;; 2. look for the <link rel="alternate"
795 ;; type="application/rss+xml" and use that if it is there.
796 (let ((links (nnrss-get-rsslinks parsed-page)))
798 (let* ((xml (nnrss-fetch
799 (cdr (assoc 'href (cadar links)))))
800 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
801 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
803 ;; 3. look for links on the site in the following order:
804 ;; - onsite links ending in .rss, .rdf, or .xml
805 ;; - onsite links containing any of the above
806 ;; - offsite links ending in .rss, .rdf, or .xml
807 ;; - offsite links containing any of the above
808 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
809 (match-string 0 url)))
810 (hrefs (nnrss-order-hrefs
811 base-uri (nnrss-extract-hrefs parsed-page)))
813 (while (and (eq rss-link nil) (not (eq hrefs nil)))
814 (let ((href-data (nnrss-fetch (car hrefs))))
815 (if (nnrss-rss-p href-data)
816 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
817 (setq rss-link (nnrss-rss-title-description
818 rss-ns href-data (car hrefs))))
819 (setq hrefs (cdr hrefs)))))
820 (if rss-link rss-link
823 (nnrss-find-rss-via-syndic8 url))))))))
825 (defun nnrss-find-rss-via-syndic8 (url)
826 "Query syndic8 for the rss feeds it has for URL."
827 (if (not (locate-library "xml-rpc"))
829 (message "XML-RPC is not available... not checking Syndic8.")
832 (let ((feedid (xml-rpc-method-call
833 "http://www.syndic8.com/xmlrpc.php"
837 (let* ((feedinfo (xml-rpc-method-call
838 "http://www.syndic8.com/xmlrpc.php"
846 (cdr (assoc "status" listinfo))
849 (cdr (assoc "sitename" listinfo))
853 "sitename" listinfo)))
856 "dataurl" listinfo)))))))
858 (if (not (> (length urllist) 1))
860 (let ((completion-ignore-case t)
862 (mapcar (lambda (listinfo)
863 (cons (cdr (assoc "sitename" listinfo))
865 (cdr (assoc "feedid" listinfo)))))
869 "Multiple feeds found. Select one: "
870 selection nil t) urllist)))))))))
872 (defun nnrss-rss-p (data)
873 "Test if DATA is an RSS feed.
874 Simply ensures that the first element is rss or rdf."
875 (or (eq (caar data) 'rss)
876 (eq (caar data) 'rdf:RDF)))
878 (defun nnrss-rss-title-description (rss-namespace data url)
879 "Return the title of an RSS feed."
880 (if (nnrss-rss-p data)
881 (let ((description (intern (concat rss-namespace "description")))
882 (title (intern (concat rss-namespace "title")))
883 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
886 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
887 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
890 (defun nnrss-get-namespace-prefix (el uri)
891 "Given EL (containing a parsed element) and URI (containing a string
892 that gives the URI for which you want to retrieve the namespace
893 prefix), return the prefix."
894 (let* ((prefix (car (rassoc uri (cadar el))))
896 (split-string (symbol-name prefix) ":")))
897 (ns (cond ((eq (length nslist) 1) ; no prefix given
899 ((eq (length nslist) 2) ; extract prefix
901 (if (and ns (not (string= ns "")))
908 ;;; nnrss.el ends here