(gnus-summary-set-local-parameters): Always evaluate parameter's operands.
[gnus] / lisp / nnwarchive.el
1 ;;; nnwarchive.el --- interfacing with web archives
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: news egroups mail-archive
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Note: You need to have `url' (w3 0.46) or greater version
27 ;; installed for some functions of this backend to work.
28
29 ;; Todo:
30 ;; 1. To support more web archives.
31 ;; 2. Generalize webmail to other MHonArc archive.
32
33 ;;; Code:
34
35 (eval-when-compile (require 'cl))
36
37 (require 'nnoo)
38 (require 'message)
39 (require 'gnus-util)
40 (require 'gnus)
41 (require 'gnus-bcklg)
42 (require 'nnmail)
43 (require 'mm-util)
44 (require 'mm-url)
45
46 (nnoo-declare nnwarchive)
47
48 (defvar nnwarchive-type-definition
49   '((egroups
50      (address . "www.egroups.com")
51      (open-url
52       "http://www.egroups.com/login.cgi?&login_email=%s&login_password=%s"
53       nnwarchive-login nnwarchive-passwd)
54      (list-url
55       "http://www.egroups.com/mygroups")
56      (list-dissect . nnwarchive-egroups-list)
57      (list-groups . nnwarchive-egroups-list-groups)
58      (xover-url
59       "http://www.egroups.com/messages/%s/%d" group aux)
60      (xover-last-url
61       "http://www.egroups.com/messages/%s/" group)
62      (xover-page-size . 13)
63      (xover-dissect . nnwarchive-egroups-xover)
64      (article-url
65       "http://www.egroups.com/message/%s/%d?source=1" group article)
66      (article-dissect . nnwarchive-egroups-article)
67      (authentication . t)
68      (article-offset . 0)
69      (xover-files . nnwarchive-egroups-xover-files))
70     (mail-archive
71      (address . "www.mail-archive.com")
72      (open-url)
73      (list-url
74       "http://www.mail-archive.com/lists.html")
75      (list-dissect . nnwarchive-mail-archive-list)
76      (list-groups . nnwarchive-mail-archive-list-groups)
77      (xover-url
78       "http://www.mail-archive.com/%s/mail%d.html" group aux)
79      (xover-last-url
80       "http://www.mail-archive.com/%s/maillist.html" group)
81      (xover-page-size)
82      (xover-dissect . nnwarchive-mail-archive-xover)
83      (article-url
84       "http://www.mail-archive.com/%s/msg%05d.html" group article1)
85      (article-dissect . nnwarchive-mail-archive-article)
86      (xover-files . nnwarchive-mail-archive-xover-files)
87      (authentication)
88      (article-offset . 1))))
89
90 (defvar nnwarchive-default-type 'egroups)
91
92 (defvoo nnwarchive-directory (nnheader-concat gnus-directory "warchive/")
93   "Where nnwarchive will save its files.")
94
95 (defvoo nnwarchive-type nil
96     "The type of nnwarchive.")
97
98 (defvoo nnwarchive-address ""
99   "The address of nnwarchive.")
100
101 (defvoo nnwarchive-login nil
102   "Your login name for the group.")
103
104 (defvoo nnwarchive-passwd nil
105   "Your password for the group.")
106
107 (defvoo nnwarchive-groups nil)
108
109 (defvoo nnwarchive-headers-cache nil)
110
111 (defvoo nnwarchive-authentication nil)
112
113 (defvoo nnwarchive-nov-is-evil nil)
114
115 (defconst nnwarchive-version "nnwarchive 1.0")
116
117 ;;; Internal variables
118
119 (defvoo nnwarchive-open-url nil)
120 (defvoo nnwarchive-open-dissect nil)
121
122 (defvoo nnwarchive-list-url nil)
123 (defvoo nnwarchive-list-dissect nil)
124 (defvoo nnwarchive-list-groups nil)
125
126 (defvoo nnwarchive-xover-files nil)
127 (defvoo nnwarchive-xover-url nil)
128 (defvoo nnwarchive-xover-last-url nil)
129 (defvoo nnwarchive-xover-dissect nil)
130 (defvoo nnwarchive-xover-page-size nil)
131
132 (defvoo nnwarchive-article-url nil)
133 (defvoo nnwarchive-article-dissect nil)
134 (defvoo nnwarchive-xover-files nil)
135 (defvoo nnwarchive-article-offset 0)
136
137 (defvoo nnwarchive-buffer nil)
138
139 (defvoo nnwarchive-keep-backlog 300)
140 (defvar nnwarchive-backlog-articles nil)
141 (defvar nnwarchive-backlog-hashtb nil)
142
143 (defvoo nnwarchive-headers nil)
144
145
146 ;;; Interface functions
147
148 (nnoo-define-basics nnwarchive)
149
150 (defun nnwarchive-set-default (type)
151   (let ((defs (cdr (assq type nnwarchive-type-definition)))
152         def)
153     (dolist (def defs)
154       (set (intern (concat "nnwarchive-" (symbol-name (car def))))
155            (cdr def)))))
156
157 (defmacro nnwarchive-backlog (&rest form)
158   `(let ((gnus-keep-backlog nnwarchive-keep-backlog)
159          (gnus-backlog-buffer
160           (format " *nnwarchive backlog %s*" nnwarchive-address))
161          (gnus-backlog-articles nnwarchive-backlog-articles)
162          (gnus-backlog-hashtb nnwarchive-backlog-hashtb))
163      (unwind-protect
164          (progn ,@form)
165        (setq nnwarchive-backlog-articles gnus-backlog-articles
166              nnwarchive-backlog-hashtb gnus-backlog-hashtb))))
167 (put 'nnwarchive-backlog 'lisp-indent-function 0)
168 (put 'nnwarchive-backlog 'edebug-form-spec '(form body))
169
170 (defun nnwarchive-backlog-enter-article (group number buffer)
171   (nnwarchive-backlog
172     (gnus-backlog-enter-article group number buffer)))
173
174 (defun nnwarchive-get-article (article &optional group server buffer)
175   (if (numberp article)
176       (if (nnwarchive-backlog
177             (gnus-backlog-request-article group article
178                                           (or buffer nntp-server-buffer)))
179           (cons group article)
180         (let (contents)
181           (save-excursion
182             (set-buffer nnwarchive-buffer)
183             (goto-char (point-min))
184             (let ((article1 (- article nnwarchive-article-offset)))
185               (nnwarchive-url nnwarchive-article-url))
186             (setq contents (funcall nnwarchive-article-dissect group article)))
187           (when contents
188             (save-excursion
189               (set-buffer (or buffer nntp-server-buffer))
190               (erase-buffer)
191               (insert contents)
192               (nnwarchive-backlog-enter-article group article (current-buffer))
193               (nnheader-report 'nnwarchive "Fetched article %s" article)
194               (cons group article)))))
195     nil))
196
197 (deffoo nnwarchive-retrieve-headers (articles &optional group server fetch-old)
198   (nnwarchive-possibly-change-server group server)
199   (if (or gnus-nov-is-evil nnwarchive-nov-is-evil)
200       (with-temp-buffer
201         (with-current-buffer nntp-server-buffer
202           (erase-buffer))
203         (let ((buf (current-buffer)) b e)
204           (dolist (art articles)
205             (nnwarchive-get-article art group server buf)
206             (setq b (goto-char (point-min)))
207             (if (search-forward "\n\n" nil t)
208                 (forward-char -1)
209               (goto-char (point-max)))
210             (setq e (point))
211             (with-current-buffer nntp-server-buffer
212               (insert (format "221 %d Article retrieved.\n" art))
213               (insert-buffer-substring buf b e)
214               (insert ".\n"))))
215         'headers)
216     (setq nnwarchive-headers (cdr (assoc group nnwarchive-headers-cache)))
217     (save-excursion
218       (set-buffer nnwarchive-buffer)
219       (erase-buffer)
220       (funcall nnwarchive-xover-files group articles))
221     (save-excursion
222       (set-buffer nntp-server-buffer)
223       (erase-buffer)
224       (let (header)
225       (dolist (art articles)
226         (if (setq header (assq art nnwarchive-headers))
227             (nnheader-insert-nov (cdr header))))))
228     (let ((elem (assoc group nnwarchive-headers-cache)))
229       (if elem
230           (setcdr elem nnwarchive-headers)
231         (push (cons group nnwarchive-headers) nnwarchive-headers-cache)))
232     'nov))
233
234 (deffoo nnwarchive-request-group (group &optional server dont-check)
235   (nnwarchive-possibly-change-server nil server)
236   (when (and (not dont-check) nnwarchive-list-groups)
237     (funcall nnwarchive-list-groups (list group))
238     (nnwarchive-write-groups))
239   (let ((elem (assoc group nnwarchive-groups)))
240     (cond
241      ((not elem)
242       (nnheader-report 'nnwarchive "Group does not exist"))
243      (t
244       (nnheader-report 'nnwarchive "Opened group %s" group)
245       (nnheader-insert
246        "211 %d %d %d %s\n" (or (cadr elem) 0) 1 (or (cadr elem) 0)
247        (prin1-to-string group))
248       t))))
249
250 (deffoo nnwarchive-request-article (article &optional group server buffer)
251   (nnwarchive-possibly-change-server group server)
252   (nnwarchive-get-article article group server buffer))
253
254 (deffoo nnwarchive-close-server (&optional server)
255   (when (and (nnwarchive-server-opened server)
256              (gnus-buffer-live-p nnwarchive-buffer))
257     (save-excursion
258       (set-buffer nnwarchive-buffer)
259       (kill-buffer nnwarchive-buffer)))
260   (nnwarchive-backlog
261     (gnus-backlog-shutdown))
262   (nnoo-close-server 'nnwarchive server))
263
264 (deffoo nnwarchive-request-list (&optional server)
265   (nnwarchive-possibly-change-server nil server)
266   (save-excursion
267     (set-buffer nnwarchive-buffer)
268     (erase-buffer)
269     (if nnwarchive-list-url
270         (nnwarchive-url nnwarchive-list-url))
271     (if nnwarchive-list-dissect
272         (funcall nnwarchive-list-dissect))
273     (nnwarchive-write-groups)
274     (nnwarchive-generate-active))
275   t)
276
277 (deffoo nnwarchive-open-server (server &optional defs connectionless)
278   (nnoo-change-server 'nnwarchive server defs)
279   (nnwarchive-init server)
280   (when nnwarchive-authentication
281     (setq nnwarchive-login
282           (or nnwarchive-login
283               (read-string
284                  (format "Login at %s: " server)
285                  user-mail-address)))
286     (setq nnwarchive-passwd
287           (or nnwarchive-passwd
288               (read-passwd
289                (format "Password for %s at %s: "
290                        nnwarchive-login server)))))
291   (unless nnwarchive-groups
292     (nnwarchive-read-groups))
293   (save-excursion
294     (set-buffer nnwarchive-buffer)
295     (erase-buffer)
296     (if nnwarchive-open-url
297         (nnwarchive-url nnwarchive-open-url))
298     (if nnwarchive-open-dissect
299         (funcall nnwarchive-open-dissect)))
300   t)
301
302 (nnoo-define-skeleton nnwarchive)
303
304 ;;; Internal functions
305
306 (defun nnwarchive-possibly-change-server (&optional group server)
307   (nnwarchive-init server)
308   (when (and server
309              (not (nnwarchive-server-opened server)))
310     (nnwarch