1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
46 (eval '(require 'xml))
50 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
51 "Where nnrss will save its files.")
53 ;; (group max rss-url)
54 (defvoo nnrss-server-data nil)
56 ;; (num timestamp url subject author date extra)
57 (defvoo nnrss-group-data nil)
58 (defvoo nnrss-group-max 0)
59 (defvoo nnrss-group-min 1)
60 (defvoo nnrss-group nil)
61 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
62 (defvoo nnrss-status-string "")
64 (defconst nnrss-version "nnrss 1.0")
66 (defvar nnrss-group-alist '()
67 "List of RSS addresses.")
69 (defvar nnrss-use-local nil)
71 (defvar nnrss-description-field 'X-Gnus-Description
72 "Field name used for DESCRIPTION.
73 To use the description in headers, put this name into `nnmail-extra-headers'.")
75 (defvar nnrss-url-field 'X-Gnus-Url
76 "Field name used for URL.
77 To use the description in headers, put this name into `nnmail-extra-headers'.")
79 (defvar nnrss-content-function nil
80 "A function which is called in `nnrss-request-article'.
81 The arguments are (ENTRY GROUP ARTICLE).
82 ENTRY is the record of the current headline. GROUP is the group name.
83 ARTICLE is the article number of the current headline.")
85 (defvar nnrss-file-coding-system mm-universal-coding-system
86 "Coding system used when reading and writing files.")
88 (defvar nnrss-compatible-encoding-alist '((iso-8859-1 . windows-1252))
89 "Alist of encodings and those supersets.
90 The cdr of each element is used to decode data if it is available when
91 the car is what the data specify as the encoding. Or, the car is used
92 for decoding when the cdr that the data specify is not available.")
94 (defvar nnrss-wash-html-in-text-plain-parts nil
95 "*Non-nil means render text in text/plain parts as HTML.
96 The function specified by the `mm-text-html-renderer' variable will be
97 used to render text. If it is nil, text will simply be folded.")
99 (nnoo-define-basics nnrss)
101 ;;; Interface functions
103 (defsubst nnrss-format-string (string)
104 (gnus-replace-in-string string " *\n *" " "))
106 (defun nnrss-decode-group-name (group)
107 (if (and group (mm-coding-system-p 'utf-8))
108 (setq group (mm-decode-coding-string group 'utf-8))
111 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
112 (setq group (nnrss-decode-group-name group))
113 (nnrss-possibly-change-group group server)
116 (set-buffer nntp-server-buffer)
118 (dolist (article articles)
119 (if (setq e (assq article nnrss-group-data))
120 (insert (number-to-string (car e)) "\t" ;; number
125 (or (nth 4 e) "(nobody)")
131 (format "<%d@%s.nnrss>" (car e) group)
142 (memq nnrss-description-field
143 nnmail-extra-headers))
144 (concat (symbol-name nnrss-description-field)
146 (nnrss-format-string (nth 6 e))
150 (memq nnrss-url-field
151 nnmail-extra-headers))
152 (concat (symbol-name nnrss-url-field)
154 (nnrss-format-string (nth 2 e))
160 (deffoo nnrss-request-group (group &optional server dont-check)
161 (setq group (nnrss-decode-group-name group))
162 (nnheader-message 6 "nnrss: Requesting %s..." group)
163 (nnrss-possibly-change-group group server)
167 (nnrss-check-group group server)
168 (nnheader-report 'nnrss "Opened group %s" group)
170 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
171 (prin1-to-string group)
173 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
175 (deffoo nnrss-close-group (group &optional server)
179 (defvar mm-text-html-renderer)
180 (defvar mm-text-html-washer-alist))
182 (deffoo nnrss-request-article (article &optional group server buffer)
183 (setq group (nnrss-decode-group-name group))
184 (when (stringp article)
185 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
186 (string-to-number (match-string 1 article))
188 (nnrss-possibly-change-group group server)
189 (let ((e (assq article nnrss-group-data))
190 (nntp-server-buffer (or buffer nntp-server-buffer))
193 (with-current-buffer nntp-server-buffer
196 (insert "Newsgroups: " group "\n"))
198 (insert "Subject: " (nth 3 e) "\n"))
200 (insert "From: " (nth 4 e) "\n"))
202 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
203 (let ((header (buffer-string))
206 (enclosure (nth 7 e))
208 ;; Enable encoding of Newsgroups header in XEmacs.
209 (default-enable-multibyte-characters t)
210 (rfc2047-header-encoding-alist
211 (if (mm-coding-system-p 'utf-8)
212 (cons '("Newsgroups" . utf-8)
213 rfc2047-header-encoding-alist)
214 rfc2047-header-encoding-alist))
215 rfc2047-encode-encoded-words body fn)
216 (when (or text link enclosure comments)
218 (insert "<#multipart type=alternative>\n"
219 "<#part type=\"text/plain\">\n")
224 (if (and nnrss-wash-html-in-text-plain-parts
227 (setq fn (or (cdr (assq mm-text-html-renderer
228 mm-text-html-washer-alist))
229 mm-text-html-renderer))))
231 (narrow-to-region body (point-max))
234 (apply (car fn) (cdr fn)))
237 (re-search-forward "[^\t\n ]" nil t)
239 (delete-region body (point))
240 (goto-char (point-max))
241 (skip-chars-backward "\t\n ")
243 (delete-region (point) (point-max))
245 (while (re-search-forward "\n+" nil t)
248 ;; See `nnrss-check-group', which inserts "<br /><br />".
249 (when (search-forward "<br /><br />" nil t)
252 (replace-match "\n\n")))
254 (let ((fill-column default-fill-column)
255 (window (get-buffer-window nntp-server-buffer)))
258 (max 1 (/ (* (window-width window) 7) 8))))
259 (fill-region (point) (point-max))
260 (goto-char (point-max))
261 ;; XEmacs version of `fill-region' inserts newline.
264 (when (or link enclosure)
269 (insert (car enclosure) " "
270 (nth 2 enclosure) " "
271 (nth 3 enclosure) "\n"))
273 (insert comments "\n"))
274 (setq body (buffer-substring body (point)))
276 "<#part type=\"text/html\">\n"
277 "<html><head></head><body>\n")
281 (insert "<p><a href=\"" link "\">link</a></p>\n"))
283 (insert "<p><a href=\"" (car enclosure) "\">"
284 (cadr enclosure) "</a> " (nth 2 enclosure)
285 " " (nth 3 enclosure) "</p>\n"))
287 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
288 (insert "</body></html>\n"
296 "Content-Type: text/plain; charset=gnus-decoded\n"
297 "Content-Transfer-Encoding: 8bit\n\n"
300 3 "Warning - there might be invalid characters"))))
301 (goto-char (point-min))
302 (search-forward "\n\n")
304 (insert (format "Message-ID: <%d@%s.nnrss>\n"
306 (let ((rfc2047-encoding-type 'mime)
307 rfc2047-encode-max-chars)
308 (rfc2047-encode-string
309 (gnus-replace-in-string group "[\t\n ]+" "_")))))
310 (when nnrss-content-function
311 (funcall nnrss-content-function e group article))))
314 (nnheader-report 'nnrss err))
316 (nnheader-report 'nnrss "no such id: %d" article))
318 (nnheader-report 'nnrss "article %s retrieved" (car e))
319 ;; we return the article number.
320 (cons nnrss-group (car e))))))
322 (deffoo nnrss-request-list (&optional server)
323 (nnrss-possibly-change-group nil server)
324 (nnrss-generate-active)
327 (deffoo nnrss-open-server (server &optional defs connectionless)
328 (nnrss-read-server-data server)
329 (nnoo-change-server 'nnrss server defs)
332 (deffoo nnrss-request-expire-articles
333 (articles group &optional server force)
334 (setq group (nnrss-decode-group-name group))
335 (nnrss-possibly-change-group group server)
336 (let (e days not-expirable changed)
337 (dolist (art articles)
338 (if (and (setq e (assq art nnrss-group-data))
339 (nnmail-expired-article-p
341 (if (listp (setq days (nth 1 e))) days
342 (days-to-time (- days (time-to-days '(0 0)))))
344 (setq nnrss-group-data (delq e nnrss-group-data)
346 (push art not-expirable)))
348 (nnrss-save-group-data group server))
351 (deffoo nnrss-request-delete-group (group &optional force server)
352 (setq group (nnrss-decode-group-name group))
353 (nnrss-possibly-change-group group server)
355 ;; There may be two or more entries in `nnrss-group-alist' since
356 ;; this function didn't delete them formerly.
357 (while (setq elem (assoc group nnrss-group-alist))
358 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
359 (setq nnrss-server-data
360 (delq (assoc group nnrss-server-data) nnrss-server-data))
361 (nnrss-save-server-data server)
363 (delete-file (nnrss-make-filename group server)))
366 (deffoo nnrss-request-list-newsgroups (&optional server)
367 (nnrss-possibly-change-group nil server)
369 (set-buffer nntp-server-buffer)
371 (dolist (elem nnrss-group-alist)
373 (insert (car elem) "\t" (third elem) "\n"))))
376 (nnoo-define-skeleton nnrss)
378 ;;; Internal functions
379 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
381 (defun nnrss-get-encoding ()
382 "Return an encoding attribute specified in the current xml contents.
383 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
384 it is used instead. If the xml contents doesn't specify the encoding,
385 return `utf-8' which is the default encoding for xml if it is available,
386 otherwise return nil."
387 (goto-char (point-min))
388 (if (re-search-forward
389 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
391 (let ((encoding (intern (downcase (or (match-string 1)
392 (match-string 2))))))
394 (mm-coding-system-p (cdr (assq encoding
395 nnrss-compatible-encoding-alist)))
396 (mm-coding-system-p encoding)
397 (mm-coding-system-p (car (rassq encoding
398 nnrss-compatible-encoding-alist)))))
399 (mm-coding-system-p 'utf-8)))
401 (defun nnrss-fetch (url &optional local)
402 "Fetch URL and put it in a the expected Lisp structure."
403 (mm-with-unibyte-buffer
404 ;;some CVS versions of url.el need this to close the connection quickly
405 (let (cs xmlform htmlform)
406 ;; bit o' work necessary for w3 pre-cvs and post-cvs
408 (let ((coding-system-for-read 'binary))
409 (insert-file-contents url))
410 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
411 ;; to `mm-url-insert'?
412 (let ((coding-system-for-read 'binary))
415 (error (if (or debug-on-quit debug-on-error)
416 (signal (car err) (cdr err))
417 (message "nnrss: Failed to fetch %s" url))))))
418 (nnheader-remove-cr-followed-by-lf)
419 ;; Decode text according to the encoding attribute.
420 (when (setq cs (nnrss-get-encoding))
421 (mm-decode-coding-region (point-min) (point-max) cs)
422 (mm-enable-multibyte))
423 (goto-char (point-min))
425 ;; Because xml-parse-region can't deal with anything that isn't
426 ;; xml and w3-parse-buffer can't deal with some xml, we have to
427 ;; parse with xml-parse-region first and, if that fails, parse
428 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
429 ;; why w3-parse-buffer fails to parse some well-formed xml and
433 (setq xmlform (xml-parse-region (point-min) (point-max)))
436 (setq htmlform (caddar (w3-parse-buffer
440 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
446 (defun nnrss-possibly-change-group (&optional group server)
448 (not (nnrss-server-opened server)))
449 (nnrss-open-server server))
450 (when (and group (not (equal group nnrss-group)))
451 (nnrss-read-group-data group server)
452 (setq nnrss-group group)))
454 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
456 (defun nnrss-generate-active ()
457 (when (y-or-n-p "Fetch extra categories? ")
458 (mapc 'funcall nnrss-extra-categories))
460 (set-buffer nntp-server-buffer)
462 (dolist (elem nnrss-group-alist)
463 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
464 (dolist (elem nnrss-server-data)
465 (unless (assoc (car elem) nnrss-group-alist)
466 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
468 (eval-and-compile (autoload 'timezone-parse-date "timezone"))
470 (defun nnrss-normalize-date (date)
471 "Return a date string of DATE in the RFC822 style.
472 This function handles the ISO 8601 date format described in
473 <URL:http://www.w3.org/TR/NOTE-datetime>, and also the RFC822 style
474 which RSS 2.0 allows."
475 (let (case-fold-search vector year month day time zone cts)
478 ((string-match " [0-9]+ " date)
479 (setq vector (timezone-parse-date date)
480 year (string-to-number (aref vector 0)))
482 (setq month (string-to-number (aref vector 1))
483 day (string-to-number (aref vector 2)))
484 (unless (>= (length (setq time (aref vector 3))) 3)
485 (setq time "00:00:00"))
486 (when (and (setq zone (aref vector 4))
487 (not (string-match "\\`[A-Z+-]" zone)))
494 "\\(199[0-9]\\|20[0-9][0-9]\\)"
503 "\\([012][0-9]:[0-5][0-9]\\)"
507 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
509 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
510 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
513 (setq year (string-to-number (match-string 1 date))
514 month (string-to-number (or (match-string 2 date) "1"))
515 day (string-to-number (or (match-string 3 date) "1"))
516 time (if (match-beginning 5)
517 (substring date (match-beginning 4) (match-end 5))
518 (concat (or (match-string 4 date) "00:00") ":00"))
519 zone (cond ((match-beginning 6)
520 (concat (match-string 6 date)
521 (match-string 7 date)))
522 ((match-beginning 9) ;; Z
524 (t ;; nil if zone is not provided.
525 (match-string 8 date))))))
528 (setq cts (current-time-string (encode-time 0 0 0 day month year)))
529 (format "%s, %02d %s %04d %s%s"
530 (substring cts 0 3) day (substring cts 4 7) year time
534 (message-make-date))))
538 (defun nnrss-read-server-data (server)
539 (setq nnrss-server-data nil)
540 (let ((file (nnrss-make-filename "nnrss" server)))
541 (when (file-exists-p file)
542 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
543 ;; file names. So, we use `insert-file-contents' instead.
544 (mm-with-multibyte-buffer
545 (let ((coding-system-for-read nnrss-file-coding-system)
546 (file-name-coding-system nnmail-pathname-coding-system))
547 (insert-file-contents file)
548 (eval-region (point-min) (point-max)))))))
550 (defun nnrss-save-server-data (server)
551 (gnus-make-directory nnrss-directory)
552 (let ((coding-system-for-write nnrss-file-coding-system)
553 (file-name-coding-system nnmail-pathname-coding-system))
554 (with-temp-file (nnrss-make-filename "nnrss" server)
555 (insert (format ";; -*- coding: %s; -*-\n"
556 nnrss-file-coding-system))
557 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
559 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
561 (defun nnrss-read-group-data (group server)
562 (setq nnrss-group-data nil)
563 (if (hash-table-p nnrss-group-hashtb)
564 (clrhash nnrss-group-hashtb)
565 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
566 (let ((pair (assoc group nnrss-server-data)))
567 (setq nnrss-group-max (or (cadr pair) 0))
568 (setq nnrss-group-min (+ nnrss-group-max 1)))
569 (let ((file (nnrss-make-filename group server)))
570 (when (file-exists-p file)
571 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
572 ;; file names. So, we use `insert-file-contents' instead.
573 (mm-with-multibyte-buffer
574 (let ((coding-system-for-read nnrss-file-coding-system)
575 (file-name-coding-system nnmail-pathname-coding-system))
576 (insert-file-contents file)
577 (eval-region (point-min) (point-max))))
578 (dolist (e nnrss-group-data)
579 (puthash (nth 9 e) t nnrss-group-hashtb)
580 (when (and (car e) (> nnrss-group-min (car e)))
581 (setq nnrss-group-min (car e)))
582 (when (and (car e) (< nnrss-group-max (car e)))
583 (setq nnrss-group-max (car e)))))))
585 (defun nnrss-save-group-data (group server)
586 (gnus-make-directory nnrss-directory)
587 (let ((coding-system-for-write nnrss-file-coding-system)
588 (file-name-coding-system nnmail-pathname-coding-system))
589 (with-temp-file (nnrss-make-filename group server)
590 (insert (format ";; -*- coding: %s; -*-\n"
591 nnrss-file-coding-system))
592 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
594 (defun nnrss-make-filename (name server)
596 (nnrss-translate-file-chars
599 (not (equal server ""))
605 (gnus-add-shutdown 'nnrss-close 'gnus)
607 (defun nnrss-close ()
608 "Clear internal nnrss variables."
609 (setq nnrss-group-data nil
610 nnrss-server-data nil
611 nnrss-group-hashtb nil
612 nnrss-group-alist nil))
616 (defun nnrss-no-cache (url)
619 (defun nnrss-insert-w3 (url)
620 (mm-with-unibyte-current-buffer
623 (error (if (or debug-on-quit debug-on-error)
624 (signal (car err) (cdr err))
625 (message "nnrss: Failed to fetch %s" url))))))
627 (defun nnrss-decode-entities-string (string)
629 (mm-with-multibyte-buffer
631 (mm-url-decode-entities-nbsp)
634 (defalias 'nnrss-insert 'nnrss-insert-w3)
636 (defun nnrss-mime-encode-string (string)
637 (mm-with-multibyte-buffer
639 (mm-url-decode-entities-nbsp)
640 (goto-char (point-min))
641 (while (re-search-forward "[\t\n ]+" nil t)
643 (goto-char (point-min))
644 (skip-chars-forward " ")
645 (delete-region (point-min) (point))
646 (goto-char (point-max))
647 (skip-chars-forward " ")
648 (delete-region (point) (point-max))
649 (let ((rfc2047-encoding-type 'mime)
650 rfc2047-encode-max-chars)
651 (rfc2047-encode-region (point-min) (point-max)))
652 (goto-char (point-min))
653 (while (search-forward "\n" nil t)
654 (delete-backward-char 1))
659 (defun nnrss-check-group (group server)
660 (let (file xml subject url extra changed author date feed-subject
661 enclosure comments rss-ns rdf-ns content-ns dc-ns
663 (if (and nnrss-use-local
664 (file-exists-p (setq file (expand-file-name
665 (nnrss-translate-file-chars
666 (concat group ".xml"))
668 (setq xml (nnrss-fetch file t))
669 (setq url (or (nth 2 (assoc group nnrss-server-data))
670 (second (assoc group nnrss-group-alist))))
677 (format "URL to search for %s: " group) "http://")))))
678 (let ((pair (assoc group nnrss-server-data)))
680 (setcdr (cdr pair) (list url))
681 (push (list group nnrss-group-max url) nnrss-server-data)))
683 (setq xml (nnrss-fetch url)))
685 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
686 ;; for more RSS namespaces.
687 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
688 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
689 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
690 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
691 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
692 (when (and (listp item)
693 (string= (concat rss-ns "item") (car item))
694 (progn (setq hash-index (md5 (gnus-prin1-to-string item)))
695 (not (gethash hash-index nnrss-group-hashtb))))
696 (setq subject (nnrss-node-text rss-ns 'title item))
697 (setq url (nnrss-decode-entities-string
698 (nnrss-node-text rss-ns 'link (cddr item))))
699 (setq extra (or (nnrss-node-text content-ns 'encoded item)
700 (nnrss-node-text rss-ns 'description item)))
701 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
702 (setq extra (concat feed-subject "<br /><br />" extra)))
703 (setq author (or (nnrss-node-text rss-ns 'author item)
704 (nnrss-node-text dc-ns 'creator item)
705 (nnrss-node-text dc-ns 'contributor item)))
706 (setq date (nnrss-normalize-date
707 (or (nnrss-node-text dc-ns 'date item)
708 (nnrss-node-text rss-ns 'pubDate item))))
709 (setq comments (nnrss-node-text rss-ns 'comments item))
710 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
711 (let ((url (cdr (assq 'url enclosure)))
712 (len (cdr (assq 'length enclosure)))
713 (type (cdr (assq 'type enclosure)))
716 (if (and len (integerp (setq len (string-to-number len))))
717 ;; actually already in `ls-lisp-format-file-size' but
718 ;; probably not worth to require it for one function
719 (do ((size (/ len 1.0) (/ size 1024.0))
720 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
723 (format "%.1f%s" size (car post-fixes))))
725 (setq url (or url ""))
726 (setq name (if (string-match "/\\([^/]*\\)$" url)
729 (setq type (or type ""))
730 (setq enclosure (list url name len type))))
733 (incf nnrss-group-max)
736 (and subject (nnrss-mime-encode-string subject))
737 (and author (nnrss-mime-encode-string author))
739 (and extra (nnrss-decode-entities-string extra))
744 (puthash hash-index t nnrss-group-hashtb)
748 (nnrss-save-group-data group server)
749 (let ((pair (assoc group nnrss-server-data)))
751 (setcar (cdr pair) nnrss-group-max)
752 (push (list group nnrss-group-max) nnrss-server-data)))
753 (nnrss-save-server-data server))))
755 (defun nnrss-opml-import (opml-file)
756 "OPML subscriptions import.
757 Read the file and attempt to subscribe to each Feed in the file."
758 (interactive "fImport file: ")
761 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
763 (not (string-match "\\`[\t ]*\\'" xmlurl))
765 (y-or-n-p (format "Subscribe to %s " xmlurl))
769 (gnus-group-make-rss-group xmlurl)
773 "Failed to subscribe to %s (%s); type any key to continue: "
775 (error-message-string err))
776 (let ((echo-keystrokes 0))
778 (nnrss-find-el 'outline
779 (mm-with-multibyte-buffer
780 (insert-file-contents opml-file)
781 (xml-parse-region (point-min) (point-max))))))
783 (defun nnrss-opml-export ()
784 "OPML subscription export.
785 Export subscriptions to a buffer in OPML Format."
787 (with-current-buffer (get-buffer-create "*OPML Export*")
788 (mm-set-buffer-file-coding-system 'utf-8)
789 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
790 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
791 "<opml version=\"1.1\">\n"
793 " <title>mySubscriptions</title>\n"
794 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
796 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
797 " <ownerName>" (user-full-name) "</ownerName>\n"
800 (dolist (sub nnrss-group-alist)
801 (insert " <outline text=\"" (car sub)
802 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
805 (pop-to-buffer "*OPML Export*")
806 (when (fboundp 'sgml-mode)
809 (defun nnrss-generate-download-script ()
810 "Generate a download script in the current buffer.
811 It is useful when `(setq nnrss-use-local t)'."
813 (insert "#!/bin/sh\n")
814 (insert "WGET=wget\n")
815 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
816 (dolist (elem nnrss-server-data)
817 (let ((url (or (nth 2 elem)
818 (second (assoc (car elem) nnrss-group-alist)))))
819 (insert "$WGET -q -O \"$RSSDIR\"/'"
820 (nnrss-translate-file-chars (concat (car elem) ".xml"))
823 (defun nnrss-translate-file-chars (name)
824 (let ((nnheader-file-name-translation-alist
825 (append nnheader-file-name-translation-alist '((?' . ?_)))))
826 (nnheader-translate-file-chars name)))
828 (defvar nnrss-moreover-url
829 "http://w.moreover.com/categories/category_list_rss.html"
830 "The url of moreover.com categories.")
832 (defun nnrss-snarf-moreover-categories ()
833 "Snarf RSS links from moreover.com."
835 (let (category name url changed)
837 (nnrss-insert nnrss-moreover-url)
838 (goto-char (point-min))
839 (while (re-search-forward
840 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
842 (setq category (match-string 1))
843 (setq url (match-string 2)
844 name (mm-url-decode-entities-string
845 (rfc2231-decode-encoded-string
848 (setq name (concat category "." name)))
849 (unless (assoc name nnrss-server-data)
851 (push (list name 0 url) nnrss-server-data)))))
853 (nnrss-save-server-data ""))))
855 (defun nnrss-node-text (namespace local-name element)
856 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
858 (text (if (and node (listp node))
859 (nnrss-node-just-text node)
861 (cleaned-text (if text
862 (gnus-replace-in-string
863 (gnus-replace-in-string
864 text "^[\000-\037\177]+\\|^ +\\| +$" "")
866 (if (string-equal "" cleaned-text)
870 (defun nnrss-node-just-text (node)
871 (if (and node (listp node))
872 (mapconcat 'nnrss-node-just-text (cddr node) " ")
875 (defun nnrss-find-el (tag data &optional found-list)
876 "Find the all matching elements in the data.
877 Careful with this on large documents!"
881 (when (equal tag (car bit))
882 ;; Old xml.el may return a list of string.
883 (when (and (consp (caddr bit))
884 (stringp (caaddr bit)))
885 (setcar (cddr bit) (caaddr bit)))
889 (if (and (consp (car-safe (caddr bit)))
890 (not (stringp (caddr bit))))
898 tag (cddr bit))))))))
901 (defun nnrss-rsslink-p (el)
902 "Test if the element we are handed is an RSS autodiscovery link."
903 (and (eq (car-safe el) 'link)
904 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
905 (or (string-equal (cdr (assoc 'type (cadr el)))
906 "application/rss+xml")
907 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
909 (defun nnrss-get-rsslinks (data)
910 "Extract the <link> elements that are links to RSS from the parsed data."
913 (if (nnrss-rsslink-p el) el))
914 (nnrss-find-el 'link data))))
916 (defun nnrss-extract-hrefs (data)
917 "Recursively extract hrefs from a page's source.
918 DATA should be the output of `xml-parse-region' or
920 (mapcar (lambda (ahref)
921 (cdr (assoc 'href (cadr ahref))))
922 (nnrss-find-el 'a data)))
924 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
925 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
926 (not (string-match "://" ,item)))
927 (setq ,onsite-list (append ,onsite-list (list ,item))))
928 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
930 (defun nnrss-order-hrefs (base-uri hrefs)
931 "Given a list of hrefs, sort them using the following priorities:
932 1. links ending in .rss
933 2. links ending in .rdf
934 3. links ending in .xml
935 4. links containing the above
938 BASE-URI is used to determine the location of the links and
939 whether they are `offsite' or `onsite'."
940 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
941 rss-onsite-in rdf-onsite-in xml-onsite-in
942 rss-offsite-end rdf-offsite-end xml-offsite-end
943 rss-offsite-in rdf-offsite-in xml-offsite-in)
946 ((string-match "\\.rss$" href)
948 base-uri href rss-onsite-end rss-offsite-end))
949 ((string-match "\\.rdf$" href)
951 base-uri href rdf-onsite-end rdf-offsite-end))
952 ((string-match "\\.xml$" href)
954 base-uri href xml-onsite-end xml-offsite-end))
955 ((string-match "rss" href)
957 base-uri href rss-onsite-in rss-offsite-in))
958 ((string-match "rdf" href)
960 base-uri href rdf-onsite-in rdf-offsite-in))
961 ((string-match "xml" href)
963 base-uri href xml-onsite-in xml-offsite-in))))
965 rss-onsite-end rdf-onsite-end xml-onsite-end
966 rss-onsite-in rdf-onsite-in xml-onsite-in
967 rss-offsite-end rdf-offsite-end xml-offsite-end
968 rss-offsite-in rdf-offsite-in xml-offsite-in)))
970 (defun nnrss-discover-feed (url)
971 "Given a page, find an RSS feed using Mark Pilgrim's
972 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
974 (let ((parsed-page (nnrss-fetch url)))
976 ;; 1. if this url is the rss, use it.
977 (if (nnrss-rss-p parsed-page)
978 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
979 (nnrss-rss-title-description rss-ns parsed-page url))
981 ;; 2. look for the <link rel="alternate"
982 ;; type="application/rss+xml" and use that if it is there.
983 (let ((links (nnrss-get-rsslinks parsed-page)))
985 (let* ((xml (nnrss-fetch
986 (cdr (assoc 'href (cadar links)))))
987 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
988 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
990 ;; 3. look for links on the site in the following order:
991 ;; - onsite links ending in .rss, .rdf, or .xml
992 ;; - onsite links containing any of the above
993 ;; - offsite links ending in .rss, .rdf, or .xml
994 ;; - offsite links containing any of the above
995 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
996 (match-string 0 url)))
997 (hrefs (nnrss-order-hrefs
998 base-uri (nnrss-extract-hrefs parsed-page)))
1000 (while (and (eq rss-link nil) (not (eq hrefs nil)))
1001 (let ((href-data (nnrss-fetch (car hrefs))))
1002 (if (nnrss-rss-p href-data)
1003 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
1004 (setq rss-link (nnrss-rss-title-description
1005 rss-ns href-data (car hrefs))))
1006 (setq hrefs (cdr hrefs)))))
1007 (if rss-link rss-link
1010 (nnrss-find-rss-via-syndic8 url))))))))
1012 (defun nnrss-find-rss-via-syndic8 (url)
1013 "Query syndic8 for the rss feeds it has for URL."
1014 (if (not (locate-library "xml-rpc"))
1016 (message "XML-RPC is not available... not checking Syndic8.")
1019 (let ((feedid (xml-rpc-method-call
1020 "http://www.syndic8.com/xmlrpc.php"
1024 (let* ((feedinfo (xml-rpc-method-call
1025 "http://www.syndic8.com/xmlrpc.php"
1026 'syndic8.GetFeedInfo
1033 (cdr (assoc "status" listinfo))
1036 (cdr (assoc "sitename" listinfo))
1040 "sitename" listinfo)))
1043 "dataurl" listinfo)))))))
1045 (if (not (> (length urllist) 1))
1047 (let ((completion-ignore-case t)
1049 (mapcar (lambda (listinfo)
1050 (cons (cdr (assoc "sitename" listinfo))
1052 (cdr (assoc "feedid" listinfo)))))
1056 "Multiple feeds found. Select one: "
1057 selection nil t) urllist)))))))))
1059 (defun nnrss-rss-p (data)
1060 "Test if DATA is an RSS feed.
1061 Simply ensures that the first element is rss or rdf."
1062 (or (eq (caar data) 'rss)
1063 (eq (caar data) 'rdf:RDF)))
1065 (defun nnrss-rss-title-description (rss-namespace data url)
1066 "Return the title of an RSS feed."
1067 (if (nnrss-rss-p data)
1068 (let ((description (intern (concat rss-namespace "description")))
1069 (title (intern (concat rss-namespace "title")))
1070 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1073 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1074 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1075 (cons 'href url)))))
1077 (defun nnrss-get-namespace-prefix (el uri)
1078 "Given EL (containing a parsed element) and URI (containing a string
1079 that gives the URI for which you want to retrieve the namespace
1080 prefix), return the prefix."
1081 (let* ((prefix (car (rassoc uri (cadar el))))
1083 (split-string (symbol-name prefix) ":")))
1084 (ns (cond ((eq (length nslist) 1) ; no prefix given
1086 ((eq (length nslist) 2) ; extract prefix
1088 (if (and ns (not (string= ns "")))
1095 ;;; nnrss.el ends here
1097 ;;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267