1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 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 ;; Enable encoding of Newsgroups header in XEmacs.
230 (default-enable-multibyte-characters t)
231 (rfc2047-header-encoding-alist
232 (if (mm-coding-system-p 'utf-8)
233 (cons '("Newsgroups" . utf-8)
234 rfc2047-header-encoding-alist)
235 rfc2047-header-encoding-alist))
236 rfc2047-encode-encoded-words body fn)
237 (when (or text link enclosure comments)
239 (insert "<#multipart type=alternative>\n"
240 "<#part type=\"text/plain\">\n")
245 (if (and nnrss-wash-html-in-text-plain-parts
248 (setq fn (or (cdr (assq mm-text-html-renderer
249 mm-text-html-washer-alist))
250 mm-text-html-renderer))))
252 (narrow-to-region body (point-max))
255 (apply (car fn) (cdr fn)))
258 (re-search-forward "[^\t\n ]" nil t)
260 (delete-region body (point))
261 (goto-char (point-max))
262 (skip-chars-backward "\t\n ")
264 (delete-region (point) (point-max))
266 (while (re-search-forward "\n+" nil t)
269 ;; See `nnrss-check-group', which inserts "<br /><br />".
270 (when (search-forward "<br /><br />" nil t)
273 (replace-match "\n\n")))
275 (let ((fill-column default-fill-column)
276 (window (get-buffer-window nntp-server-buffer)))
279 (max 1 (/ (* (window-width window) 7) 8))))
280 (fill-region (point) (point-max))
281 (goto-char (point-max))
282 ;; XEmacs version of `fill-region' inserts newline.
285 (when (or link enclosure)
290 (insert (car enclosure) " "
291 (nth 2 enclosure) " "
292 (nth 3 enclosure) "\n"))
294 (insert comments "\n"))
295 (setq body (buffer-substring body (point)))
297 "<#part type=\"text/html\">\n"
298 "<html><head></head><body>\n")
302 (insert "<p><a href=\"" link "\">link</a></p>\n"))
304 (insert "<p><a href=\"" (car enclosure) "\">"
305 (cadr enclosure) "</a> " (nth 2 enclosure)
306 " " (nth 3 enclosure) "</p>\n"))
308 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
309 (insert "</body></html>\n"
317 "Content-Type: text/plain; charset=gnus-decoded\n"
318 "Content-Transfer-Encoding: 8bit\n\n"
321 3 "Warning - there might be invalid characters"))))
322 (goto-char (point-min))
323 (search-forward "\n\n")
325 (insert (format "Message-ID: <%d@%s.nnrss>\n"
327 (let ((rfc2047-encoding-type 'mime)
328 rfc2047-encode-max-chars)
329 (rfc2047-encode-string
330 (gnus-replace-in-string group "[\t\n ]+" "_")))))
331 (when nnrss-content-function
332 (funcall nnrss-content-function e group article))))
335 (nnheader-report 'nnrss err))
337 (nnheader-report 'nnrss "no such id: %d" article))
339 (nnheader-report 'nnrss "article %s retrieved" (car e))
340 ;; we return the article number.
341 (cons nnrss-group (car e))))))
343 (deffoo nnrss-request-list (&optional server)
344 (nnrss-possibly-change-group nil server)
345 (nnrss-generate-active)
348 (deffoo nnrss-open-server (server &optional defs connectionless)
349 (nnrss-read-server-data server)
350 (nnoo-change-server 'nnrss server defs)
353 (deffoo nnrss-request-expire-articles
354 (articles group &optional server force)
355 (setq group (nnrss-decode-group-name group))
356 (nnrss-possibly-change-group group server)
357 (let (e days not-expirable changed)
358 (dolist (art articles)
359 (if (and (setq e (assq art nnrss-group-data))
360 (nnmail-expired-article-p
362 (if (listp (setq days (nth 1 e))) days
363 (days-to-time (- days (time-to-days '(0 0)))))
365 (setq nnrss-group-data (delq e nnrss-group-data)
367 (push art not-expirable)))
369 (nnrss-save-group-data group server))
372 (deffoo nnrss-request-delete-group (group &optional force server)
373 (setq group (nnrss-decode-group-name group))
374 (nnrss-possibly-change-group group server)
376 ;; There may be two or more entries in `nnrss-group-alist' since
377 ;; this function didn't delete them formerly.
378 (while (setq elem (assoc group nnrss-group-alist))
379 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
380 (setq nnrss-server-data
381 (delq (assoc group nnrss-server-data) nnrss-server-data))
382 (nnrss-save-server-data server)
384 (let ((file-name-coding-system nnmail-pathname-coding-system))
385 (delete-file (nnrss-make-filename group server))))
388 (deffoo nnrss-request-list-newsgroups (&optional server)
389 (nnrss-possibly-change-group nil server)
391 (set-buffer nntp-server-buffer)
393 (dolist (elem nnrss-group-alist)
395 (insert (car elem) "\t" (third elem) "\n"))))
398 (nnoo-define-skeleton nnrss)
400 ;;; Internal functions
401 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
403 (defun nnrss-get-encoding ()
404 "Return an encoding attribute specified in the current xml contents.
405 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
406 it is used instead. If the xml contents doesn't specify the encoding,
407 return `utf-8' which is the default encoding for xml if it is available,
408 otherwise return nil."
409 (goto-char (point-min))
410 (if (re-search-forward
411 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
413 (let ((encoding (intern (downcase (or (match-string 1)
414 (match-string 2))))))
416 (mm-coding-system-p (cdr (assq encoding
417 nnrss-compatible-encoding-alist)))
418 (mm-coding-system-p encoding)
419 (mm-coding-system-p (car (rassq encoding
420 nnrss-compatible-encoding-alist)))))
421 (mm-coding-system-p 'utf-8)))
423 (declare-function w3-parse-buffer "ext:w3-parse" (&optional buff))
425 (defun nnrss-fetch (url &optional local)
426 "Fetch URL and put it in a the expected Lisp structure."
427 (mm-with-unibyte-buffer
428 ;;some CVS versions of url.el need this to close the connection quickly
429 (let (cs xmlform htmlform)
430 ;; bit o' work necessary for w3 pre-cvs and post-cvs
432 (let ((coding-system-for-read 'binary))
433 (insert-file-contents url))
434 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
435 ;; to `mm-url-insert'?
436 (let ((coding-system-for-read 'binary))
439 (error (if (or debug-on-quit debug-on-error)
440 (signal (car err) (cdr err))
441 (message "nnrss: Failed to fetch %s" url))))))
442 (nnheader-remove-cr-followed-by-lf)
443 ;; Decode text according to the encoding attribute.
444 (when (setq cs (nnrss-get-encoding))
446 (mm-decode-coding-string (buffer-string) cs)
448 (mm-enable-multibyte))))
449 (goto-char (point-min))
451 ;; Because xml-parse-region can't deal with anything that isn't
452 ;; xml and w3-parse-buffer can't deal with some xml, we have to
453 ;; parse with xml-parse-region first and, if that fails, parse
454 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
455 ;; why w3-parse-buffer fails to parse some well-formed xml and
459 (setq xmlform (xml-parse-region (point-min) (point-max)))
462 (setq htmlform (caddar (w3-parse-buffer
466 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
472 (defun nnrss-possibly-change-group (&optional group server)
474 (not (nnrss-server-opened server)))
475 (nnrss-open-server server))
476 (when (and group (not (equal group nnrss-group)))
477 (nnrss-read-group-data group server)
478 (setq nnrss-group group)))
480 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
482 (defun nnrss-generate-active ()
483 (when (y-or-n-p "Fetch extra categories? ")
484 (mapc 'funcall nnrss-extra-categories))
486 (set-buffer nntp-server-buffer)
488 (dolist (elem nnrss-group-alist)
489 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
490 (dolist (elem nnrss-server-data)
491 (unless (assoc (car elem) nnrss-group-alist)
492 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
494 (eval-and-compile (autoload 'timezone-parse-date "timezone"))
496 (defun nnrss-normalize-date (date)
497 "Return a date string of DATE in the RFC822 style.
498 This function handles the ISO 8601 date format described in
499 <URL:http://www.w3.org/TR/NOTE-datetime>, and also the RFC822 style
500 which RSS 2.0 allows."
501 (let (case-fold-search vector year month day time zone cts)
504 ((string-match " [0-9]+ " date)
505 (setq vector (timezone-parse-date date)
506 year (string-to-number (aref vector 0)))
508 (setq month (string-to-number (aref vector 1))
509 day (string-to-number (aref vector 2)))
510 (unless (>= (length (setq time (aref vector 3))) 3)
511 (setq time "00:00:00"))
512 (when (and (setq zone (aref vector 4))
513 (not (string-match "\\`[A-Z+-]" zone)))
520 "\\(199[0-9]\\|20[0-9][0-9]\\)"
529 "\\([012][0-9]:[0-5][0-9]\\)"
533 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
535 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
536 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
539 (setq year (string-to-number (match-string 1 date))
540 month (string-to-number (or (match-string 2 date) "1"))
541 day (string-to-number (or (match-string 3 date) "1"))
542 time (if (match-beginning 5)
543 (substring date (match-beginning 4) (match-end 5))
544 (concat (or (match-string 4 date) "00:00") ":00"))
545 zone (cond ((match-beginning 6)
546 (concat (match-string 6 date)
547 (match-string 7 date)))
548 ((match-beginning 9) ;; Z
550 (t ;; nil if zone is not provided.
551 (match-string 8 date))))))
554 (setq cts (current-time-string (encode-time 0 0 0 day month year)))
555 (format "%s, %02d %s %04d %s%s"
556 (substring cts 0 3) day (substring cts 4 7) year time
560 (message-make-date))))
564 (defun nnrss-read-server-data (server)
565 (setq nnrss-server-data nil)
566 (let ((file (nnrss-make-filename "nnrss" server))
567 (file-name-coding-system nnmail-pathname-coding-system))
568 (when (file-exists-p file)
569 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
570 ;; file names. So, we use `insert-file-contents' instead.
571 (mm-with-multibyte-buffer
572 (let ((coding-system-for-read nnrss-file-coding-system))
573 (insert-file-contents file)
574 (eval-region (point-min) (point-max)))))))
576 (defun nnrss-save-server-data (server)
577 (gnus-make-directory nnrss-directory)
578 (let ((coding-system-for-write nnrss-file-coding-system)
579 (file-name-coding-system nnmail-pathname-coding-system))
580 (with-temp-file (nnrss-make-filename "nnrss" server)
581 (insert (format ";; -*- coding: %s; -*-\n"
582 nnrss-file-coding-system))
583 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
585 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
587 (defun nnrss-read-group-data (group server)
588 (setq nnrss-group-data nil)
589 (if (hash-table-p nnrss-group-hashtb)
590 (clrhash nnrss-group-hashtb)
591 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
592 (let ((pair (assoc group nnrss-server-data)))
593 (setq nnrss-group-max (or (cadr pair) 0))
594 (setq nnrss-group-min (+ nnrss-group-max 1)))
595 (let ((file (nnrss-make-filename group server))
596 (file-name-coding-system nnmail-pathname-coding-system))
597 (when (file-exists-p file)
598 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
599 ;; file names. So, we use `insert-file-contents' instead.
600 (mm-with-multibyte-buffer
601 (let ((coding-system-for-read nnrss-file-coding-system))
602 (insert-file-contents file)
603 (eval-region (point-min) (point-max))))
604 (dolist (e nnrss-group-data)
605 (puthash (nth 9 e) t nnrss-group-hashtb)
606 (when (and (car e) (> nnrss-group-min (car e)))
607 (setq nnrss-group-min (car e)))
608 (when (and (car e) (< nnrss-group-max (car e)))
609 (setq nnrss-group-max (car e)))))))
611 (defun nnrss-save-group-data (group server)
612 (gnus-make-directory nnrss-directory)
613 (let ((coding-system-for-write nnrss-file-coding-system)
614 (file-name-coding-system nnmail-pathname-coding-system))
615 (with-temp-file (nnrss-make-filename group server)
616 (insert (format ";; -*- coding: %s; -*-\n"
617 nnrss-file-coding-system))
618 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
620 (defun nnrss-make-filename (name server)
622 (nnrss-translate-file-chars
625 (not (equal server ""))
631 (gnus-add-shutdown 'nnrss-close 'gnus)
633 (defun nnrss-close ()
634 "Clear internal nnrss variables."
635 (setq nnrss-group-data nil
636 nnrss-server-data nil
637 nnrss-group-hashtb nil
638 nnrss-group-alist nil))
642 (defun nnrss-no-cache (url)
645 (defun nnrss-insert-w3 (url)
646 (mm-with-unibyte-current-buffer
649 (error (if (or debug-on-quit debug-on-error)
650 (signal (car err) (cdr err))
651 (message "nnrss: Failed to fetch %s" url))))))
653 (defun nnrss-decode-entities-string (string)
655 (mm-with-multibyte-buffer
657 (mm-url-decode-entities-nbsp)
660 (defalias 'nnrss-insert 'nnrss-insert-w3)
662 (defun nnrss-mime-encode-string (string)
663 (mm-with-multibyte-buffer
665 (mm-url-decode-entities-nbsp)
666 (goto-char (point-min))
667 (while (re-search-forward "[\t\n ]+" nil t)
669 (goto-char (point-min))
670 (skip-chars-forward " ")
671 (delete-region (point-min) (point))
672 (goto-char (point-max))
673 (skip-chars-forward " ")
674 (delete-region (point) (point-max))
675 (let ((rfc2047-encoding-type 'mime)
676 rfc2047-encode-max-chars)
677 (rfc2047-encode-region (point-min) (point-max)))
678 (goto-char (point-min))
679 (while (search-forward "\n" nil t)
680 (delete-backward-char 1))
685 (defun nnrss-make-hash-index (item)
686 (setq item (gnus-remove-if
689 (memq (car field) nnrss-ignore-article-fields)))
691 (md5 (gnus-prin1-to-string item)
693 nnrss-file-coding-system))
695 (defun nnrss-check-group (group server)
696 (let (file xml subject url extra changed author date feed-subject
697 enclosure comments rss-ns rdf-ns content-ns dc-ns
699 (if (and nnrss-use-local
700 (file-exists-p (setq file (expand-file-name
701 (nnrss-translate-file-chars
702 (concat group ".xml"))
704 (setq xml (nnrss-fetch file t))
705 (setq url (or (nth 2 (assoc group nnrss-server-data))
706 (second (assoc group nnrss-group-alist))))
713 (format "URL to search for %s: " group) "http://")))))
714 (let ((pair (assoc group nnrss-server-data)))
716 (setcdr (cdr pair) (list url))
717 (push (list group nnrss-group-max url) nnrss-server-data)))
719 (setq xml (nnrss-fetch url)))
721 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
722 ;; for more RSS namespaces.
723 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
724 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
725 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
726 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
727 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
728 (when (and (listp item)
729 (string= (concat rss-ns "item") (car item))
730 (progn (setq hash-index (nnrss-make-hash-index item))
731 (not (gethash hash-index nnrss-group-hashtb))))
732 (setq subject (nnrss-node-text rss-ns 'title item))
733 (setq url (nnrss-decode-entities-string
734 (nnrss-node-text rss-ns 'link (cddr item))))
735 (setq extra (or (nnrss-node-text content-ns 'encoded item)
736 (nnrss-node-text rss-ns 'description item)))
737 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
738 (setq extra (concat feed-subject "<br /><br />" extra)))
739 (setq author (or (nnrss-node-text rss-ns 'author item)
740 (nnrss-node-text dc-ns 'creator item)
741 (nnrss-node-text dc-ns 'contributor item)))
742 (setq date (nnrss-normalize-date
743 (or (nnrss-node-text dc-ns 'date item)
744 (nnrss-node-text rss-ns 'pubDate item))))
745 (setq comments (nnrss-node-text rss-ns 'comments item))
746 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
747 (let ((url (cdr (assq 'url enclosure)))
748 (len (cdr (assq 'length enclosure)))
749 (type (cdr (assq 'type enclosure)))
752 (if (and len (integerp (setq len (string-to-number len))))
753 ;; actually already in `ls-lisp-format-file-size' but
754 ;; probably not worth to require it for one function
755 (do ((size (/ len 1.0) (/ size 1024.0))
756 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
759 (format "%.1f%s" size (car post-fixes))))
761 (setq url (or url ""))
762 (setq name (if (string-match "/\\([^/]*\\)$" url)
765 (setq type (or type ""))
766 (setq enclosure (list url name len type))))
769 (incf nnrss-group-max)
772 (and subject (nnrss-mime-encode-string subject))
773 (and author (nnrss-mime-encode-string author))
775 (and extra (nnrss-decode-entities-string extra))
780 (puthash hash-index t nnrss-group-hashtb)
784 (nnrss-save-group-data group server)
785 (let ((pair (assoc group nnrss-server-data)))
787 (setcar (cdr pair) nnrss-group-max)
788 (push (list group nnrss-group-max) nnrss-server-data)))
789 (nnrss-save-server-data server))))
791 (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
793 (defun nnrss-opml-import (opml-file)
794 "OPML subscriptions import.
795 Read the file and attempt to subscribe to each Feed in the file."
796 (interactive "fImport file: ")
799 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
801 (not (string-match "\\`[\t ]*\\'" xmlurl))
803 (y-or-n-p (format "Subscribe to %s " xmlurl))
807 (gnus-group-make-rss-group xmlurl)
811 "Failed to subscribe to %s (%s); type any key to continue: "
813 (error-message-string err))
814 (let ((echo-keystrokes 0))
816 (nnrss-find-el 'outline
817 (mm-with-multibyte-buffer
818 (insert-file-contents opml-file)
819 (xml-parse-region (point-min) (point-max))))))
821 (defun nnrss-opml-export ()
822 "OPML subscription export.
823 Export subscriptions to a buffer in OPML Format."
825 (with-current-buffer (get-buffer-create "*OPML Export*")
826 (mm-set-buffer-file-coding-system 'utf-8)
827 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
828 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
829 "<opml version=\"1.1\">\n"
831 " <title>mySubscriptions</title>\n"
832 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
834 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
835 " <ownerName>" (user-full-name) "</ownerName>\n"
838 (dolist (sub nnrss-group-alist)
839 (insert " <outline text=\"" (car sub)
840 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
843 (pop-to-buffer "*OPML Export*")
844 (when (fboundp 'sgml-mode)
847 (defun nnrss-generate-download-script ()
848 "Generate a download script in the current buffer.
849 It is useful when `(setq nnrss-use-local t)'."
851 (insert "#!/bin/sh\n")
852 (insert "WGET=wget\n")
853 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
854 (dolist (elem nnrss-server-data)
855 (let ((url (or (nth 2 elem)
856 (second (assoc (car elem) nnrss-group-alist)))))
857 (insert "$WGET -q -O \"$RSSDIR\"/'"
858 (nnrss-translate-file-chars (concat (car elem) ".xml"))
861 (defun nnrss-translate-file-chars (name)
862 (let ((nnheader-file-name-translation-alist
863 (append nnheader-file-name-translation-alist '((?' . ?_)))))
864 (nnheader-translate-file-chars name)))
866 (defvar nnrss-moreover-url
867 "http://w.moreover.com/categories/category_list_rss.html"
868 "The url of moreover.com categories.")
870 (defun nnrss-snarf-moreover-categories ()
871 "Snarf RSS links from moreover.com."
873 (let (category name url changed)
875 (nnrss-insert nnrss-moreover-url)
876 (goto-char (point-min))
877 (while (re-search-forward
878 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
880 (setq category (match-string 1))
881 (setq url (match-string 2)
882 name (mm-url-decode-entities-string
883 (rfc2231-decode-encoded-string
886 (setq name (concat category "." name)))
887 (unless (assoc name nnrss-server-data)
889 (push (list name 0 url) nnrss-server-data)))))
891 (nnrss-save-server-data ""))))
893 (defun nnrss-node-text (namespace local-name element)
894 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
896 (text (if (and node (listp node))
897 (nnrss-node-just-text node)
899 (cleaned-text (if text
900 (gnus-replace-in-string
901 (gnus-replace-in-string
902 text "^[\000-\037\177]+\\|^ +\\| +$" "")
904 (if (string-equal "" cleaned-text)
908 (defun nnrss-node-just-text (node)
909 (if (and node (listp node))
910 (mapconcat 'nnrss-node-just-text (cddr node) " ")
913 (defun nnrss-find-el (tag data &optional found-list)
914 "Find the all matching elements in the data.
915 Careful with this on large documents!"
919 (when (equal tag (car bit))
920 ;; Old xml.el may return a list of string.
921 (when (and (consp (caddr bit))
922 (stringp (caaddr bit)))
923 (setcar (cddr bit) (caaddr bit)))
927 (if (and (consp (car-safe (caddr bit)))
928 (not (stringp (caddr bit))))
936 tag (cddr bit))))))))
939 (defun nnrss-rsslink-p (el)
940 "Test if the element we are handed is an RSS autodiscovery link."
941 (and (eq (car-safe el) 'link)
942 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
943 (or (string-equal (cdr (assoc 'type (cadr el)))
944 "application/rss+xml")
945 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
947 (defun nnrss-get-rsslinks (data)
948 "Extract the <link> elements that are links to RSS from the parsed data."
951 (if (nnrss-rsslink-p el) el))
952 (nnrss-find-el 'link data))))
954 (defun nnrss-extract-hrefs (data)
955 "Recursively extract hrefs from a page's source.
956 DATA should be the output of `xml-parse-region' or
958 (mapcar (lambda (ahref)
959 (cdr (assoc 'href (cadr ahref))))
960 (nnrss-find-el 'a data)))
962 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
963 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
964 (not (string-match "://" ,item)))
965 (setq ,onsite-list (append ,onsite-list (list ,item))))
966 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
968 (defun nnrss-order-hrefs (base-uri hrefs)
969 "Given a list of hrefs, sort them using the following priorities:
970 1. links ending in .rss
971 2. links ending in .rdf
972 3. links ending in .xml
973 4. links containing the above
976 BASE-URI is used to determine the location of the links and
977 whether they are `offsite' or `onsite'."
978 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
979 rss-onsite-in rdf-onsite-in xml-onsite-in
980 rss-offsite-end rdf-offsite-end xml-offsite-end
981 rss-offsite-in rdf-offsite-in xml-offsite-in)
984 ((string-match "\\.rss$" href)
986 base-uri href rss-onsite-end rss-offsite-end))
987 ((string-match "\\.rdf$" href)
989 base-uri href rdf-onsite-end rdf-offsite-end))
990 ((string-match "\\.xml$" href)
992 base-uri href xml-onsite-end xml-offsite-end))
993 ((string-match "rss" href)
995 base-uri href rss-onsite-in rss-offsite-in))
996 ((string-match "rdf" href)
998 base-uri href rdf-onsite-in rdf-offsite-in))
999 ((string-match "xml" href)
1001 base-uri href xml-onsite-in xml-offsite-in))))
1003 rss-onsite-end rdf-onsite-end xml-onsite-end
1004 rss-onsite-in rdf-onsite-in xml-onsite-in
1005 rss-offsite-end rdf-offsite-end xml-offsite-end
1006 rss-offsite-in rdf-offsite-in xml-offsite-in)))
1008 (defun nnrss-discover-feed (url)
1009 "Given a page, find an RSS feed using Mark Pilgrim's
1010 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
1012 (let ((parsed-page (nnrss-fetch url)))
1014 ;; 1. if this url is the rss, use it.
1015 (if (nnrss-rss-p parsed-page)
1016 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
1017 (nnrss-rss-title-description rss-ns parsed-page url))
1019 ;; 2. look for the <link rel="alternate"
1020 ;; type="application/rss+xml" and use that if it is there.
1021 (let ((links (nnrss-get-rsslinks parsed-page)))
1023 (let* ((xml (nnrss-fetch
1024 (cdr (assoc 'href (cadar links)))))
1025 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
1026 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
1028 ;; 3. look for links on the site in the following order:
1029 ;; - onsite links ending in .rss, .rdf, or .xml
1030 ;; - onsite links containing any of the above
1031 ;; - offsite links ending in .rss, .rdf, or .xml
1032 ;; - offsite links containing any of the above
1033 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
1034 (match-string 0 url)))
1035 (hrefs (nnrss-order-hrefs
1036 base-uri (nnrss-extract-hrefs parsed-page)))
1038 (while (and (eq rss-link nil) (not (eq hrefs nil)))
1039 (let ((href-data (nnrss-fetch (car hrefs))))
1040 (if (nnrss-rss-p href-data)
1041 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
1042 (setq rss-link (nnrss-rss-title-description
1043 rss-ns href-data (car hrefs))))
1044 (setq hrefs (cdr hrefs)))))
1045 (if rss-link rss-link
1048 (nnrss-find-rss-via-syndic8 url))))))))
1050 (defun nnrss-find-rss-via-syndic8 (url)
1051 "Query syndic8 for the rss feeds it has for URL."
1052 (if (not (locate-library "xml-rpc"))
1054 (message "XML-RPC is not available... not checking Syndic8.")
1057 (let ((feedid (xml-rpc-method-call
1058 "http://www.syndic8.com/xmlrpc.php"
1062 (let* ((feedinfo (xml-rpc-method-call
1063 "http://www.syndic8.com/xmlrpc.php"
1064 'syndic8.GetFeedInfo
1071 (cdr (assoc "status" listinfo))
1074 (cdr (assoc "sitename" listinfo))
1078 "sitename" listinfo)))
1081 "dataurl" listinfo)))))))
1083 (if (not (> (length urllist) 1))
1085 (let ((completion-ignore-case t)
1087 (mapcar (lambda (listinfo)
1088 (cons (cdr (assoc "sitename" listinfo))
1090 (cdr (assoc "feedid" listinfo)))))
1094 "Multiple feeds found. Select one: "
1095 selection nil t) urllist)))))))))
1097 (defun nnrss-rss-p (data)
1098 "Test if DATA is an RSS feed.
1099 Simply ensures that the first element is rss or rdf."
1100 (or (eq (caar data) 'rss)
1101 (eq (caar data) 'rdf:RDF)))
1103 (defun nnrss-rss-title-description (rss-namespace data url)
1104 "Return the title of an RSS feed."
1105 (if (nnrss-rss-p data)
1106 (let ((description (intern (concat rss-namespace "description")))
1107 (title (intern (concat rss-namespace "title")))
1108 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1111 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1112 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1113 (cons 'href url)))))
1115 (defun nnrss-get-namespace-prefix (el uri)
1116 "Given EL (containing a parsed element) and URI (containing a string
1117 that gives the URI for which you want to retrieve the namespace
1118 prefix), return the prefix."
1119 (let* ((prefix (car (rassoc uri (cadar el))))
1121 (split-string (symbol-name prefix) ":")))
1122 (ns (cond ((eq (length nslist) 1) ; no prefix given
1124 ((eq (length nslist) 2) ; extract prefix
1126 (if (and ns (not (string= ns "")))
1132 ;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267
1133 ;;; nnrss.el ends here