Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / nnrss.el
blob8115057723c02a2110298ddf6c09b5490e694b5e
1 ;;; nnrss.el --- interfacing with RSS
3 ;; Copyright (C) 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: RSS
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (require 'gnus)
30 (require 'nnoo)
31 (require 'nnmail)
32 (require 'message)
33 (require 'mm-util)
34 (require 'gnus-util)
35 (require 'time-date)
36 (require 'rfc2231)
37 (require 'mm-url)
38 (require 'rfc2047)
39 (require 'mml)
40 (require 'xml)
42 (defgroup nnrss nil
43 "RSS access for Gnus."
44 :group 'gnus)
46 (nnoo-declare nnrss)
48 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
49 "Where nnrss will save its files.")
51 (defvoo nnrss-ignore-article-fields '(slash:comments)
52 "*List of fields that should be ignored when comparing RSS articles.
53 Some RSS feeds update article fields during their lives, e.g. to
54 indicate the number of comments or the number of times the
55 articles have been seen. However, if there is a difference
56 between the local article and the distant one, the latter is
57 considered to be new. To avoid this and discard some fields, set
58 this variable to the list of fields to be ignored.")
60 ;; (group max rss-url)
61 (defvoo nnrss-server-data nil)
63 ;; (num timestamp url subject author date extra)
64 (defvoo nnrss-group-data nil)
65 (defvoo nnrss-group-max 0)
66 (defvoo nnrss-group-min 1)
67 (defvoo nnrss-group nil)
68 (defvoo nnrss-group-hashtb (make-hash-table :test 'equal))
69 (defvoo nnrss-status-string "")
71 (defconst nnrss-version "nnrss 1.0")
73 (defvar nnrss-group-alist '()
74 "List of RSS addresses.")
76 (defvar nnrss-use-local nil
77 "If non-nil nnrss will read the feeds from local files in nnrss-directory.")
79 (defvar nnrss-description-field 'X-Gnus-Description
80 "Field name used for DESCRIPTION.
81 To use the description in headers, put this name into `nnmail-extra-headers'.")
83 (defvar nnrss-url-field 'X-Gnus-Url
84 "Field name used for URL.
85 To use the description in headers, put this name into `nnmail-extra-headers'.")
87 (defvar nnrss-content-function nil
88 "A function which is called in `nnrss-request-article'.
89 The arguments are (ENTRY GROUP ARTICLE).
90 ENTRY is the record of the current headline. GROUP is the group name.
91 ARTICLE is the article number of the current headline.")
93 (defcustom nnrss-file-coding-system mm-universal-coding-system
94 "Coding system used when reading and writing files.
95 If you run Gnus with various versions of Emacsen, the value of this
96 variable should be the coding system that all those Emacsen support.
97 Note that you have to regenerate all the nnrss groups if you change
98 the value. Moreover, you should be patient even if you are made to
99 read the same articles twice, that arises for the difference of the
100 versions of xml.el."
101 :group 'nnrss
102 :type 'coding-system)
104 (defvar nnrss-compatible-encoding-alist
105 (delq nil (mapcar (lambda (elem)
106 (if (and (mm-coding-system-p (car elem))
107 (mm-coding-system-p (cdr elem)))
108 elem))
109 mm-charset-override-alist))
110 "Alist of encodings and those supersets.
111 The cdr of each element is used to decode data if it is available when
112 the car is what the data specify as the encoding. Or, the car is used
113 for decoding when the cdr that the data specify is not available.")
115 (nnoo-define-basics nnrss)
117 ;;; Interface functions
119 (defsubst nnrss-format-string (string)
120 (replace-regexp-in-string " *\n *" " " string))
122 (defun nnrss-decode-group-name (group)
123 (if (and group (mm-coding-system-p 'utf-8))
124 (setq group (decode-coding-string group 'utf-8))
125 group))
127 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
128 (setq group (nnrss-decode-group-name group))
129 (nnrss-possibly-change-group group server)
130 (let (e)
131 (with-current-buffer nntp-server-buffer
132 (erase-buffer)
133 (dolist (article articles)
134 (if (setq e (assq article nnrss-group-data))
135 (insert (number-to-string (car e)) "\t" ;; number
136 ;; subject
137 (or (nth 3 e) "")
138 "\t"
139 ;; from
140 (or (nth 4 e) "(nobody)")
141 "\t"
142 ;; date
143 (or (nth 5 e) "")
144 "\t"
145 ;; id
146 (format "<%d@%s.nnrss>" (car e) group)
147 "\t"
148 ;; refs
149 "\t"
150 ;; chars
151 "-1" "\t"
152 ;; lines
153 "-1" "\t"
154 ;; Xref
155 "" "\t"
156 (if (and (nth 6 e)
157 (memq nnrss-description-field
158 nnmail-extra-headers))
159 (concat (symbol-name nnrss-description-field)
160 ": "
161 (nnrss-format-string (nth 6 e))
162 "\t")
164 (if (and (nth 2 e)
165 (memq nnrss-url-field
166 nnmail-extra-headers))
167 (concat (symbol-name nnrss-url-field)
168 ": "
169 (nnrss-format-string (nth 2 e))
170 "\t")
172 "\n")))))
173 'nov)
175 (deffoo nnrss-request-group (group &optional server dont-check info)
176 (setq group (nnrss-decode-group-name group))
177 (nnheader-message 6 "nnrss: Requesting %s..." group)
178 (nnrss-possibly-change-group group server)
179 (prog1
180 (if dont-check
182 (nnrss-check-group group server)
183 (nnheader-report 'nnrss "Opened group %s" group)
184 (nnheader-insert
185 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
186 (prin1-to-string group)
188 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
190 (deffoo nnrss-close-group (group &optional server)
193 (deffoo nnrss-request-article (article &optional group server buffer)
194 (setq group (nnrss-decode-group-name group))
195 (when (stringp article)
196 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
197 (string-to-number (match-string 1 article))
198 0)))
199 (nnrss-possibly-change-group group server)
200 (let ((e (assq article nnrss-group-data))
201 (nntp-server-buffer (or buffer nntp-server-buffer))
202 post err)
203 (when e
204 (with-current-buffer nntp-server-buffer
205 (erase-buffer)
206 (if group
207 (insert "Newsgroups: " group "\n"))
208 (if (nth 3 e)
209 (insert "Subject: " (nth 3 e) "\n"))
210 (if (nth 4 e)
211 (insert "From: " (nth 4 e) "\n"))
212 (if (nth 5 e)
213 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
214 (let ((header (buffer-string))
215 (text (nth 6 e))
216 (link (nth 2 e))
217 (enclosure (nth 7 e))
218 (comments (nth 8 e))
219 (rfc2047-header-encoding-alist
220 (if (mm-coding-system-p 'utf-8)
221 (cons '("Newsgroups" . utf-8)
222 rfc2047-header-encoding-alist)
223 rfc2047-header-encoding-alist))
224 rfc2047-encode-encoded-words body fn)
225 (when (or text link enclosure comments)
226 (insert "\n")
227 (insert "<#multipart type=alternative>\n"
228 "<#part type=\"text/plain\">\n")
229 (setq body (point))
230 (when text
231 (insert text)
232 (goto-char body)
233 (while (re-search-forward "\n+" nil t)
234 (replace-match " "))
235 (goto-char body)
236 ;; See `nnrss-check-group', which inserts "<br /><br />".
237 (when (search-forward "<br /><br />" nil t)
238 (if (eobp)
239 (replace-match "\n")
240 (replace-match "\n\n")))
241 (unless (eobp)
242 (let ((fill-column (default-value 'fill-column))
243 (window (get-buffer-window nntp-server-buffer)))
244 (when window
245 (setq fill-column
246 (max 1 (/ (* (window-width window) 7) 8))))
247 (fill-region (point) (point-max))
248 (goto-char (point-max))
249 (unless (bolp)
250 (insert "\n"))))
251 (when (or link enclosure)
252 (insert "\n")))
253 (when link
254 (insert link "\n"))
255 (when enclosure
256 (insert (car enclosure) " "
257 (nth 2 enclosure) " "
258 (nth 3 enclosure) "\n"))
259 (when comments
260 (insert comments "\n"))
261 (setq body (buffer-substring body (point)))
262 (insert "<#/part>\n"
263 "<#part type=\"text/html\">\n"
264 "<html><head></head><body>\n")
265 (when text
266 (insert text "\n"))
267 (when link
268 (insert "<p><a href=\"" link "\">link</a></p>\n"))
269 (when enclosure
270 (insert "<p><a href=\"" (car enclosure) "\">"
271 (cadr enclosure) "</a> " (nth 2 enclosure)
272 " " (nth 3 enclosure) "</p>\n"))
273 (when comments
274 (insert "<p><a href=\"" comments "\">comments</a></p>\n"))
275 (insert "</body></html>\n"
276 "<#/part>\n"
277 "<#/multipart>\n"))
278 (condition-case nil
279 ;; Allow `mml-to-mime' to generate MIME article without
280 ;; making inquiry to a user for unknown encoding.
281 (let ((mml-confirmation-set
282 (cons 'unknown-encoding mml-confirmation-set)))
283 (mml-to-mime))
284 (error
285 (erase-buffer)
286 (insert header
287 "Content-Type: text/plain; charset=gnus-decoded\n"
288 "Content-Transfer-Encoding: 8bit\n\n"
289 body)
290 (nnheader-message
291 3 "Warning - there might be invalid characters"))))
292 (goto-char (point-min))
293 (search-forward "\n\n")
294 (forward-line -1)
295 (insert (format "Message-ID: <%d@%s.nnrss>\n"
296 (car e)
297 (let ((rfc2047-encoding-type 'mime)
298 rfc2047-encode-max-chars)
299 (rfc2047-encode-string
300 (replace-regexp-in-string "[\t\n ]+" "_" group)))))
301 (when nnrss-content-function
302 (funcall nnrss-content-function e group article))))
303 (cond
304 (err
305 (nnheader-report 'nnrss err))
306 ((not e)
307 (nnheader-report 'nnrss "no such id: %d" article))
309 (nnheader-report 'nnrss "article %s retrieved" (car e))
310 ;; we return the article number.
311 (cons nnrss-group (car e))))))
313 (deffoo nnrss-open-server (server &optional defs connectionless)
314 (nnrss-read-server-data server)
315 (nnoo-change-server 'nnrss server defs)
318 (deffoo nnrss-request-expire-articles
319 (articles group &optional server force)
320 (setq group (nnrss-decode-group-name group))
321 (nnrss-possibly-change-group group server)
322 (let (e days not-expirable changed)
323 (dolist (art articles)
324 (if (and (setq e (assq art nnrss-group-data))
325 (nnmail-expired-article-p
326 group
327 (if (listp (setq days (nth 1 e))) days
328 (days-to-time (- days (time-to-days '(0 0)))))
329 force))
330 (setq nnrss-group-data (delq e nnrss-group-data)
331 changed t)
332 (push art not-expirable)))
333 (if changed
334 (nnrss-save-group-data group server))
335 not-expirable))
337 (deffoo nnrss-request-delete-group (group &optional force server)
338 (setq group (nnrss-decode-group-name group))
339 (nnrss-possibly-change-group group server)
340 (let (elem)
341 ;; There may be two or more entries in `nnrss-group-alist' since
342 ;; this function didn't delete them formerly.
343 (while (setq elem (assoc group nnrss-group-alist))
344 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
345 (setq nnrss-server-data
346 (delq (assoc group nnrss-server-data) nnrss-server-data))
347 (nnrss-save-server-data server)
348 (ignore-errors
349 (let ((file-name-coding-system nnmail-pathname-coding-system))
350 (delete-file (nnrss-make-filename group server))))
353 (deffoo nnrss-request-list-newsgroups (&optional server)
354 (nnrss-possibly-change-group nil server)
355 (with-current-buffer nntp-server-buffer
356 (erase-buffer)
357 (dolist (elem nnrss-group-alist)
358 (if (third elem)
359 (insert (car elem) "\t" (third elem) "\n"))))
362 (deffoo nnrss-retrieve-groups (groups &optional server)
363 (dolist (group groups)
364 (setq group (nnrss-decode-group-name group))
365 (nnrss-possibly-change-group group server)
366 (nnrss-check-group group server))
367 (with-current-buffer nntp-server-buffer
368 (erase-buffer)
369 (dolist (group groups)
370 (let ((elem (assoc (gnus-group-decoded-name group) nnrss-server-data)))
371 (insert (format "%S %s 1 y\n" group (or (cadr elem) 0)))))
372 'active))
374 (nnoo-define-skeleton nnrss)
376 ;;; Internal functions
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=\\(?:\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
386 nil t)
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 discard-comments))
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
405 (if local
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))
411 (condition-case err
412 (mm-url-insert url)
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))
419 (insert (prog1
420 (decode-coding-string (buffer-string) cs)
421 (erase-buffer)
422 (mm-enable-multibyte))))
423 (goto-char (point-min))
425 (condition-case err1
426 (setq xmlform (xml-parse-region (point-min) (point-max)))
427 (error
428 (condition-case err2
429 (setq htmlform (libxml-parse-html-region (point-min) (point-max)))
430 (error
431 (message "\
432 nnrss: %s: Not valid XML %s and libxml-parse-html-region doesn't work %s"
433 url err1 err2)))))
434 (if htmlform
435 htmlform
436 xmlform))))
438 (defun nnrss-possibly-change-group (&optional group server)
439 (when (and 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))))
458 ;; RFC822
459 ((string-match " [0-9]+ " date)
460 (setq vector (timezone-parse-date date)
461 year (string-to-number (aref vector 0)))
462 (when (>= year 1969)
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)))
469 (setq zone nil))))
470 ;; ISO 8601
471 ((string-match
472 (eval-when-compile
473 (concat
474 ;; 1. year
475 "\\(199[0-9]\\|20[0-9][0-9]\\)"
476 "\\(?:-"
477 ;; 2. month
478 "\\([01][0-9]\\)"
479 "\\(?:-"
480 ;; 3. day
481 "\\([0-3][0-9]\\)"
482 "\\)?\\)?\\(?:T"
483 ;; 4. hh:mm
484 "\\([012][0-9]:[0-5][0-9]\\)"
485 "\\(?:"
486 ;; 5. :ss
487 "\\(:[0-5][0-9]\\)"
488 "\\(?:\\.[0-9]+\\)?\\)?\\)?"
489 ;; 6+7,8,9. zone
490 "\\(?:\\(?:\\([+-][012][0-9]\\):\\([0-5][0-9]\\)\\)"
491 "\\|\\([+-][012][0-9][0-5][0-9]\\)"
492 "\\|\\(Z\\)\\)?"))
493 date)
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
504 "+0000")
505 (t ;; nil if zone is not provided.
506 (match-string 8 date))))))
507 (if month
508 (progn
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
512 (if zone
513 (concat " " zone)
514 "")))
515 (message-make-date given))))
517 ;;; data functions
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))
534 (insert "\n")
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)
548 (load file nil t t)
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)
566 (expand-file-name
567 (nnrss-translate-file-chars
568 (concat name
569 (and server
570 (not (equal server ""))
571 "-")
572 server
573 ".el"))
574 nnrss-directory))
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))
585 ;;; URL interface
587 (defun nnrss-no-cache (url)
590 (defun nnrss-insert (url)
591 (condition-case err
592 (mm-url-insert url)
593 (error (if (or debug-on-quit debug-on-error)
594 (signal (car err) (cdr err))
595 (message "nnrss: Failed to fetch %s" url)))))
597 (defun nnrss-decode-entities-string (string)
598 (if string
599 (mm-with-multibyte-buffer
600 (insert string)
601 (mm-url-decode-entities-nbsp)
602 (buffer-string))))
604 (defun nnrss-mime-encode-string (string)
605 (mm-with-multibyte-buffer
606 (insert string)
607 (mm-url-decode-entities-nbsp)
608 (goto-char (point-min))
609 (while (re-search-forward "[\t\n ]+" nil t)
610 (replace-match " "))
611 (goto-char (point-min))
612 (skip-chars-forward " ")
613 (delete-region (point-min) (point))
614 (goto-char (point-max))
615 (skip-chars-forward " ")
616 (delete-region (point) (point-max))
617 (let ((rfc2047-encoding-type 'mime)
618 rfc2047-encode-max-chars)
619 (rfc2047-encode-region (point-min) (point-max)))
620 (goto-char (point-min))
621 (while (search-forward "\n" nil t)
622 (delete-char -1))
623 (buffer-string)))
625 ;;; Snarf functions
626 (defun nnrss-make-hash-index (item)
627 (gnus-message 9 "nnrss: Making hash index of %s" (gnus-prin1-to-string item))
628 (setq item (gnus-remove-if
629 (lambda (field)
630 (when (listp field)
631 (memq (car field) nnrss-ignore-article-fields)))
632 item))
633 (md5 (gnus-prin1-to-string item)
634 nil nil
635 nnrss-file-coding-system))
637 (defun nnrss-check-group (group server)
638 (let (file xml subject url extra changed author date feed-subject
639 enclosure comments rss-ns rdf-ns content-ns dc-ns
640 hash-index)
641 (if (and nnrss-use-local
642 (file-exists-p (setq file (expand-file-name
643 (nnrss-translate-file-chars
644 (concat group ".xml"))
645 nnrss-directory))))
646 (setq xml (nnrss-fetch file t))
647 (setq url (or (nth 2 (assoc group nnrss-server-data))
648 (second (assoc group nnrss-group-alist))))
649 (unless url
650 (setq url
651 (cdr
652 (assoc 'href
653 (nnrss-discover-feed
654 (read-string
655 (format "URL to search for %s: " group) "http://")))))
656 (let ((pair (assoc group nnrss-server-data)))
657 (if pair
658 (setcdr (cdr pair) (list url))
659 (push (list group nnrss-group-max url) nnrss-server-data)))
660 (setq changed t))
661 (setq xml (nnrss-fetch url)))
662 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
663 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
664 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
665 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
666 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
667 (when (and (listp item)
668 (string= (concat rss-ns "item") (car item))
669 (progn (setq hash-index (nnrss-make-hash-index item))
670 (not (gethash hash-index nnrss-group-hashtb))))
671 (setq subject (nnrss-node-text rss-ns 'title item))
672 (setq url (nnrss-decode-entities-string
673 (nnrss-node-text rss-ns 'link (cddr item))))
674 (setq extra (or (nnrss-node-text content-ns 'encoded item)
675 (nnrss-node-text rss-ns 'description item)))
676 (if (setq feed-subject (nnrss-node-text dc-ns 'subject item))
677 (setq extra (concat feed-subject "<br /><br />" extra)))
678 (setq author (or (nnrss-node-text rss-ns 'author item)
679 (nnrss-node-text dc-ns 'creator item)
680 (nnrss-node-text dc-ns 'contributor item)))
681 (setq date (nnrss-normalize-date
682 (or (nnrss-node-text dc-ns 'date item)
683 (nnrss-node-text rss-ns 'pubDate item))))
684 (setq comments (nnrss-node-text rss-ns 'comments item))
685 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
686 (let ((url (cdr (assq 'url enclosure)))
687 (len (cdr (assq 'length enclosure)))
688 (type (cdr (assq 'type enclosure)))
689 (name))
690 (setq len
691 (if (and len (integerp (setq len (string-to-number len))))
692 ;; actually already in `ls-lisp-format-file-size' but
693 ;; probably not worth to require it for one function
694 (do ((size (/ len 1.0) (/ size 1024.0))
695 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
696 (cdr post-fixes)))
697 ((< size 1024)
698 (format "%.1f%s" size (car post-fixes))))
699 "0"))
700 (setq url (or url ""))
701 (setq name (if (string-match "/\\([^/]*\\)$" url)
702 (match-string 1 url)
703 "file"))
704 (setq type (or type ""))
705 (setq enclosure (list url name len type))))
706 (push
707 (list
708 (incf nnrss-group-max)
709 (current-time)
711 (and subject (nnrss-mime-encode-string subject))
712 (and author (nnrss-mime-encode-string author))
713 date
714 (and extra (nnrss-decode-entities-string extra))
715 enclosure
716 comments
717 hash-index)
718 nnrss-group-data)
719 (puthash hash-index t nnrss-group-hashtb)
720 (setq changed t))
721 (setq extra nil))
722 (when changed
723 (nnrss-save-group-data group server)
724 (let ((pair (assoc group nnrss-server-data)))
725 (if pair
726 (setcar (cdr pair) nnrss-group-max)
727 (push (list group nnrss-group-max) nnrss-server-data)))
728 (nnrss-save-server-data server))))
730 (declare-function gnus-group-make-rss-group "gnus-group" (&optional url))
732 (defun nnrss-opml-import (opml-file)
733 "OPML subscriptions import.
734 Read the file and attempt to subscribe to each Feed in the file."
735 (interactive "fImport file: ")
736 (mapc
737 (lambda (node)
738 (let ((xmlurl (cdr (assq 'xmlUrl (cadr node)))))
739 (when (and xmlurl
740 (not (string-match "\\`[\t ]*\\'" xmlurl))
741 (prog1
742 (y-or-n-p (format "Subscribe to %s " xmlurl))
743 (message "")))
744 (condition-case err
745 (progn
746 (gnus-group-make-rss-group xmlurl)
747 (forward-line 1))
748 (error
749 (message
750 "Failed to subscribe to %s (%s); type any key to continue: "
751 xmlurl
752 (error-message-string err))
753 (let ((echo-keystrokes 0))
754 (read-char)))))))
755 (nnrss-find-el 'outline
756 (mm-with-multibyte-buffer
757 (insert-file-contents opml-file)
758 (xml-parse-region (point-min) (point-max))))))
760 (defun nnrss-opml-export ()
761 "OPML subscription export.
762 Export subscriptions to a buffer in OPML Format."
763 (interactive)
764 (with-current-buffer (get-buffer-create "*OPML Export*")
765 (set-buffer-file-coding-system 'utf-8)
766 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
767 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
768 "<opml version=\"1.1\">\n"
769 " <head>\n"
770 " <title>mySubscriptions</title>\n"
771 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
772 "</dateCreated>\n"
773 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
774 " <ownerName>" (user-full-name) "</ownerName>\n"
775 " </head>\n"
776 " <body>\n")
777 (dolist (sub nnrss-group-alist)
778 (insert " <outline text=\"" (car sub)
779 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
780 (insert " </body>\n"
781 "</opml>\n"))
782 (pop-to-buffer "*OPML Export*")
783 (when (fboundp 'sgml-mode)
784 (sgml-mode)))
786 (defun nnrss-generate-download-script ()
787 "Generate a download script in the current buffer.
788 It is useful when `(setq nnrss-use-local t)'."
789 (interactive)
790 (insert "#!/bin/sh\n")
791 (insert "WGET=wget\n")
792 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
793 (dolist (elem nnrss-server-data)
794 (let ((url (or (nth 2 elem)
795 (second (assoc (car elem) nnrss-group-alist)))))
796 (insert "$WGET -q -O \"$RSSDIR\"/'"
797 (nnrss-translate-file-chars (concat (car elem) ".xml"))
798 "' '" url "'\n"))))
800 (defun nnrss-translate-file-chars (name)
801 (let ((nnheader-file-name-translation-alist
802 (append nnheader-file-name-translation-alist '((?' . ?_)))))
803 (nnheader-translate-file-chars name)))
805 (defun nnrss-node-text (namespace local-name element)
806 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
807 element))
808 (text (if (and node (listp node))
809 (nnrss-node-just-text node)
810 node))
811 (cleaned-text (if text
812 (replace-regexp-in-string
813 "\r\n" "\n"
814 (replace-regexp-in-string
815 "^[\000-\037\177]+\\|^ +\\| +$" ""
816 text)))))
817 (if (string-equal "" cleaned-text)
819 cleaned-text)))
821 (defun nnrss-node-just-text (node)
822 (if (and node (listp node))
823 (mapconcat 'nnrss-node-just-text (cddr node) " ")
824 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!"
829 (when (consp data)
830 (dolist (bit data)
831 (when (car-safe bit)
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)))
837 (setq found-list
838 (append found-list
839 (list bit))))
840 (if (and (consp (car-safe (caddr bit)))
841 (not (stringp (caddr bit))))
842 (setq found-list
843 (append found-list
844 (nnrss-find-el
845 tag (caddr bit))))
846 (setq found-list
847 (append found-list
848 (nnrss-find-el
849 tag (cddr bit))))))))
850 found-list)
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."
862 (delq nil (mapcar
863 (lambda (el)
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
886 5. offsite links
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)
894 (dolist (href hrefs)
895 (cond ((null href))
896 ((string-match "\\.rss$" href)
897 (nnrss-match-macro
898 base-uri href rss-onsite-end rss-offsite-end))
899 ((string-match "\\.rdf$" href)
900 (nnrss-match-macro
901 base-uri href rdf-onsite-end rdf-offsite-end))
902 ((string-match "\\.xml$" href)
903 (nnrss-match-macro
904 base-uri href xml-onsite-end xml-offsite-end))
905 ((string-match "rss" href)
906 (nnrss-match-macro
907 base-uri href rss-onsite-in rss-offsite-in))
908 ((string-match "rdf" href)
909 (nnrss-match-macro
910 base-uri href rdf-onsite-in rdf-offsite-in))
911 ((string-match "xml" href)
912 (nnrss-match-macro
913 base-uri href xml-onsite-in xml-offsite-in))))
914 (append
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)))
932 (if links
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)))
949 (rss-link nil))
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)))))
957 (if rss-link
958 rss-link
959 ;; 4. check syndic8
960 (nnrss-find-rss-via-syndic8 url))))))))
962 (declare-function xml-rpc-method-call "ext:xml-rpc"
963 (server-url method &rest params))
965 (defun nnrss-find-rss-via-syndic8 (url)
966 "Query syndic8 for the rss feeds it has for URL."
967 (if (not (locate-library "xml-rpc"))
968 (progn
969 (message "XML-RPC is not available... not checking Syndic8.")
970 nil)
971 (require 'xml-rpc)
972 (let ((feedid (xml-rpc-method-call
973 "http://www.syndic8.com/xmlrpc.php"
974 'syndic8.FindSites
975 url)))
976 (when feedid
977 (let* ((feedinfo (xml-rpc-method-call
978 "http://www.syndic8.com/xmlrpc.php"
979 'syndic8.GetFeedInfo
980 feedid))
981 (urllist
982 (delq nil
983 (mapcar
984 (lambda (listinfo)
985 (if (string-equal
986 (cdr (assoc "status" listinfo))
987 "Syndicated")
988 (cons
989 (cdr (assoc "sitename" listinfo))
990 (list
991 (cons 'title
992 (cdr (assoc
993 "sitename" listinfo)))
994 (cons 'href
995 (cdr (assoc
996 "dataurl" listinfo)))))))
997 feedinfo))))
998 (if (not (> (length urllist) 1))
999 (cdar urllist)
1000 (let ((completion-ignore-case t)
1001 (selection
1002 (mapcar (lambda (listinfo)
1003 (cons (cdr (assoc "sitename" listinfo))
1004 (string-to-number
1005 (cdr (assoc "feedid" listinfo)))))
1006 feedinfo)))
1007 (cdr (assoc
1008 (gnus-completing-read
1009 "Multiple feeds found. Select one"
1010 selection t) urllist)))))))))
1012 (defun nnrss-rss-p (data)
1013 "Test if DATA is an RSS feed.
1014 Simply ensures that the first element is rss or rdf."
1015 (or (eq (caar data) 'rss)
1016 (eq (caar data) 'rdf:RDF)))
1018 (defun nnrss-rss-title-description (rss-namespace data url)
1019 "Return the title of an RSS feed."
1020 (if (nnrss-rss-p data)
1021 (let ((description (intern (concat rss-namespace "description")))
1022 (title (intern (concat rss-namespace "title")))
1023 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
1024 data)))
1025 (list
1026 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
1027 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
1028 (cons 'href url)))))
1030 (defun nnrss-get-namespace-prefix (el uri)
1031 "Given EL (containing a parsed element) and URI (containing a string
1032 that gives the URI for which you want to retrieve the namespace
1033 prefix), return the prefix."
1034 (let* ((prefix (car (rassoc uri (cadar el))))
1035 (nslist (if prefix
1036 (split-string (symbol-name prefix) ":")))
1037 (ns (cond ((eq (length nslist) 1) ; no prefix given
1039 ((eq (length nslist) 2) ; extract prefix
1040 (cadr nslist)))))
1041 (if (and ns (not (string= ns "")))
1042 (concat ns ":")
1043 ns)))
1045 (provide 'nnrss)
1047 ;;; nnrss.el ends here