1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007 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 3, 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.
32 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
34 (eval-when-compile (require 'cl))
50 (eval '(require 'xml))
54 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
55 "Where nnrss will save its files.")
57 (defvoo nnrss-ignore-article-fields '(slash:comments)
58 "*List of fields that should be ignored when comparing RSS articles.
59 Some RSS feeds update article fields during their lives, e.g. to
60 indicate the number of comments or the number of times the
61 articles have been seen. However, if there is a difference
62 between the local article and the distant one, the latter is
63 considered to be new. To avoid this and discard some fields, set
64 this variable to the list of fields to be ignored.")
66 ;; (group max rss-url)
67 (defvoo nnrss-server-data nil)
69 ;; (num timestamp url subject author date extra)
70 (defvoo nnrss-group-data nil)
71 (defvoo nnrss-group-max 0)
72 (defvoo nnrss-group-min 1)
73 (defvoo nnrss-group nil)
74 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
75 (defvoo nnrss-status-string "")
77 (defconst nnrss-version "nnrss 1.0")
79 (defvar nnrss-group-alist '()
80 "List of RSS addresses.")
82 (defvar nnrss-use-local nil)
84 (defvar nnrss-description-field 'X-Gnus-Description
85 "Field name used for DESCRIPTION.
86 To use the description in headers, put this name into `nnmail-extra-headers'.")
88 (defvar nnrss-url-field 'X-Gnus-Url
89 "Field name used for URL.
90 To use the description in headers, put this name into `nnmail-extra-headers'.")
92 (defvar nnrss-content-function nil
93 "A function which is called in `nnrss-request-article'.
94 The arguments are (ENTRY GROUP ARTICLE).
95 ENTRY is the record of the current headline. GROUP is the group name.
96 ARTICLE is the article number of the current headline.")
98 (defvar nnrss-file-coding-system mm-universal-coding-system
99 "*Coding system used when reading and writing files.
100 If you run Gnus with various versions of Emacsen, the value of this
101 variable should be the coding system that all those Emacsen support.
102 Note that you have to regenerate all the nnrss groups if you change
103 the value. Moreover, you should be patient even if you are made to
104 read the same articles twice, that arises for the difference of the
105 versions of xml.el.")
107 (defvar nnrss-compatible-encoding-alist
108 (delq nil (mapcar (lambda (elem)
109 (if (and (mm-coding-system-p (car elem))
110 (mm-coding-system-p (cdr elem)))
112 mm-charset-override-alist))
113 "Alist of encodings and those supersets.
114 The cdr of each element is used to decode data if it is available when
115 the car is what the data specify as the encoding. Or, the car is used
116 for decoding when the cdr that the data specify is not available.")
118 (defvar nnrss-wash-html-in-text-plain-parts nil
119 "*Non-nil means render text in text/plain parts as HTML.
120 The function specified by the `mm-text-html-renderer' variable will be
121 used to render text. If it is nil, text will simply be folded.")
123 (nnoo-define-basics nnrss)
125 ;;; Interface functions
127 (defsubst nnrss-format-string (string)
128 (gnus-replace-in-string string " *\n *" " "))
130 (defun nnrss-decode-group-name (group)
131 (if (and group (mm-coding-system-p 'utf-8))
132 (setq group (mm-decode-coding-string group 'utf-8))
135 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
136 (setq group (nnrss-decode-group-name group))
137 (nnrss-possibly-change-group group server)
140 (set-buffer nntp-server-buffer)
142 (dolist (article articles)
143 (if (setq e (assq article nnrss-group-data))
144 (insert (number-to-string (car e)) "\t" ;; number
149 (or (nth 4 e) "(nobody)")
155 (format "<%d@%s.nnrss>" (car e) group)
166 (memq nnrss-description-field
167 nnmail-extra-headers))
168 (concat (symbol-name nnrss-description-field)
170 (nnrss-format-string (nth 6 e))
174 (memq nnrss-url-field
175 nnmail-extra-headers))
176 (concat (symbol-name nnrss-url-field)
178 (nnrss-format-string (nth 2 e))
184 (deffoo nnrss-request-group (group &optional server dont-check)
185 (setq group (nnrss-decode-group-name group))
186 (nnheader-message 6 "nnrss: Requesting %s..." group)
187 (nnrss-possibly-change-group group server)
191 (nnrss-check-group group server)
192 (nnheader-report 'nnrss "Opened group %s" group)
194 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
195 (prin1-to-string group)
197 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
199 (deffoo nnrss-close-group (group &optional server)
202 (defvar mm-text-html-renderer)
203 (defvar mm-text-html-washer-alist)
205 (deffoo nnrss-request-article (article &optional group server buffer)
206 (setq group (nnrss-decode-group-name group))
207 (when (stringp article)
208 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
209 (string-to-number (match-string 1 article))
211 (nnrss-possibly-change-group group server)
212 (let ((e (assq article nnrss-group-data))
213 (nntp-server-buffer (or buffer nntp-server-buffer))
216 (with-current-buffer nntp-server-buffer
219 (insert "Newsgroups: " group "\n"))
221 (insert "Subject: " (nth 3 e) "\n"))
223 (insert "From: " (nth 4 e) "\n"))
225 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
226 (let ((header (buffer-string))
229 (enclosure (nth 7 e))
231 ;; Enable encoding of Newsgroups header in XEmacs.
232 (default-enable-multibyte-characters t)
233 (rfc2047-header-encoding-alist
234 (if (mm-coding-system-p 'utf-8)
235 (cons '("Newsgroups" . utf-8)
236 rfc2047-header-encoding-alist)
237 rfc2047-header-encoding-alist))
238 rfc2047-encode-encoded-words body fn)
239 (when (or text link enclosure comments)
241 (insert "<#multipart type=alternative>\n"
242 "<#part type=\"text/plain\">\n")
247 (if (and nnrss-wash-html-in-text-plain-parts
250 (setq fn (or (cdr (assq mm-text-html-renderer
251 mm-text-html-washer-alist))
252 mm-text-html-renderer))))
254 (narrow-to-region body (point-max))
257 (apply (car fn) (cdr fn)))
260 (re-search-forward "[^\t\n ]" nil t)
262 (delete-region body (point))
263 (goto-char (point-max))
264 (skip-chars-backward "\t\n ")
266 (delete-region (point) (point-max))
268 (while (re-search-forward "\n+" nil t)
271 ;; See `nnrss-check-group', which inserts "<br /><br />".
272 (when (search-forward "<br /><br />" nil t)
275 (replace-match "\n\n")))
277 (let ((fill-column default-fill-column)
278 (window (get-buffer-window nntp-server-buffer)))
281 (max 1 (/ (* (window-width window) 7) 8))))
282 (fill-region (point) (point-max))
283 (goto-char (point-max))
284 ;; XEmacs version of `fill-region' inserts newline.
287 (when (or link enclosure)
292 (insert (car enclosure) " "
293 (nth 2 enclosure) " "
294 (nth 3 enclosure) "\n"))
296 (insert comments "\n"))
297 (setq body (buffer-substring body (point)))
299 "<#part type=\"text/html\">\n"
300 "<html><head></head><body>\n")
304 (insert "<p><a href=\"" link "\">link</a></p>\n"))
306 (insert "<p><a href=\"" (car enclosure) "\">"
307 (cadr enclosure) "</a> " (nth 2 enclosure)
308 " " (nth 3 enclosure) "</p>\n"))
310 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
311 (insert "</body></html>\n"
319 "Content-Type: text/plain; charset=gnus-decoded\n"
320 "Content-Transfer-Encoding: 8bit\n\n"
323 3 "Warning - there might be invalid characters"))))
324 (goto-char (point-min))
325 (search-forward "\n\n")
327 (insert (format "Message-ID: <%d@%s.nnrss>\n"
329 (let ((rfc2047-encoding-type 'mime)
330 rfc2047-encode-max-chars)
331 (rfc2047-encode-string
332 (gnus-replace-in-string group "[\t\n ]+" "_")))))
333 (when nnrss-content-function
334 (funcall nnrss-content-function e group article))))
337 (nnheader-report 'nnrss err))
339 (nnheader-report 'nnrss "no such id: %d" article))
341 (nnheader-report 'nnrss "article %s retrieved" (car e))
342 ;; we return the article number.
343 (cons nnrss-group (car e))))))
345 (deffoo nnrss-request-list (&optional server)
346 (nnrss-possibly-change-group nil server)
347 (nnrss-generate-active)
350 (deffoo nnrss-open-server (server &optional defs connectionless)
351 (nnrss-read-server-data server)
352 (nnoo-change-server 'nnrss server defs)
355 (deffoo nnrss-request-expire-articles
356 (articles group &optional server force)
357 (setq group (nnrss-decode-group-name group))
358 (nnrss-possibly-change-group group server)
359 (let (e days not-expirable changed)
360 (dolist (art articles)
361 (if (and (setq e (assq art nnrss-group-data))
362 (nnmail-expired-article-p
364 (if (listp (setq days (nth 1 e))) days
365 (days-to-time (- days (time-to-days '(0 0)))))
367 (setq nnrss-group-data (delq e nnrss-group-data)
369 (push art not-expirable)))
371 (nnrss-save-group-data group server))
374 (deffoo nnrss-request-delete-group (group &optional force server)
375 (setq group (nnrss-decode-group-name group))
376 (nnrss-possibly-change-group group server)
378 ;; There may be two or more entries in `nnrss-group-alist' since
379 ;; this function didn't delete them formerly.
380 (while (setq elem (assoc group nnrss-group-alist))
381 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
382 (setq nnrss-server-data
383 (delq (assoc group nnrss-server-data) nnrss-server-data))
384 (nnrss-save-server-data server)
386 (let ((file-name-coding-system nnmail-pathname-coding-system))
387 (delete-file (nnrss-make-filename group server))))
390 (deffoo nnrss-request-list-newsgroups (&optional server)
391 (nnrss-possibly-change-group nil server)
393 (set-buffer nntp-server-buffer)
395 (dolist (elem nnrss-group-alist)
397 (insert (car elem) "\t" (third elem) "\n"))))
400 (nnoo-define-skeleton nnrss)
402 ;;; Internal functions
403 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
405 (defun nnrss-get-encoding ()
406 "Return an encoding attribute specified in the current xml contents.
407 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
408 it is used instead. If the xml contents doesn't specify the encoding,
409 return `utf-8' which is the default encoding for xml if it is available,
410 otherwise return nil."
411 (goto-char (point-min))
412 (if (re-search-forward
413 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
415 (let ((encoding (intern (downcase (or (match-string 1)
416 (match-string 2))))))
418 (mm-coding-system-p (cdr (assq encoding
419 nnrss-compatible-encoding-alist)))
420 (mm-coding-system-p encoding)
421 (mm-coding-system-p (car (rassq encoding
422 nnrss-compatible-encoding-alist)))))
423 (mm-coding-system-p 'utf-8)))
425 (declare-function w3-parse-buffer "ext:w3-parse" (&optional buff))
427 (defun nnrss-fetch (url &optional local)
428 "Fetch URL and put it in a the expected Lisp structure."
429 (mm-with-unibyte-buffer
430 ;;some CVS versions of url.el need this to close the connection quickly
431 (let (cs xmlform htmlform)
432 ;; bit o' work necessary for w3 pre-cvs and post-cvs
434 (let ((coding-system-for-read 'binary))
435 (insert-file-contents url))
436 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
437 ;; to `mm-url-insert'?
438 (let ((coding-system-for-read 'binary))
441 (error (if (or debug-on-quit debug-on-error)
442 (signal (car err) (cdr err))
443 (message "nnrss: Failed to fetch %s" url))))))
444 (nnheader-remove-cr-followed-by-lf)
445 ;; Decode text according to the encoding attribute.
446 (when (setq cs (nnrss-get-encoding))
448 (mm-decode-coding-string (buffer-string) cs)
450 (mm-enable-multibyte))))
451 (goto-char (point-min))
453 ;; Because xml-parse-region can't deal with anything that isn't
454 ;; xml and w3-parse-buffer can't deal with some xml, we have to
455 ;; parse with xml-parse-region first and, if that fails, parse
456 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
457 ;; why w3-parse-buffer fails to parse some well-formed xml and
461 (setq xmlform (xml-parse-region (point-min) (point-max)))
464 (setq htmlform (caddar (w3-parse-buffer
468 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
474 (defun nnrss-possibly-change-group (&optional group server)
476 (not (nnrss-server-opened server)))
477 (nnrss-open-server server))
478 (when (and group (not (equal group nnrss-group)))
479 (nnrss-read-group-data group server)
480 (setq nnrss-group group)))
482 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
484 (defun nnrss-generate-active ()
485 (when (y-or-n-p "Fetch extra categories? ")
486 (mapc 'funcall nnrss-extra-categories))
488 (set-buffer nntp-server-buffer)
490 (dolist (elem nnrss-group-alist)
491 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
492 (dolist (elem nnrss-server-data)
493 (unless (assoc (car elem) nnrss-group-alist)
494 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
496 (eval-and-compile (autoload 'timezone-parse-date "timezone"))
498 (defun nnrss-normalize-date (date)
499 "Return a date string of DATE in the RFC822 style.
500 This function handles the ISO 8601 date format described in
501 <URL:http://www.w3.org/TR/NOTE-datetime>, and also the RFC822 style
502 which RSS 2.0 allows."
503 (let (case-fold-search vector year month day time zone cts)
506 ((string-match " [0-9]+ " date)
507 (setq vector (timezone-parse-date date)
508 year (string-to-number (aref vector 0)))
510 (setq month (string-to-number (aref vector 1))
511 day (string-to-number (aref vector 2)))
512 (unless (>= (length (setq time (aref vector 3))) 3)
513 (setq time "00:00:00"))
514 (when (and (setq zone (aref vector 4))
515 (not (string-match "\\`[A-Z+-]" zone)))
522 "\\(199[0-9]\\|20[0-9][0-9]\\)"
531 "\\([012][0-9]:[0-5][0-9]\\)"
535 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
537 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
538 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
541 (setq year (string-to-number (match-string 1 date))
542 month (string-to-number (or (match-string 2 date) "1"))
543 day (string-to-number (or (match-string 3 date) "1"))
544 time (if (match-beginning 5)
545 (substring date (match-beginning 4) (match-end 5))
546 (concat (or (match-string 4 date) "00:00") ":00"))
547 zone (cond ((match-beginning 6)
548 (concat (match-string 6 date)
549 (match-string 7 date)))
550 ((match-beginning 9) ;; Z
552 (t ;; nil if zone is not provided.
553 (match-string 8 date))))))
556 (setq cts (current-time-string (encode-time 0 0 0 day month year)))
557 (format "%s, %02d %s %04d %s%s"
558 (substring cts 0 3) day (substring cts 4 7) year time
562 (message-make-date))))
566 (defun nnrss-read-server-data (server)
567 (setq nnrss-server-data nil)
568 (let ((file (nnrss-make-filename "nnrss" server))
569 (file-name-coding-system nnmail-pathname-coding-system))
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 (insert-file-contents file)
576 (eval-region (point-min) (point-max)))))))
578 (defun nnrss-save-server-data (server)
579 (gnus-make-directory nnrss-directory)
580 (let ((coding-system-for-write nnrss-file-coding-system)
581 (file-name-coding-system nnmail-pathname-coding-system))
582 (with-temp-file (nnrss-make-filename "nnrss" server)
583 (insert (format ";; -*- coding: %s; -*-\n"
584 nnrss-file-coding-system))
585 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
587 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
589 (defun nnrss-read-group-data (group server)
590 (setq nnrss-group-data nil)
591 (if (hash-table-p nnrss-group-hashtb)
592 (clrhash nnrss-group-hashtb)
593 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
594 (let ((pair (assoc group nnrss-server-data)))
595 (setq nnrss-group-max (or (cadr pair) 0))
596 (setq nnrss-group-min (+ nnrss-group-max 1)))
597 (let ((file (nnrss-make-filename group server))
598 (file-name-coding-system nnmail-pathname-coding-system))
599 (when (file-exists-p file)
600 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
601 ;; file names. So, we use `insert-file-contents' instead.
602 (mm-with-multibyte-buffer
603 (let ((coding-system-for-read nnrss-file-coding-system))
604 (insert-file-contents file)
605 (eval-region (point-min) (point-max))))
606 (dolist (e nnrss-group-data)
607 (puthash (nth 9 e) t nnrss-group-hashtb)
608 (when (and (car e) (> nnrss-group-min (car e)))
609 (setq nnrss-group-min (car e)))
610 (when (and (car e) (< nnrss-group-max (car e)))
611 (setq nnrss-group-max (car e)))))))
613 (defun nnrss-save-group-data (group server)
614 (gnus-make-directory nnrss-directory)
615 (let ((coding-system-for-write nnrss-file-coding-system)
616 (file-name-coding-system nnmail-pathname-coding-system))
617 (with-temp-file (nnrss-make-filename group server)
618 (insert (format ";; -*- coding: %s; -*-\n"
619 nnrss-file-coding-system))
620 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
622 (defun nnrss-make-filename (name server)
624 (nnrss-translate-file-chars
627 (not (equal server ""))
633 (gnus-add-shutdown 'nnrss-close 'gnus)
635 (defun nnrss-close ()
636 "Clear internal nnrss variables."
637 (setq nnrss-group-data nil
638 nnrss-server-data nil
639 nnrss-group-hashtb nil
640 nnrss-group-alist nil))
644 (defun nnrss-no-cache (url)
647 (defun nnrss-insert-w3 (url)
648 (mm-with-unibyte-current-buffer
651 (error (if (or debug-on-quit debug-on-error)
652 (signal (car err) (cdr err))
653 (message "nnrss: Failed to fetch %s" url))))))
655 (defun nnrss-decode-entities-string (string)
657 (mm-with-multibyte-buffer
659 (mm-url-decode-entities-nbsp)
662 (defalias 'nnrss-insert 'nnrss-insert-w3)
664 (defun nnrss-mime-encode-string (string)
665 (mm-with-multibyte-buffer
667 (mm-url-decode-entities-nbsp)
668 (goto-char (point-min))
669 (while (re-search-forward "[\t\n ]+" nil t)
671 (goto-char (point-min))
672 (skip-chars-forward " ")
673 (delete-region (point-min) (point))
674 (goto-char (point-max))
675 (skip-chars-forward " ")
676 (delete-region (point) (point-max))
677 (let ((rfc2047-encoding-type 'mime)
678 rfc2047-encode-max-chars)
679 (rfc2047-encode-region (point-min) (point-max)))
680 (goto-char (point-min))
681 (while (search-forward "\n" nil t)
682 (delete-backward-char 1))
687 (defun nnrss-make-hash-index (item)
688 (setq item (gnus-remove-if
691 (memq (car field) nnrss-ignore-article-fields)))
693 (md5 (gnus-prin1-to-string item)
695 nnrss-file-coding-system))
697 (defun nnrss-check-group (group server)
698 (let (file xml subject url extra changed author date feed-subject
699 enclosure comments rss-ns rdf-ns content-ns dc-ns
701 (if (and nnrss-use-local
702 (file-exists-p (setq file (expand-file-name
703 (nnrss-translate-file-chars
704 (concat group ".xml"))
706 (setq xml (nnrss-fetch file t))
707 (setq url (or (nth 2 (assoc group nnrss-server-data))
708 (second (assoc group nnrss-group-alist))))
715 (format "URL to search for %s: " group) "http://")))))
716 (let ((pair (assoc group nnrss-server-data)))
718 (setcdr (cdr pair) (list url))
719 (push (list group nnrss-group-max url) nnrss-server-data)))
721 (setq xml (nnrss-fetch url)))
723 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
724 ;; for more RSS namespaces.
725 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
726 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
727 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
728 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
729 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
730 (when (and (listp item)
731 (string= (concat rss-ns "item") (car item))
732 (progn (setq hash-index (nnrss-make-hash-index item))
733 (not (gethash hash-index nnrss-group-hashtb))))
734 (setq subject (nnrss-node-text rss-ns 'title item))
735 (setq url (nnrss-decode-entities-string
736 (nnrss-node-text rss-ns 'link (cddr item))))
737 (setq extra (or (nnrss-node-text content-ns 'encoded item)
738 (nnrss-node-text rss-ns 'description item)))
739 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
740 (setq extra (concat feed-subject "<br /><br />" extra)))
741 (setq author (or (nnrss-node-text rss-ns 'author item)
742 (nnrss-node-text dc-ns 'creator item)
743 (nnrss-node-text dc-ns 'contributor item)))
744 (setq date (nnrss-normalize-date
745 (or (nnrss-node-text dc-ns 'date item)
746 (nnrss-node-text rss-ns 'pubDate item))))
747 (setq comments (nnrss-node-text rss-ns 'comments item))
748 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
749 (let ((url (cdr (assq 'url enclosure)))
750 (len (cdr (assq 'length enclosure)))
751 (type (cdr (assq 'type enclosure)))
754 (if (and len (integerp (setq len (string-to-number len))))
755 ;; actually already in `ls-lisp-format-file-size' but
756 ;; probably not worth to require it for one function
757 (do ((size (/ len 1.0) (/ size 1024.0))
758 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
761 (format "%.1f%s" size (car post-fixes))))
763 (setq url (or url ""))
764 (setq name (if (string-match "/\\([^/]*\\)$" url)
767 (setq type (or type ""))
768 (setq enclosure (list url name len type))))
771 (incf nnrss-group-max)
774 (and subject (nnrss-mime-encode-string subject))
775 (and author (nnrss-mime-encode-string author))
777 (and extra (nnrss-decode-entities-string extra))
782 (puthash hash-index t nnrss-group-hashtb)
786 (nnrss-save-group-data group server)
787 (let ((pair (assoc group nnrss-server-data)))
789 (setcar (cdr pair) nnrss-group-max)
790 (push (list group nnrss-group-max) nnrss-server-data)))
791 (nnrss-save-server-data server))))
793 (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
795 (defun nnrss-opml-import (opml-file)
796 "OPML subscriptions import.
797 Read the file and attempt to subscribe to each Feed in the file."
798 (interactive "fImport file: ")
801 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
803 (not (string-match "\\`[\t ]*\\'" xmlurl))
805 (y-or-n-p (format "Subscribe to %s " xmlurl))
809 (gnus-group-make-rss-group xmlurl)
813 "Failed to subscribe to %s (%s); type any key to continue: "
815 (error-message-string err))
816 (let ((echo-keystrokes 0))
818 (nnrss-find-el 'outline
819 (mm-with-multibyte-buffer
820 (insert-file-contents opml-file)
821 (xml-parse-region (point-min) (point-max))))))
823 (defun nnrss-opml-export ()
824 "OPML subscription export.
825 Export subscriptions to a buffer in OPML Format."
827 (with-current-buffer (get-buffer-create "*OPML Export*")
828 (mm-set-buffer-file-coding-system 'utf-8)
829 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
830 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
831 "<opml version=\"1.1\">\n"
833 " <title>mySubscriptions</title>\n"
834 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
836 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
837 " <ownerName>" (user-full-name) "</ownerName>\n"
840 (dolist (sub nnrss-group-alist)
841 (insert " <outline text=\"" (car sub)
842 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
845 (pop-to-buffer "*OPML Export*")
846 (when (fboundp 'sgml-mode)
849 (defun nnrss-generate-download-script ()
850 "Generate a download script in the current buffer.
851 It is useful when `(setq nnrss-use-local t)'."
853 (insert "#!/bin/sh\n")
854 (insert "WGET=wget\n")
855 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
856 (dolist (elem nnrss-server-data)
857 (let ((url (or (nth 2 elem)
858 (second (assoc (car elem) nnrss-group-alist)))))
859 (insert "$WGET -q -O \"$RSSDIR\"/'"
860 (nnrss-translate-file-chars (concat (car elem) ".xml"))
863 (defun nnrss-translate-file-chars (name)
864 (let ((nnheader-file-name-translation-alist
865 (append nnheader-file-name-translation-alist '((?' . ?_)))))
866 (nnheader-translate-file-chars name)))
868 (defvar nnrss-moreover-url
869 "http://w.moreover.com/categories/category_list_rss.html"
870 "The url of moreover.com categories.")
872 (defun nnrss-snarf-moreover-categories ()
873 "Snarf RSS links from moreover.com."
875 (let (category name url changed)
877 (nnrss-insert nnrss-moreover-url)
878 (goto-char (point-min))
879 (while (re-search-forward
880 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
882 (setq category (match-string 1))
883 (setq url (match-string 2)
884 name (mm-url-decode-entities-string
885 (rfc2231-decode-encoded-string
888 (setq name (concat category "." name)))
889 (unless (assoc name nnrss-server-data)
891 (push (list name 0 url) nnrss-server-data)))))
893 (nnrss-save-server-data ""))))
895 (defun nnrss-node-text (namespace local-name element)
896 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
898 (text (if (and node (listp node))
899 (nnrss-node-just-text node)
901 (cleaned-text (if text
902 (gnus-replace-in-string
903 (gnus-replace-in-string
904 text "^[\000-\037\177]+\\|^ +\\| +$" "")
906 (if (string-equal "" cleaned-text)
910 (defun nnrss-node-just-text (node)
911 (if (and node (listp node))
912 (mapconcat 'nnrss-node-just-text (cddr node) " ")
915 (defun nnrss-find-el (tag data &optional found-list)
916 "Find the all matching elements in the data.
917 Careful with this on large documents!"
921 (when (equal tag (car bit))
922 ;; Old xml.el may return a list of string.
923 (when (and (consp (caddr bit))
924 (stringp (caaddr bit)))
925 (setcar (cddr bit) (caaddr bit)))
929 (if (and (consp (car-safe (caddr bit)))
930 (not (stringp (caddr bit))))
938 tag (cddr bit))))))))
941 (defun nnrss-rsslink-p (el)
942 "Test if the element we are handed is an RSS autodiscovery link."
943 (and (eq (car-safe el) 'link)
944 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
945 (or (string-equal (cdr (assoc 'type (cadr el)))
946 "application/rss+xml")
947 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
949 (defun nnrss-get-rsslinks (data)
950 "Extract the <link> elements that are links to RSS from the parsed data."
953 (if (nnrss-rsslink-p el) el))
954 (nnrss-find-el 'link data))))
956 (defun nnrss-extract-hrefs (data)
957 "Recursively extract hrefs from a page's source.
958 DATA should be the output of `xml-parse-region' or
960 (mapcar (lambda (ahref)
961 (cdr (assoc 'href (cadr ahref))))
962 (nnrss-find-el 'a data)))
964 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
965 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
966 (not (string-match "://" ,item)))
967 (setq ,onsite-list (append ,onsite-list (list ,item))))
968 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
970 (defun nnrss-order-hrefs (base-uri hrefs)
971 "Given a list of hrefs, sort them using the following priorities:
972 1. links ending in .rss
973 2. links ending in .rdf
974 3. links ending in .xml
975 4. links containing the above
978 BASE-URI is used to determine the location of the links and
979 whether they are `offsite' or `onsite'."
980 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
981 rss-onsite-in rdf-onsite-in xml-onsite-in
982 rss-offsite-end rdf-offsite-end xml-offsite-end
983 rss-offsite-in rdf-offsite-in xml-offsite-in)
986 ((string-match "\\.rss$" href)
988 base-uri href rss-onsite-end rss-offsite-end))
989 ((string-match "\\.rdf$" href)
991 base-uri href rdf-onsite-end rdf-offsite-end))
992 ((string-match "\\.xml$" href)
994 base-uri href xml-onsite-end xml-offsite-end))
995 ((string-match "rss" href)
997 base-uri href rss-onsite-in rss-offsite-in))
998 ((string-match "rdf" href)
1000 base-uri href rdf-onsite-in rdf-offsite-in))
1001 ((string-match "xml" href)
1003 base-uri href xml-onsite-in xml-offsite-in))))
1005 rss-onsite-end rdf-onsite-end xml-onsite-end
1006 rss-onsite-in rdf-onsite-in xml-onsite-in
1007 rss-offsite-end rdf-offsite-end xml-offsite-end
1008 rss-offsite-in rdf-offsite-in xml-offsite-in)))
1010 (defun nnrss-discover-feed (url)
1011 "Given a page, find an RSS feed using Mark Pilgrim's
1012 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
1014 (let ((parsed-page (nnrss-fetch url)))
1016 ;; 1. if this url is the rss, use it.
1017 (if (nnrss-rss-p parsed-page)
1018 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
1019 (nnrss-rss-title-description rss-ns parsed-page url))
1021 ;; 2. look for the <link rel="alternate"
1022 ;; type="application/rss+xml" and use that if it is there.
1023 (let ((links (nnrss-get-rsslinks parsed-page)))
1025 (let* ((xml (nnrss-fetch
1026 (cdr (assoc 'href (cadar links)))))
1027 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
1028 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
1030 ;; 3. look for links on the site in the following order:
1031 ;; - onsite links ending in .rss, .rdf, or .xml
1032 ;; - onsite links containing any of the above
1033 ;; - offsite links ending in .rss, .rdf, or .xml
1034 ;; - offsite links containing any of the above
1035 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
1036 (match-string 0 url)))
1037 (hrefs (nnrss-order-hrefs
1038 base-uri (nnrss-extract-hrefs parsed-page)))
1040 (while (and (eq rss-link nil) (not (eq hrefs nil)))
1041 (let ((href-data (nnrss-fetch (car hrefs))))
1042 (if (nnrss-rss-p href-data)
1043 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
1044 (setq rss-link (nnrss-rss-title-description
1045 rss-ns href-data (car hrefs))))
1046 (setq hrefs (cdr hrefs)))))
1047 (if rss-link rss-link
1050 (nnrss-find-rss-via-syndic8 url))))))))
1052 (defun nnrss-find-rss-via-syndic8 (url)
1053 "Query syndic8 for the rss feeds it has for URL."
1054 (if (not (locate-library "xml-rpc"))
1056 (message "XML-RPC is not available... not checking Syndic8.")
1059 (let ((feedid (xml-rpc-method-call
1060 "http://www.syndic8.com/xmlrpc.php"
1064 (let* ((feedinfo (xml-rpc-method-call
1065 "http://www.syndic8.com/xmlrpc.php"
1066 'syndic8.GetFeedInfo
1073 (cdr (assoc "status" listinfo))
1076 (cdr (assoc "sitename" listinfo))
1080 "sitename" listinfo)))
1083 "dataurl" listinfo)))))))
1085 (if (not (> (length urllist) 1))
1087 (let ((completion-ignore-case t)
1089 (mapcar (lambda (listinfo)
1090 (cons (cdr (assoc "sitename" listinfo))
1092 (cdr (assoc "feedid" listinfo)))))
1096 "Multiple feeds found. Select one: "
1097 selection nil t) urllist)))))))))
1099 (defun nnrss-rss-p (data)
1100 "Test if DATA is an RSS feed.
1101 Simply ensures that the first element is rss or rdf."
1102 (or (eq (caar data) 'rss)
1103 (eq (caar data) 'rdf:RDF)))
1105 (defun nnrss-rss-title-description (rss-namespace data url)
1106 "Return the title of an RSS feed."
1107 (if (nnrss-rss-p data)
1108 (let ((description (intern (concat rss-namespace "description")))
1109 (title (intern (concat rss-namespace "title")))
1110 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1113 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1114 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1115 (cons 'href url)))))
1117 (defun nnrss-get-namespace-prefix (el uri)
1118 "Given EL (containing a parsed element) and URI (containing a string
1119 that gives the URI for which you want to retrieve the namespace
1120 prefix), return the prefix."
1121 (let* ((prefix (car (rassoc uri (cadar el))))
1123 (split-string (symbol-name prefix) ":")))
1124 (ns (cond ((eq (length nslist) 1) ; no prefix given
1126 ((eq (length nslist) 2) ; extract prefix
1128 (if (and ns (not (string= ns "")))
1135 ;;; nnrss.el ends here
1137 ;;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267