1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl))
44 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
45 "Where nnrss will save its files.")
47 (defvoo nnrss-ignore-article-fields '(slash:comments)
48 "*List of fields that should be ignored when comparing RSS articles.
49 Some RSS feeds update article fields during their lives, e.g. to
50 indicate the number of comments or the number of times the
51 articles have been seen. However, if there is a difference
52 between the local article and the distant one, the latter is
53 considered to be new. To avoid this and discard some fields, set
54 this variable to the list of fields to be ignored.")
56 ;; (group max rss-url)
57 (defvoo nnrss-server-data nil)
59 ;; (num timestamp url subject author date extra)
60 (defvoo nnrss-group-data nil)
61 (defvoo nnrss-group-max 0)
62 (defvoo nnrss-group-min 1)
63 (defvoo nnrss-group nil)
64 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
65 (defvoo nnrss-status-string "")
67 (defconst nnrss-version "nnrss 1.0")
69 (defvar nnrss-group-alist '()
70 "List of RSS addresses.")
72 (defvar nnrss-use-local nil
73 "If non-nil nnrss will read the feeds from local files in nnrss-directory.")
75 (defvar nnrss-description-field 'X-Gnus-Description
76 "Field name used for DESCRIPTION.
77 To use the description in headers, put this name into `nnmail-extra-headers'.")
79 (defvar nnrss-url-field 'X-Gnus-Url
80 "Field name used for URL.
81 To use the description in headers, put this name into `nnmail-extra-headers'.")
83 (defvar nnrss-content-function nil
84 "A function which is called in `nnrss-request-article'.
85 The arguments are (ENTRY GROUP ARTICLE).
86 ENTRY is the record of the current headline. GROUP is the group name.
87 ARTICLE is the article number of the current headline.")
89 (defvar nnrss-file-coding-system mm-universal-coding-system
90 "*Coding system used when reading and writing files.
91 If you run Gnus with various versions of Emacsen, the value of this
92 variable should be the coding system that all those Emacsen support.
93 Note that you have to regenerate all the nnrss groups if you change
94 the value. Moreover, you should be patient even if you are made to
95 read the same articles twice, that arises for the difference of the
98 (defvar nnrss-compatible-encoding-alist
99 (delq nil (mapcar (lambda (elem)
100 (if (and (mm-coding-system-p (car elem))
101 (mm-coding-system-p (cdr elem)))
103 mm-charset-override-alist))
104 "Alist of encodings and those supersets.
105 The cdr of each element is used to decode data if it is available when
106 the car is what the data specify as the encoding. Or, the car is used
107 for decoding when the cdr that the data specify is not available.")
109 (nnoo-define-basics nnrss)
111 ;;; Interface functions
113 (defsubst nnrss-format-string (string)
114 (gnus-replace-in-string string " *\n *" " "))
116 (defun nnrss-decode-group-name (group)
117 (if (and group (mm-coding-system-p 'utf-8))
118 (setq group (mm-decode-coding-string group 'utf-8))
121 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
122 (setq group (nnrss-decode-group-name group))
123 (nnrss-possibly-change-group group server)
125 (with-current-buffer nntp-server-buffer
127 (dolist (article articles)
128 (if (setq e (assq article nnrss-group-data))
129 (insert (number-to-string (car e)) "\t" ;; number
134 (or (nth 4 e) "(nobody)")
140 (format "<%d@%s.nnrss>" (car e) group)
151 (memq nnrss-description-field
152 nnmail-extra-headers))
153 (concat (symbol-name nnrss-description-field)
155 (nnrss-format-string (nth 6 e))
159 (memq nnrss-url-field
160 nnmail-extra-headers))
161 (concat (symbol-name nnrss-url-field)
163 (nnrss-format-string (nth 2 e))
169 (deffoo nnrss-request-group (group &optional server dont-check info)
170 (setq group (nnrss-decode-group-name group))
171 (nnheader-message 6 "nnrss: Requesting %s..." group)
172 (nnrss-possibly-change-group group server)
176 (nnrss-check-group group server)
177 (nnheader-report 'nnrss "Opened group %s" group)
179 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
180 (prin1-to-string group)
182 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
184 (deffoo nnrss-close-group (group &optional server)
187 (deffoo nnrss-request-article (article &optional group server buffer)
188 (setq group (nnrss-decode-group-name group))
189 (when (stringp article)
190 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
191 (string-to-number (match-string 1 article))
193 (nnrss-possibly-change-group group server)
194 (let ((e (assq article nnrss-group-data))
195 (nntp-server-buffer (or buffer nntp-server-buffer))
198 (with-current-buffer nntp-server-buffer
201 (insert "Newsgroups: " group "\n"))
203 (insert "Subject: " (nth 3 e) "\n"))
205 (insert "From: " (nth 4 e) "\n"))
207 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
208 (let ((header (buffer-string))
211 (enclosure (nth 7 e))
213 (rfc2047-header-encoding-alist
214 (if (mm-coding-system-p 'utf-8)
215 (cons '("Newsgroups" . utf-8)
216 rfc2047-header-encoding-alist)
217 rfc2047-header-encoding-alist))
218 rfc2047-encode-encoded-words body fn)
219 (when (or text link enclosure comments)
221 (insert "<#multipart type=alternative>\n"
222 "<#part type=\"text/plain\">\n")
227 (while (re-search-forward "\n+" nil t)
230 ;; See `nnrss-check-group', which inserts "<br /><br />".
231 (when (search-forward "<br /><br />" nil t)
234 (replace-match "\n\n")))
236 (let ((fill-column (default-value 'fill-column))
237 (window (get-buffer-window nntp-server-buffer)))
240 (max 1 (/ (* (window-width window) 7) 8))))
241 (fill-region (point) (point-max))
242 (goto-char (point-max))
243 ;; XEmacs version of `fill-region' inserts newline.
246 (when (or link enclosure)
251 (insert (car enclosure) " "
252 (nth 2 enclosure) " "
253 (nth 3 enclosure) "\n"))
255 (insert comments "\n"))
256 (setq body (buffer-substring body (point)))
258 "<#part type=\"text/html\">\n"
259 "<html><head></head><body>\n")
263 (insert "<p><a href=\"" link "\">link</a></p>\n"))
265 (insert "<p><a href=\"" (car enclosure) "\">"
266 (cadr enclosure) "</a> " (nth 2 enclosure)
267 " " (nth 3 enclosure) "</p>\n"))
269 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
270 (insert "</body></html>\n"
274 ;; Allow `mml-to-mime' to generate MIME article without
275 ;; making inquiry to a user for unknown encoding.
276 (let ((mml-confirmation-set
277 (cons 'unknown-encoding mml-confirmation-set)))
282 "Content-Type: text/plain; charset=gnus-decoded\n"
283 "Content-Transfer-Encoding: 8bit\n\n"
286 3 "Warning - there might be invalid characters"))))
287 (goto-char (point-min))
288 (search-forward "\n\n")
290 (insert (format "Message-ID: <%d@%s.nnrss>\n"
292 (let ((rfc2047-encoding-type 'mime)
293 rfc2047-encode-max-chars)
294 (rfc2047-encode-string
295 (gnus-replace-in-string group "[\t\n ]+" "_")))))
296 (when nnrss-content-function
297 (funcall nnrss-content-function e group article))))
300 (nnheader-report 'nnrss err))
302 (nnheader-report 'nnrss "no such id: %d" article))
304 (nnheader-report 'nnrss "article %s retrieved" (car e))
305 ;; we return the article number.
306 (cons nnrss-group (car e))))))
308 (deffoo nnrss-open-server (server &optional defs connectionless)
309 (nnrss-read-server-data server)
310 (nnoo-change-server 'nnrss server defs)
313 (deffoo nnrss-request-expire-articles
314 (articles group &optional server force)
315 (setq group (nnrss-decode-group-name group))
316 (nnrss-possibly-change-group group server)
317 (let (e days not-expirable changed)
318 (dolist (art articles)
319 (if (and (setq e (assq art nnrss-group-data))
320 (nnmail-expired-article-p
322 (if (listp (setq days (nth 1 e))) days
323 (days-to-time (- days (time-to-days '(0 0)))))
325 (setq nnrss-group-data (delq e nnrss-group-data)
327 (push art not-expirable)))
329 (nnrss-save-group-data group server))
332 (deffoo nnrss-request-delete-group (group &optional force server)
333 (setq group (nnrss-decode-group-name group))
334 (nnrss-possibly-change-group group server)
336 ;; There may be two or more entries in `nnrss-group-alist' since
337 ;; this function didn't delete them formerly.
338 (while (setq elem (assoc group nnrss-group-alist))
339 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
340 (setq nnrss-server-data
341 (delq (assoc group nnrss-server-data) nnrss-server-data))
342 (nnrss-save-server-data server)
344 (let ((file-name-coding-system nnmail-pathname-coding-system))
345 (delete-file (nnrss-make-filename group server))))
348 (deffoo nnrss-request-list-newsgroups (&optional server)
349 (nnrss-possibly-change-group nil server)
350 (with-current-buffer nntp-server-buffer
352 (dolist (elem nnrss-group-alist)
354 (insert (car elem) "\t" (third elem) "\n"))))
357 (deffoo nnrss-retrieve-groups (groups &optional server)
358 (dolist (group groups)
359 (setq group (nnrss-decode-group-name group))
360 (nnrss-possibly-change-group group server)
361 (nnrss-check-group group server))
362 (with-current-buffer nntp-server-buffer
364 (dolist (group groups)
365 (let ((elem (assoc (gnus-group-decoded-name group) nnrss-server-data)))
366 (insert (format "%S %s 1 y\n" group (or (cadr elem) 0)))))
369 (nnoo-define-skeleton nnrss)
371 ;;; Internal functions
372 (defun nnrss-get-encoding ()
373 "Return an encoding attribute specified in the current xml contents.
374 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
375 it is used instead. If the xml contents doesn't specify the encoding,
376 return `utf-8' which is the default encoding for xml if it is available,
377 otherwise return nil."
378 (goto-char (point-min))
379 (if (re-search-forward
380 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
382 (let ((encoding (intern (downcase (or (match-string 1)
383 (match-string 2))))))
385 (mm-coding-system-p (cdr (assq encoding
386 nnrss-compatible-encoding-alist)))
387 (mm-coding-system-p encoding)
388 (mm-coding-system-p (car (rassq encoding
389 nnrss-compatible-encoding-alist)))))
390 (mm-coding-system-p 'utf-8)))
392 (declare-function libxml-parse-html-region "xml.c"
393 (start end &optional base-url discard-comments))
394 (defun nnrss-fetch (url &optional local)
395 "Fetch URL and put it in a the expected Lisp structure."
396 (mm-with-unibyte-buffer
397 ;;some versions of url.el need this to close the connection quickly
398 (let (cs xmlform htmlform)
399 ;; bit o' work necessary for w3 pre-cvs and post-cvs
401 (let ((coding-system-for-read 'binary))
402 (insert-file-contents url))
403 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
404 ;; to `mm-url-insert'?
405 (let ((coding-system-for-read 'binary))
408 (error (if (or debug-on-quit debug-on-error)
409 (signal (car err) (cdr err))
410 (message "nnrss: Failed to fetch %s" url))))))
411 (nnheader-remove-cr-followed-by-lf)
412 ;; Decode text according to the encoding attribute.
413 (when (setq cs (nnrss-get-encoding))
415 (mm-decode-coding-string (buffer-string) cs)
417 (mm-enable-multibyte))))
418 (goto-char (point-min))
421 (setq xmlform (xml-parse-region (point-min) (point-max)))
424 (setq htmlform (libxml-parse-html-region (point-min) (point-max)))
427 nnrss: %s: Not valid XML %s and libxml-parse-html-region doesn't work %s"
433 (defun nnrss-possibly-change-group (&optional group server)
435 (not (nnrss-server-opened server)))
436 (nnrss-open-server server))
437 (when (and group (not (equal group nnrss-group)))
438 (nnrss-read-group-data group server)
439 (setq nnrss-group group)))
441 (autoload 'timezone-parse-date "timezone")
443 (defun nnrss-normalize-date (date)
444 "Return a date string of DATE in the RFC822 style.
445 This function handles the ISO 8601 date format described in
446 URL `http://www.w3.org/TR/NOTE-datetime', and also the RFC822 style
447 which RSS 2.0 allows."
448 (let (case-fold-search vector year month day time zone cts given)
449 (cond ((null date)) ; do nothing for this case
450 ;; if the date is just digits (unix time stamp):
451 ((string-match "^[0-9]+$" date)
452 (setq given (seconds-to-time (string-to-number date))))
454 ((string-match " [0-9]+ " date)
455 (setq vector (timezone-parse-date date)
456 year (string-to-number (aref vector 0)))
458 (setq month (string-to-number (aref vector 1))
459 day (string-to-number (aref vector 2)))
460 (unless (>= (length (setq time (aref vector 3))) 3)
461 (setq time "00:00:00"))
462 (when (and (setq zone (aref vector 4))
463 (not (string-match "\\`[A-Z+-]" zone)))
470 "\\(199[0-9]\\|20[0-9][0-9]\\)"
479 "\\([012][0-9]:[0-5][0-9]\\)"
483 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
485 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
486 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
489 (setq year (string-to-number (match-string 1 date))
490 month (string-to-number (or (match-string 2 date) "1"))
491 day (string-to-number (or (match-string 3 date) "1"))
492 time (if (match-beginning 5)
493 (substring date (match-beginning 4) (match-end 5))
494 (concat (or (match-string 4 date) "00:00") ":00"))
495 zone (cond ((match-beginning 6)
496 (concat (match-string 6 date)
497 (match-string 7 date)))
498 ((match-beginning 9) ;; Z
500 (t ;; nil if zone is not provided.
501 (match-string 8 date))))))
504 (setq cts (current-time-string (encode-time 0 0 0 day month year)))
505 (format "%s, %02d %s %04d %s%s"
506 (substring cts 0 3) day (substring cts 4 7) year time
510 (message-make-date given))))
514 (defun nnrss-read-server-data (server)
515 (setq nnrss-server-data nil)
516 (let ((file (nnrss-make-filename "nnrss" server))
517 (file-name-coding-system nnmail-pathname-coding-system))
518 (when (file-exists-p file)
519 (load file nil t t))))
521 (defun nnrss-save-server-data (server)
522 (gnus-make-directory nnrss-directory)
523 (let ((coding-system-for-write nnrss-file-coding-system)
524 (file-name-coding-system nnmail-pathname-coding-system))
525 (with-temp-file (nnrss-make-filename "nnrss" server)
526 (insert (format ";; -*- coding: %s; -*-\n"
527 nnrss-file-coding-system))
528 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
530 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
532 (defun nnrss-read-group-data (group server)
533 (setq nnrss-group-data nil)
534 (if (hash-table-p nnrss-group-hashtb)
535 (clrhash nnrss-group-hashtb)
536 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
537 (let ((pair (assoc group nnrss-server-data)))
538 (setq nnrss-group-max (or (cadr pair) 0))
539 (setq nnrss-group-min (+ nnrss-group-max 1)))
540 (let ((file (nnrss-make-filename group server))
541 (file-name-coding-system nnmail-pathname-coding-system))
542 (when (file-exists-p file)
544 (dolist (e nnrss-group-data)
545 (puthash (nth 9 e) t nnrss-group-hashtb)
546 (when (and (car e) (> nnrss-group-min (car e)))
547 (setq nnrss-group-min (car e)))
548 (when (and (car e) (< nnrss-group-max (car e)))
549 (setq nnrss-group-max (car e)))))))
551 (defun nnrss-save-group-data (group server)
552 (gnus-make-directory nnrss-directory)
553 (let ((coding-system-for-write nnrss-file-coding-system)
554 (file-name-coding-system nnmail-pathname-coding-system))
555 (with-temp-file (nnrss-make-filename group server)
556 (insert (format ";; -*- coding: %s; -*-\n"
557 nnrss-file-coding-system))
558 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
560 (defun nnrss-make-filename (name server)
562 (nnrss-translate-file-chars
565 (not (equal server ""))
571 (gnus-add-shutdown 'nnrss-close 'gnus)
573 (defun nnrss-close ()
574 "Clear internal nnrss variables."
575 (setq nnrss-group-data nil
576 nnrss-server-data nil
577 nnrss-group-hashtb nil
578 nnrss-group-alist nil))
582 (defun nnrss-no-cache (url)
585 (defun nnrss-insert (url)
586 (mm-with-unibyte-current-buffer
589 (error (if (or debug-on-quit debug-on-error)
590 (signal (car err) (cdr err))
591 (message "nnrss: Failed to fetch %s" url))))))
593 (defun nnrss-decode-entities-string (string)
595 (mm-with-multibyte-buffer
597 (mm-url-decode-entities-nbsp)
600 (defun nnrss-mime-encode-string (string)
601 (mm-with-multibyte-buffer
603 (mm-url-decode-entities-nbsp)
604 (goto-char (point-min))
605 (while (re-search-forward "[\t\n ]+" nil t)
607 (goto-char (point-min))
608 (skip-chars-forward " ")
609 (delete-region (point-min) (point))
610 (goto-char (point-max))
611 (skip-chars-forward " ")
612 (delete-region (point) (point-max))
613 (let ((rfc2047-encoding-type 'mime)
614 rfc2047-encode-max-chars)
615 (rfc2047-encode-region (point-min) (point-max)))
616 (goto-char (point-min))
617 (while (search-forward "\n" nil t)
622 (defun nnrss-make-hash-index (item)
623 (gnus-message 9 "nnrss: Making hash index of %s" (gnus-prin1-to-string item))
624 (setq item (gnus-remove-if
627 (memq (car field) nnrss-ignore-article-fields)))
629 (md5 (gnus-prin1-to-string item)
631 nnrss-file-coding-system))
633 (defun nnrss-check-group (group server)
634 (let (file xml subject url extra changed author date feed-subject
635 enclosure comments rss-ns rdf-ns content-ns dc-ns
637 (if (and nnrss-use-local
638 (file-exists-p (setq file (expand-file-name
639 (nnrss-translate-file-chars
640 (concat group ".xml"))
642 (setq xml (nnrss-fetch file t))
643 (setq url (or (nth 2 (assoc group nnrss-server-data))
644 (second (assoc group nnrss-group-alist))))
651 (format "URL to search for %s: " group) "http://")))))
652 (let ((pair (assoc group nnrss-server-data)))
654 (setcdr (cdr pair) (list url))
655 (push (list group nnrss-group-max url) nnrss-server-data)))
657 (setq xml (nnrss-fetch url)))
658 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
659 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
660 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
661 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
662 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
663 (when (and (listp item)
664 (string= (concat rss-ns "item") (car item))
665 (progn (setq hash-index (nnrss-make-hash-index item))
666 (not (gethash hash-index nnrss-group-hashtb))))
667 (setq subject (nnrss-node-text rss-ns 'title item))
668 (setq url (nnrss-decode-entities-string
669 (nnrss-node-text rss-ns 'link (cddr item))))
670 (setq extra (or (nnrss-node-text content-ns 'encoded item)
671 (nnrss-node-text rss-ns 'description item)))
672 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
673 (setq extra (concat feed-subject "<br /><br />" extra)))
674 (setq author (or (nnrss-node-text rss-ns 'author item)
675 (nnrss-node-text dc-ns 'creator item)
676 (nnrss-node-text dc-ns 'contributor item)))
677 (setq date (nnrss-normalize-date
678 (or (nnrss-node-text dc-ns 'date item)
679 (nnrss-node-text rss-ns 'pubDate item))))
680 (setq comments (nnrss-node-text rss-ns 'comments item))
681 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
682 (let ((url (cdr (assq 'url enclosure)))
683 (len (cdr (assq 'length enclosure)))
684 (type (cdr (assq 'type enclosure)))
687 (if (and len (integerp (setq len (string-to-number len))))
688 ;; actually already in `ls-lisp-format-file-size' but
689 ;; probably not worth to require it for one function
690 (do ((size (/ len 1.0) (/ size 1024.0))
691 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
694 (format "%.1f%s" size (car post-fixes))))
696 (setq url (or url ""))
697 (setq name (if (string-match "/\\([^/]*\\)$" url)
700 (setq type (or type ""))
701 (setq enclosure (list url name len type))))
704 (incf nnrss-group-max)
707 (and subject (nnrss-mime-encode-string subject))
708 (and author (nnrss-mime-encode-string author))
710 (and extra (nnrss-decode-entities-string extra))
715 (puthash hash-index t nnrss-group-hashtb)
719 (nnrss-save-group-data group server)
720 (let ((pair (assoc group nnrss-server-data)))
722 (setcar (cdr pair) nnrss-group-max)
723 (push (list group nnrss-group-max) nnrss-server-data)))
724 (nnrss-save-server-data server))))
726 (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
728 (defun nnrss-opml-import (opml-file)
729 "OPML subscriptions import.
730 Read the file and attempt to subscribe to each Feed in the file."
731 (interactive "fImport file: ")
734 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
736 (not (string-match "\\`[\t ]*\\'" xmlurl))
738 (y-or-n-p (format "Subscribe to %s " xmlurl))
742 (gnus-group-make-rss-group xmlurl)
746 "Failed to subscribe to %s (%s); type any key to continue: "
748 (error-message-string err))
749 (let ((echo-keystrokes 0))
751 (nnrss-find-el 'outline
752 (mm-with-multibyte-buffer
753 (insert-file-contents opml-file)
754 (xml-parse-region (point-min) (point-max))))))
756 (defun nnrss-opml-export ()
757 "OPML subscription export.
758 Export subscriptions to a buffer in OPML Format."
760 (with-current-buffer (get-buffer-create "*OPML Export*")
761 (mm-set-buffer-file-coding-system 'utf-8)
762 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
763 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
764 "<opml version=\"1.1\">\n"
766 " <title>mySubscriptions</title>\n"
767 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
769 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
770 " <ownerName>" (user-full-name) "</ownerName>\n"
773 (dolist (sub nnrss-group-alist)
774 (insert " <outline text=\"" (car sub)
775 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
778 (pop-to-buffer "*OPML Export*")
779 (when (fboundp 'sgml-mode)
782 (defun nnrss-generate-download-script ()
783 "Generate a download script in the current buffer.
784 It is useful when `(setq nnrss-use-local t)'."
786 (insert "#!/bin/sh\n")
787 (insert "WGET=wget\n")
788 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
789 (dolist (elem nnrss-server-data)
790 (let ((url (or (nth 2 elem)
791 (second (assoc (car elem) nnrss-group-alist)))))
792 (insert "$WGET -q -O \"$RSSDIR\"/'"
793 (nnrss-translate-file-chars (concat (car elem) ".xml"))
796 (defun nnrss-translate-file-chars (name)
797 (let ((nnheader-file-name-translation-alist
798 (append nnheader-file-name-translation-alist '((?' . ?_)))))
799 (nnheader-translate-file-chars name)))
801 (defun nnrss-node-text (namespace local-name element)
802 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
804 (text (if (and node (listp node))
805 (nnrss-node-just-text node)
807 (cleaned-text (if text
808 (gnus-replace-in-string
809 (gnus-replace-in-string
810 text "^[\000-\037\177]+\\|^ +\\| +$" "")
812 (if (string-equal "" cleaned-text)
816 (defun nnrss-node-just-text (node)
817 (if (and node (listp node))
818 (mapconcat 'nnrss-node-just-text (cddr node) " ")
821 (defun nnrss-find-el (tag data &optional found-list)
822 "Find the all matching elements in the data.
823 Careful with this on large documents!"
827 (when (equal tag (car bit))
828 ;; Old xml.el may return a list of string.
829 (when (and (consp (caddr bit))
830 (stringp (caaddr bit)))
831 (setcar (cddr bit) (caaddr bit)))
835 (if (and (consp (car-safe (caddr bit)))
836 (not (stringp (caddr bit))))
844 tag (cddr bit))))))))
847 (defun nnrss-rsslink-p (el)
848 "Test if the element we are handed is an RSS autodiscovery link."
849 (and (eq (car-safe el) 'link)
850 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
851 (or (string-equal (cdr (assoc 'type (cadr el)))
852 "application/rss+xml")
853 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
855 (defun nnrss-get-rsslinks (data)
856 "Extract the <link> elements that are links to RSS from the parsed data."
859 (if (nnrss-rsslink-p el) el))
860 (nnrss-find-el 'link data))))
862 (defun nnrss-extract-hrefs (data)
863 "Recursively extract hrefs from a page's source.
864 DATA should be the output of `xml-parse-region'."
865 (mapcar (lambda (ahref)
866 (cdr (assoc 'href (cadr ahref))))
867 (nnrss-find-el 'a data)))
869 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
870 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
871 (not (string-match "://" ,item)))
872 (setq ,onsite-list (append ,onsite-list (list ,item))))
873 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
875 (defun nnrss-order-hrefs (base-uri hrefs)
876 "Given a list of hrefs, sort them using the following priorities:
877 1. links ending in .rss
878 2. links ending in .rdf
879 3. links ending in .xml
880 4. links containing the above
883 BASE-URI is used to determine the location of the links and
884 whether they are `offsite' or `onsite'."
885 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
886 rss-onsite-in rdf-onsite-in xml-onsite-in
887 rss-offsite-end rdf-offsite-end xml-offsite-end
888 rss-offsite-in rdf-offsite-in xml-offsite-in)
891 ((string-match "\\.rss$" href)
893 base-uri href rss-onsite-end rss-offsite-end))
894 ((string-match "\\.rdf$" href)
896 base-uri href rdf-onsite-end rdf-offsite-end))
897 ((string-match "\\.xml$" href)
899 base-uri href xml-onsite-end xml-offsite-end))
900 ((string-match "rss" href)
902 base-uri href rss-onsite-in rss-offsite-in))
903 ((string-match "rdf" href)
905 base-uri href rdf-onsite-in rdf-offsite-in))
906 ((string-match "xml" href)
908 base-uri href xml-onsite-in xml-offsite-in))))
910 rss-onsite-end rdf-onsite-end xml-onsite-end
911 rss-onsite-in rdf-onsite-in xml-onsite-in
912 rss-offsite-end rdf-offsite-end xml-offsite-end
913 rss-offsite-in rdf-offsite-in xml-offsite-in)))
915 (defun nnrss-discover-feed (url)
916 "Given a page, find an RSS feed.
917 Use Mark Pilgrim's `ultra-liberal rss locator'."
918 (let ((parsed-page (nnrss-fetch url)))
919 ;; 1. if this url is the rss, use it.
920 (if (nnrss-rss-p parsed-page)
921 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
922 (nnrss-rss-title-description rss-ns parsed-page url))
924 ;; 2. look for the <link rel="alternate"
925 ;; type="application/rss+xml" and use that if it is there.
926 (let ((links (nnrss-get-rsslinks parsed-page)))
928 (let* ((xml (nnrss-fetch
929 (cdr (assoc 'href (cadar links)))))
930 (rss-ns (nnrss-get-namespace-prefix
931 xml "http://purl.org/rss/1.0/")))
932 (nnrss-rss-title-description
933 rss-ns xml (cdr (assoc 'href (cadar links)))))
935 ;; 3. look for links on the site in the following order:
936 ;; - onsite links ending in .rss, .rdf, or .xml
937 ;; - onsite links containing any of the above
938 ;; - offsite links ending in .rss, .rdf, or .xml
939 ;; - offsite links containing any of the above
940 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
941 (match-string 0 url)))
942 (hrefs (nnrss-order-hrefs
943 base-uri (nnrss-extract-hrefs parsed-page)))
945 (while (and (eq rss-link nil) (not (eq hrefs nil)))
946 (let ((href-data (nnrss-fetch (car hrefs))))
947 (if (nnrss-rss-p href-data)
948 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
949 (setq rss-link (nnrss-rss-title-description
950 rss-ns href-data (car hrefs))))
951 (setq hrefs (cdr hrefs)))))
955 (nnrss-find-rss-via-syndic8 url))))))))
957 (declare-function xml-rpc-method-call "ext:xml-rpc"
958 (server-url method &rest params))
960 (defun nnrss-find-rss-via-syndic8 (url)
961 "Query syndic8 for the rss feeds it has for URL."
962 (if (not (locate-library "xml-rpc"))
964 (message "XML-RPC is not available... not checking Syndic8.")
967 (let ((feedid (xml-rpc-method-call
968 "http://www.syndic8.com/xmlrpc.php"
972 (let* ((feedinfo (xml-rpc-method-call
973 "http://www.syndic8.com/xmlrpc.php"
981 (cdr (assoc "status" listinfo))
984 (cdr (assoc "sitename" listinfo))
988 "sitename" listinfo)))
991 "dataurl" listinfo)))))))
993 (if (not (> (length urllist) 1))
995 (let ((completion-ignore-case t)
997 (mapcar (lambda (listinfo)
998 (cons (cdr (assoc "sitename" listinfo))
1000 (cdr (assoc "feedid" listinfo)))))
1003 (gnus-completing-read
1004 "Multiple feeds found. Select one"
1005 selection t) urllist)))))))))
1007 (defun nnrss-rss-p (data)
1008 "Test if DATA is an RSS feed.
1009 Simply ensures that the first element is rss or rdf."
1010 (or (eq (caar data) 'rss)
1011 (eq (caar data) 'rdf:RDF)))
1013 (defun nnrss-rss-title-description (rss-namespace data url)
1014 "Return the title of an RSS feed."
1015 (if (nnrss-rss-p data)
1016 (let ((description (intern (concat rss-namespace "description")))
1017 (title (intern (concat rss-namespace "title")))
1018 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1021 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1022 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1023 (cons 'href url)))))
1025 (defun nnrss-get-namespace-prefix (el uri)
1026 "Given EL (containing a parsed element) and URI (containing a string
1027 that gives the URI for which you want to retrieve the namespace
1028 prefix), return the prefix."
1029 (let* ((prefix (car (rassoc uri (cadar el))))
1031 (split-string (symbol-name prefix) ":")))
1032 (ns (cond ((eq (length nslist) 1) ; no prefix given
1034 ((eq (length nslist) 2) ; extract prefix
1036 (if (and ns (not (string= ns "")))
1042 ;;; nnrss.el ends here