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