(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / gnus / nnrss.el
blobee5d65603541b8e519da24d8821f47d3445acbdd
1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: RSS
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (require 'gnus)
31 (require 'nnoo)
32 (require 'nnmail)
33 (require 'message)
34 (require 'mm-util)
35 (require 'gnus-util)
36 (require 'time-date)
37 (require 'rfc2231)
38 (require 'mm-url)
39 (require 'rfc2047)
40 (require 'mml)
41 (eval-when-compile
42 (ignore-errors
43 (require 'xml)))
44 (eval '(require 'xml))
46 (nnoo-declare nnrss)
48 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
49 "Where nnrss will save its files.")
51 ;; (group max rss-url)
52 (defvoo nnrss-server-data nil)
54 ;; (num timestamp url subject author date extra)
55 (defvoo nnrss-group-data nil)
56 (defvoo nnrss-group-max 0)
57 (defvoo nnrss-group-min 1)
58 (defvoo nnrss-group nil)
59 (defvoo nnrss-group-hashtb nil)
60 (defvoo nnrss-status-string "")
62 (defconst nnrss-version "nnrss 1.0")
64 (defvar nnrss-group-alist '()
65 "List of RSS addresses.")
67 (defvar nnrss-use-local nil)
69 (defvar nnrss-description-field 'X-Gnus-Description
70 "Field name used for DESCRIPTION.
71 To use the description in headers, put this name into `nnmail-extra-headers'.")
73 (defvar nnrss-url-field 'X-Gnus-Url
74 "Field name used for URL.
75 To use the description in headers, put this name into `nnmail-extra-headers'.")
77 (defvar nnrss-content-function nil
78 "A function which is called in `nnrss-request-article'.
79 The arguments are (ENTRY GROUP ARTICLE).
80 ENTRY is the record of the current headline. GROUP is the group name.
81 ARTICLE is the article number of the current headline.")
83 (defvar nnrss-file-coding-system mm-universal-coding-system
84 "Coding system used when reading and writing files.")
86 (defvar nnrss-compatible-encoding-alist '((iso-8859-1 . windows-1252))
87 "Alist of encodings and those supersets.
88 The cdr of each element is used to decode data if it is available when
89 the car is what the data specify as the encoding. Or, the car is used
90 for decoding when the cdr that the data specify is not available.")
92 (nnoo-define-basics nnrss)
94 ;;; Interface functions
96 (defsubst nnrss-format-string (string)
97 (gnus-replace-in-string string " *\n *" " "))
99 (defun nnrss-decode-group-name (group)
100 (if (and group (mm-coding-system-p 'utf-8))
101 (setq group (mm-decode-coding-string group 'utf-8))
102 group))
104 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
105 (setq group (nnrss-decode-group-name group))
106 (nnrss-possibly-change-group group server)
107 (let (e)
108 (save-excursion
109 (set-buffer nntp-server-buffer)
110 (erase-buffer)
111 (dolist (article articles)
112 (if (setq e (assq article nnrss-group-data))
113 (insert (number-to-string (car e)) "\t" ;; number
114 ;; subject
115 (or (nth 3 e) "")
116 "\t"
117 ;; from
118 (or (nth 4 e) "(nobody)")
119 "\t"
120 ;; date
121 (or (nth 5 e) "")
122 "\t"
123 ;; id
124 (format "<%d@%s.nnrss>" (car e) group)
125 "\t"
126 ;; refs
127 "\t"
128 ;; chars
129 "-1" "\t"
130 ;; lines
131 "-1" "\t"
132 ;; Xref
133 "" "\t"
134 (if (and (nth 6 e)
135 (memq nnrss-description-field
136 nnmail-extra-headers))
137 (concat (symbol-name nnrss-description-field)
138 ": "
139 (nnrss-format-string (nth 6 e))
140 "\t")
142 (if (and (nth 2 e)
143 (memq nnrss-url-field
144 nnmail-extra-headers))
145 (concat (symbol-name nnrss-url-field)
146 ": "
147 (nnrss-format-string (nth 2 e))
148 "\t")
150 "\n")))))
151 'nov)
153 (deffoo nnrss-request-group (group &optional server dont-check)
154 (setq group (nnrss-decode-group-name group))
155 (nnheader-message 6 "nnrss: Requesting %s..." group)
156 (nnrss-possibly-change-group group server)
157 (prog1
158 (if dont-check
160 (nnrss-check-group group server)
161 (nnheader-report 'nnrss "Opened group %s" group)
162 (nnheader-insert
163 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
164 (prin1-to-string group)
166 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
168 (deffoo nnrss-close-group (group &optional server)
171 (deffoo nnrss-request-article (article &optional group server buffer)
172 (setq group (nnrss-decode-group-name group))
173 (when (stringp article)
174 (setq article (if (string-match "\\`<\\([0-9]+\\)@" article)
175 (string-to-number (match-string 1 article))
176 0)))
177 (nnrss-possibly-change-group group server)
178 (let ((e (assq article nnrss-group-data))
179 (nntp-server-buffer (or buffer nntp-server-buffer))
180 post err)
181 (when e
182 (with-current-buffer nntp-server-buffer
183 (erase-buffer)
184 (if group
185 (insert "Newsgroups: " group "\n"))
186 (if (nth 3 e)
187 (insert "Subject: " (nth 3 e) "\n"))
188 (if (nth 4 e)
189 (insert "From: " (nth 4 e) "\n"))
190 (if (nth 5 e)
191 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
192 (let ((header (buffer-string))
193 (text (if (nth 6 e)
194 (mapconcat 'identity
195 (delete "" (split-string (nth 6 e) "\n+"))
196 " ")))
197 (link (nth 2 e))
198 (enclosure (nth 7 e))
199 ;; Enable encoding of Newsgroups header in XEmacs.
200 (default-enable-multibyte-characters t)
201 (rfc2047-header-encoding-alist
202 (if (mm-coding-system-p 'utf-8)
203 (cons '("Newsgroups" . utf-8)
204 rfc2047-header-encoding-alist)
205 rfc2047-header-encoding-alist))
206 rfc2047-encode-encoded-words body)
207 (when (or text link enclosure)
208 (insert "\n")
209 (insert "<#multipart type=alternative>\n"
210 "<#part type=\"text/plain\">\n")
211 (setq body (point))
212 (when text
213 (insert text "\n")
214 (when (or link enclosure)
215 (insert "\n")))
216 (when link
217 (insert link "\n"))
218 (when enclosure
219 (insert (car enclosure) " "
220 (nth 2 enclosure) " "
221 (nth 3 enclosure) "\n"))
222 (setq body (buffer-substring body (point)))
223 (insert "<#/part>\n"
224 "<#part type=\"text/html\">\n"
225 "<html><head></head><body>\n")
226 (when text
227 (insert text "\n"))
228 (when link
229 (insert "<p><a href=\"" link "\">link</a></p>\n"))
230 (when enclosure
231 (insert "<p><a href=\"" (car enclosure) "\">"
232 (cadr enclosure) "</a> " (nth 2 enclosure)
233 " " (nth 3 enclosure) "</p>\n"))
234 (insert "</body></html>\n"
235 "<#/part>\n"
236 "<#/multipart>\n"))
237 (condition-case nil
238 (mml-to-mime)
239 (error
240 (erase-buffer)
241 (insert header
242 "Content-Type: text/plain; charset=gnus-decoded\n"
243 "Content-Transfer-Encoding: 8bit\n\n"
244 body)
245 (nnheader-message
246 3 "Warning - there might be invalid characters"))))
247 (goto-char (point-min))
248 (search-forward "\n\n")
249 (forward-line -1)
250 (insert (format "Message-ID: <%d@%s.nnrss>\n"
251 (car e)
252 (let ((rfc2047-encoding-type 'mime)
253 rfc2047-encode-max-chars)
254 (rfc2047-encode-string
255 (gnus-replace-in-string group "[\t\n ]+" "_")))))
256 (when nnrss-content-function
257 (funcall nnrss-content-function e group article))))
258 (cond
259 (err
260 (nnheader-report 'nnrss err))
261 ((not e)
262 (nnheader-report 'nnrss "no such id: %d" article))
264 (nnheader-report 'nnrss "article %s retrieved" (car e))
265 ;; we return the article number.
266 (cons nnrss-group (car e))))))
268 (deffoo nnrss-request-list (&optional server)
269 (nnrss-possibly-change-group nil server)
270 (nnrss-generate-active)
273 (deffoo nnrss-open-server (server &optional defs connectionless)
274 (nnrss-read-server-data server)
275 (nnoo-change-server 'nnrss server defs)
278 (deffoo nnrss-request-expire-articles
279 (articles group &optional server force)
280 (setq group (nnrss-decode-group-name group))
281 (nnrss-possibly-change-group group server)
282 (let (e days not-expirable changed)
283 (dolist (art articles)
284 (if (and (setq e (assq art nnrss-group-data))
285 (nnmail-expired-article-p
286 group
287 (if (listp (setq days (nth 1 e))) days
288 (days-to-time (- days (time-to-days '(0 0)))))
289 force))
290 (setq nnrss-group-data (delq e nnrss-group-data)
291 changed t)
292 (push art not-expirable)))
293 (if changed
294 (nnrss-save-group-data group server))
295 not-expirable))
297 (deffoo nnrss-request-delete-group (group &optional force server)
298 (setq group (nnrss-decode-group-name group))
299 (nnrss-possibly-change-group group server)
300 (let (elem)
301 ;; There may be two or more entries in `nnrss-group-alist' since
302 ;; this function didn't delete them formerly.
303 (while (setq elem (assoc group nnrss-group-alist))
304 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
305 (setq nnrss-server-data
306 (delq (assoc group nnrss-server-data) nnrss-server-data))
307 (nnrss-save-server-data server)
308 (ignore-errors
309 (delete-file (nnrss-make-filename group server)))
312 (deffoo nnrss-request-list-newsgroups (&optional server)
313 (nnrss-possibly-change-group nil server)
314 (save-excursion
315 (set-buffer nntp-server-buffer)
316 (erase-buffer)
317 (dolist (elem nnrss-group-alist)
318 (if (third elem)
319 (insert (car elem) "\t" (third elem) "\n"))))
322 (nnoo-define-skeleton nnrss)
324 ;;; Internal functions
325 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
327 (defun nnrss-get-encoding ()
328 "Return an encoding attribute specified in the current xml contents.
329 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
330 it is used instead. If the xml contents doesn't specify the encoding,
331 return `utf-8' which is the default encoding for xml if it is available,
332 otherwise return nil."
333 (goto-char (point-min))
334 (if (re-search-forward
335 "<\\?[^>]*encoding=\\(\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
336 nil t)
337 (let ((encoding (intern (downcase (or (match-string 2)
338 (match-string 3))))))
340 (mm-coding-system-p (cdr (assq encoding
341 nnrss-compatible-encoding-alist)))
342 (mm-coding-system-p encoding)
343 (mm-coding-system-p (car (rassq encoding
344 nnrss-compatible-encoding-alist)))))
345 (mm-coding-system-p 'utf-8)))
347 (defun nnrss-fetch (url &optional local)
348 "Fetch URL and put it in a the expected Lisp structure."
349 (mm-with-unibyte-buffer
350 ;;some CVS versions of url.el need this to close the connection quickly
351 (let (cs xmlform htmlform)
352 ;; bit o' work necessary for w3 pre-cvs and post-cvs
353 (if local
354 (let ((coding-system-for-read 'binary))
355 (insert-file-contents url))
356 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
357 ;; to `mm-url-insert'?
358 (let ((coding-system-for-read 'binary))
359 (mm-url-insert url)))
360 (nnheader-remove-cr-followed-by-lf)
361 ;; Decode text according to the encoding attribute.
362 (when (setq cs (nnrss-get-encoding))
363 (mm-decode-coding-region (point-min) (point-max) cs)
364 (mm-enable-multibyte))
365 (goto-char (point-min))
367 ;; Because xml-parse-region can't deal with anything that isn't
368 ;; xml and w3-parse-buffer can't deal with some xml, we have to
369 ;; parse with xml-parse-region first and, if that fails, parse
370 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
371 ;; why w3-parse-buffer fails to parse some well-formed xml and
372 ;; fix it.
374 (condition-case err1
375 (setq xmlform (xml-parse-region (point-min) (point-max)))
376 (error
377 (condition-case err2
378 (setq htmlform (caddar (w3-parse-buffer
379 (current-buffer))))
380 (error
381 (message "\
382 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
383 url err1 err2)))))
384 (if htmlform
385 htmlform
386 xmlform))))
388 (defun nnrss-possibly-change-group (&optional group server)
389 (when (and server
390 (not (nnrss-server-opened server)))
391 (nnrss-open-server server))
392 (when (and group (not (equal group nnrss-group)))
393 (nnrss-read-group-data group server)
394 (setq nnrss-group group)))
396 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
398 (defun nnrss-generate-active ()
399 (when (y-or-n-p "Fetch extra categories? ")
400 (dolist (func nnrss-extra-categories)
401 (funcall func)))
402 (save-excursion
403 (set-buffer nntp-server-buffer)
404 (erase-buffer)
405 (dolist (elem nnrss-group-alist)
406 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
407 (dolist (elem nnrss-server-data)
408 (unless (assoc (car elem) nnrss-group-alist)
409 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
411 ;;; data functions
413 (defun nnrss-read-server-data (server)
414 (setq nnrss-server-data nil)
415 (let ((file (nnrss-make-filename "nnrss" server)))
416 (when (file-exists-p file)
417 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
418 ;; file names. So, we use `insert-file-contents' instead.
419 (mm-with-multibyte-buffer
420 (let ((coding-system-for-read nnrss-file-coding-system)
421 (file-name-coding-system nnmail-pathname-coding-system))
422 (insert-file-contents file)
423 (eval-region (point-min) (point-max)))))))
425 (defun nnrss-save-server-data (server)
426 (gnus-make-directory nnrss-directory)
427 (let ((coding-system-for-write nnrss-file-coding-system)
428 (file-name-coding-system nnmail-pathname-coding-system))
429 (with-temp-file (nnrss-make-filename "nnrss" server)
430 (insert (format ";; -*- coding: %s; -*-\n"
431 nnrss-file-coding-system))
432 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
433 (insert "\n")
434 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
436 (defun nnrss-read-group-data (group server)
437 (setq nnrss-group-data nil)
438 (setq nnrss-group-hashtb (gnus-make-hashtable))
439 (let ((pair (assoc group nnrss-server-data)))
440 (setq nnrss-group-max (or (cadr pair) 0))
441 (setq nnrss-group-min (+ nnrss-group-max 1)))
442 (let ((file (nnrss-make-filename group server)))
443 (when (file-exists-p file)
444 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
445 ;; file names. So, we use `insert-file-contents' instead.
446 (mm-with-multibyte-buffer
447 (let ((coding-system-for-read nnrss-file-coding-system)
448 (file-name-coding-system nnmail-pathname-coding-system))
449 (insert-file-contents file)
450 (eval-region (point-min) (point-max))))
451 (dolist (e nnrss-group-data)
452 (gnus-sethash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
453 (when (and (car e) (> nnrss-group-min (car e)))
454 (setq nnrss-group-min (car e)))
455 (when (and (car e) (< nnrss-group-max (car e)))
456 (setq nnrss-group-max (car e)))))))
458 (defun nnrss-save-group-data (group server)
459 (gnus-make-directory nnrss-directory)
460 (let ((coding-system-for-write nnrss-file-coding-system)
461 (file-name-coding-system nnmail-pathname-coding-system))
462 (with-temp-file (nnrss-make-filename group server)
463 (insert (format ";; -*- coding: %s; -*-\n"
464 nnrss-file-coding-system))
465 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
467 (defun nnrss-make-filename (name server)
468 (expand-file-name
469 (nnrss-translate-file-chars
470 (concat name
471 (and server
472 (not (equal server ""))
473 "-")
474 server
475 ".el"))
476 nnrss-directory))
478 (gnus-add-shutdown 'nnrss-close 'gnus)
480 (defun nnrss-close ()
481 "Clear internal nnrss variables."
482 (setq nnrss-group-data nil
483 nnrss-server-data nil
484 nnrss-group-hashtb nil
485 nnrss-group-alist nil))
487 ;;; URL interface
489 (defun nnrss-no-cache (url)
492 (defun nnrss-insert-w3 (url)
493 (mm-with-unibyte-current-buffer
494 (mm-url-insert url)))
496 (defun nnrss-decode-entities-string (string)
497 (if string
498 (mm-with-multibyte-buffer
499 (insert string)
500 (mm-url-decode-entities-nbsp)
501 (buffer-string))))
503 (defalias 'nnrss-insert 'nnrss-insert-w3)
505 (defun nnrss-mime-encode-string (string)
506 (mm-with-multibyte-buffer
507 (insert string)
508 (mm-url-decode-entities-nbsp)
509 (goto-char (point-min))
510 (while (re-search-forward "[\t\n ]+" nil t)
511 (replace-match " "))
512 (goto-char (point-min))
513 (skip-chars-forward " ")
514 (delete-region (point-min) (point))
515 (goto-char (point-max))
516 (skip-chars-forward " ")
517 (delete-region (point) (point-max))
518 (let ((rfc2047-encoding-type 'mime)
519 rfc2047-encode-max-chars)
520 (rfc2047-encode-region (point-min) (point-max)))
521 (goto-char (point-min))
522 (while (search-forward "\n" nil t)
523 (delete-backward-char 1))
524 (buffer-string)))
526 ;;; Snarf functions
528 (defun nnrss-check-group (group server)
529 (let (file xml subject url extra changed author date
530 enclosure rss-ns rdf-ns content-ns dc-ns)
531 (if (and nnrss-use-local
532 (file-exists-p (setq file (expand-file-name
533 (nnrss-translate-file-chars
534 (concat group ".xml"))
535 nnrss-directory))))
536 (setq xml (nnrss-fetch file t))
537 (setq url (or (nth 2 (assoc group nnrss-server-data))
538 (second (assoc group nnrss-group-alist))))
539 (unless url
540 (setq url
541 (cdr
542 (assoc 'href
543 (nnrss-discover-feed
544 (read-string
545 (format "URL to search for %s: " group) "http://")))))
546 (let ((pair (assoc group nnrss-server-data)))
547 (if pair
548 (setcdr (cdr pair) (list url))
549 (push (list group nnrss-group-max url) nnrss-server-data)))
550 (setq changed t))
551 (setq xml (nnrss-fetch url)))
552 ;; See
553 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
554 ;; for more RSS namespaces.
555 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
556 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
557 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
558 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
559 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
560 (when (and (listp item)
561 (string= (concat rss-ns "item") (car item))
562 (if (setq url (nnrss-decode-entities-string
563 (nnrss-node-text rss-ns 'link (cddr item))))
564 (not (gnus-gethash url nnrss-group-hashtb))
565 (setq extra (or (nnrss-node-text content-ns 'encoded item)
566 (nnrss-node-text rss-ns 'description item)))
567 (not (gnus-gethash extra nnrss-group-hashtb))))
568 (setq subject (nnrss-node-text rss-ns 'title item))
569 (setq extra (or extra
570 (nnrss-node-text content-ns 'encoded item)
571 (nnrss-node-text rss-ns 'description item)))
572 (setq author (or (nnrss-node-text rss-ns 'author item)
573 (nnrss-node-text dc-ns 'creator item)
574 (nnrss-node-text dc-ns 'contributor item)))
575 (setq date (or (nnrss-node-text dc-ns 'date item)
576 (nnrss-node-text rss-ns 'pubDate item)
577 (message-make-date)))
578 (when (setq enclosure (cadr (assq (intern (concat rss-ns "enclosure")) item)))
579 (let ((url (cdr (assq 'url enclosure)))
580 (len (cdr (assq 'length enclosure)))
581 (type (cdr (assq 'type enclosure)))
582 (name))
583 (setq len
584 (if (and len (integerp (setq len (string-to-number len))))
585 ;; actually already in `ls-lisp-format-file-size' but
586 ;; probably not worth to require it for one function
587 (do ((size (/ len 1.0) (/ size 1024.0))
588 (post-fixes (list "" "k" "M" "G" "T" "P" "E")
589 (cdr post-fixes)))
590 ((< size 1024)
591 (format "%.1f%s" size (car post-fixes))))
592 "0"))
593 (setq url (or url ""))
594 (setq name (if (string-match "/\\([^/]*\\)$" url)
595 (match-string 1 url)
596 "file"))
597 (setq type (or type ""))
598 (setq enclosure (list url name len type))))
599 (push
600 (list
601 (incf nnrss-group-max)
602 (current-time)
604 (and subject (nnrss-mime-encode-string subject))
605 (and author (nnrss-mime-encode-string author))
606 date
607 (and extra (nnrss-decode-entities-string extra))
608 enclosure)
609 nnrss-group-data)
610 (gnus-sethash (or url extra) t nnrss-group-hashtb)
611 (setq changed t))
612 (setq extra nil))
613 (when changed
614 (nnrss-save-group-data group server)
615 (let ((pair (assoc group nnrss-server-data)))
616 (if pair
617 (setcar (cdr pair) nnrss-group-max)
618 (push (list group nnrss-group-max) nnrss-server-data)))
619 (nnrss-save-server-data server))))
621 (defun nnrss-opml-import (opml-file)
622 "OPML subscriptions import.
623 Read the file and attempt to subscribe to each Feed in the file."
624 (interactive "fImport file: ")
625 (mapcar
626 (lambda (node) (gnus-group-make-rss-group
627 (cdr (assq 'xmlUrl (cadr node)))))
628 (nnrss-find-el 'outline
629 (progn
630 (find-file opml-file)
631 (xml-parse-region (point-min)
632 (point-max))))))
634 (defun nnrss-opml-export ()
635 "OPML subscription export.
636 Export subscriptions to a buffer in OPML Format."
637 (interactive)
638 (with-current-buffer (get-buffer-create "*OPML Export*")
639 (mm-set-buffer-file-coding-system 'utf-8)
640 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
641 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
642 "<opml version=\"1.1\">\n"
643 " <head>\n"
644 " <title>mySubscriptions</title>\n"
645 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
646 "</dateCreated>\n"
647 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
648 " <ownerName>" (user-full-name) "</ownerName>\n"
649 " </head>\n"
650 " <body>\n")
651 (dolist (sub nnrss-group-alist)
652 (insert " <outline text=\"" (car sub)
653 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
654 (insert " </body>\n"
655 "</opml>\n"))
656 (pop-to-buffer "*OPML Export*")
657 (when (fboundp 'sgml-mode)
658 (sgml-mode)))
660 (defun nnrss-generate-download-script ()
661 "Generate a download script in the current buffer.
662 It is useful when `(setq nnrss-use-local t)'."
663 (interactive)
664 (insert "#!/bin/sh\n")
665 (insert "WGET=wget\n")
666 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
667 (dolist (elem nnrss-server-data)
668 (let ((url (or (nth 2 elem)
669 (second (assoc (car elem) nnrss-group-alist)))))
670 (insert "$WGET -q -O \"$RSSDIR\"/'"
671 (nnrss-translate-file-chars (concat (car elem) ".xml"))
672 "' '" url "'\n"))))
674 (defun nnrss-translate-file-chars (name)
675 (let ((nnheader-file-name-translation-alist
676 (append nnheader-file-name-translation-alist '((?' . ?_)))))
677 (nnheader-translate-file-chars name)))
679 (defvar nnrss-moreover-url
680 "http://w.moreover.com/categories/category_list_rss.html"
681 "The url of moreover.com categories.")
683 (defun nnrss-snarf-moreover-categories ()
684 "Snarf RSS links from moreover.com."
685 (interactive)
686 (let (category name url changed)
687 (with-temp-buffer
688 (nnrss-insert nnrss-moreover-url)
689 (goto-char (point-min))
690 (while (re-search-forward
691 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
692 (if (match-string 1)
693 (setq category (match-string 1))
694 (setq url (match-string 2)
695 name (mm-url-decode-entities-string
696 (rfc2231-decode-encoded-string
697 (match-string 3))))
698 (if category
699 (setq name (concat category "." name)))
700 (unless (assoc name nnrss-server-data)
701 (setq changed t)
702 (push (list name 0 url) nnrss-server-data)))))
703 (if changed
704 (nnrss-save-server-data ""))))
706 (defun nnrss-node-text (namespace local-name element)
707 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
708 element))
709 (text (if (and node (listp node))
710 (nnrss-node-just-text node)
711 node))
712 (cleaned-text (if text
713 (gnus-replace-in-string
714 (gnus-replace-in-string
715 text "^[\000-\037\177]+\\|^ +\\| +$" "")
716 "\r\n" "\n"))))
717 (if (string-equal "" cleaned-text)
719 cleaned-text)))
721 (defun nnrss-node-just-text (node)
722 (if (and node (listp node))
723 (mapconcat 'nnrss-node-just-text (cddr node) " ")
724 node))
726 (defun nnrss-find-el (tag data &optional found-list)
727 "Find the all matching elements in the data.
728 Careful with this on large documents!"
729 (when (consp data)
730 (dolist (bit data)
731 (when (car-safe bit)
732 (when (equal tag (car bit))
733 ;; Old xml.el may return a list of string.
734 (when (and (consp (caddr bit))
735 (stringp (caaddr bit)))
736 (setcar (cddr bit) (caaddr bit)))
737 (setq found-list
738 (append found-list
739 (list bit))))
740 (if (and (consp (car-safe (caddr bit)))
741 (not (stringp (caddr bit))))
742 (setq found-list
743 (append found-list
744 (nnrss-find-el
745 tag (caddr bit))))
746 (setq found-list
747 (append found-list
748 (nnrss-find-el
749 tag (cddr bit))))))))
750 found-list)
752 (defun nnrss-rsslink-p (el)
753 "Test if the element we are handed is an RSS autodiscovery link."
754 (and (eq (car-safe el) 'link)
755 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
756 (or (string-equal (cdr (assoc 'type (cadr el)))
757 "application/rss+xml")
758 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
760 (defun nnrss-get-rsslinks (data)
761 "Extract the <link> elements that are links to RSS from the parsed data."
762 (delq nil (mapcar
763 (lambda (el)
764 (if (nnrss-rsslink-p el) el))
765 (nnrss-find-el 'link data))))
767 (defun nnrss-extract-hrefs (data)
768 "Recursively extract hrefs from a page's source.
769 DATA should be the output of `xml-parse-region' or
770 `w3-parse-buffer'."
771 (mapcar (lambda (ahref)
772 (cdr (assoc 'href (cadr ahref))))
773 (nnrss-find-el 'a data)))
775 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
776 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
777 (not (string-match "://" ,item)))
778 (setq ,onsite-list (append ,onsite-list (list ,item))))
779 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
781 (defun nnrss-order-hrefs (base-uri hrefs)
782 "Given a list of hrefs, sort them using the following priorities:
783 1. links ending in .rss
784 2. links ending in .rdf
785 3. links ending in .xml
786 4. links containing the above
787 5. offsite links
789 BASE-URI is used to determine the location of the links and
790 whether they are `offsite' or `onsite'."
791 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
792 rss-onsite-in rdf-onsite-in xml-onsite-in
793 rss-offsite-end rdf-offsite-end xml-offsite-end
794 rss-offsite-in rdf-offsite-in xml-offsite-in)
795 (dolist (href hrefs)
796 (cond ((null href))
797 ((string-match "\\.rss$" href)
798 (nnrss-match-macro
799 base-uri href rss-onsite-end rss-offsite-end))
800 ((string-match "\\.rdf$" href)
801 (nnrss-match-macro
802 base-uri href rdf-onsite-end rdf-offsite-end))
803 ((string-match "\\.xml$" href)
804 (nnrss-match-macro
805 base-uri href xml-onsite-end xml-offsite-end))
806 ((string-match "rss" href)
807 (nnrss-match-macro
808 base-uri href rss-onsite-in rss-offsite-in))
809 ((string-match "rdf" href)
810 (nnrss-match-macro
811 base-uri href rdf-onsite-in rdf-offsite-in))
812 ((string-match "xml" href)
813 (nnrss-match-macro
814 base-uri href xml-onsite-in xml-offsite-in))))
815 (append
816 rss-onsite-end rdf-onsite-end xml-onsite-end
817 rss-onsite-in rdf-onsite-in xml-onsite-in
818 rss-offsite-end rdf-offsite-end xml-offsite-end
819 rss-offsite-in rdf-offsite-in xml-offsite-in)))
821 (defun nnrss-discover-feed (url)
822 "Given a page, find an RSS feed using Mark Pilgrim's
823 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
825 (let ((parsed-page (nnrss-fetch url)))
827 ;; 1. if this url is the rss, use it.
828 (if (nnrss-rss-p parsed-page)
829 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
830 (nnrss-rss-title-description rss-ns parsed-page url))
832 ;; 2. look for the <link rel="alternate"
833 ;; type="application/rss+xml" and use that if it is there.
834 (let ((links (nnrss-get-rsslinks parsed-page)))
835 (if links
836 (let* ((xml (nnrss-fetch
837 (cdr (assoc 'href (cadar links)))))
838 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
839 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
841 ;; 3. look for links on the site in the following order:
842 ;; - onsite links ending in .rss, .rdf, or .xml
843 ;; - onsite links containing any of the above
844 ;; - offsite links ending in .rss, .rdf, or .xml
845 ;; - offsite links containing any of the above
846 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
847 (match-string 0 url)))
848 (hrefs (nnrss-order-hrefs
849 base-uri (nnrss-extract-hrefs parsed-page)))
850 (rss-link nil))
851 (while (and (eq rss-link nil) (not (eq hrefs nil)))
852 (let ((href-data (nnrss-fetch (car hrefs))))
853 (if (nnrss-rss-p href-data)
854 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
855 (setq rss-link (nnrss-rss-title-description
856 rss-ns href-data (car hrefs))))
857 (setq hrefs (cdr hrefs)))))
858 (if rss-link rss-link
860 ;; 4. check syndic8
861 (nnrss-find-rss-via-syndic8 url))))))))
863 (defun nnrss-find-rss-via-syndic8 (url)
864 "Query syndic8 for the rss feeds it has for URL."
865 (if (not (locate-library "xml-rpc"))
866 (progn
867 (message "XML-RPC is not available... not checking Syndic8.")
868 nil)
869 (require 'xml-rpc)
870 (let ((feedid (xml-rpc-method-call
871 "http://www.syndic8.com/xmlrpc.php"
872 'syndic8.FindSites
873 url)))
874 (when feedid
875 (let* ((feedinfo (xml-rpc-method-call
876 "http://www.syndic8.com/xmlrpc.php"
877 'syndic8.GetFeedInfo
878 feedid))
879 (urllist
880 (delq nil
881 (mapcar
882 (lambda (listinfo)
883 (if (string-equal
884 (cdr (assoc "status" listinfo))
885 "Syndicated")
886 (cons
887 (cdr (assoc "sitename" listinfo))
888 (list
889 (cons 'title
890 (cdr (assoc
891 "sitename" listinfo)))
892 (cons 'href
893 (cdr (assoc
894 "dataurl" listinfo)))))))
895 feedinfo))))
896 (if (not (> (length urllist) 1))
897 (cdar urllist)
898 (let ((completion-ignore-case t)
899 (selection
900 (mapcar (lambda (listinfo)
901 (cons (cdr (assoc "sitename" listinfo))
902 (string-to-number
903 (cdr (assoc "feedid" listinfo)))))
904 feedinfo)))
905 (cdr (assoc
906 (completing-read
907 "Multiple feeds found. Select one: "
908 selection nil t) urllist)))))))))
910 (defun nnrss-rss-p (data)
911 "Test if DATA is an RSS feed.
912 Simply ensures that the first element is rss or rdf."
913 (or (eq (caar data) 'rss)
914 (eq (caar data) 'rdf:RDF)))
916 (defun nnrss-rss-title-description (rss-namespace data url)
917 "Return the title of an RSS feed."
918 (if (nnrss-rss-p data)
919 (let ((description (intern (concat rss-namespace "description")))
920 (title (intern (concat rss-namespace "title")))
921 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
922 data)))
923 (list
924 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
925 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
926 (cons 'href url)))))
928 (defun nnrss-get-namespace-prefix (el uri)
929 "Given EL (containing a parsed element) and URI (containing a string
930 that gives the URI for which you want to retrieve the namespace
931 prefix), return the prefix."
932 (let* ((prefix (car (rassoc uri (cadar el))))
933 (nslist (if prefix
934 (split-string (symbol-name prefix) ":")))
935 (ns (cond ((eq (length nslist) 1) ; no prefix given
937 ((eq (length nslist) 2) ; extract prefix
938 (cadr nslist)))))
939 (if (and ns (not (string= ns "")))
940 (concat ns ":")
941 ns)))
943 (provide 'nnrss)
946 ;;; nnrss.el ends here
948 ;;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267