1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
42 ;; Report failure to find w3 at load time if appropriate.
51 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
52 "Where nnrss will save its files.")
54 ;; (group max rss-url)
55 (defvoo nnrss-server-data nil)
57 ;; (num timestamp url subject author date extra)
58 (defvoo nnrss-group-data nil)
59 (defvoo nnrss-group-max 0)
60 (defvoo nnrss-group-min 1)
61 (defvoo nnrss-group nil)
62 (defvoo nnrss-group-hashtb nil)
63 (defvoo nnrss-status-string "")
65 (defconst nnrss-version "nnrss 1.0")
67 (defvar nnrss-group-alist
69 "http://macweek.zdnet.com/macweek.xml")
71 "http://lwn.net/headlines/rss")
73 "http://www.fool.com/About/headlines/rss_headlines.asp")
75 "http://www.newsforge.com/newsforge.rdf")
77 "http://www.slashdot.com/slashdot.rdf")
79 "http://www.cnn.com/cnn.rss")
81 "http://freshmeat.net/backend/fm.rdf")
82 ("The.Guardian.newspaper"
83 "http://www.guardianunlimited.co.uk/rss/1,,,00.xml")
85 "http://monkeyfist.com/rdf.php3")
87 "http://www.newsforge.com/newsforge.rss")
89 "http://www.reutershealth.com/eline.rss")
91 "http://www.salon.com/feed/RDF/salon_use.rdf")
93 "http://www.wired.com/news_drop/netcenter/netcenter.rdf")
95 "http://www.itn.co.uk/itn.rdf")
97 "http://www.oreillynet.com/meerkat/?_fl=rss10")
99 "http://monkeyfist.com/rss1.php3")
100 ("Reuters.Health.rdf"
101 "http://www.reutershealth.com/eline.rdf")))
103 (defvar nnrss-use-local nil)
105 (nnoo-define-basics nnrss)
107 ;;; Interface functions
109 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
110 (nnrss-possibly-change-group group server)
113 (set-buffer nntp-server-buffer)
115 (dolist (article articles)
116 (if (setq e (assq article nnrss-group-data))
117 (insert (number-to-string (car e)) "\t" ;; number
119 (nnrss-string-as-multibyte (nth 3 e)) "")
122 (nnrss-string-as-multibyte (nth 4 e)) "")
126 (format "<%d@%s.nnrss>" (car e) group)
134 (deffoo nnrss-request-group (group &optional server dont-check)
135 (nnrss-possibly-change-group group server)
138 (nnrss-check-group group server)
139 (nnheader-report 'nnrss "Opened group %s" group)
141 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
142 (prin1-to-string group)
145 (deffoo nnrss-close-group (group &optional server)
148 (deffoo nnrss-request-article (article &optional group server buffer)
149 (nnrss-possibly-change-group group server)
150 (let ((e (assq article nnrss-group-data))
151 (nntp-server-buffer (or buffer nntp-server-buffer))
155 (with-current-buffer nntp-server-buffer
157 (goto-char (point-min))
159 (insert "Subject: " (nnrss-string-as-multibyte (nth 3 e)) "\n"))
161 (insert "From: " (nnrss-string-as-multibyte (nth 4 e)) "\n"))
163 (insert "Date: " (nnrss-string-as-multibyte (nth 5 e)) "\n"))
164 (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
167 (let ((point (point)))
168 (insert (nnrss-string-as-multibyte (nth 6 e)) "\n\n")
169 (fill-region point (point))))
171 (insert (nth 2 e) "\n")))))
174 (nnheader-report 'nnrss err))
176 (nnheader-report 'nnrss "No such id: %d" article))
178 (nnheader-report 'nnrss "Article %s retrieved" (car e))
179 ;; We return the article number.
180 (cons nnrss-group (car e))))))
182 (deffoo nnrss-request-list (&optional server)
183 (nnrss-possibly-change-group nil server)
184 (nnrss-generate-active)
187 (deffoo nnrss-open-server (server &optional defs connectionless)
188 (nnoo-change-server 'nnrss server defs)
191 (deffoo nnrss-request-expire-articles
192 (articles group &optional server force)
193 (nnrss-possibly-change-group group server)
194 (let (e changed days)
195 (dolist (art articles)
196 (when (setq e (assq art nnrss-group-data))
197 (if (nnmail-expired-article-p
199 (if (listp (setq days (nth 1 e))) days (days-to-time days))
201 (setq nnrss-group-data (delq e nnrss-group-data)
204 (nnrss-save-group-data group server))))
206 (deffoo nnrss-request-delete-group (group &optional force server)
207 (nnrss-possibly-change-group group server)
208 (setq nnrss-server-data
209 (delq (assoc group nnrss-server-data) nnrss-server-data))
210 (nnrss-save-server-data server)
211 (let ((file (expand-file-name (concat group (and server
212 (not (equal server ""))
214 server ".el") nnrss-directory)))
218 (nnoo-define-skeleton nnrss)
220 ;;; Internal functions
222 (defun nnrss-possibly-change-group (&optional group server)
224 (not (nnrss-server-opened server)))
225 (nnrss-read-server-data server)
226 (nnrss-open-server server))
227 (when (and group (not (equal group nnrss-group)))
228 (nnrss-read-group-data group server)
229 (setq nnrss-group group)))
231 (defun nnrss-generate-active ()
233 (set-buffer nntp-server-buffer)
235 (dolist (elem nnrss-group-alist)
236 (insert (prin1-to-string (car elem)) " 0 1 y\n"))
237 (dolist (elem nnrss-server-data)
238 (unless (assoc (car elem) nnrss-group-alist)
239 (insert (prin1-to-string (car elem)) " 0 1 y\n")))))
243 (defun nnrss-read-server-data (server)
244 (setq nnrss-server-data nil)
245 (let ((file (expand-file-name (concat "nnrss" (and server
246 (not (equal server ""))
251 (when (file-exists-p file)
253 (let ((coding-system-for-read 'binary))
254 (insert-file-contents file))
255 (goto-char (point-min))
258 (defun nnrss-save-server-data (server)
259 (gnus-make-directory nnrss-directory)
260 (let ((file (expand-file-name (concat "nnrss" (and server
261 (not (equal server ""))
265 (let ((coding-system-for-write 'binary))
267 (insert "(setq nnrss-server-data '"
268 (prin1-to-string nnrss-server-data)
271 (defun nnrss-read-group-data (group server)
272 (setq nnrss-group-data nil)
273 (setq nnrss-group-hashtb (gnus-make-hashtable))
274 (let ((pair (assoc group nnrss-server-data)))
275 (setq nnrss-group-max (or (cadr pair) 0))
276 (setq nnrss-group-min (+ nnrss-group-max 1)))
277 (let ((file (expand-file-name (concat group (and server
278 (not (equal server ""))
282 (when (file-exists-p file)
284 (let ((coding-system-for-read 'binary))
285 (insert-file-contents file))
286 (goto-char (point-min))
288 (dolist (e nnrss-group-data)
289 (gnus-sethash (nth 2 e) e nnrss-group-hashtb)
290 (if (and (car e) (> nnrss-group-min (car e)))
291 (setq nnrss-group-min (car e)))
292 (if (and (car e) (< nnrss-group-max (car e)))
293 (setq nnrss-group-max (car e)))))))
295 (defun nnrss-save-group-data (group server)
296 (gnus-make-directory nnrss-directory)
297 (let ((file (expand-file-name (concat group (and server
298 (not (equal server ""))
302 (let ((coding-system-for-write 'binary))
304 (insert "(setq nnrss-group-data '"
305 (prin1-to-string nnrss-group-data)
310 (defun nnrss-no-cache (url)
313 (defun nnrss-insert-w3 (url)
316 (let ((url-cache-creation-function 'nnrss-no-cache))
317 (mm-with-unibyte-current-buffer
318 (nnweb-insert url))))
320 (defun nnrss-decode-entities-unibyte-string (string)
321 (mm-with-unibyte-buffer
323 (nnweb-decode-entities)
324 (buffer-substring (point-min) (point-max))))
326 (defalias 'nnrss-insert 'nnrss-insert-w3)
328 (if (featurep 'xemacs)
329 (defalias 'nnrss-string-as-multibyte 'identity)
330 (defalias 'nnrss-string-as-multibyte 'string-as-multibyte))
334 (defun nnrss-check-group (group server)
335 (let ((w3-html-entities (cons '(nbsp . 32) w3-html-entities))
336 file xml subject url extra changed author date)
337 (mm-with-unibyte-buffer
338 (if (and nnrss-use-local
339 (file-exists-p (setq file (expand-file-name
340 (concat group ".xml")
342 (insert-file-contents file)
343 (setq url (or (nth 2 (assoc group nnrss-server-data))
344 (second (assoc group nnrss-group-alist))))
347 (read-string (format "RSS url of %s: " group "http://")))
348 (let ((pair (assoc group nnrss-server-data)))
350 (setcdr (cdr pair) (list url))
351 (push (list group nnrss-group-max url) nnrss-server-data)))
354 (goto-char (point-min))
355 (while (re-search-forward "\r\n?" nil t)
356 (replace-match "\n"))
357 (goto-char (point-min))
358 (if (re-search-forward "<rdf\\|<rss" nil t)
359 (goto-char (match-beginning 0)))
360 (setq xml (xml-parse-region (point) (point-max))))
361 (while (and xml (not (assq 'item xml)))
362 (unless (listp (car (setq xml (cddar xml))))
365 (when (and (listp item)
366 (eq 'item (car item))
367 (setq url (caddr (assq 'link (cddr item))))
368 (setq url (nnrss-decode-entities-unibyte-string url))
369 (not (gnus-gethash url nnrss-group-hashtb)))
370 (setq subject (caddr (assq 'title (cddr item))))
371 (setq extra (or (caddr (assq 'description (cddr item)))
372 (caddr (assq 'dc:description (cddr item)))))
373 (setq author (caddr (assq 'dc:creator (cddr item))))
374 (setq date (or (caddr (assq 'dc:date (cddr item)))
375 (message-make-date)))
378 (incf nnrss-group-max)
379 (time-to-days (current-time))
381 (and subject (nnrss-decode-entities-unibyte-string subject))
382 (and author (nnrss-decode-entities-unibyte-string author))
384 (and extra (nnrss-decode-entities-unibyte-string extra)))
386 (gnus-sethash url (car nnrss-group-data) nnrss-group-hashtb)
389 (nnrss-save-group-data group server)
390 (let ((pair (assoc group nnrss-server-data)))
392 (setcar (cdr pair) nnrss-group-max)
393 (push (list group nnrss-group-max) nnrss-server-data)))
394 (nnrss-save-server-data server))))
398 ;;; nnrss.el ends here