]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnrss.el
Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-248
[gnu-emacs] / lisp / gnus / nnrss.el
1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: RSS
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
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))
45
46 (nnoo-declare nnrss)
47
48 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
49 "Where nnrss will save its files.")
50
51 ;; (group max rss-url)
52 (defvoo nnrss-server-data nil)
53
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 "")
61
62 (defconst nnrss-version "nnrss 1.0")
63
64 (defvar nnrss-group-alist '()
65 "List of RSS addresses.")
66
67 (defvar nnrss-use-local nil)
68
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'.")
72
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'.")
76
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.")
82
83 (defvar nnrss-file-coding-system mm-universal-coding-system
84 "Coding system used when reading and writing files.")
85
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.")
91
92 (nnoo-define-basics nnrss)
93
94 ;;; Interface functions
95
96 (defsubst nnrss-format-string (string)
97 (gnus-replace-in-string string " *\n *" " "))
98
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))
103
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")
141 "")
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")
149 "")
150 "\n")))))
151 'nov)
152
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
159 t
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)
165 t))
166 (nnheader-message 6 "nnrss: Requesting %s...done" group)))
167
168 (deffoo nnrss-close-group (group &optional server)
169 t)
170
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 ;; Enable encoding of Newsgroups header in XEmacs.
199 (default-enable-multibyte-characters t)
200 (rfc2047-header-encoding-alist
201 (if (mm-coding-system-p 'utf-8)
202 (cons '("Newsgroups" . utf-8)
203 rfc2047-header-encoding-alist)
204 rfc2047-header-encoding-alist))
205 rfc2047-encode-encoded-words body)
206 (when (or text link)
207 (insert "\n")
208 (insert "<#multipart type=alternative>\n"
209 "<#part type=\"text/plain\">\n")
210 (setq body (point))
211 (if text
212 (progn
213 (insert text "\n")
214 (when link
215 (insert "\n" link "\n")))
216 (when link
217 (insert link "\n")))
218 (setq body (buffer-substring body (point)))
219 (insert "<#/part>\n"
220 "<#part type=\"text/html\">\n"
221 "<html><head></head><body>\n")
222 (when text
223 (insert text "\n"))
224 (when link
225 (insert "<p><a href=\"" link "\">link</a></p>\n"))
226 (insert "</body></html>\n"
227 "<#/part>\n"
228 "<#/multipart>\n"))
229 (condition-case nil
230 (mml-to-mime)
231 (error
232 (erase-buffer)
233 (insert header
234 "Content-Type: text/plain; charset=gnus-decoded\n"
235 "Content-Transfer-Encoding: 8bit\n\n"
236 body)
237 (nnheader-message
238 3 "Warning - there might be invalid characters"))))
239 (goto-char (point-min))
240 (search-forward "\n\n")
241 (forward-line -1)
242 (insert (format "Message-ID: <%d@%s.nnrss>\n"
243 (car e)
244 (let ((rfc2047-encoding-type 'mime)
245 rfc2047-encode-max-chars)
246 (rfc2047-encode-string
247 (gnus-replace-in-string group "[\t\n ]+" "_")))))
248 (when nnrss-content-function
249 (funcall nnrss-content-function e group article))))
250 (cond
251 (err
252 (nnheader-report 'nnrss err))
253 ((not e)
254 (nnheader-report 'nnrss "no such id: %d" article))
255 (t
256 (nnheader-report 'nnrss "article %s retrieved" (car e))
257 ;; we return the article number.
258 (cons nnrss-group (car e))))))
259
260 (deffoo nnrss-request-list (&optional server)
261 (nnrss-possibly-change-group nil server)
262 (nnrss-generate-active)
263 t)
264
265 (deffoo nnrss-open-server (server &optional defs connectionless)
266 (nnrss-read-server-data server)
267 (nnoo-change-server 'nnrss server defs)
268 t)
269
270 (deffoo nnrss-request-expire-articles
271 (articles group &optional server force)
272 (setq group (nnrss-decode-group-name group))
273 (nnrss-possibly-change-group group server)
274 (let (e days not-expirable changed)
275 (dolist (art articles)
276 (if (and (setq e (assq art nnrss-group-data))
277 (nnmail-expired-article-p
278 group
279 (if (listp (setq days (nth 1 e))) days
280 (days-to-time (- days (time-to-days '(0 0)))))
281 force))
282 (setq nnrss-group-data (delq e nnrss-group-data)
283 changed t)
284 (push art not-expirable)))
285 (if changed
286 (nnrss-save-group-data group server))
287 not-expirable))
288
289 (deffoo nnrss-request-delete-group (group &optional force server)
290 (setq group (nnrss-decode-group-name group))
291 (nnrss-possibly-change-group group server)
292 (let (elem)
293 ;; There may be two or more entries in `nnrss-group-alist' since
294 ;; this function didn't delete them formerly.
295 (while (setq elem (assoc group nnrss-group-alist))
296 (setq nnrss-group-alist (delq elem nnrss-group-alist))))
297 (setq nnrss-server-data
298 (delq (assoc group nnrss-server-data) nnrss-server-data))
299 (nnrss-save-server-data server)
300 (ignore-errors
301 (delete-file (nnrss-make-filename group server)))
302 t)
303
304 (deffoo nnrss-request-list-newsgroups (&optional server)
305 (nnrss-possibly-change-group nil server)
306 (save-excursion
307 (set-buffer nntp-server-buffer)
308 (erase-buffer)
309 (dolist (elem nnrss-group-alist)
310 (if (third elem)
311 (insert (car elem) "\t" (third elem) "\n"))))
312 t)
313
314 (nnoo-define-skeleton nnrss)
315
316 ;;; Internal functions
317 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
318
319 (defun nnrss-get-encoding ()
320 "Return an encoding attribute specified in the current xml contents.
321 If `nnrss-compatible-encoding-alist' specifies the compatible encoding,
322 it is used instead. If the xml contents doesn't specify the encoding,
323 return `utf-8' which is the default encoding for xml if it is available,
324 otherwise return nil."
325 (goto-char (point-min))
326 (if (re-search-forward
327 "<\\?[^>]*encoding=\\(\"\\([^\">]+\\)\"\\|'\\([^'>]+\\)'\\)"
328 nil t)
329 (let ((encoding (intern (downcase (or (match-string 2)
330 (match-string 3))))))
331 (or
332 (mm-coding-system-p (cdr (assq encoding
333 nnrss-compatible-encoding-alist)))
334 (mm-coding-system-p encoding)
335 (mm-coding-system-p (car (rassq encoding
336 nnrss-compatible-encoding-alist)))))
337 (mm-coding-system-p 'utf-8)))
338
339 (defun nnrss-fetch (url &optional local)
340 "Fetch URL and put it in a the expected Lisp structure."
341 (mm-with-unibyte-buffer
342 ;;some CVS versions of url.el need this to close the connection quickly
343 (let (cs xmlform htmlform)
344 ;; bit o' work necessary for w3 pre-cvs and post-cvs
345 (if local
346 (let ((coding-system-for-read 'binary))
347 (insert-file-contents url))
348 ;; FIXME: shouldn't binding `coding-system-for-read' be moved
349 ;; to `mm-url-insert'?
350 (let ((coding-system-for-read 'binary))
351 (mm-url-insert url)))
352 (nnheader-remove-cr-followed-by-lf)
353 ;; Decode text according to the encoding attribute.
354 (when (setq cs (nnrss-get-encoding))
355 (mm-decode-coding-region (point-min) (point-max) cs)
356 (mm-enable-multibyte))
357 (goto-char (point-min))
358
359 ;; Because xml-parse-region can't deal with anything that isn't
360 ;; xml and w3-parse-buffer can't deal with some xml, we have to
361 ;; parse with xml-parse-region first and, if that fails, parse
362 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
363 ;; why w3-parse-buffer fails to parse some well-formed xml and
364 ;; fix it.
365
366 (condition-case err1
367 (setq xmlform (xml-parse-region (point-min) (point-max)))
368 (error
369 (condition-case err2
370 (setq htmlform (caddar (w3-parse-buffer
371 (current-buffer))))
372 (error
373 (message "\
374 nnrss: %s: Not valid XML %s and w3-parse doesn't work %s"
375 url err1 err2)))))
376 (if htmlform
377 htmlform
378 xmlform))))
379
380 (defun nnrss-possibly-change-group (&optional group server)
381 (when (and server
382 (not (nnrss-server-opened server)))
383 (nnrss-open-server server))
384 (when (and group (not (equal group nnrss-group)))
385 (nnrss-read-group-data group server)
386 (setq nnrss-group group)))
387
388 (defvar nnrss-extra-categories '(nnrss-snarf-moreover-categories))
389
390 (defun nnrss-generate-active ()
391 (when (y-or-n-p "Fetch extra categories? ")
392 (dolist (func nnrss-extra-categories)
393 (funcall func)))
394 (save-excursion
395 (set-buffer nntp-server-buffer)
396 (erase-buffer)
397 (dolist (elem nnrss-group-alist)
398 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
399 (dolist (elem nnrss-server-data)
400 (unless (assoc (car elem) nnrss-group-alist)
401 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
402
403 ;;; data functions
404
405 (defun nnrss-read-server-data (server)
406 (setq nnrss-server-data nil)
407 (let ((file (nnrss-make-filename "nnrss" server)))
408 (when (file-exists-p file)
409 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
410 ;; file names. So, we use `insert-file-contents' instead.
411 (mm-with-multibyte-buffer
412 (let ((coding-system-for-read nnrss-file-coding-system)
413 (file-name-coding-system nnmail-pathname-coding-system))
414 (insert-file-contents file)
415 (eval-region (point-min) (point-max)))))))
416
417 (defun nnrss-save-server-data (server)
418 (gnus-make-directory nnrss-directory)
419 (let ((coding-system-for-write nnrss-file-coding-system)
420 (file-name-coding-system nnmail-pathname-coding-system))
421 (with-temp-file (nnrss-make-filename "nnrss" server)
422 (insert (format ";; -*- coding: %s; -*-\n"
423 nnrss-file-coding-system))
424 (gnus-prin1 `(setq nnrss-group-alist ',nnrss-group-alist))
425 (insert "\n")
426 (gnus-prin1 `(setq nnrss-server-data ',nnrss-server-data)))))
427
428 (defun nnrss-read-group-data (group server)
429 (setq nnrss-group-data nil)
430 (setq nnrss-group-hashtb (gnus-make-hashtable))
431 (let ((pair (assoc group nnrss-server-data)))
432 (setq nnrss-group-max (or (cadr pair) 0))
433 (setq nnrss-group-min (+ nnrss-group-max 1)))
434 (let ((file (nnrss-make-filename group server)))
435 (when (file-exists-p file)
436 ;; In Emacs 21.3 and earlier, `load' doesn't support non-ASCII
437 ;; file names. So, we use `insert-file-contents' instead.
438 (mm-with-multibyte-buffer
439 (let ((coding-system-for-read nnrss-file-coding-system)
440 (file-name-coding-system nnmail-pathname-coding-system))
441 (insert-file-contents file)
442 (eval-region (point-min) (point-max))))
443 (dolist (e nnrss-group-data)
444 (gnus-sethash (or (nth 2 e) (nth 6 e)) t nnrss-group-hashtb)
445 (when (and (car e) (> nnrss-group-min (car e)))
446 (setq nnrss-group-min (car e)))
447 (when (and (car e) (< nnrss-group-max (car e)))
448 (setq nnrss-group-max (car e)))))))
449
450 (defun nnrss-save-group-data (group server)
451 (gnus-make-directory nnrss-directory)
452 (let ((coding-system-for-write nnrss-file-coding-system)
453 (file-name-coding-system nnmail-pathname-coding-system))
454 (with-temp-file (nnrss-make-filename group server)
455 (insert (format ";; -*- coding: %s; -*-\n"
456 nnrss-file-coding-system))
457 (gnus-prin1 `(setq nnrss-group-data ',nnrss-group-data)))))
458
459 (defun nnrss-make-filename (name server)
460 (expand-file-name
461 (nnrss-translate-file-chars
462 (concat name
463 (and server
464 (not (equal server ""))
465 "-")
466 server
467 ".el"))
468 nnrss-directory))
469
470 (gnus-add-shutdown 'nnrss-close 'gnus)
471
472 (defun nnrss-close ()
473 "Clear internal nnrss variables."
474 (setq nnrss-group-data nil
475 nnrss-server-data nil
476 nnrss-group-hashtb nil
477 nnrss-group-alist nil))
478
479 ;;; URL interface
480
481 (defun nnrss-no-cache (url)
482 "")
483
484 (defun nnrss-insert-w3 (url)
485 (mm-with-unibyte-current-buffer
486 (mm-url-insert url)))
487
488 (defun nnrss-decode-entities-string (string)
489 (if string
490 (mm-with-multibyte-buffer
491 (insert string)
492 (mm-url-decode-entities-nbsp)
493 (buffer-string))))
494
495 (defalias 'nnrss-insert 'nnrss-insert-w3)
496
497 (defun nnrss-mime-encode-string (string)
498 (mm-with-multibyte-buffer
499 (insert string)
500 (mm-url-decode-entities-nbsp)
501 (goto-char (point-min))
502 (while (re-search-forward "[\t\n ]+" nil t)
503 (replace-match " "))
504 (goto-char (point-min))
505 (skip-chars-forward " ")
506 (delete-region (point-min) (point))
507 (goto-char (point-max))
508 (skip-chars-forward " ")
509 (delete-region (point) (point-max))
510 (let ((rfc2047-encoding-type 'mime)
511 rfc2047-encode-max-chars)
512 (rfc2047-encode-region (point-min) (point-max)))
513 (goto-char (point-min))
514 (while (search-forward "\n" nil t)
515 (delete-backward-char 1))
516 (buffer-string)))
517
518 ;;; Snarf functions
519
520 (defun nnrss-check-group (group server)
521 (let (file xml subject url extra changed author
522 date rss-ns rdf-ns content-ns dc-ns)
523 (if (and nnrss-use-local
524 (file-exists-p (setq file (expand-file-name
525 (nnrss-translate-file-chars
526 (concat group ".xml"))
527 nnrss-directory))))
528 (setq xml (nnrss-fetch file t))
529 (setq url (or (nth 2 (assoc group nnrss-server-data))
530 (second (assoc group nnrss-group-alist))))
531 (unless url
532 (setq url
533 (cdr
534 (assoc 'href
535 (nnrss-discover-feed
536 (read-string
537 (format "URL to search for %s: " group) "http://")))))
538 (let ((pair (assoc group nnrss-server-data)))
539 (if pair
540 (setcdr (cdr pair) (list url))
541 (push (list group nnrss-group-max url) nnrss-server-data)))
542 (setq changed t))
543 (setq xml (nnrss-fetch url)))
544 ;; See
545 ;; http://feeds.archive.org/validator/docs/howto/declare_namespaces.html
546 ;; for more RSS namespaces.
547 (setq dc-ns (nnrss-get-namespace-prefix xml "http://purl.org/dc/elements/1.1/")
548 rdf-ns (nnrss-get-namespace-prefix xml "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
549 rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")
550 content-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/modules/content/"))
551 (dolist (item (nreverse (nnrss-find-el (intern (concat rss-ns "item")) xml)))
552 (when (and (listp item)
553 (string= (concat rss-ns "item") (car item))
554 (if (setq url (nnrss-decode-entities-string
555 (nnrss-node-text rss-ns 'link (cddr item))))
556 (not (gnus-gethash url nnrss-group-hashtb))
557 (setq extra (or (nnrss-node-text content-ns 'encoded item)
558 (nnrss-node-text rss-ns 'description item)))
559 (not (gnus-gethash extra nnrss-group-hashtb))))
560 (setq subject (nnrss-node-text rss-ns 'title item))
561 (setq extra (or extra
562 (nnrss-node-text content-ns 'encoded item)
563 (nnrss-node-text rss-ns 'description item)))
564 (setq author (or (nnrss-node-text rss-ns 'author item)
565 (nnrss-node-text dc-ns 'creator item)
566 (nnrss-node-text dc-ns 'contributor item)))
567 (setq date (or (nnrss-node-text dc-ns 'date item)
568 (nnrss-node-text rss-ns 'pubDate item)
569 (message-make-date)))
570 (push
571 (list
572 (incf nnrss-group-max)
573 (current-time)
574 url
575 (and subject (nnrss-mime-encode-string subject))
576 (and author (nnrss-mime-encode-string author))
577 date
578 (and extra (nnrss-decode-entities-string extra)))
579 nnrss-group-data)
580 (gnus-sethash (or url extra) t nnrss-group-hashtb)
581 (setq changed t))
582 (setq extra nil))
583 (when changed
584 (nnrss-save-group-data group server)
585 (let ((pair (assoc group nnrss-server-data)))
586 (if pair
587 (setcar (cdr pair) nnrss-group-max)
588 (push (list group nnrss-group-max) nnrss-server-data)))
589 (nnrss-save-server-data server))))
590
591 (defun nnrss-opml-import (opml-file)
592 "OPML subscriptions import.
593 Read the file and attempt to subscribe to each Feed in the file."
594 (interactive "fImport file: ")
595 (mapcar
596 (lambda (node) (gnus-group-make-rss-group
597 (cdr (assq 'xmlUrl (cadr node)))))
598 (nnrss-find-el 'outline
599 (progn
600 (find-file opml-file)
601 (xml-parse-region (point-min)
602 (point-max))))))
603
604 (defun nnrss-opml-export ()
605 "OPML subscription export.
606 Export subscriptions to a buffer in OPML Format."
607 (interactive)
608 (with-current-buffer (get-buffer-create "*OPML Export*")
609 (mm-set-buffer-file-coding-system 'utf-8)
610 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
611 "<!-- OPML generated by Emacs Gnus' nnrss.el -->\n"
612 "<opml version=\"1.1\">\n"
613 " <head>\n"
614 " <title>mySubscriptions</title>\n"
615 " <dateCreated>" (format-time-string "%a, %d %b %Y %T %z")
616 "</dateCreated>\n"
617 " <ownerEmail>" user-mail-address "</ownerEmail>\n"
618 " <ownerName>" (user-full-name) "</ownerName>\n"
619 " </head>\n"
620 " <body>\n")
621 (dolist (sub nnrss-group-alist)
622 (insert " <outline text=\"" (car sub)
623 "\" xmlUrl=\"" (cadr sub) "\"/>\n"))
624 (insert " </body>\n"
625 "</opml>\n"))
626 (pop-to-buffer "*OPML Export*")
627 (when (fboundp 'sgml-mode)
628 (sgml-mode)))
629
630 (defun nnrss-generate-download-script ()
631 "Generate a download script in the current buffer.
632 It is useful when `(setq nnrss-use-local t)'."
633 (interactive)
634 (insert "#!/bin/sh\n")
635 (insert "WGET=wget\n")
636 (insert "RSSDIR='" (expand-file-name nnrss-directory) "'\n")
637 (dolist (elem nnrss-server-data)
638 (let ((url (or (nth 2 elem)
639 (second (assoc (car elem) nnrss-group-alist)))))
640 (insert "$WGET -q -O \"$RSSDIR\"/'"
641 (nnrss-translate-file-chars (concat (car elem) ".xml"))
642 "' '" url "'\n"))))
643
644 (defun nnrss-translate-file-chars (name)
645 (let ((nnheader-file-name-translation-alist
646 (append nnheader-file-name-translation-alist '((?' . ?_)))))
647 (nnheader-translate-file-chars name)))
648
649 (defvar nnrss-moreover-url
650 "http://w.moreover.com/categories/category_list_rss.html"
651 "The url of moreover.com categories.")
652
653 (defun nnrss-snarf-moreover-categories ()
654 "Snarf RSS links from moreover.com."
655 (interactive)
656 (let (category name url changed)
657 (with-temp-buffer
658 (nnrss-insert nnrss-moreover-url)
659 (goto-char (point-min))
660 (while (re-search-forward
661 "<a name=\"\\([^\"]+\\)\">\\|<a href=\"\\(http://[^\"]*moreover\\.com[^\"]+page\\?c=\\([^\"&]+\\)&o=rss\\)" nil t)
662 (if (match-string 1)
663 (setq category (match-string 1))
664 (setq url (match-string 2)
665 name (mm-url-decode-entities-string
666 (rfc2231-decode-encoded-string
667 (match-string 3))))
668 (if category
669 (setq name (concat category "." name)))
670 (unless (assoc name nnrss-server-data)
671 (setq changed t)
672 (push (list name 0 url) nnrss-server-data)))))
673 (if changed
674 (nnrss-save-server-data ""))))
675
676 (defun nnrss-node-text (namespace local-name element)
677 (let* ((node (assq (intern (concat namespace (symbol-name local-name)))
678 element))
679 (text (if (and node (listp node))
680 (nnrss-node-just-text node)
681 node))
682 (cleaned-text (if text (gnus-replace-in-string
683 text "^[\000-\037\177]+\\|^ +\\| +$" ""))))
684 (if (string-equal "" cleaned-text)
685 nil
686 cleaned-text)))
687
688 (defun nnrss-node-just-text (node)
689 (if (and node (listp node))
690 (mapconcat 'nnrss-node-just-text (cddr node) " ")
691 node))
692
693 (defun nnrss-find-el (tag data &optional found-list)
694 "Find the all matching elements in the data.
695 Careful with this on large documents!"
696 (when (consp data)
697 (dolist (bit data)
698 (when (car-safe bit)
699 (when (equal tag (car bit))
700 ;; Old xml.el may return a list of string.
701 (when (and (consp (caddr bit))
702 (stringp (caaddr bit)))
703 (setcar (cddr bit) (caaddr bit)))
704 (setq found-list
705 (append found-list
706 (list bit))))
707 (if (and (consp (car-safe (caddr bit)))
708 (not (stringp (caddr bit))))
709 (setq found-list
710 (append found-list
711 (nnrss-find-el
712 tag (caddr bit))))
713 (setq found-list
714 (append found-list
715 (nnrss-find-el
716 tag (cddr bit))))))))
717 found-list)
718
719 (defun nnrss-rsslink-p (el)
720 "Test if the element we are handed is an RSS autodiscovery link."
721 (and (eq (car-safe el) 'link)
722 (string-equal (cdr (assoc 'rel (cadr el))) "alternate")
723 (or (string-equal (cdr (assoc 'type (cadr el)))
724 "application/rss+xml")
725 (string-equal (cdr (assoc 'type (cadr el))) "text/xml"))))
726
727 (defun nnrss-get-rsslinks (data)
728 "Extract the <link> elements that are links to RSS from the parsed data."
729 (delq nil (mapcar
730 (lambda (el)
731 (if (nnrss-rsslink-p el) el))
732 (nnrss-find-el 'link data))))
733
734 (defun nnrss-extract-hrefs (data)
735 "Recursively extract hrefs from a page's source.
736 DATA should be the output of `xml-parse-region' or
737 `w3-parse-buffer'."
738 (mapcar (lambda (ahref)
739 (cdr (assoc 'href (cadr ahref))))
740 (nnrss-find-el 'a data)))
741
742 (defmacro nnrss-match-macro (base-uri item onsite-list offsite-list)
743 `(cond ((or (string-match (concat "^" ,base-uri) ,item)
744 (not (string-match "://" ,item)))
745 (setq ,onsite-list (append ,onsite-list (list ,item))))
746 (t (setq ,offsite-list (append ,offsite-list (list ,item))))))
747
748 (defun nnrss-order-hrefs (base-uri hrefs)
749 "Given a list of hrefs, sort them using the following priorities:
750 1. links ending in .rss
751 2. links ending in .rdf
752 3. links ending in .xml
753 4. links containing the above
754 5. offsite links
755
756 BASE-URI is used to determine the location of the links and
757 whether they are `offsite' or `onsite'."
758 (let (rss-onsite-end rdf-onsite-end xml-onsite-end
759 rss-onsite-in rdf-onsite-in xml-onsite-in
760 rss-offsite-end rdf-offsite-end xml-offsite-end
761 rss-offsite-in rdf-offsite-in xml-offsite-in)
762 (dolist (href hrefs)
763 (cond ((null href))
764 ((string-match "\\.rss$" href)
765 (nnrss-match-macro
766 base-uri href rss-onsite-end rss-offsite-end))
767 ((string-match "\\.rdf$" href)
768 (nnrss-match-macro
769 base-uri href rdf-onsite-end rdf-offsite-end))
770 ((string-match "\\.xml$" href)
771 (nnrss-match-macro
772 base-uri href xml-onsite-end xml-offsite-end))
773 ((string-match "rss" href)
774 (nnrss-match-macro
775 base-uri href rss-onsite-in rss-offsite-in))
776 ((string-match "rdf" href)
777 (nnrss-match-macro
778 base-uri href rdf-onsite-in rdf-offsite-in))
779 ((string-match "xml" href)
780 (nnrss-match-macro
781 base-uri href xml-onsite-in xml-offsite-in))))
782 (append
783 rss-onsite-end rdf-onsite-end xml-onsite-end
784 rss-onsite-in rdf-onsite-in xml-onsite-in
785 rss-offsite-end rdf-offsite-end xml-offsite-end
786 rss-offsite-in rdf-offsite-in xml-offsite-in)))
787
788 (defun nnrss-discover-feed (url)
789 "Given a page, find an RSS feed using Mark Pilgrim's
790 `ultra-liberal rss locator' (http://diveintomark.org/2002/08/15.html)."
791
792 (let ((parsed-page (nnrss-fetch url)))
793
794 ;; 1. if this url is the rss, use it.
795 (if (nnrss-rss-p parsed-page)
796 (let ((rss-ns (nnrss-get-namespace-prefix parsed-page "http://purl.org/rss/1.0/")))
797 (nnrss-rss-title-description rss-ns parsed-page url))
798
799 ;; 2. look for the <link rel="alternate"
800 ;; type="application/rss+xml" and use that if it is there.
801 (let ((links (nnrss-get-rsslinks parsed-page)))
802 (if links
803 (let* ((xml (nnrss-fetch
804 (cdr (assoc 'href (cadar links)))))
805 (rss-ns (nnrss-get-namespace-prefix xml "http://purl.org/rss/1.0/")))
806 (nnrss-rss-title-description rss-ns xml (cdr (assoc 'href (cadar links)))))
807
808 ;; 3. look for links on the site in the following order:
809 ;; - onsite links ending in .rss, .rdf, or .xml
810 ;; - onsite links containing any of the above
811 ;; - offsite links ending in .rss, .rdf, or .xml
812 ;; - offsite links containing any of the above
813 (let* ((base-uri (progn (string-match ".*://[^/]+/?" url)
814 (match-string 0 url)))
815 (hrefs (nnrss-order-hrefs
816 base-uri (nnrss-extract-hrefs parsed-page)))
817 (rss-link nil))
818 (while (and (eq rss-link nil) (not (eq hrefs nil)))
819 (let ((href-data (nnrss-fetch (car hrefs))))
820 (if (nnrss-rss-p href-data)
821 (let* ((rss-ns (nnrss-get-namespace-prefix href-data "http://purl.org/rss/1.0/")))
822 (setq rss-link (nnrss-rss-title-description
823 rss-ns href-data (car hrefs))))
824 (setq hrefs (cdr hrefs)))))
825 (if rss-link rss-link
826
827 ;; 4. check syndic8
828 (nnrss-find-rss-via-syndic8 url))))))))
829
830 (defun nnrss-find-rss-via-syndic8 (url)
831 "Query syndic8 for the rss feeds it has for URL."
832 (if (not (locate-library "xml-rpc"))
833 (progn
834 (message "XML-RPC is not available... not checking Syndic8.")
835 nil)
836 (require 'xml-rpc)
837 (let ((feedid (xml-rpc-method-call
838 "http://www.syndic8.com/xmlrpc.php"
839 'syndic8.FindSites
840 url)))
841 (when feedid
842 (let* ((feedinfo (xml-rpc-method-call
843 "http://www.syndic8.com/xmlrpc.php"
844 'syndic8.GetFeedInfo
845 feedid))
846 (urllist
847 (delq nil
848 (mapcar
849 (lambda (listinfo)
850 (if (string-equal
851 (cdr (assoc "status" listinfo))
852 "Syndicated")
853 (cons
854 (cdr (assoc "sitename" listinfo))
855 (list
856 (cons 'title
857 (cdr (assoc
858 "sitename" listinfo)))
859 (cons 'href
860 (cdr (assoc
861 "dataurl" listinfo)))))))
862 feedinfo))))
863 (if (not (> (length urllist) 1))
864 (cdar urllist)
865 (let ((completion-ignore-case t)
866 (selection
867 (mapcar (lambda (listinfo)
868 (cons (cdr (assoc "sitename" listinfo))
869 (string-to-int
870 (cdr (assoc "feedid" listinfo)))))
871 feedinfo)))
872 (cdr (assoc
873 (completing-read
874 "Multiple feeds found. Select one: "
875 selection nil t) urllist)))))))))
876
877 (defun nnrss-rss-p (data)
878 "Test if DATA is an RSS feed.
879 Simply ensures that the first element is rss or rdf."
880 (or (eq (caar data) 'rss)
881 (eq (caar data) 'rdf:RDF)))
882
883 (defun nnrss-rss-title-description (rss-namespace data url)
884 "Return the title of an RSS feed."
885 (if (nnrss-rss-p data)
886 (let ((description (intern (concat rss-namespace "description")))
887 (title (intern (concat rss-namespace "title")))
888 (channel (nnrss-find-el (intern (concat rss-namespace "channel"))
889 data)))
890 (list
891 (cons 'description (caddr (nth 0 (nnrss-find-el description channel))))
892 (cons 'title (caddr (nth 0 (nnrss-find-el title channel))))
893 (cons 'href url)))))
894
895 (defun nnrss-get-namespace-prefix (el uri)
896 "Given EL (containing a parsed element) and URI (containing a string
897 that gives the URI for which you want to retrieve the namespace
898 prefix), return the prefix."
899 (let* ((prefix (car (rassoc uri (cadar el))))
900 (nslist (if prefix
901 (split-string (symbol-name prefix) ":")))
902 (ns (cond ((eq (length nslist) 1) ; no prefix given
903 "")
904 ((eq (length nslist) 2) ; extract prefix
905 (cadr nslist)))))
906 (if (and ns (not (string= ns "")))
907 (concat ns ":")
908 ns)))
909
910 (provide 'nnrss)
911
912
913 ;;; nnrss.el ends here
914
915 ;;; arch-tag: 12910c07-0cdf-44fb-8d2c-416ded64c267