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