1 ;;; nnrss.el --- interfacing with RSS
2 ;; Copyright (C) 2001, 2002, 2003 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 (eval '(require 'xml))
46 (defvoo nnrss-directory (nnheader-concat gnus-directory "rss/")
47 "Where nnrss will save its files.")
49 ;; (group max rss-url)
50 (defvoo nnrss-server-data nil)
52 ;; (num timestamp url subject author date extra)
53 (defvoo nnrss-group-data nil)
54 (defvoo nnrss-group-max 0)
55 (defvoo nnrss-group-min 1)
56 (defvoo nnrss-group nil)
57 (defvoo nnrss-group-hashtb nil)
58 (defvoo nnrss-status-string "")
60 (defconst nnrss-version "nnrss 1.0")
62 (defvar nnrss-group-alist '()
63 "List of RSS addresses.")
65 (defvar nnrss-use-local nil)
67 (defvar nnrss-description-field 'X-Gnus-Description
68 "Field name used for DESCRIPTION.
69 To use the description in headers, put this name into `nnmail-extra-headers'.")
71 (defvar nnrss-url-field 'X-Gnus-Url
72 "Field name used for URL.
73 To use the description in headers, put this name into `nnmail-extra-headers'.")
75 (defvar nnrss-content-function nil
76 "A function which is called in `nnrss-request-article'.
77 The arguments are (ENTRY GROUP ARTICLE).
78 ENTRY is the record of the current headline. GROUP is the group name.
79 ARTICLE is the article number of the current headline.")
81 (nnoo-define-basics nnrss)
83 ;;; Interface functions
85 (deffoo nnrss-retrieve-headers (articles &optional group server fetch-old)
86 (nnrss-possibly-change-group group server)
89 (set-buffer nntp-server-buffer)
91 (dolist (article articles)
92 (if (setq e (assq article nnrss-group-data))
93 (insert (number-to-string (car e)) "\t" ;; number
95 (nnrss-format-string (nth 3 e)) "")
98 (nnrss-format-string (nth 4 e))
103 (format "<%d@%s.nnrss>" (car e) group)
110 (memq nnrss-description-field
111 nnmail-extra-headers))
112 (concat (symbol-name nnrss-description-field)
114 (nnrss-format-string (nth 6 e))
118 (memq nnrss-url-field
119 nnmail-extra-headers))
120 (concat (symbol-name nnrss-url-field)
122 (nnrss-format-string (nth 2 e))
128 (deffoo nnrss-request-group (group &optional server dont-check)
129 (nnrss-possibly-change-group group server)
132 (nnrss-check-group group server)
133 (nnheader-report 'nnrss "Opened group %s" group)
135 "211 %d %d %d %s\n" nnrss-group-max nnrss-group-min nnrss-group-max
136 (prin1-to-string group)
139 (deffoo nnrss-close-group (group &optional server)
142 (deffoo nnrss-request-article (article &optional group server buffer)
143 (nnrss-possibly-change-group group server)
144 (let ((e (assq article nnrss-group-data))
145 (boundary "=-=-=-=-=-=-=-=-=-")
146 (nntp-server-buffer (or buffer nntp-server-buffer))
150 (with-current-buffer nntp-server-buffer
152 (goto-char (point-min))
153 (insert "Mime-Version: 1.0\nContent-Type: multipart/alternative; boundary=\"" boundary "\"\n")
155 (insert "Newsgroups: " group "\n"))
157 (insert "Subject: " (nnrss-format-string (nth 3 e)) "\n"))
159 (insert "From: " (nnrss-format-string (nth 4 e)) "\n"))
161 (insert "Date: " (nnrss-format-string (nth 5 e)) "\n"))
162 (insert "Message-ID: " (format "<%d@%s.nnrss>" (car e) group) "\n")
164 (let ((text (if (nth 6 e)
165 (nnrss-string-as-multibyte (nth 6 e))))
168 (insert "\n\n--" boundary "\nContent-Type: text/plain\n\n")
169 (let ((point (point)))
173 (while (re-search-forward "\n" nil t)
175 (goto-char (point-max))
179 (insert "\n\n--" boundary "\nContent-Type: text/html\n\n")
180 (let ((point (point)))
182 (progn (insert "<html><head></head><body>\n" text "\n</body></html>")
184 (while (re-search-forward "\n" nil t)
186 (goto-char (point-max))
189 (insert "<p><a href=\"" link "\">link</a></p>\n"))))
190 (if nnrss-content-function
191 (funcall nnrss-content-function e group article)))))
194 (nnheader-report 'nnrss err))
196 (nnheader-report 'nnrss "no such id: %d" article))
198 (nnheader-report 'nnrss "article %s retrieved" (car e))
199 ;; we return the article number.
200 (cons nnrss-group (car e))))))
202 (deffoo nnrss-request-list (&optional server)
203 (nnrss-possibly-change-group nil server)
204 (nnrss-generate-active)
207 (deffoo nnrss-open-server (server &optional defs connectionless)
208 (nnrss-read-server-data server)
209 (nnoo-change-server 'nnrss server defs)
212 (deffoo nnrss-request-expire-articles
213 (articles group &optional server force)
214 (nnrss-possibly-change-group group server)
215 (let (e days not-expirable changed)
216 (dolist (art articles)
217 (if (and (setq e (assq art nnrss-group-data))
218 (nnmail-expired-article-p
220 (if (listp (setq days (nth 1 e))) days
221 (days-to-time (- days (time-to-days '(0 0)))))
223 (setq nnrss-group-data (delq e nnrss-group-data)
225 (push art not-expirable)))
227 (nnrss-save-group-data group server))
230 (deffoo nnrss-request-delete-group (group &optional force server)
231 (nnrss-possibly-change-group group server)
232 (setq nnrss-server-data
233 (delq (assoc group nnrss-server-data) nnrss-server-data))
234 (nnrss-save-server-data server)
235 (let ((file (expand-file-name
236 (nnrss-translate-file-chars
237 (concat group (and server
238 (not (equal server ""))
240 server ".el")) nnrss-directory)))
245 (deffoo nnrss-request-list-newsgroups (&optional server)
246 (nnrss-possibly-change-group nil server)
248 (set-buffer nntp-server-buffer)
250 (dolist (elem nnrss-group-alist)
252 (insert (car elem) "\t" (third elem) "\n"))))
255 (nnoo-define-skeleton nnrss)
257 ;;; Internal functions
258 (eval-when-compile (defun xml-rpc-method-call (&rest args)))
259 (defun nnrss-fetch (url &optional local)
260 "Fetch the url and put it in a the expected lisp structure."
262 ;some CVS versions of url.el need this to close the connection quickly
263 (let* (xmlform htmlform)
264 ;; bit o' work necessary for w3 pre-cvs and post-cvs
266 (let ((coding-system-for-read 'binary))
267 (insert-file-contents url))
270 ;; Because xml-parse-region can't deal with anything that isn't
271 ;; xml and w3-parse-buffer can't deal with some xml, we have to
272 ;; parse with xml-parse-region first and, if that fails, parse
273 ;; with w3-parse-buffer. Yuck. Eventually, someone should find out
274 ;; why w3-parse-buffer fails to parse some well-formed xml and
278 (setq xmlform (xml-parse-region (point-min) (point-max)))
279 (error (if (fboundp 'w3-parse-buffer)
280 (setq htmlform (caddar (w3-parse-buffer
282 (message "nnrss: Not valid XML and w3 parse not available (%s)"