1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001-2015 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
))
43 (eval '(require 'xml
))
47 (defvoo nnrss-directory
(nnheader-concat gnus-directory
"rss/")
48 "Where nnrss will save its files.")
50 (defvoo nnrss-ignore-article-fields
'(slash:comments
)
51 "*List of fields that should be ignored when comparing RSS articles.
52 Some RSS feeds update article fields during their lives, e.g. to
53 indicate the number of comments or the number of times the
54 articles have been seen. However, if there is a difference
55 between the local article and the distant one, the latter is
56 considered to be new. To avoid this and discard some fields, set
57 this variable to the list of fields to be ignored.")
59 ;; (group max rss-url)
60 (defvoo nnrss-server-data nil
)
62 ;; (num timestamp url subject author date extra)
63 (defvoo nnrss-group-data nil
)
64 (defvoo nnrss-group-max
0)
65 (defvoo nnrss-group-min
1)
66 (defvoo nnrss-group nil
)
67 (defvoo nnrss-group-hashtb
(make-hash-table :test
'equal
))
68 (defvoo nnrss-status-string
"")
70 (defconst nnrss-version
"nnrss 1.0")
72 (defvar nnrss-group-alist
'()
73 "List of RSS addresses.")
75 (defvar nnrss-use-local nil
76 "If non-nil nnrss will read the feeds from local files in nnrss-directory.")
78 (defvar nnrss-description-field
'X-Gnus-Description
79 "Field name used for DESCRIPTION.
80 To use the description in headers, put this name into `nnmail-extra-headers'.")
82 (defvar nnrss-url-field
'X-Gnus-Url
83 "Field name used for URL.
84 To use the description in headers, put this name into `nnmail-extra-headers'.")
86 (defvar nnrss-content-function nil
87 "A function which is called in `nnrss-request-article'.
88 The arguments are (ENTRY GROUP ARTICLE).
89 ENTRY is the record of the current headline. GROUP is the group name.
90 ARTICLE is the article number of the current headline.")
92 (defvar nnrss-file-coding-system mm-universal-coding-system
93 "*Coding system used when reading and writing files.
94 If you run Gnus with various versions of Emacsen, the value of this
95 variable should be the coding system that all those Emacsen support.
96 Note that you have to regenerate all the nnrss groups if you change
97 the value. Moreover, you should be patient even if you are made to
98 read the same articles twice, that arises for the difference of the
101 (defvar nnrss-compatible-encoding-alist
102 (delq nil
(mapcar (lambda (elem)
103 (if (and (mm-coding-system-p (car elem
))
104 (mm-coding-system-p (cdr elem
)))
106 mm-charset-override-alist
))
107 "Alist of encodings and those supersets.
108 The cdr of each element is used to decode data if it is available when
109 the car is what the data specify as the encoding. Or, the car is used
110 for decoding when the cdr that the data specify is not available.")
112 (nnoo-define-basics nnrss
)
114 ;;; Interface functions
116 (defsubst nnrss-format-string
(string)
117 (gnus-replace-in-string string
" *\n *" " "))
119 (defun nnrss-decode-group-name (group)
120 (if (and group
(mm-coding-system-p 'utf-8
))
121 (setq group
(mm-decode-coding-string group
'utf-8
))
124 (deffoo nnrss-retrieve-headers
(articles &optional group server fetch-old
)
125 (setq group
(nnrss-decode-group-name group
))
126 (nnrss-possibly-change-group group server
)
128 (with-current-buffer nntp-server-buffer
130 (dolist (article articles
)
131 (if (setq e
(assq article nnrss-group-data
))
132 (insert (number-to-string (car e
)) "\t" ;; number
137 (or (nth 4 e
) "(nobody)")
143 (format "<%d@%s.nnrss>" (car e
) group
)
154 (memq nnrss-description-field
155 nnmail-extra-headers
))
156 (concat (symbol-name nnrss-description-field
)
158 (nnrss-format-string (nth 6 e
))
162 (memq nnrss-url-field
163 nnmail-extra-headers
))
164 (concat (symbol-name nnrss-url-field
)
166 (nnrss-format-string (nth 2 e
))
172 (deffoo nnrss-request-group
(group &optional server dont-check info
)
173 (setq group
(nnrss-decode-group-name group
))
174 (nnheader-message 6 "nnrss: Requesting %s..." group
)
175 (nnrss-possibly-change-group group server
)
179 (nnrss-check-group group server
)
180 (nnheader-report 'nnrss
"Opened group %s" group
)
182 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
183 (prin1-to-string group
)
185 (nnheader-message 6 "nnrss: Requesting %s...done" group
)))
187 (deffoo nnrss-close-group
(group &optional server
)
190 (deffoo nnrss-request-article
(article &optional group server buffer
)
191 (setq group
(nnrss-decode-group-name group
))
192 (when (stringp article
)
193 (setq article
(if (string-match "\\`<\\([0-9]+\\)@" article
)
194 (string-to-number (match-string 1 article
))
196 (nnrss-possibly-change-group group server
)
197 (let ((e (assq article nnrss-group-data
))
198 (nntp-server-buffer (or buffer nntp-server-buffer
))
201 (with-current-buffer nntp-server-buffer
204 (insert "Newsgroups: " group
"\n"))
206 (insert "Subject: " (nth 3 e
) "\n"))
208 (insert "From: " (nth 4 e
) "\n"))
210 (insert "Date: " (nnrss-format-string (nth 5 e
)) "\n"))
211 (let ((header (buffer-string))
214 (enclosure (nth 7 e
))
216 (rfc2047-header-encoding-alist
217 (if (mm-coding-system-p 'utf-8
)
218 (cons '("Newsgroups" . utf-8
)
219 rfc2047-header-encoding-alist
)
220 rfc2047-header-encoding-alist
))
221 rfc2047-encode-encoded-words body fn
)
222 (when (or text link enclosure comments
)
224 (insert "<#multipart type=alternative>\n"
225 "<#part type=\"text/plain\">\n")
230 (while (re-search-forward "\n+" nil t
)
233 ;; See `nnrss-check-group', which inserts "<br /><br />".
234 (when (search-forward "<br /><br />" nil t
)
237 (replace-match "\n\n")))
239 (let ((fill-column (default-value 'fill-column
))
240 (window (get-buffer-window nntp-server-buffer
)))
243 (max 1 (/ (* (window-width window
) 7) 8))))
244 (fill-region (point) (point-max))
245 (goto-char (point-max))
246 ;; XEmacs version of `fill-region' inserts newline.
249 (when (or link enclosure
)
254 (insert (car enclosure
) " "
255 (nth 2 enclosure
) " "
256 (nth 3 enclosure
) "\n"))
258 (insert comments
"\n"))
259 (setq body
(buffer-substring body
(point)))
261 "<#part type=\"text/html\">\n"
262 "<html><head></head><body>\n")
266 (insert "<p><a href=\"" link
"\">link</a></p>\n"))
268 (insert "<p><a href=\"" (car enclosure
) "\">"
269 (cadr enclosure
) "</a> " (nth 2 enclosure
)
270 " " (nth 3 enclosure
) "</p>\n"))
272 (insert "<p><a href=\"" comments
"\">comments</a></p>\n"))
273 (insert "</body></html>\n"
277 ;; Allow `mml-to-mime' to generate MIME article without
278 ;; making inquiry to a user for unknown encoding.
279 (let ((mml-confirmation-set
280 (cons 'unknown-encoding mml-confirmation-set
)))
285 "Content-Type: text/plain; charset=gnus-decoded\n"
286 "Content-Transfer-Encoding: 8bit\n\n"
289 3 "Warning - there might be invalid characters"))))
290 (goto-char (point-min))
291 (search-forward "\n\n")
293 (insert (format "Message-ID: <%d@%s.nnrss>\n"
295 (let ((rfc2047-encoding-type 'mime
)
296 rfc2047-encode-max-chars
)
297 (rfc2047-encode-string
298 (gnus-replace-in-string group
"[\t\n ]+" "_")))))
299 (when nnrss-content-function
300 (funcall nnrss-content-function e group article
))))
303 (nnheader-report 'nnrss err
))
305 (nnheader-report 'nnrss
"no such id: %d" article
))
307 (nnheader-report 'nnrss
"article %s retrieved" (car e
))
308 ;; we return the article number.
309 (cons nnrss-group
(car e
))))))
311 (deffoo nnrss-open-server
(server &optional defs connectionless
)
312 (nnrss-read-server-data server
)
313 (nnoo-change-server 'nnrss server defs
)
316 (deffoo nnrss-request-expire-articles
317 (articles group
&optional server force
)
318 (setq group
(nnrss-decode-group-name group
))
319 (nnrss-possibly-change-group group server
)
320 (let (e days not-expirable changed
)
321 (dolist (art articles
)
322 (if (and (setq e
(assq art nnrss-group-data
))
323 (nnmail-expired-article-p
325 (if (listp (setq days
(nth 1 e
))) days
326 (days-to-time (- days
(time-to-days '(0 0)))))
328 (setq nnrss-group-data
(delq e nnrss-group-data
)
330 (push art not-expirable
)))
332 (nnrss-save-group-data group server
))
335 (deffoo nnrss-request-delete-group
(group &optional force server
)
336 (setq group
(nnrss-decode-group-name group
))
337 (nnrss-possibly-change-group group server
)
339 ;; There may be two or more entries in `nnrss-group-alist' since
340 ;; this function didn't delete them formerly.
341 (while (setq elem
(assoc group nnrss-group-alist
))
342 (setq nnrss-group-alist
(delq elem nnrss-group-alist
))))
343 (setq nnrss-server-data
344 (delq (assoc group nnrss-server-data
) nnrss-server-data
))
345 (nnrss-save-server-data server
)
347 (let ((file-name-coding-system nnmail-pathname-coding-system
))
348 (delete-file (nnrss-make-filename group server
))))
351 (deffoo nnrss-request-list-newsgroups
(&optional server
)
352 (nnrss-possibly-change-group nil server
)
353 (with-current-buffer nntp-server-buffer
355 (dolist (elem nnrss-group-alist
)
357 (insert (car elem
) "\t" (third elem
) "\n"))))
360 (deffoo nnrss-retrieve-groups
(groups &optional server
)
361 (dolist (group groups
)
362 (setq group
(nnrss-decode-group-name group
))
363 (nnrss-possibly-change-group group server
)
364 (nnrss-check-group group server
))
365 (with-current-buffer nntp-server-buffer
367 (dolist (group groups
)
368 (let ((elem (assoc (gnus-group-decoded-name group
) nnrss-server-data
)))
369 (insert (format "%S %s 1 y\n" group
(or (cadr elem
) 0)))))
372 (nnoo-define-skeleton nnrss
)
374 ;;; Internal functions
375 (eval-when-compile (defun xml-rpc-method-call (&rest args
)))
377 (defun nnrss-get-encoding ()
378 "Return an encoding attribute specified in the current xml contents.
379 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
380 it is used instead. If the xml contents doesn't specify the encoding,
381 return `utf-8' which is the default encoding for xml if it is available,
382 otherwise return nil."
383 (goto-char (point-min))
384 (if (re-search-forward
385 "<\\?[^>]*encoding=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
387 (let ((encoding (intern (downcase (or (match-string 1)
388 (match-string 2))))))
390 (mm-coding-system-p (cdr (assq encoding
391 nnrss-compatible-encoding-alist
)))
392 (mm-coding-system-p encoding
)
393 (mm-coding-system-p (car (rassq encoding
394 nnrss-compatible-encoding-alist
)))))
395 (mm-coding-system-p 'utf-8
)))
397 (declare-function libxml-parse-html-region
"xml.c"
398 (start end
&optional base-url
))
399 (defun nnrss-fetch (url &optional local
)
400 "Fetch URL and put it in a the expected Lisp structure."
401 (mm-with-unibyte-buffer
402 ;;some versions of url.el need this to close the connection quickly
403 (let (cs xmlform htmlform
)
404 ;; bit o' work necessary for w3 pre-cvs and post-cvs
406 (let ((coding-system-for-read 'binary
))
407 (insert-file-contents url
))
408 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
409 ;; to `mm-url-insert'?
410 (let ((coding-system-for-read 'binary
))
413 (error (if (or debug-on-quit debug-on-error
)
414 (signal (car err
) (cdr err
))
415 (message "nnrss: Failed to fetch %s" url
))))))
416 (nnheader-remove-cr-followed-by-lf)
417 ;; Decode text according to the encoding attribute.
418 (when (setq cs
(nnrss-get-encoding))
420 (mm-decode-coding-string (buffer-string) cs
)
422 (mm-enable-multibyte))))
423 (goto-char (point-min))
426 (setq xmlform
(xml-parse-region (point-min) (point-max)))
429 (setq htmlform
(libxml-parse-html-region (point-min) (point-max)))
432 nnrss: %s: Not valid XML %s and libxml-parse-html-region doesn't work %s"
438 (defun nnrss-possibly-change-group (&optional group server
)
440 (not (nnrss-server-opened server
)))
441 (nnrss-open-server server
))
442 (when (and group
(not (equal group nnrss-group
)))
443 (nnrss-read-group-data group server
)
444 (setq nnrss-group group
)))
446 (autoload 'timezone-parse-date
"timezone")
448 (defun nnrss-normalize-date (date)
449 "Return a date string of DATE in the RFC822 style.
450 This function handles the ISO 8601 date format described in
451 URL `http://www.w3.org/TR/NOTE-datetime', and also the RFC822 style
452 which RSS 2.0 allows."
453 (let (case-fold-search vector year month day time zone cts given
)
454 (cond ((null date
)) ; do nothing for this case
455 ;; if the date is just digits (unix time stamp):
456 ((string-match "^[0-9]+$" date
)
457 (setq given
(seconds-to-time (string-to-number date
))))
459 ((string-match " [0-9]+ " date
)
460 (setq vector
(timezone-parse-date date
)
461 year
(string-to-number (aref vector
0)))
463 (setq month
(string-to-number (aref vector
1))
464 day
(string-to-number (aref vector
2)))
465 (unless (>= (length (setq time
(aref vector
3))) 3)
466 (setq time
"00:00:00"))
467 (when (and (setq zone
(aref vector
4))
468 (not (string-match "\\`[A-Z+-]" zone
)))
475 "\\(199[0-9]\\|20[0-9][0-9]\\)"
484 "\\([012][0-9]:[0-5][0-9]\\)"
488 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
490 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
491 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
494 (setq year
(string-to-number (match-string 1 date
))
495 month
(string-to-number (or (match-string 2 date
) "1"))
496 day
(string-to-number (or (match-string 3 date
) "1"))
497 time
(if (match-beginning 5)
498 (substring date
(match-beginning 4) (match-end 5))
499 (concat (or (match-string 4 date
) "00:00") ":00"))
500 zone
(cond ((match-beginning 6)
501 (concat (match-string 6 date
)
502 (match-string 7 date
)))
503 ((match-beginning 9) ;; Z
505 (t ;; nil if zone is not provided.
506 (match-string 8 date
))))))
509 (setq cts
(current-time-string (encode-time 0 0 0 day month year
)))
510 (format "%s, %02d %s %04d %s%s"
511 (substring cts
0 3) day
(substring cts
4 7) year time
515 (message-make-date given
))))
519 (defun nnrss-read-server-data (server)
520 (setq nnrss-server-data nil
)
521 (let ((file (nnrss-make-filename "nnrss" server
))
522 (file-name-coding-system nnmail-pathname-coding-system
))
523 (when (file-exists-p file
)
524 (load file nil t t
))))
526 (defun nnrss-save-server-data (server)
527 (gnus-make-directory nnrss-directory
)
528 (let ((coding-system-for-write nnrss-file-coding-system
)
529 (file-name-coding-system nnmail-pathname-coding-system
))
530 (with-temp-file (nnrss-make-filename "nnrss" server
)
531 (insert (format ";; -*- coding: %s; -*-\n"
532 nnrss-file-coding-system
))
533 (gnus-prin1 `(setq nnrss-group-alist
',nnrss-group-alist
))
535 (gnus-prin1 `(setq nnrss-server-data
',nnrss-server-data
)))))
537 (defun nnrss-read-group-data (group server
)
538 (setq nnrss-group-data nil
)
539 (if (hash-table-p nnrss-group-hashtb
)
540 (clrhash nnrss-group-hashtb
)
541 (setq nnrss-group-hashtb
(make-hash-table :test
'equal
)))
542 (let ((pair (assoc group nnrss-server-data
)))
543 (setq nnrss-group-max
(or (cadr pair
) 0))
544 (setq nnrss-group-min
(+ nnrss-group-max
1)))
545 (let ((file (nnrss-make-filename group server
))
546 (file-name-coding-system nnmail-pathname-coding-system
))
547 (when (file-exists-p file
)
549 (dolist (e nnrss-group-data
)
550 (puthash (nth 9 e
) t nnrss-group-hashtb
)
551 (when (and (car e
) (> nnrss-group-min
(car e
)))
552 (setq nnrss-group-min
(car e
)))
553 (when (and (car e
) (< nnrss-group-max
(car e
)))
554 (setq nnrss-group-max
(car e
)))))))
556 (defun nnrss-save-group-data (group server
)
557 (gnus-make-directory nnrss-directory
)
558 (let ((coding-system-for-write nnrss-file-coding-system
)
559 (file-name-coding-system nnmail-pathname-coding-system
))
560 (with-temp-file (nnrss-make-filename group server
)
561 (insert (format ";; -*- coding: %s; -*-\n"
562 nnrss-file-coding-system
))
563 (gnus-prin1 `(setq nnrss-group-data
',nnrss-group-data
)))))
565 (defun nnrss-make-filename (name server
)
567 (nnrss-translate-file-chars
570 (not (equal server
""))
576 (gnus-add-shutdown 'nnrss-close
'gnus
)
578 (defun nnrss-close ()
579 "Clear internal nnrss variables."
580 (setq nnrss-group-data nil
581 nnrss-server-data nil
582 nnrss-group-hashtb nil
583 nnrss-group-alist nil
))
587 (defun nnrss-no-cache (url)
590 (defun nnrss-insert (url)
591 (mm-with-unibyte-current-buffer
594 (error (if (or debug-on-quit debug-on-error
)
595 (signal (car err
) (cdr err
))
596 (message "nnrss: Failed to fetch %s" url
))))))
598 (defun nnrss-decode-entities-string (string)
600 (mm-with-multibyte-buffer
602 (mm-url-decode-entities-nbsp)
605 (defun nnrss-mime-encode-string (string)
606 (mm-with-multibyte-buffer
608 (mm-url-decode-entities-nbsp)
609 (goto-char (point-min))
610 (while (re-search-forward "[\t\n ]+" nil t
)
612 (goto-char (point-min))
613 (skip-chars-forward " ")
614 (delete-region (point-min) (point))
615 (goto-char (point-max))
616 (skip-chars-forward " ")
617 (delete-region (point) (point-max))
618 (let ((rfc2047-encoding-type 'mime
)
619 rfc2047-encode-max-chars
)
620 (rfc2047-encode-region (point-min) (point-max)))
621 (goto-char (point-min))
622 (while (search-forward "\n" nil t
)
627 (defun nnrss-make-hash-index (item)
628 (gnus-message 9 "nnrss: Making hash index of %s" (gnus-prin1-to-string item
))
629 (setq item
(gnus-remove-if
632 (memq (car field
) nnrss-ignore-article-fields
)))
634 (md5 (gnus-prin1-to-string item
)
636 nnrss-file-coding-system
))
638 (defun nnrss-check-group (group server
)
639 (let (file xml subject url extra changed author date feed-subject
640 enclosure comments rss-ns rdf-ns content-ns dc-ns
642 (if (and nnrss-use-local
643 (file-exists-p (setq file
(expand-file-name
644 (nnrss-translate-file-chars
645 (concat group
".xml"))
647 (setq xml
(nnrss-fetch file t
))
648 (setq url
(or (nth 2 (assoc group nnrss-server-data
))
649 (second (assoc group nnrss-group-alist
))))
656 (format "URL to search for %s: " group
) "http://")))))
657 (let ((pair (assoc group nnrss-server-data
)))
659 (setcdr (cdr pair
) (list url
))
660 (push (list group nnrss-group-max url
) nnrss-server-data
)))
662 (setq xml
(nnrss-fetch url
)))
663 (setq dc-ns
(nnrss-get-namespace-prefix xml
"http://purl.org/dc/elements/1.1/")
664 rdf-ns
(nnrss-get-namespace-prefix xml
"http://www.w3.org/1999/02/22-rdf-syntax-ns#")
665 rss-ns
(nnrss-get-namespace-prefix xml
"http://purl.org/rss/1.0/")
666 content-ns
(nnrss-get-namespace-prefix xml
"http://purl.org/rss/1.0/modules/content/"))
667 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns
"item")) xml
)))
668 (when (and (listp item
)
669 (string= (concat rss-ns
"item") (car item
))
670 (progn (setq hash-index
(nnrss-make-hash-index item
))
671 (not (gethash hash-index nnrss-group-hashtb
))))
672 (setq subject
(nnrss-node-text rss-ns
'title item
))
673 (setq url
(nnrss-decode-entities-string
674 (nnrss-node-text rss-ns
'link
(cddr item
))))
675 (setq extra
(or (nnrss-node-text content-ns
'encoded item
)
676 (nnrss-node-text rss-ns
'description item
)))
677 (if (setq feed-subject
(nnrss-node-text dc-ns
'subject item
))
678 (setq extra
(concat feed-subject
"<br /><br />" extra
)))
679 (setq author
(or (nnrss-node-text rss-ns
'author item
)
680 (nnrss-node-text dc-ns
'creator item
)
681 (nnrss-node-text dc-ns
'contributor item
)))
682 (setq date
(nnrss-normalize-date
683 (or (nnrss-node-text dc-ns
'date item
)
684 (nnrss-node-text rss-ns
'pubDate item
))))
685 (setq comments
(nnrss-node-text rss-ns
'comments item
))
686 (when (setq enclosure
(cadr (assq (intern (concat rss-ns
"enclosure")) item
)))
687 (let ((url (cdr (assq 'url enclosure
)))
688 (len (cdr (assq 'length enclosure
)))
689 (type (cdr (assq 'type enclosure
)))
692 (if (and len
(integerp (setq len
(string-to-number len
))))
693 ;; actually already in `ls-lisp-format-file-size' but
694 ;; probably not worth to require it for one function
695 (do ((size (/ len
1.0) (/ size
1024.0))
696 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
699 (format "%.1f%s" size
(car post-fixes
))))
701 (setq url
(or url
""))
702 (setq name
(if (string-match "/\\([^/]*\\)$" url
)
705 (setq type
(or type
""))
706 (setq enclosure
(list url name len type
))))
709 (incf nnrss-group-max
)
712 (and subject
(nnrss-mime-encode-string subject
))
713 (and author
(nnrss-mime-encode-string author
))
715 (and extra
(nnrss-decode-entities-string extra
))
720 (puthash hash-index t nnrss-group-hashtb
)
724 (nnrss-save-group-data group server
)
725 (let ((pair (assoc group nnrss-server-data
)))
727 (setcar (cdr pair
) nnrss-group-max
)
728 (push (list group nnrss-group-max
) nnrss-server-data
)))
729 (nnrss-save-server-data server
))))
731 (declare-function gnus-group-make-rss-group
"gnus-group" (&optional url
))
733 (defun nnrss-opml-import (opml-file)
734 "OPML subscriptions import.
735 Read the file and attempt to subscribe to each Feed in the file."
736 (interactive "fImport file: ")
739 (let ((xmlurl (cdr (assq 'xmlUrl
(cadr node
)))))
741 (not (string-match "\\`[\t ]*\\'" xmlurl
))
743 (y-or-n-p (format "Subscribe to %s " xmlurl
))
747 (gnus-group-make-rss-group xmlurl
)
751 "Failed to subscribe to %s (%s); type any key to continue: "
753 (error-message-string err
))
754 (let ((echo-keystrokes 0))
756 (nnrss-find-el 'outline
757 (mm-with-multibyte-buffer
758 (insert-file-contents opml-file
)
759 (xml-parse-region (point-min) (point-max))))))
761 (defun nnrss-opml-export ()
762 "OPML subscription export.
763 Export subscriptions to a buffer in OPML Format."
765 (with-current-buffer (get-buffer-create "*OPML Export*")
766 (mm-set-buffer-file-coding-system 'utf-8
)
767 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
768 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
769 "<opml version=\"1.1\">\n"
771 " <title>mySubscriptions</title>\n"
772 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
774 " <ownerEmail>" user-mail-address
"</ownerEmail>\n"
775 " <ownerName>" (user-full-name) "</ownerName>\n"
778 (dolist (sub nnrss-group-alist
)
779 (insert " <outline text=\"" (car sub
)
780 "\" xmlUrl=\"" (cadr sub
) "\"/>\n"))
783 (pop-to-buffer "*OPML Export*")
784 (when (fboundp 'sgml-mode
)
787 (defun nnrss-generate-download-script ()
788 "Generate a download script in the current buffer.
789 It is useful when `(setq nnrss-use-local t)'."
791 (insert "#!/bin/sh\n")
792 (insert "WGET=wget\n")
793 (insert "RSSDIR='" (expand-file-name nnrss-directory
) "'\n")
794 (dolist (elem nnrss-server-data
)
795 (let ((url (or (nth 2 elem
)
796 (second (assoc (car elem
) nnrss-group-alist
)))))
797 (insert "$WGET -q -O \"$RSSDIR\"/'"
798 (nnrss-translate-file-chars (concat (car elem
) ".xml"))
801 (defun nnrss-translate-file-chars (name)
802 (let ((nnheader-file-name-translation-alist
803 (append nnheader-file-name-translation-alist
'((?
' . ?_
)))))
804 (nnheader-translate-file-chars name
)))
806 (defun nnrss-node-text (namespace local-name element
)
807 (let* ((node (assq (intern (concat namespace
(symbol-name local-name
)))
809 (text (if (and node
(listp node
))
810 (nnrss-node-just-text node
)
812 (cleaned-text (if text
813 (gnus-replace-in-string
814 (gnus-replace-in-string
815 text
"^[\000-\037\177]+\\|^ +\\| +$" "")
817 (if (string-equal "" cleaned-text
)
821 (defun nnrss-node-just-text (node)
822 (if (and node
(listp node
))
823 (mapconcat 'nnrss-node-just-text
(cddr node
) " ")
826 (defun nnrss-find-el (tag data
&optional found-list
)
827 "Find the all matching elements in the data.
828 Careful with this on large documents!"
832 (when (equal tag
(car bit
))
833 ;; Old xml.el may return a list of string.
834 (when (and (consp (caddr bit
))
835 (stringp (caaddr bit
)))
836 (setcar (cddr bit
) (caaddr bit
)))
840 (if (and (consp (car-safe (caddr bit
)))
841 (not (stringp (caddr bit
))))
849 tag
(cddr bit
))))))))
852 (defun nnrss-rsslink-p (el)
853 "Test if the element we are handed is an RSS autodiscovery link."
854 (and (eq (car-safe el
) 'link
)
855 (string-equal (cdr (assoc 'rel
(cadr el
))) "alternate")
856 (or (string-equal (cdr (assoc 'type
(cadr el
)))
857 "application/rss+xml")
858 (string-equal (cdr (assoc 'type
(cadr el
))) "text/xml"))))
860 (defun nnrss-get-rsslinks (data)
861 "Extract the <link> elements that are links to RSS from the parsed data."
864 (if (nnrss-rsslink-p el
) el
))
865 (nnrss-find-el 'link data
))))
867 (defun nnrss-extract-hrefs (data)
868 "Recursively extract hrefs from a page's source.
869 DATA should be the output of `xml-parse-region'."
870 (mapcar (lambda (ahref)
871 (cdr (assoc 'href
(cadr ahref
))))
872 (nnrss-find-el 'a data
)))
874 (defmacro nnrss-match-macro
(base-uri item onsite-list offsite-list
)
875 `(cond ((or (string-match (concat "^" ,base-uri
) ,item
)
876 (not (string-match "://" ,item
)))
877 (setq ,onsite-list
(append ,onsite-list
(list ,item
))))
878 (t (setq ,offsite-list
(append ,offsite-list
(list ,item
))))))
880 (defun nnrss-order-hrefs (base-uri hrefs
)
881 "Given a list of hrefs, sort them using the following priorities:
882 1. links ending in .rss
883 2. links ending in .rdf
884 3. links ending in .xml
885 4. links containing the above
888 BASE-URI is used to determine the location of the links and
889 whether they are `offsite' or `onsite'."
890 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
891 rss-onsite-in rdf-onsite-in xml-onsite-in
892 rss-offsite-end rdf-offsite-end xml-offsite-end
893 rss-offsite-in rdf-offsite-in xml-offsite-in
)
896 ((string-match "\\.rss$" href
)
898 base-uri href rss-onsite-end rss-offsite-end
))
899 ((string-match "\\.rdf$" href
)
901 base-uri href rdf-onsite-end rdf-offsite-end
))
902 ((string-match "\\.xml$" href
)
904 base-uri href xml-onsite-end xml-offsite-end
))
905 ((string-match "rss" href
)
907 base-uri href rss-onsite-in rss-offsite-in
))
908 ((string-match "rdf" href
)
910 base-uri href rdf-onsite-in rdf-offsite-in
))
911 ((string-match "xml" href
)
913 base-uri href xml-onsite-in xml-offsite-in
))))
915 rss-onsite-end rdf-onsite-end xml-onsite-end
916 rss-onsite-in rdf-onsite-in xml-onsite-in
917 rss-offsite-end rdf-offsite-end xml-offsite-end
918 rss-offsite-in rdf-offsite-in xml-offsite-in
)))
920 (defun nnrss-discover-feed (url)
921 "Given a page, find an RSS feed.
922 Use Mark Pilgrim's `ultra-liberal rss locator'."
923 (let ((parsed-page (nnrss-fetch url
)))
924 ;; 1. if this url is the rss, use it.
925 (if (nnrss-rss-p parsed-page
)
926 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page
"http://purl.org/rss/1.0/")))
927 (nnrss-rss-title-description rss-ns parsed-page url
))
929 ;; 2. look for the <link rel="alternate"
930 ;; type="application/rss+xml" and use that if it is there.
931 (let ((links (nnrss-get-rsslinks parsed-page
)))
933 (let* ((xml (nnrss-fetch
934 (cdr (assoc 'href
(cadar links
)))))
935 (rss-ns (nnrss-get-namespace-prefix
936 xml
"http://purl.org/rss/1.0/")))
937 (nnrss-rss-title-description
938 rss-ns xml
(cdr (assoc 'href
(cadar links
)))))
940 ;; 3. look for links on the site in the following order:
941 ;; - onsite links ending in .rss, .rdf, or .xml
942 ;; - onsite links containing any of the above
943 ;; - offsite links ending in .rss, .rdf, or .xml
944 ;; - offsite links containing any of the above
945 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url
)
946 (match-string 0 url
)))
947 (hrefs (nnrss-order-hrefs
948 base-uri
(nnrss-extract-hrefs parsed-page
)))
950 (while (and (eq rss-link nil
) (not (eq hrefs nil
)))
951 (let ((href-data (nnrss-fetch (car hrefs
))))
952 (if (nnrss-rss-p href-data
)
953 (let* ((rss-ns (nnrss-get-namespace-prefix href-data
"http://purl.org/rss/1.0/")))
954 (setq rss-link
(nnrss-rss-title-description
955 rss-ns href-data
(car hrefs
))))
956 (setq hrefs
(cdr hrefs
)))))
960 (nnrss-find-rss-via-syndic8 url
))))))))
962 (defun nnrss-find-rss-via-syndic8 (url)
963 "Query syndic8 for the rss feeds it has for URL."
964 (if (not (locate-library "xml-rpc"))
966 (message "XML-RPC is not available... not checking Syndic8.")
969 (let ((feedid (xml-rpc-method-call
970 "http://www.syndic8.com/xmlrpc.php"
974 (let* ((feedinfo (xml-rpc-method-call
975 "http://www.syndic8.com/xmlrpc.php"
983 (cdr (assoc "status" listinfo
))
986 (cdr (assoc "sitename" listinfo
))
990 "sitename" listinfo
)))
993 "dataurl" listinfo
)))))))
995 (if (not (> (length urllist
) 1))
997 (let ((completion-ignore-case t
)
999 (mapcar (lambda (listinfo)
1000 (cons (cdr (assoc "sitename" listinfo
))
1002 (cdr (assoc "feedid" listinfo
)))))
1005 (gnus-completing-read
1006 "Multiple feeds found. Select one"
1007 selection t
) urllist
)))))))))
1009 (defun nnrss-rss-p (data)
1010 "Test if DATA is an RSS feed.
1011 Simply ensures that the first element is rss or rdf."
1012 (or (eq (caar data
) 'rss
)
1013 (eq (caar data
) 'rdf
:RDF
)))
1015 (defun nnrss-rss-title-description (rss-namespace data url
)
1016 "Return the title of an RSS feed."
1017 (if (nnrss-rss-p data
)
1018 (let ((description (intern (concat rss-namespace
"description")))
1019 (title (intern (concat rss-namespace
"title")))
1020 (channel (nnrss-find-el (intern (concat rss-namespace
"channel"))
1023 (cons 'description
(caddr (nth 0 (nnrss-find-el description channel
))))
1024 (cons 'title
(caddr (nth 0 (nnrss-find-el title channel
))))
1025 (cons 'href url
)))))
1027 (defun nnrss-get-namespace-prefix (el uri
)
1028 "Given EL (containing a parsed element) and URI (containing a string
1029 that gives the URI for which you want to retrieve the namespace
1030 prefix), return the prefix."
1031 (let* ((prefix (car (rassoc uri
(cadar el
))))
1033 (split-string (symbol-name prefix
) ":")))
1034 (ns (cond ((eq (length nslist
) 1) ; no prefix given
1036 ((eq (length nslist
) 2) ; extract prefix
1038 (if (and ns
(not (string= ns
"")))
1044 ;;; nnrss.el ends here