1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001-2014 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 ;; For Emacs <22.2 and XEmacs.
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile (require 'cl))
47 (eval '(require 'xml))
51 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
52 "Where nnrss will save its files.")
54 (defvoo nnrss-ignore-article-fields '(slash:comments)
55 "*List of fields that should be ignored when comparing RSS articles.
56 Some RSS feeds update article fields during their lives, e.g. to
57 indicate the number of comments or the number of times the
58 articles have been seen. However, if there is a difference
59 between the local article and the distant one, the latter is
60 considered to be new. To avoid this and discard some fields, set
61 this variable to the list of fields to be ignored.")
63 ;; (group max rss-url)
64 (defvoo nnrss-server-data nil)
66 ;; (num timestamp url subject author date extra)
67 (defvoo nnrss-group-data nil)
68 (defvoo nnrss-group-max 0)
69 (defvoo nnrss-group-min 1)
70 (defvoo nnrss-group nil)
71 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
72 (defvoo nnrss-status-string "")
74 (defconst nnrss-version "nnrss 1.0")
76 (defvar nnrss-group-alist '()
77 "List of RSS addresses.")
79 (defvar nnrss-use-local nil
80 "If non-nil nnrss will read the feeds from local files in nnrss-directory.")
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 (nnoo-define-basics nnrss)
118 ;;; Interface functions
120 (defsubst nnrss-format-string (string)
121 (gnus-replace-in-string string " *\n *" " "))
123 (defun nnrss-decode-group-name (group)
124 (if (and group (mm-coding-system-p 'utf-8))
125 (setq group (mm-decode-coding-string group 'utf-8))
128 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
129 (setq group (nnrss-decode-group-name group))
130 (nnrss-possibly-change-group group server)
132 (with-current-buffer nntp-server-buffer
134 (dolist (article articles)
135 (if (setq e (assq article nnrss-group-data))
136 (insert (number-to-string (car e)) "\t" ;; number
141 (or (nth 4 e) "(nobody)")
147 (format "<%d@%s.nnrss>" (car e) group)
158 (memq nnrss-description-field
159 nnmail-extra-headers))
160 (concat (symbol-name nnrss-description-field)
162 (nnrss-format-string (nth 6 e))
166 (memq nnrss-url-field
167 nnmail-extra-headers))
168 (concat (symbol-name nnrss-url-field)
170 (nnrss-format-string (nth 2 e))
176 (deffoo nnrss-request-group (group &optional server dont-check info)
177 (setq group (nnrss-decode-group-name group))
178 (nnheader-message 6 "nnrss: Requesting %s..." group)
179 (nnrss-possibly-change-group group server)
183 (nnrss-check-group group server)
184 (nnheader-report 'nnrss "Opened group %s" group)
186 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
187 (prin1-to-string group)
189 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
191 (deffoo nnrss-close-group (group &optional server)
194 (deffoo nnrss-request-article (article &optional group server buffer)
195 (setq group (nnrss-decode-group-name group))
196 (when (stringp article)
197 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
198 (string-to-number (match-string 1 article))
200 (nnrss-possibly-change-group group server)
201 (let ((e (assq article nnrss-group-data))
202 (nntp-server-buffer (or buffer nntp-server-buffer))
205 (with-current-buffer nntp-server-buffer
208 (insert "Newsgroups: " group "\n"))
210 (insert "Subject: " (nth 3 e) "\n"))
212 (insert "From: " (nth 4 e) "\n"))
214 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
215 (let ((header (buffer-string))
218 (enclosure (nth 7 e))
220 (rfc2047-header-encoding-alist
221 (if (mm-coding-system-p 'utf-8)
222 (cons '("Newsgroups" . utf-8)
223 rfc2047-header-encoding-alist)
224 rfc2047-header-encoding-alist))
225 rfc2047-encode-encoded-words body fn)
226 (when (or text link enclosure comments)
228 (insert "<#multipart type=alternative>\n"
229 "<#part type=\"text/plain\">\n")
234 (while (re-search-forward "\n+" nil t)
237 ;; See `nnrss-check-group', which inserts "<br /><br />".
238 (when (search-forward "<br /><br />" nil t)
241 (replace-match "\n\n")))
243 (let ((fill-column (default-value 'fill-column))
244 (window (get-buffer-window nntp-server-buffer)))
247 (max 1 (/ (* (window-width window) 7) 8))))
248 (fill-region (point) (point-max))
249 (goto-char (point-max))
250 ;; XEmacs version of `fill-region' inserts newline.
253 (when (or link enclosure)
258 (insert (car enclosure) " "
259 (nth 2 enclosure) " "
260 (nth 3 enclosure) "\n"))
262 (insert comments "\n"))
263 (setq body (buffer-substring body (point)))
265 "<#part type=\"text/html\">\n"
266 "<html><head></head><body>\n")
270 (insert "<p><a href=\"" link "\">link</a></p>\n"))
272 (insert "<p><a href=\"" (car enclosure) "\">"
273 (cadr enclosure) "</a> " (nth 2 enclosure)
274 " " (nth 3 enclosure) "</p>\n"))
276 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
277 (insert "</body></html>\n"
281 ;; Allow `mml-to-mime' to generate MIME article without
282 ;; making inquiry to a user for unknown encoding.
283 (let ((mml-confirmation-set
284 (cons 'unknown-encoding mml-confirmation-set)))
289 "Content-Type: text/plain; charset=gnus-decoded\n"
290 "Content-Transfer-Encoding: 8bit\n\n"
293 3 "Warning - there might be invalid characters"))))
294 (goto-char (point-min))
295 (search-forward "\n\n")
297 (insert (format "Message-ID: <%d@%s.nnrss>\n"
299 (let ((rfc2047-encoding-type 'mime)
300 rfc2047-encode-max-chars)
301 (rfc2047-encode-string
302 (gnus-replace-in-string group "[\t\n ]+" "_")))))
303 (when nnrss-content-function
304 (funcall nnrss-content-function e group article))))
307 (nnheader-report 'nnrss err))
309 (nnheader-report 'nnrss "no such id: %d" article))
311 (nnheader-report 'nnrss "article %s retrieved" (car e))
312 ;; we return the article number.
313 (cons nnrss-group (car e))))))
315 (deffoo nnrss-open-server (server &optional defs connectionless)
316 (nnrss-read-server-data server)
317 (nnoo-change-server 'nnrss server defs)
320 (deffoo nnrss-request-expire-articles
321 (articles group &optional server force)
322 (setq group (nnrss-decode-group-name group))
323 (nnrss-possibly-change-group group server)
324 (let (e days not-expirable changed)
325 (dolist (art articles)
326 (if (and (setq e (assq art nnrss-group-data))
327 (nnmail-expired-article-p
329 (if (listp (setq days (nth 1 e))) days
330 (days-to-time (- days (time-to-days '(0 0)))))
332 (setq nnrss-group-data (delq e nnrss-group-data)
334 (push art not-expirable)))
336 (nnrss-save-group-data group server))
339 (deffoo nnrss-request-delete-group (group &optional force server)
340 (setq group (nnrss-decode-group-name group))
341 (nnrss-possibly-change-group group server)
343 ;; There may be two or more entries in `nnrss-group-alist' since
344 ;; this function didn't delete them formerly.
345 (while (setq elem (assoc group nnrss-group-alist))
346 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
347 (setq nnrss-server-data
348 (delq (assoc group nnrss-server-data) nnrss-server-data))
349 (nnrss-save-server-data server)
351 (let ((file-name-coding-system nnmail-pathname-coding-system))
352 (delete-file (nnrss-make-filename group server))))
355 (deffoo nnrss-request-list-newsgroups (&optional server)
356 (nnrss-possibly-change-group nil server)
357 (with-current-buffer nntp-server-buffer
359 (dolist (elem nnrss-group-alist)
361 (insert (car elem) "\t" (third elem) "\n"))))
364 (deffoo nnrss-retrieve-groups (groups &optional server)
365 (dolist (group groups)
366 (setq group (nnrss-decode-group-name group))
367 (nnrss-possibly-change-group group server)
368 (nnrss-check-group group server))
369 (with-current-buffer nntp-server-buffer
371 (dolist (group groups)
372 (let ((elem (assoc (gnus-group-decoded-name group) nnrss-server-data)))
373 (insert (format "%S %s 1 y\n" group (or (cadr elem) 0)))))
376 (nnoo-define-skeleton nnrss)
378 ;;; Internal functions
379 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
381 (defun nnrss-get-encoding ()
382 "Return an encoding attribute specified in the current xml contents.
383 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
384 it is used instead. If the xml contents doesn't specify the encoding,
385 return `utf-8' which is the default encoding for xml if it is available,
386 otherwise return nil."
387 (goto-char (point-min))
388 (if (re-search-forward
389 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
391 (let ((encoding (intern (downcase (or (match-string 1)
392 (match-string 2))))))
394 (mm-coding-system-p (cdr (assq encoding
395 nnrss-compatible-encoding-alist)))
396 (mm-coding-system-p encoding)
397 (mm-coding-system-p (car (rassq encoding
398 nnrss-compatible-encoding-alist)))))
399 (mm-coding-system-p 'utf-8)))
401 (defun nnrss-fetch (url &optional local)
402 "Fetch URL and put it in a the expected Lisp structure."
403 (mm-with-unibyte-buffer
404 ;;some versions of url.el need this to close the connection quickly
405 (let (cs xmlform htmlform)
406 ;; bit o' work necessary for w3 pre-cvs and post-cvs
408 (let ((coding-system-for-read 'binary))
409 (insert-file-contents url))
410 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
411 ;; to `mm-url-insert'?
412 (let ((coding-system-for-read 'binary))
415 (error (if (or debug-on-quit debug-on-error)
416 (signal (car err) (cdr err))
417 (message "nnrss: Failed to fetch %s" url))))))
418 (nnheader-remove-cr-followed-by-lf)
419 ;; Decode text according to the encoding attribute.
420 (when (setq cs (nnrss-get-encoding))
422 (mm-decode-coding-string (buffer-string) cs)
424 (mm-enable-multibyte))))
425 (goto-char (point-min))
428 (setq xmlform (xml-parse-region (point-min) (point-max)))
431 (setq htmlform (libxml-parse-html-region (point-min) (point-max)))
434 nnrss: %s: Not valid XML %s and libxml-parse-html-region doesn't work %s"
440 (defun nnrss-possibly-change-group (&optional group server)
442 (not (nnrss-server-opened server)))
443 (nnrss-open-server server))
444 (when (and group (not (equal group nnrss-group)))
445 (nnrss-read-group-data group server)
446 (setq nnrss-group group)))
448 (autoload 'timezone-parse-date "timezone")
450 (defun nnrss-normalize-date (date)
451 "Return a date string of DATE in the RFC822 style.
452 This function handles the ISO 8601 date format described in
453 URL `http://www.w3.org/TR/NOTE-datetime', and also the RFC822 style
454 which RSS 2.0 allows."
455 (let (case-fold-search vector year month day time zone cts given)
456 (cond ((null date)) ; do nothing for this case
457 ;; if the date is just digits (unix time stamp):
458 ((string-match "^[0-9]+$" date)
459 (setq given (seconds-to-time (string-to-number date))))
461 ((string-match " [0-9]+ " date)
462 (setq vector (timezone-parse-date date)
463 year (string-to-number (aref vector 0)))
465 (setq month (string-to-number (aref vector 1))
466 day (string-to-number (aref vector 2)))
467 (unless (>= (length (setq time (aref vector 3))) 3)
468 (setq time "00:00:00"))
469 (when (and (setq zone (aref vector 4))
470 (not (string-match "\\`[A-Z+-]" zone)))
477 "\\(199[0-9]\\|20[0-9][0-9]\\)"
486 "\\([012][0-9]:[0-5][0-9]\\)"
490 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
492 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
493 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
496 (setq year (string-to-number (match-string 1 date))
497 month (string-to-number (or (match-string 2 date) "1"))
498 day (string-to-number (or (match-string 3 date) "1"))
499 time (if (match-beginning 5)
500 (substring date (match-beginning 4) (match-end 5))
501 (concat (or (match-string 4 date) "00:00") ":00"))
502 zone (cond ((match-beginning 6)
503 (concat (match-string 6 date)
504 (match-string 7 date)))
505 ((match-beginning 9) ;; Z
507 (t ;; nil if zone is not provided.
508 (match-string 8 date))))))
511 (setq cts (current-time-string (encode-time 0 0 0 day month year)))
512 (format "%s, %02d %s %04d %s%s"
513 (substring cts 0 3) day (substring cts 4 7) year time
517 (message-make-date given))))
521 (defun nnrss-read-server-data (server)
522 (setq nnrss-server-data nil)
523 (let ((file (nnrss-make-filename "nnrss" server))
524 (file-name-coding-system nnmail-pathname-coding-system))
525 (when (file-exists-p file)
526 (load file nil t t))))
528 (defun nnrss-save-server-data (server)
529 (gnus-make-directory nnrss-directory)
530 (let ((coding-system-for-write nnrss-file-coding-system)
531 (file-name-coding-system nnmail-pathname-coding-system))
532 (with-temp-file (nnrss-make-filename "nnrss" server)
533 (insert (format ";; -*- coding: %s; -*-\n"
534 nnrss-file-coding-system))
535 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
537 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
539 (defun nnrss-read-group-data (group server)
540 (setq nnrss-group-data nil)
541 (if (hash-table-p nnrss-group-hashtb)
542 (clrhash nnrss-group-hashtb)
543 (setq nnrss-group-hashtb (make-hash-table :test 'equal)))
544 (let ((pair (assoc group nnrss-server-data)))
545 (setq nnrss-group-max (or (cadr pair) 0))
546 (setq nnrss-group-min (+ nnrss-group-max 1)))
547 (let ((file (nnrss-make-filename group server))
548 (file-name-coding-system nnmail-pathname-coding-system))
549 (when (file-exists-p file)
551 (dolist (e nnrss-group-data)
552 (puthash (nth 9 e) t nnrss-group-hashtb)
553 (when (and (car e) (> nnrss-group-min (car e)))
554 (setq nnrss-group-min (car e)))
555 (when (and (car e) (< nnrss-group-max (car e)))
556 (setq nnrss-group-max (car e)))))))
558 (defun nnrss-save-group-data (group server)
559 (gnus-make-directory nnrss-directory)
560 (let ((coding-system-for-write nnrss-file-coding-system)
561 (file-name-coding-system nnmail-pathname-coding-system))
562 (with-temp-file (nnrss-make-filename group server)
563 (insert (format ";; -*- coding: %s; -*-\n"
564 nnrss-file-coding-system))
565 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
567 (defun nnrss-make-filename (name server)
569 (nnrss-translate-file-chars
572 (not (equal server ""))
578 (gnus-add-shutdown 'nnrss-close 'gnus)
580 (defun nnrss-close ()
581 "Clear internal nnrss variables."
582 (setq nnrss-group-data nil
583 nnrss-server-data nil
584 nnrss-group-hashtb nil
585 nnrss-group-alist nil))
589 (defun nnrss-no-cache (url)
592 (defun nnrss-insert (url)
593 (mm-with-unibyte-current-buffer
596 (error (if (or debug-on-quit debug-on-error)
597 (signal (car err) (cdr err))
598 (message "nnrss: Failed to fetch %s" url))))))
600 (defun nnrss-decode-entities-string (string)
602 (mm-with-multibyte-buffer
604 (mm-url-decode-entities-nbsp)
607 (defun nnrss-mime-encode-string (string)
608 (mm-with-multibyte-buffer
610 (mm-url-decode-entities-nbsp)
611 (goto-char (point-min))
612 (while (re-search-forward "[\t\n ]+" nil t)
614 (goto-char (point-min))
615 (skip-chars-forward " ")
616 (delete-region (point-min) (point))
617 (goto-char (point-max))
618 (skip-chars-forward " ")
619 (delete-region (point) (point-max))
620 (let ((rfc2047-encoding-type 'mime)
621 rfc2047-encode-max-chars)
622 (rfc2047-encode-region (point-min) (point-max)))
623 (goto-char (point-min))
624 (while (search-forward "\n" nil t)
629 (defun nnrss-make-hash-index (item)
630 (gnus-message 9 "nnrss: Making hash index of %s" (gnus-prin1-to-string item))
631 (setq item (gnus-remove-if
634 (memq (car field) nnrss-ignore-article-fields)))
636 (md5 (gnus-prin1-to-string item)
638 nnrss-file-coding-system))
640 (defun nnrss-check-group (group server)
641 (let (file xml subject url extra changed author date feed-subject
642 enclosure comments rss-ns rdf-ns content-ns dc-ns
644 (if (and nnrss-use-local
645 (file-exists-p (setq file (expand-file-name
646 (nnrss-translate-file-chars
647 (concat group ".xml"))
649 (setq xml (nnrss-fetch file t))
650 (setq url (or (nth 2 (assoc group nnrss-server-data))
651 (second (assoc group nnrss-group-alist))))
658 (format "URL to search for %s: " group) "http://")))))
659 (let ((pair (assoc group nnrss-server-data)))
661 (setcdr (cdr pair) (list url))
662 (push (list group nnrss-group-max url) nnrss-server-data)))
664 (setq xml (nnrss-fetch url)))
665 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
666 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
667 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
668 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
669 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
670 (when (and (listp item)
671 (string= (concat rss-ns "item") (car item))
672 (progn (setq hash-index (nnrss-make-hash-index item))
673 (not (gethash hash-index nnrss-group-hashtb))))
674 (setq subject (nnrss-node-text rss-ns 'title item))
675 (setq url (nnrss-decode-entities-string
676 (nnrss-node-text rss-ns 'link (cddr item))))
677 (setq extra (or (nnrss-node-text content-ns 'encoded item)
678 (nnrss-node-text rss-ns 'description item)))
679 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
680 (setq extra (concat feed-subject "<br /><br />" extra)))
681 (setq author (or (nnrss-node-text rss-ns 'author item)
682 (nnrss-node-text dc-ns 'creator item)
683 (nnrss-node-text dc-ns 'contributor item)))
684 (setq date (nnrss-normalize-date
685 (or (nnrss-node-text dc-ns 'date item)
686 (nnrss-node-text rss-ns 'pubDate item))))
687 (setq comments (nnrss-node-text rss-ns 'comments item))
688 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
689 (let ((url (cdr (assq 'url enclosure)))
690 (len (cdr (assq 'length enclosure)))
691 (type (cdr (assq 'type enclosure)))
694 (if (and len (integerp (setq len (string-to-number len))))
695 ;; actually already in `ls-lisp-format-file-size' but
696 ;; probably not worth to require it for one function
697 (do ((size (/ len 1.0) (/ size 1024.0))
698 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
701 (format "%.1f%s" size (car post-fixes))))
703 (setq url (or url ""))
704 (setq name (if (string-match "/\\([^/]*\\)$" url)
707 (setq type (or type ""))
708 (setq enclosure (list url name len type))))
711 (incf nnrss-group-max)
714 (and subject (nnrss-mime-encode-string subject))
715 (and author (nnrss-mime-encode-string author))
717 (and extra (nnrss-decode-entities-string extra))
722 (puthash hash-index t nnrss-group-hashtb)
726 (nnrss-save-group-data group server)
727 (let ((pair (assoc group nnrss-server-data)))
729 (setcar (cdr pair) nnrss-group-max)
730 (push (list group nnrss-group-max) nnrss-server-data)))
731 (nnrss-save-server-data server))))
733 (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
735 (defun nnrss-opml-import (opml-file)
736 "OPML subscriptions import.
737 Read the file and attempt to subscribe to each Feed in the file."
738 (interactive "fImport file: ")
741 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
743 (not (string-match "\\`[\t ]*\\'" xmlurl))
745 (y-or-n-p (format "Subscribe to %s " xmlurl))
749 (gnus-group-make-rss-group xmlurl)
753 "Failed to subscribe to %s (%s); type any key to continue: "
755 (error-message-string err))
756 (let ((echo-keystrokes 0))
758 (nnrss-find-el 'outline
759 (mm-with-multibyte-buffer
760 (insert-file-contents opml-file)
761 (xml-parse-region (point-min) (point-max))))))
763 (defun nnrss-opml-export ()
764 "OPML subscription export.
765 Export subscriptions to a buffer in OPML Format."
767 (with-current-buffer (get-buffer-create "*OPML Export*")
768 (mm-set-buffer-file-coding-system 'utf-8)
769 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
770 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
771 "<opml version=\"1.1\">\n"
773 " <title>mySubscriptions</title>\n"
774 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
776 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
777 " <ownerName>" (user-full-name) "</ownerName>\n"
780 (dolist (sub nnrss-group-alist)
781 (insert " <outline text=\"" (car sub)
782 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
785 (pop-to-buffer "*OPML Export*")
786 (when (fboundp 'sgml-mode)
789 (defun nnrss-generate-download-script ()
790 "Generate a download script in the current buffer.
791 It is useful when `(setq nnrss-use-local t)'."
793 (insert "#!/bin/sh\n")
794 (insert "WGET=wget\n")
795 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
796 (dolist (elem nnrss-server-data)
797 (let ((url (or (nth 2 elem)
798 (second (assoc (car elem) nnrss-group-alist)))))
799 (insert "$WGET -q -O \"$RSSDIR\"/'"
800 (nnrss-translate-file-chars (concat (car elem) ".xml"))
803 (defun nnrss-translate-file-chars (name)
804 (let ((nnheader-file-name-translation-alist
805 (append nnheader-file-name-translation-alist '((?' . ?_)))))
806 (nnheader-translate-file-chars name)))
808 (defun nnrss-node-text (namespace local-name element)
809 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
811 (text (if (and node (listp node))
812 (nnrss-node-just-text node)
814 (cleaned-text (if text
815 (gnus-replace-in-string
816 (gnus-replace-in-string
817 text "^[\000-\037\177]+\\|^ +\\| +$" "")
819 (if (string-equal "" cleaned-text)
823 (defun nnrss-node-just-text (node)
824 (if (and node (listp node))
825 (mapconcat 'nnrss-node-just-text (cddr node) " ")
828 (defun nnrss-find-el (tag data &optional found-list)
829 "Find the all matching elements in the data.
830 Careful with this on large documents!"
834 (when (equal tag (car bit))
835 ;; Old xml.el may return a list of string.
836 (when (and (consp (caddr bit))
837 (stringp (caaddr bit)))
838 (setcar (cddr bit) (caaddr bit)))
842 (if (and (consp (car-safe (caddr bit)))
843 (not (stringp (caddr bit))))
851 tag (cddr bit))))))))
854 (defun nnrss-rsslink-p (el)
855 "Test if the element we are handed is an RSS autodiscovery link."
856 (and (eq (car-safe el) 'link)
857 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
858 (or (string-equal (cdr (assoc 'type (cadr el)))
859 "application/rss+xml")
860 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
862 (defun nnrss-get-rsslinks (data)
863 "Extract the <link> elements that are links to RSS from the parsed data."
866 (if (nnrss-rsslink-p el) el))
867 (nnrss-find-el 'link data))))
869 (defun nnrss-extract-hrefs (data)
870 "Recursively extract hrefs from a page's source.
871 DATA should be the output of `xml-parse-region'."
872 (mapcar (lambda (ahref)
873 (cdr (assoc 'href (cadr ahref))))
874 (nnrss-find-el 'a data)))
876 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
877 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
878 (not (string-match "://" ,item)))
879 (setq ,onsite-list (append ,onsite-list (list ,item))))
880 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
882 (defun nnrss-order-hrefs (base-uri hrefs)
883 "Given a list of hrefs, sort them using the following priorities:
884 1. links ending in .rss
885 2. links ending in .rdf
886 3. links ending in .xml
887 4. links containing the above
890 BASE-URI is used to determine the location of the links and
891 whether they are `offsite' or `onsite'."
892 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
893 rss-onsite-in rdf-onsite-in xml-onsite-in
894 rss-offsite-end rdf-offsite-end xml-offsite-end
895 rss-offsite-in rdf-offsite-in xml-offsite-in)
898 ((string-match "\\.rss$" href)
900 base-uri href rss-onsite-end rss-offsite-end))
901 ((string-match "\\.rdf$" href)
903 base-uri href rdf-onsite-end rdf-offsite-end))
904 ((string-match "\\.xml$" href)
906 base-uri href xml-onsite-end xml-offsite-end))
907 ((string-match "rss" href)
909 base-uri href rss-onsite-in rss-offsite-in))
910 ((string-match "rdf" href)
912 base-uri href rdf-onsite-in rdf-offsite-in))
913 ((string-match "xml" href)
915 base-uri href xml-onsite-in xml-offsite-in))))
917 rss-onsite-end rdf-onsite-end xml-onsite-end
918 rss-onsite-in rdf-onsite-in xml-onsite-in
919 rss-offsite-end rdf-offsite-end xml-offsite-end
920 rss-offsite-in rdf-offsite-in xml-offsite-in)))
922 (defun nnrss-discover-feed (url)
923 "Given a page, find an RSS feed.
924 Use Mark Pilgrim's `ultra-liberal rss locator'."
925 (let ((parsed-page (nnrss-fetch url)))
926 ;; 1. if this url is the rss, use it.
927 (if (nnrss-rss-p parsed-page)
928 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
929 (nnrss-rss-title-description rss-ns parsed-page url))
931 ;; 2. look for the <link rel="alternate"
932 ;; type="application/rss+xml" and use that if it is there.
933 (let ((links (nnrss-get-rsslinks parsed-page)))
935 (let* ((xml (nnrss-fetch
936 (cdr (assoc 'href (cadar links)))))
937 (rss-ns (nnrss-get-namespace-prefix
938 xml "http://purl.org/rss/1.0/")))
939 (nnrss-rss-title-description
940 rss-ns xml (cdr (assoc 'href (cadar links)))))
942 ;; 3. look for links on the site in the following order:
943 ;; - onsite links ending in .rss, .rdf, or .xml
944 ;; - onsite links containing any of the above
945 ;; - offsite links ending in .rss, .rdf, or .xml
946 ;; - offsite links containing any of the above
947 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
948 (match-string 0 url)))
949 (hrefs (nnrss-order-hrefs
950 base-uri (nnrss-extract-hrefs parsed-page)))
952 (while (and (eq rss-link nil) (not (eq hrefs nil)))
953 (let ((href-data (nnrss-fetch (car hrefs))))
954 (if (nnrss-rss-p href-data)
955 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
956 (setq rss-link (nnrss-rss-title-description
957 rss-ns href-data (car hrefs))))
958 (setq hrefs (cdr hrefs)))))
962 (nnrss-find-rss-via-syndic8 url))))))))
964 (defun nnrss-find-rss-via-syndic8 (url)
965 "Query syndic8 for the rss feeds it has for URL."
966 (if (not (locate-library "xml-rpc"))
968 (message "XML-RPC is not available... not checking Syndic8.")
971 (let ((feedid (xml-rpc-method-call
972 "http://www.syndic8.com/xmlrpc.php"
976 (let* ((feedinfo (xml-rpc-method-call
977 "http://www.syndic8.com/xmlrpc.php"
985 (cdr (assoc "status" listinfo))
988 (cdr (assoc "sitename" listinfo))
992 "sitename" listinfo)))
995 "dataurl" listinfo)))))))
997 (if (not (> (length urllist) 1))
999 (let ((completion-ignore-case t)
1001 (mapcar (lambda (listinfo)
1002 (cons (cdr (assoc "sitename" listinfo))
1004 (cdr (assoc "feedid" listinfo)))))
1007 (gnus-completing-read
1008 "Multiple feeds found. Select one"
1009 selection t) urllist)))))))))
1011 (defun nnrss-rss-p (data)
1012 "Test if DATA is an RSS feed.
1013 Simply ensures that the first element is rss or rdf."
1014 (or (eq (caar data) 'rss)
1015 (eq (caar data) 'rdf:RDF)))
1017 (defun nnrss-rss-title-description (rss-namespace data url)
1018 "Return the title of an RSS feed."
1019 (if (nnrss-rss-p data)
1020 (let ((description (intern (concat rss-namespace "description")))
1021 (title (intern (concat rss-namespace "title")))
1022 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1025 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1026 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1027 (cons 'href url)))))
1029 (defun nnrss-get-namespace-prefix (el uri)
1030 "Given EL (containing a parsed element) and URI (containing a string
1031 that gives the URI for which you want to retrieve the namespace
1032 prefix), return the prefix."
1033 (let* ((prefix (car (rassoc uri (cadar el))))
1035 (split-string (symbol-name prefix) ":")))
1036 (ns (cond ((eq (length nslist) 1) ; no prefix given
1038 ((eq (length nslist) 2) ; extract prefix
1040 (if (and ns (not (string= ns "")))
1046 ;;; nnrss.el ends here