1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 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 by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
32 (eval-when-compile (require 'cl))
48 (eval '(require 'xml))
52 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
53 "Where nnrss will save its files.")
55 (defvoo nnrss-ignore-article-fields '(slash:comments)
56 "*List of fields that should be ignored when comparing RSS articles.
57 Some RSS feeds update article fields during their lives, e.g. to
58 indicate the number of comments or the number of times the
59 articles have been seen. However, if there is a difference
60 between the local article and the distant one, the latter is
61 considered to be new. To avoid this and discard some fields, set
62 this variable to the list of fields to be ignored.")
64 ;; (group max rss-url)
65 (defvoo nnrss-server-data nil)
67 ;; (num timestamp url subject author date extra)
68 (defvoo nnrss-group-data nil)
69 (defvoo nnrss-group-max 0)
70 (defvoo nnrss-group-min 1)
71 (defvoo nnrss-group nil)
72 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
73 (defvoo nnrss-status-string "")
75 (defconst nnrss-version "nnrss 1.0")
77 (defvar nnrss-group-alist '()
78 "List of RSS addresses.")
80 (defvar nnrss-use-local nil)
82 (defvar nnrss-description-field 'X-Gnus-Description
83 "Field name used for DESCRIPTION.
84 To use the description in headers, put this name into `nnmail-extra-headers'.")
86 (defvar nnrss-url-field 'X-Gnus-Url
87 "Field name used for URL.
88 To use the description in headers, put this name into `nnmail-extra-headers'.")
90 (defvar nnrss-content-function nil
91 "A function which is called in `nnrss-request-article'.
92 The arguments are (ENTRY GROUP ARTICLE).
93 ENTRY is the record of the current headline. GROUP is the group name.
94 ARTICLE is the article number of the current headline.")
96 (defvar nnrss-file-coding-system mm-universal-coding-system
97 "*Coding system used when reading and writing files.
98 If you run Gnus with various versions of Emacsen, the value of this
99 variable should be the coding system that all those Emacsen support.
100 Note that you have to regenerate all the nnrss groups if you change
101 the value. Moreover, you should be patient even if you are made to
102 read the same articles twice, that arises for the difference of the
103 versions of xml.el.")
105 (defvar nnrss-compatible-encoding-alist
106 (delq nil (mapcar (lambda (elem)
107 (if (and (mm-coding-system-p (car elem))
108 (mm-coding-system-p (cdr elem)))
110 mm-charset-override-alist))
111 "Alist of encodings and those supersets.
112 The cdr of each element is used to decode data if it is available when
113 the car is what the data specify as the encoding. Or, the car is used
114 for decoding when the cdr that the data specify is not available.")
116 (defvar nnrss-wash-html-in-text-plain-parts nil
117 "*Non-nil means render text in text/plain parts as HTML.
118 The function specified by the `mm-text-html-renderer' variable will be
119 used to render text. If it is nil, text will simply be folded.")
121 (nnoo-define-basics nnrss)
123 ;;; Interface functions
125 (defsubst nnrss-format-string (string)
126 (gnus-replace-in-string string " *\n *" " "))
128 (defun nnrss-decode-group-name (group)
129 (if (and group (mm-coding-system-p 'utf-8))
130 (setq group (mm-decode-coding-string group 'utf-8))
133 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
134 (setq group (nnrss-decode-group-name group))
135 (nnrss-possibly-change-group group server)
138 (set-buffer nntp-server-buffer)
140 (dolist (article articles)
141 (if (setq e (assq article nnrss-group-data))
142 (insert (number-to-string (car e)) "\t" ;; number
147 (or (nth 4 e) "(nobody)")
153 (format "<%d@%s.nnrss>" (car e) group)
164 (memq nnrss-description-field
165 nnmail-extra-headers))
166 (concat (symbol-name nnrss-description-field)
168 (nnrss-format-string (nth 6 e))
172 (memq nnrss-url-field
173 nnmail-extra-headers))
174 (concat (symbol-name nnrss-url-field)
176 (nnrss-format-string (nth 2 e))
182 (deffoo nnrss-request-group (group &optional server dont-check)
183 (setq group (nnrss-decode-group-name group))
184 (nnheader-message 6 "nnrss: Requesting %s..." group)
185 (nnrss-possibly-change-group group server)
189 (nnrss-check-group group server)
190 (nnheader-report 'nnrss "Opened group %s" group)
192 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
193 (prin1-to-string group)
195 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
197 (deffoo nnrss-close-group (group &optional server)
200 (defvar mm-text-html-renderer)
201 (defvar mm-text-html-washer-alist)
203 (deffoo nnrss-request-article (article &optional group server buffer)
204 (setq group (nnrss-decode-group-name group))
205 (when (stringp article)
206 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
207 (string-to-number (match-string 1 article))
209 (nnrss-possibly-change-group group server)
210 (let ((e (assq article nnrss-group-data))
211 (nntp-server-buffer (or buffer nntp-server-buffer))
214 (with-current-buffer nntp-server-buffer
217 (insert "Newsgroups: " group "\n"))
219 (insert "Subject: " (nth 3 e) "\n"))
221 (insert "From: " (nth 4 e) "\n"))
223 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
224 (let ((header (buffer-string))
227 (enclosure (nth 7 e))
229 (rfc2047-header-encoding-alist
230 (if (mm-coding-system-p 'utf-8)
231 (cons '("Newsgroups" . utf-8)
232 rfc2047-header-encoding-alist)
233 rfc2047-header-encoding-alist))
234 rfc2047-encode-encoded-words body fn)
235 (when (or text link enclosure comments)
237 (insert "<#multipart type=alternative>\n"
238 "<#part type=\"text/plain\">\n")
243 (if (and nnrss-wash-html-in-text-plain-parts
246 (setq fn (or (cdr (assq mm-text-html-renderer
247 mm-text-html-washer-alist))
248 mm-text-html-renderer))))
250 (narrow-to-region body (point-max))
253 (apply (car fn) (cdr fn)))
256 (re-search-forward "[^\t\n ]" nil t)
258 (delete-region body (point))
259 (goto-char (point-max))
260 (skip-chars-backward "\t\n ")
262 (delete-region (point) (point-max))
264 (while (re-search-forward "\n+" nil t)
267 ;; See `nnrss-check-group', which inserts "<br /><br />".
268 (when (search-forward "<br /><br />" nil t)
271 (replace-match "\n\n")))
273 (let ((fill-column (default-value 'fill-column))
274 (window (get-buffer-window nntp-server-buffer)))
277 (max 1 (/ (* (window-width window) 7) 8))))
278 (fill-region (point) (point-max))
279 (goto-char (point-max))
280 ;; XEmacs version of `fill-region' inserts newline.
283 (when (or link enclosure)
288 (insert (car enclosure) " "
289 (nth 2 enclosure) " "
290 (nth 3 enclosure) "\n"))
292 (insert comments "\n"))
293 (setq body (buffer-substring body (point)))
295 "<#part type=\"text/html\">\n"
296 "<html><head></head><body>\n")
300 (insert "<p><a href=\"" link "\">link</a></p>\n"))
302 (insert "<p><a href=\"" (car enclosure) "\">"
303 (cadr enclosure) "</a> " (nth 2 enclosure)
304 " " (nth 3 enclosure) "</p>\n"))
306 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
307 (insert "</body></html>\n"
311 ;; Allow `mml-to-mime' to generate MIME article without
312 ;; making inquiry to a user for unknown encoding.
313 (let ((mml-confirmation-set
314 (cons 'unknown-encoding mml-confirmation-set)))
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-open-server (server &optional defs connectionless)
346 (nnrss-read-server-data server)
347 (nnoo-change-server 'nnrss server defs)
350 (deffoo nnrss-request-expire-articles
351 (articles group &optional server force)
352 (setq group (nnrss-decode-group-name group))
353 (nnrss-possibly-change-group group server)
354 (let (e days not-expirable changed)
355 (dolist (art articles)
356 (if (and (setq e (assq art nnrss-group-data))
357 (nnmail-expired-article-p
359 (if (listp (setq days (nth 1 e))) days
360 (days-to-time (- days (time-to-days '(0 0)))))
362 (setq nnrss-group-data (delq e nnrss-group-data)
364 (push art not-expirable)))
366 (nnrss-save-group-data group server))
369 (deffoo nnrss-request-delete-group (group &optional force server)
370 (setq group (nnrss-decode-group-name group))
371 (nnrss-possibly-change-group group server)
373 ;; There may be two or more entries in `nnrss-group-alist' since
374 ;; this function didn't delete them formerly.
375 (while (setq elem (assoc group nnrss-group-alist))
376 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
377 (setq nnrss-server-data
378 (delq (assoc group nnrss-server-data) nnrss-server-data))
379 (nnrss-save-server-data server)
381 (let ((file-name-coding-system nnmail-pathname-coding-system))
382 (delete-file (nnrss-make-filename group server))))
385 (deffoo nnrss-request-list-newsgroups (&optional server)
386 (nnrss-possibly-change-group nil server)
388 (set-buffer nntp-server-buffer)
390 (dolist (elem nnrss-group-alist)
392 (insert (car elem) "\t" (third elem) "\n"))))
395 (deffoo nnrss-retrieve-groups (groups &optional server)
396 (nnrss-possibly-change-group nil server)
397 (dolist (group groups)
398 (nnrss-check-group group server))
400 (set-buffer nntp-server-buffer)
402 (dolist (group groups)
403 (let ((elem (assoc group nnrss-server-data)))
404 (insert (format "%S %s 1 y\n" group (or (cadr elem) 0)))))
407 (nnoo-define-skeleton nnrss)
409 ;;; Internal functions
410 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
412 (defun nnrss-get-encoding ()
413 "Return an encoding attribute specified in the current xml contents.
414 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
415 it is used instead. If the xml contents doesn't specify the encoding,
416 return `utf-8' which is the default encoding for xml if it is available,
417 otherwise return nil."
418 (goto-char (point-min))
419 (if (re-search-forward
420 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
422 (let ((encoding (intern (downcase (or (match-string 1)
423 (match-string 2))))))
425 (mm-coding-system-p (cdr (assq encoding
426 nnrss-compatible-encoding-alist)))
427 (mm-coding-system-p encoding)
428 (mm-coding-system-p (car (rassq encoding
429 nnrss-compatible-encoding-alist)))))
430 (mm-coding-system-p 'utf-8)))
432 (declare-function w3-parse-buffer "ext:w3-parse" (&optional buff))
434 (defun nnrss-fetch (url &optional local)
435 "Fetch URL and put it in a the expected Lisp structure."
436 (mm-with-unibyte-buffer
437 ;;some versions of url.el need this to close the connection quickly
438 (let (cs xmlform htmlform)
439 ;; bit o' work necessary for w3 pre-cvs and post-cvs
441 (let ((coding-system-for-read 'binary))
442 (insert-file-contents url))
443 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
444 ;; to `mm-url-insert'?
445 (let ((coding-system-for-read 'binary))
448 (error (if (or debug-on-quit debug-on-error)
449 (signal (car err) (cdr err))
450 (message "nnrss: Failed to fetch %s" url))))))
451 (nnheader-remove-cr-followed-by-lf)
452 ;; Decode text according to the encoding attribute.
453 (when (setq cs (nnrss-get-encoding))
455 (mm-decode-coding-string (buffer-string) cs)
457 (mm-enable-multibyte))))
458 (goto-char (point-min))
460 ;; Because xml-parse-region can't deal with anything that isn't
461 ;; xml and w3-parse-buffer can't deal with some xml, we have to
462 ;; parse with xml-parse-region first and, if that fails, parse
463 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
464 ;; why w3-parse-buffer fails to parse some well-formed xml and
468 (setq xmlform (xml-parse-region (point-min) (point-max)))
471 (setq htmlform (caddar (w3-parse-buffer
475 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
481 (defun nnrss-possibly-change-group (&optional group server)
483 (not (nnrss-server-opened server)))
484 (nnrss-open-server server))
485 (when (and group (not (equal group nnrss-group)))
486 (nnrss-read-group-data group server)
487 (setq nnrss-group group)))
489 (autoload 'timezone-parse-date "timezone")
491 (defun nnrss-normalize-date (date)
492 "Return a date string of DATE in the RFC822 style.
493 This function handles the ISO 8601 date format described in
494 URL `http://www.w3.org/TR/NOTE-datetime', and also the RFC822 style
495 which RSS 2.0 allows."
496 (let (case-fold-search vector year month day time zone cts given)
497 (cond ((null date)) ; do nothing for this case
498 ;; if the date is just digits (unix time stamp):
499 ((string-match "^[0-9]+$" date)
500 (setq given (seconds-to-time (string-to-number date))))
502 ((string-match " [0-9]+ " date)
503 (setq vector (timezone-parse-date date)
504 year (string-to-number (aref vector 0)))
506 (setq month (string-to-number (aref vector 1))
507 day (string-to-number (aref vector 2)))
508 (unless (>= (length (setq time (aref vector 3))) 3)
509 (setq time "00:00:00"))
510 (when (and (setq zone (aref vector 4))
511 (not (string-match "\\`[A-Z+-]" zone)))
518 "\\(199[0-9]\\|20[0-9][0-9]\\)"
527 "\\([012][0-9]:[0-5][0-9]\\)"
531 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
533 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
534 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
537 (setq year (string-to-number (match-string 1 date))
538 month (string-to-number (or (match-string 2 date) "1"))
539 day (string-to-number (or (match-string 3 date) "1"))
540 time (if (match-beginning 5)
541 (substring date (match-beginning 4) (match-end 5))
542 (concat (or (match-string 4 date) "00:00") ":00"))
543 zone (cond ((match-beginning 6)
544 (concat (match-string 6 date)
545 (match-string 7 date)))
546 ((match-beginning 9) ;; Z
548 (t ;; nil if zone is not provided.
549 (match-string 8 date))))))
552 (setq cts (current-time-string (encode-time 0 0 0 day month year)))
553 (format "%s, %02d %s %04d %s%s"
554 (substring cts 0 3) day (substring cts 4 7) year time
558 (message-make-date given))))
562 (defun nnrss-read-server-data (server)
563 (setq nnrss-server-data nil)
564 (let ((file (nnrss-make-filename "nnrss" server))
565 (file-name-coding-system nnmail-pathname-coding-system))
566 (when (file-exists-p file)
567 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
568 ;; file names. So, we use `insert-file-contents' instead.
569 (mm-with-multibyte-buffer
570 (let ((coding-system-for-read nnrss-file-coding-system))
571 (insert-file-contents file)
572 (eval-region (point-min) (point-max)))))))
574 (defun nnrss-save-server-data (server)
575 (gnus-make-directory nnrss-directory)
576 (let ((coding-system-for-write nnrss-file-coding-system)
577 (file-name-coding-system nnmail-pathname-coding-system))
578 (with-temp-file (nnrss-make-filename "nnrss" server)
579 (insert (format ";; -*- coding: %s; -*-\n"
580 nnrss-file-coding-system))
581 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
583 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
585 (defun nnrss-read-group-data (group server)
586 (setq nnrss-group-data nil)
587 (if (hash-table-p nnrss-group-hashtb)
588 (clrhash nnrss-group-hashtb)
589 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
590 (let ((pair (assoc group nnrss-server-data)))
591 (setq nnrss-group-max (or (cadr pair) 0))
592 (setq nnrss-group-min (+ nnrss-group-max 1)))
593 (let ((file (nnrss-make-filename group server))
594 (file-name-coding-system nnmail-pathname-coding-system))
595 (when (file-exists-p file)
596 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
597 ;; file names. So, we use `insert-file-contents' instead.
598 (mm-with-multibyte-buffer
599 (let ((coding-system-for-read nnrss-file-coding-system))
600 (insert-file-contents file)
601 (eval-region (point-min) (point-max))))
602 (dolist (e nnrss-group-data)
603 (puthash (nth 9 e) t nnrss-group-hashtb)
604 (when (and (car e) (> nnrss-group-min (car e)))
605 (setq nnrss-group-min (car e)))
606 (when (and (car e) (< nnrss-group-max (car e)))
607 (setq nnrss-group-max (car e)))))))
609 (defun nnrss-save-group-data (group server)
610 (gnus-make-directory nnrss-directory)
611 (let ((coding-system-for-write nnrss-file-coding-system)
612 (file-name-coding-system nnmail-pathname-coding-system))
613 (with-temp-file (nnrss-make-filename group server)
614 (insert (format ";; -*- coding: %s; -*-\n"
615 nnrss-file-coding-system))
616 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
618 (defun nnrss-make-filename (name server)
620 (nnrss-translate-file-chars
623 (not (equal server ""))
629 (gnus-add-shutdown 'nnrss-close 'gnus)
631 (defun nnrss-close ()
632 "Clear internal nnrss variables."
633 (setq nnrss-group-data nil
634 nnrss-server-data nil
635 nnrss-group-hashtb nil
636 nnrss-group-alist nil))
640 (defun nnrss-no-cache (url)
643 (defun nnrss-insert-w3 (url)
644 (mm-with-unibyte-current-buffer
647 (error (if (or debug-on-quit debug-on-error)
648 (signal (car err) (cdr err))
649 (message "nnrss: Failed to fetch %s" url))))))
651 (defun nnrss-decode-entities-string (string)
653 (mm-with-multibyte-buffer
655 (mm-url-decode-entities-nbsp)
658 (defalias 'nnrss-insert 'nnrss-insert-w3)
660 (defun nnrss-mime-encode-string (string)
661 (mm-with-multibyte-buffer
663 (mm-url-decode-entities-nbsp)
664 (goto-char (point-min))
665 (while (re-search-forward "[\t\n ]+" nil t)
667 (goto-char (point-min))
668 (skip-chars-forward " ")
669 (delete-region (point-min) (point))
670 (goto-char (point-max))
671 (skip-chars-forward " ")
672 (delete-region (point) (point-max))
673 (let ((rfc2047-encoding-type 'mime)
674 rfc2047-encode-max-chars)
675 (rfc2047-encode-region (point-min) (point-max)))
676 (goto-char (point-min))
677 (while (search-forward "\n" nil t)
682 (defun nnrss-make-hash-index (item)
683 (gnus-message 9 "nnrss: Making hash index of %s" (gnus-prin1-to-string item))
684 (setq item (gnus-remove-if
687 (memq (car field) nnrss-ignore-article-fields)))
689 (md5 (gnus-prin1-to-string item)
691 nnrss-file-coding-system))
693 (defun nnrss-check-group (group server)
694 (let (file xml subject url extra changed author date feed-subject
695 enclosure comments rss-ns rdf-ns content-ns dc-ns
697 (if (and nnrss-use-local
698 (file-exists-p (setq file (expand-file-name
699 (nnrss-translate-file-chars
700 (concat group ".xml"))
702 (setq xml (nnrss-fetch file t))
703 (setq url (or (nth 2 (assoc group nnrss-server-data))
704 (second (assoc group nnrss-group-alist))))
711 (format "URL to search for %s: " group) "http://")))))
712 (let ((pair (assoc group nnrss-server-data)))
714 (setcdr (cdr pair) (list url))
715 (push (list group nnrss-group-max url) nnrss-server-data)))
717 (setq xml (nnrss-fetch url)))
719 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
720 ;; for more RSS namespaces.
721 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
722 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
723 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
724 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
725 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
726 (when (and (listp item)
727 (string= (concat rss-ns "item") (car item))
728 (progn (setq hash-index (nnrss-make-hash-index item))
729 (not (gethash hash-index nnrss-group-hashtb))))
730 (setq subject (nnrss-node-text rss-ns 'title item))
731 (setq url (nnrss-decode-entities-string
732 (nnrss-node-text rss-ns 'link (cddr item))))
733 (setq extra (or (nnrss-node-text content-ns 'encoded item)
734 (nnrss-node-text rss-ns 'description item)))
735 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
736 (setq extra (concat feed-subject "<br /><br />" extra)))
737 (setq author (or (nnrss-node-text rss-ns 'author item)
738 (nnrss-node-text dc-ns 'creator item)
739 (nnrss-node-text dc-ns 'contributor item)))
740 (setq date (nnrss-normalize-date
741 (or (nnrss-node-text dc-ns 'date item)
742 (nnrss-node-text rss-ns 'pubDate item))))
743 (setq comments (nnrss-node-text rss-ns 'comments item))
744 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
745 (let ((url (cdr (assq 'url enclosure)))
746 (len (cdr (assq 'length enclosure)))
747 (type (cdr (assq 'type enclosure)))
750 (if (and len (integerp (setq len (string-to-number len))))
751 ;; actually already in `ls-lisp-format-file-size' but
752 ;; probably not worth to require it for one function
753 (do ((size (/ len 1.0) (/ size 1024.0))
754 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
757 (format "%.1f%s" size (car post-fixes))))
759 (setq url (or url ""))
760 (setq name (if (string-match "/\\([^/]*\\)$" url)
763 (setq type (or type ""))
764 (setq enclosure (list url name len type))))
767 (incf nnrss-group-max)
770 (and subject (nnrss-mime-encode-string subject))
771 (and author (nnrss-mime-encode-string author))
773 (and extra (nnrss-decode-entities-string extra))
778 (puthash hash-index t nnrss-group-hashtb)
782 (nnrss-save-group-data group server)
783 (let ((pair (assoc group nnrss-server-data)))
785 (setcar (cdr pair) nnrss-group-max)
786 (push (list group nnrss-group-max) nnrss-server-data)))
787 (nnrss-save-server-data server))))
789 (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
791 (defun nnrss-opml-import (opml-file)
792 "OPML subscriptions import.
793 Read the file and attempt to subscribe to each Feed in the file."
794 (interactive "fImport file: ")
797 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
799 (not (string-match "\\`[\t ]*\\'" xmlurl))
801 (y-or-n-p (format "Subscribe to %s " xmlurl))
805 (gnus-group-make-rss-group xmlurl)
809 "Failed to subscribe to %s (%s); type any key to continue: "
811 (error-message-string err))
812 (let ((echo-keystrokes 0))
814 (nnrss-find-el 'outline
815 (mm-with-multibyte-buffer
816 (insert-file-contents opml-file)
817 (xml-parse-region (point-min) (point-max))))))
819 (defun nnrss-opml-export ()
820 "OPML subscription export.
821 Export subscriptions to a buffer in OPML Format."
823 (with-current-buffer (get-buffer-create "*OPML Export*")
824 (mm-set-buffer-file-coding-system 'utf-8)
825 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
826 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
827 "<opml version=\"1.1\">\n"
829 " <title>mySubscriptions</title>\n"
830 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
832 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
833 " <ownerName>" (user-full-name) "</ownerName>\n"
836 (dolist (sub nnrss-group-alist)
837 (insert " <outline text=\"" (car sub)
838 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
841 (pop-to-buffer "*OPML Export*")
842 (when (fboundp 'sgml-mode)
845 (defun nnrss-generate-download-script ()
846 "Generate a download script in the current buffer.
847 It is useful when `(setq nnrss-use-local t)'."
849 (insert "#!/bin/sh\n")
850 (insert "WGET=wget\n")
851 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
852 (dolist (elem nnrss-server-data)
853 (let ((url (or (nth 2 elem)
854 (second (assoc (car elem) nnrss-group-alist)))))
855 (insert "$WGET -q -O \"$RSSDIR\"/'"
856 (nnrss-translate-file-chars (concat (car elem) ".xml"))
859 (defun nnrss-translate-file-chars (name)
860 (let ((nnheader-file-name-translation-alist
861 (append nnheader-file-name-translation-alist '((?' . ?_)))))
862 (nnheader-translate-file-chars name)))
864 (defun nnrss-node-text (namespace local-name element)
865 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
867 (text (if (and node (listp node))
868 (nnrss-node-just-text node)
870 (cleaned-text (if text
871 (gnus-replace-in-string
872 (gnus-replace-in-string
873 text "^[\000-\037\177]+\\|^ +\\| +$" "")
875 (if (string-equal "" cleaned-text)
879 (defun nnrss-node-just-text (node)
880 (if (and node (listp node))
881 (mapconcat 'nnrss-node-just-text (cddr node) " ")
884 (defun nnrss-find-el (tag data &optional found-list)
885 "Find the all matching elements in the data.
886 Careful with this on large documents!"
890 (when (equal tag (car bit))
891 ;; Old xml.el may return a list of string.
892 (when (and (consp (caddr bit))
893 (stringp (caaddr bit)))
894 (setcar (cddr bit) (caaddr bit)))
898 (if (and (consp (car-safe (caddr bit)))
899 (not (stringp (caddr bit))))
907 tag (cddr bit))))))))
910 (defun nnrss-rsslink-p (el)
911 "Test if the element we are handed is an RSS autodiscovery link."
912 (and (eq (car-safe el) 'link)
913 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
914 (or (string-equal (cdr (assoc 'type (cadr el)))
915 "application/rss+xml")
916 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
918 (defun nnrss-get-rsslinks (data)
919 "Extract the <link> elements that are links to RSS from the parsed data."
922 (if (nnrss-rsslink-p el) el))
923 (nnrss-find-el 'link data))))
925 (defun nnrss-extract-hrefs (data)
926 "Recursively extract hrefs from a page's source.
927 DATA should be the output of `xml-parse-region' or
929 (mapcar (lambda (ahref)
930 (cdr (assoc 'href (cadr ahref))))
931 (nnrss-find-el 'a data)))
933 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
934 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
935 (not (string-match "://" ,item)))
936 (setq ,onsite-list (append ,onsite-list (list ,item))))
937 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
939 (defun nnrss-order-hrefs (base-uri hrefs)
940 "Given a list of hrefs, sort them using the following priorities:
941 1. links ending in .rss
942 2. links ending in .rdf
943 3. links ending in .xml
944 4. links containing the above
947 BASE-URI is used to determine the location of the links and
948 whether they are `offsite' or `onsite'."
949 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
950 rss-onsite-in rdf-onsite-in xml-onsite-in
951 rss-offsite-end rdf-offsite-end xml-offsite-end
952 rss-offsite-in rdf-offsite-in xml-offsite-in)
955 ((string-match "\\.rss$" href)
957 base-uri href rss-onsite-end rss-offsite-end))
958 ((string-match "\\.rdf$" href)
960 base-uri href rdf-onsite-end rdf-offsite-end))
961 ((string-match "\\.xml$" href)
963 base-uri href xml-onsite-end xml-offsite-end))
964 ((string-match "rss" href)
966 base-uri href rss-onsite-in rss-offsite-in))
967 ((string-match "rdf" href)
969 base-uri href rdf-onsite-in rdf-offsite-in))
970 ((string-match "xml" href)
972 base-uri href xml-onsite-in xml-offsite-in))))
974 rss-onsite-end rdf-onsite-end xml-onsite-end
975 rss-onsite-in rdf-onsite-in xml-onsite-in
976 rss-offsite-end rdf-offsite-end xml-offsite-end
977 rss-offsite-in rdf-offsite-in xml-offsite-in)))
979 (defun nnrss-discover-feed (url)
980 "Given a page, find an RSS feed using Mark Pilgrim's
981 `ultra-liberal rss locator' (URL `http://diveintomark.org/2002/08/15.html')."
983 (let ((parsed-page (nnrss-fetch url)))
985 ;; 1. if this url is the rss, use it.
986 (if (nnrss-rss-p parsed-page)
987 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
988 (nnrss-rss-title-description rss-ns parsed-page url))
990 ;; 2. look for the <link rel="alternate"
991 ;; type="application/rss+xml" and use that if it is there.
992 (let ((links (nnrss-get-rsslinks parsed-page)))
994 (let* ((xml (nnrss-fetch
995 (cdr (assoc 'href (cadar links)))))
996 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
997 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
999 ;; 3. look for links on the site in the following order:
1000 ;; - onsite links ending in .rss, .rdf, or .xml
1001 ;; - onsite links containing any of the above
1002 ;; - offsite links ending in .rss, .rdf, or .xml
1003 ;; - offsite links containing any of the above
1004 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
1005 (match-string 0 url)))
1006 (hrefs (nnrss-order-hrefs
1007 base-uri (nnrss-extract-hrefs parsed-page)))
1009 (while (and (eq rss-link nil) (not (eq hrefs nil)))
1010 (let ((href-data (nnrss-fetch (car hrefs))))
1011 (if (nnrss-rss-p href-data)
1012 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
1013 (setq rss-link (nnrss-rss-title-description
1014 rss-ns href-data (car hrefs))))
1015 (setq hrefs (cdr hrefs)))))
1016 (if rss-link rss-link
1019 (nnrss-find-rss-via-syndic8 url))))))))
1021 (defun nnrss-find-rss-via-syndic8 (url)
1022 "Query syndic8 for the rss feeds it has for URL."
1023 (if (not (locate-library "xml-rpc"))
1025 (message "XML-RPC is not available... not checking Syndic8.")
1028 (let ((feedid (xml-rpc-method-call
1029 "http://www.syndic8.com/xmlrpc.php"
1033 (let* ((feedinfo (xml-rpc-method-call
1034 "http://www.syndic8.com/xmlrpc.php"
1035 'syndic8.GetFeedInfo
1042 (cdr (assoc "status" listinfo))
1045 (cdr (assoc "sitename" listinfo))
1049 "sitename" listinfo)))
1052 "dataurl" listinfo)))))))
1054 (if (not (> (length urllist) 1))
1056 (let ((completion-ignore-case t)
1058 (mapcar (lambda (listinfo)
1059 (cons (cdr (assoc "sitename" listinfo))
1061 (cdr (assoc "feedid" listinfo)))))
1065 "Multiple feeds found. Select one: "
1066 selection nil t) urllist)))))))))
1068 (defun nnrss-rss-p (data)
1069 "Test if DATA is an RSS feed.
1070 Simply ensures that the first element is rss or rdf."
1071 (or (eq (caar data) 'rss)
1072 (eq (caar data) 'rdf:RDF)))
1074 (defun nnrss-rss-title-description (rss-namespace data url)
1075 "Return the title of an RSS feed."
1076 (if (nnrss-rss-p data)
1077 (let ((description (intern (concat rss-namespace "description")))
1078 (title (intern (concat rss-namespace "title")))
1079 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1082 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1083 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1084 (cons 'href url)))))
1086 (defun nnrss-get-namespace-prefix (el uri)
1087 "Given EL (containing a parsed element) and URI (containing a string
1088 that gives the URI for which you want to retrieve the namespace
1089 prefix), return the prefix."
1090 (let* ((prefix (car (rassoc uri (cadar el))))
1092 (split-string (symbol-name prefix) ":")))
1093 (ns (cond ((eq (length nslist) 1) ; no prefix given
1095 ((eq (length nslist) 2) ; extract prefix
1097 (if (and ns (not (string= ns "")))
1103 ;;; nnrss.el ends here