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