* texi/Makefile.in (install-ja-info): Specify `EMACS' and `infodir'.
[elisp/gnus.git-] / lisp / nndb.el
1 ;;; nndb.el --- nndb access for Gnus
2 ;; Copyright (C) 1997,98 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;         Kai Grossjohann <grossjohann@ls6.informatik.uni-dortmund.de>
6 ;;         Joe Hildebrand <joe.hildebrand@ilg.com>
7 ;;         David Blacka <davidb@rwhois.net>
8 ;; Keywords: news
9
10 ;; This file is NOT part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; This was based upon Kai Grossjohan's shamessly snarfed code and
30 ;;; further modified by Joe Hildebrand.  It has been updated for Red
31 ;;; Gnus.
32
33 ;; TODO:
34 ;;
35 ;; * Fix bug where server connection can be lost and impossible to regain
36 ;;   This hasn't happened to me in a while; think it was fixed in Rgnus
37 ;;
38 ;; * make it handle different nndb servers seemlessly
39 ;;
40 ;; * Optimize expire if FORCE
41 ;;
42 ;; * Optimize move (only expire once)
43 ;;
44 ;; * Deal with add/deletion of groups
45 ;;
46 ;; * make the backend TOUCH an article when marked as expireable (will
47 ;;   make article expire 'expiry' days after that moment).
48
49 ;;-
50 ;; Register nndb with known select methods.
51
52 (gnus-declare-backend "nndb" 'mail 'respool 'address 'prompt-address)
53
54 ;;; Code:
55
56 (require 'nnmail)
57 (require 'nnheader)
58 (require 'nntp)
59 (eval-when-compile (require 'cl))
60
61 (eval-and-compile
62   (autoload 'news-setup "rnewspost")
63   (autoload 'news-reply-mode "rnewspost")
64   (autoload 'cancel-timer "timer")
65   (autoload 'telnet "telnet" nil t)
66   (autoload 'telnet-send-input "telnet" nil t)
67   (autoload 'gnus-declare-backend "gnus-start"))
68
69 ;; Declare nndb as derived from nntp
70
71 (nnoo-declare nndb nntp)
72
73 ;; Variables specific to nndb
74
75 ;;- currently not used but just in case...
76 (defvoo nndb-deliver-program "nndel"
77   "*The program used to put a message in an NNDB group.")
78
79 (defvoo nndb-server-side-expiry nil
80   "If t, expiry calculation will occur on the server side.")
81
82 (defvoo nndb-set-expire-date-on-mark nil
83   "If t, the expiry date for a given article will be set to the time
84 it was marked as expireable; otherwise the date will be the time the
85 article was posted to nndb")
86
87 ;; Variables copied from nntp
88
89 (defvoo nndb-server-opened-hook '(nntp-send-authinfo-from-file)
90   "Like nntp-server-opened-hook."
91   nntp-server-opened-hook)
92
93 (defvoo nndb-address "localhost"
94   "*The name of the NNDB server."
95   nntp-address)
96
97 (defvoo nndb-port-number 9000
98   "*Port number to connect to."
99   nntp-port-number)
100
101 ;; change to 'news if you are actually using nndb for news
102 (defvoo nndb-article-type 'mail)
103
104 (defvoo nndb-status-string nil "" nntp-status-string)
105
106 \f
107
108 (defconst nndb-version "nndb 0.7"
109   "Version numbers of this version of NNDB.")
110
111 \f
112 ;;; Interface functions.
113
114 (nnoo-define-basics nndb)
115
116 ;;------------------------------------------------------------------
117
118 ;; this function turns the lisp list into a string list.  There is
119 ;; probably a more efficient way to do this.
120 (defun nndb-build-article-string (articles)
121   (let (art-string art)
122     (while articles
123       (setq art (pop articles))
124       (setq art-string (concat art-string art " ")))
125     art-string))
126
127 (defun nndb-build-expire-rest-list (total expire)
128   (let (art rest)
129     (while total
130       (setq art (pop total))
131       (if (memq art expire)
132           ()
133         (push art rest)))
134     rest))
135
136
137 ;;
138 (deffoo nndb-request-type (group &optional article)
139   nndb-article-type)
140
141 ;; nndb-request-update-info does not exist and is not needed
142
143 ;; nndb-request-update-mark does not exist; it should be used to TOUCH
144 ;; articles as they are marked exipirable
145 (defun nndb-touch-article (group article)
146   (nntp-send-command nil "X-TOUCH" article))
147
148 (deffoo nndb-request-update-mark
149     (group article mark)
150   "Sets the expiry date for ARTICLE in GROUP to now, if the mark is 'E'"
151   (if (and nndb-set-expire-date-on-mark (string-equal mark "E"))
152       (nndb-touch-article group article))
153   mark)
154
155 ;; nndb-request-create-group -- currently this isn't necessary; nndb
156 ;;   creates groups on demand.
157
158 ;; todo -- use some other time than the creation time of the article
159 ;;         best is time since article has been marked as expirable
160
161 (defun nndb-request-expire-articles-local
162   (articles &optional group server force)
163   "Let gnus do the date check and issue the delete commands."
164   (let (msg art delete-list (num-delete 0) rest)
165     (nntp-possibly-change-group group server)
166     (while articles
167       (setq art (pop articles))
168       (nntp-send-command "^\\([23]\\|^423\\).*\n" "X-DATE" art)
169       (setq msg (nndb-status-message))
170       (if (string-match "^423" msg)
171           ()
172         (or (string-match "'\\(.+\\)'" msg)
173             (error "Not a valid response for X-DATE command: %s"
174                    msg))
175         (if (nnmail-expired-article-p
176              group
177              (date-to-time (substring msg (match-beginning 1) (match-end 1)))
178              force)
179             (progn
180               (setq delete-list (concat delete-list " " (int-to-string art)))
181               (setq num-delete  (1+ num-delete)))
182           (push art rest))))
183     (if (> (length delete-list) 0)
184         (progn
185           (nnheader-message 5 "Deleting %s article(s) from %s"
186                             (int-to-string num-delete) group)
187           (nntp-send-command "^[23].*\n" "X-DELETE" delete-list))
188       )
189
190     (nnheader-message 5 "")
191     (nconc rest articles)))
192
193 (defun nndb-get-remote-expire-response ()
194   (let (list)
195     (set-buffer nntp-server-buffer)
196     (goto-char (point-min))
197     (if (looking-at "^[34]")
198         ;; x-expire returned error--presume no articles were expirable)
199         (setq list nil)
200       ;; otherwise, pull all of the following numbers into the list
201       (re-search-forward "follows\r?\n?" nil t)
202       (while (re-search-forward "^[0-9]+$" nil t)
203         (push (string-to-int (match-string 0)) list)))
204     list))
205
206 (defun nndb-request-expire-articles-remote
207   (articles &optional group server force)
208   "Let the nndb backend expire articles"
209   (let (days art-string delete-list (num-delete 0))
210     (nntp-possibly-change-group group server)
211
212     ;; first calculate the wait period in days
213     (setq days (or (and nnmail-expiry-wait-function
214                         (funcall nnmail-expiry-wait-function group))
215                    nnmail-expiry-wait))
216     ;; now handle the special cases
217     (cond (force
218            (setq days 0))
219           ((eq days 'never)
220            ;; This isn't an expirable group.
221            (setq days -1))
222           ((eq days 'immediate)
223            (setq days 0)))
224
225
226     ;; build article string
227     (setq art-string (concat days " " (nndb-build-article-string articles)))
228     (nntp-send-command "^\.\r?\n\\|^[345].*\n" "X-EXPIRE" art-string)
229
230     (setq delete-list (nndb-get-remote-expire-response))
231     (setq num-delete (length delete-list))
232     (if (> num-delete 0)
233         (nnheader-message 5 "Deleting %s article(s) from %s"
234                           (int-to-string num-delete) group))
235
236     (nndb-build-expire-rest-list articles delete-list)))
237
238 (deffoo nndb-request-expire-articles
239     (articles &optional group server force)
240   "Expires ARTICLES from GROUP on SERVER.
241 If FORCE, delete regardless of exiration date, otherwise use normal
242 expiry mechanism."
243   (if nndb-server-side-expiry
244       (nndb-request-expire-articles-remote articles group server force)
245     (nndb-request-expire-articles-local articles group server force)))
246
247 (deffoo nndb-request-move-article
248     (article group server accept-form &optional last)
249   "Move ARTICLE (a number) from GROUP on SERVER.
250 Evals ACCEPT-FORM in current buffer, where the article is.
251 Optional LAST is ignored."
252   ;; we guess that the second arg in accept-form is the new group,
253   ;; which it will be for nndb, which is all that matters anyway
254   (let ((new-group (nth 1 accept-form)) result)
255     (nntp-possibly-change-group group server)
256
257     ;; use the move command for nndb-to-nndb moves
258     (if (string-match "^nndb" new-group)
259         (let ((new-group-name (gnus-group-real-name new-group)))
260           (nntp-send-command "^[23].*\n" "X-MOVE" article new-group-name)
261           (cons new-group article))
262       ;; else move normally
263       (let ((artbuf (get-buffer-create " *nndb move*")))
264         (and
265          (nndb-request-article article group server artbuf)
266          (save-excursion
267            (set-buffer artbuf)
268            (insert-buffer-substring nntp-server-buffer)
269            (setq result (eval accept-form))
270            (kill-buffer (current-buffer))
271            result)
272          (nndb-request-expire-articles (list article)
273                                        group
274                                        server
275                                        t))
276         result)
277       )))
278
279 (deffoo nndb-request-accept-article (group server &optional last)
280   "The article in the current buffer is put into GROUP."
281   (nntp-possibly-change-group group server)
282   (let (art msg)
283     (when (nntp-send-command "^[23].*\r?\n" "ACCEPT" group)
284       (nnheader-insert "")
285       (nntp-send-buffer "^[23].*\n"))
286
287     (set-buffer nntp-server-buffer)
288     (setq msg (buffer-substring (point-min) (point-max)))
289     (or (string-match "^\\([0-9]+\\)" msg)
290         (error "nndb: %s" msg))
291     (setq art (substring msg (match-beginning 1) (match-end 1)))
292     (nnheader-message 5 "nndb: accepted %s" art)
293     (list art)))
294
295 (deffoo nndb-request-replace-article (article group buffer)
296   "ARTICLE is the number of the article in GROUP to be replaced with the contents of the BUFFER."
297   (set-buffer buffer)
298   (when (nntp-send-command "^[23].*\r?\n" "X-REPLACE" (int-to-string article))
299     (nnheader-insert "")
300     (nntp-send-buffer "^[23.*\n")
301     (list (int-to-string article))))
302
303 ; nndb-request-delete-group does not exist
304 ; todo -- maybe later
305
306 ; nndb-request-rename-group does not exist
307 ; todo -- maybe later
308
309 ;; -- standard compatability functions
310
311 (deffoo nndb-status-message (&optional server)
312   "Return server status as a string."
313   (set-buffer nntp-server-buffer)
314   (buffer-substring (point-min) (point-max)))
315
316 ;; Import stuff from nntp
317
318 (nnoo-import nndb
319   (nntp))
320
321 (provide 'nndb)