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