* gnus-sum.el (gnus-summary-move-article): pass move-is-internal
[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   (autoload 'news-setup "rnewspost")
64   (autoload 'news-reply-mode "rnewspost")
65   (autoload 'cancel-timer "timer")
66   (autoload 'telnet "telnet" nil t)
67   (autoload 'telnet-send-input "telnet" nil t)
68   (autoload 'gnus-declare-backend "gnus-start"))
69
70 ;; Declare nndb as derived from nntp
71
72 (nnoo-declare nndb nntp)
73
74 ;; Variables specific to nndb
75
76 ;;- currently not used but just in case...
77 (defvoo nndb-deliver-program "nndel"
78   "*The program used to put a message in an NNDB group.")
79
80 (defvoo nndb-server-side-expiry nil
81   "If t, expiry calculation will occur on the server side.")
82
83 (defvoo nndb-set-expire-date-on-mark nil
84   "If t, the expiry date for a given article will be set to the time
85 it was marked as expireable; otherwise the date will be the time the
86 article was posted to nndb")
87
88 ;; Variables copied from nntp
89
90 (defvoo nndb-server-opened-hook '(nntp-send-authinfo-from-file)
91   "Like nntp-server-opened-hook."
92   nntp-server-opened-hook)
93
94 (defvoo nndb-address "localhost"
95   "*The name of the NNDB server."
96   nntp-address)
97
98 (defvoo nndb-port-number 9000
99   "*Port number to connect to."
100   nntp-port-number)
101
102 ;; change to 'news if you are actually using nndb for news
103 (defvoo nndb-article-type 'mail)
104
105 (defvoo nndb-status-string nil "" nntp-status-string)
106
107 \f
108
109 (defconst nndb-version "nndb 0.7"
110   "Version numbers of this version of NNDB.")
111
112 \f
113 ;;; Interface functions.
114
115 (nnoo-define-basics nndb)
116
117 ;;------------------------------------------------------------------
118
119 ;; this function turns the lisp list into a string list.  There is
120 ;; probably a more efficient way to do this.
121 (defun nndb-build-article-string (articles)
122   (let (art-string art)
123     (while articles
124       (setq art (pop articles))
125       (setq art-string (concat art-string art " ")))
126     art-string))
127
128 (defun nndb-build-expire-rest-list (total expire)
129   (let (art rest)
130     (while total
131       (setq art (pop total))
132       (if (memq art expire)
133           ()
134         (push art rest)))
135     rest))
136
137
138 ;;
139 (deffoo nndb-request-type (group &optional article)
140   nndb-article-type)
141
142 ;; nndb-request-update-info does not exist and is not needed
143
144 ;; nndb-request-update-mark does not exist; it should be used to TOUCH
145 ;; articles as they are marked exipirable
146 (defun nndb-touch-article (group article)
147   (nntp-send-command nil "X-TOUCH" article))
148
149 (deffoo nndb-request-update-mark
150     (group article mark)
151   "Sets the expiry date for ARTICLE in GROUP to now, if the mark is 'E'"
152   (if (and nndb-set-expire-date-on-mark (string-equal mark "E"))
153       (nndb-touch-article group article))
154   mark)
155
156 ;; nndb-request-create-group -- currently this isn't necessary; nndb
157 ;;   creates groups on demand.
158
159 ;; todo -- use some other time than the creation time of the article
160 ;;         best is time since article has been marked as expirable
161
162 (defun nndb-request-expire-articles-local
163   (articles &optional group server force)
164   "Let gnus do the date check and issue the delete commands."
165   (let (msg art delete-list (num-delete 0) rest)
166     (nntp-possibly-change-group group server)
167     (while articles
168       (setq art (pop articles))
169       (nntp-send-command "^\\([23]\\|^423\\).*\n" "X-DATE" art)
170       (setq msg (nndb-status-message))
171       (if (string-match "^423" msg)
172           ()
173         (or (string-match "'\\(.+\\)'" msg)
174             (error "Not a valid response for X-DATE command: %s"
175                    msg))
176         (if (nnmail-expired-article-p
177              group
178              (date-to-time (substring msg (match-beginning 1) (match-end 1)))
179              force)
180             (progn
181               (setq delete-list (concat delete-list " " (int-to-string art)))
182               (setq num-delete  (1+ num-delete)))
183           (push art rest))))
184     (if (> (length delete-list) 0)
185         (progn
186           (nnheader-message 5 "Deleting %s article(s) from %s"
187                             (int-to-string num-delete) group)
188           (nntp-send-command "^[23].*\n" "X-DELETE" delete-list))
189       )
190
191     (nnheader-message 5 "")
192     (nconc rest articles)))
193
194 (defun nndb-get-remote-expire-response ()
195   (let (list)
196     (set-buffer nntp-server-buffer)
197     (goto-char (point-min))
198     (if (looking-at "^[34]")
199        ;; x-expire returned error--presume no articles were expirable)
200         (setq list nil)
201       ;; otherwise, pull all of the following numbers into the list
202       (re-search-forward "follows\r?\n?" nil t)
203       (while (re-search-forward "^[0-9]+$" nil t)
204       (push (string-to-int (match-string 0)) list)))
205     list))
206
207 (defun nndb-request-expire-articles-remote
208   (articles &optional group server force)
209   "Let the nndb backend expire articles"
210   (let (days art-string delete-list (num-delete 0))
211     (nntp-possibly-change-group group server)
212
213     ;; first calculate the wait period in days
214     (setq days (or (and nnmail-expiry-wait-function
215                         (funcall nnmail-expiry-wait-function group))
216     nnmail-expiry-wait))
217     ;; now handle the special cases
218     (cond (force
219     (setq days 0))
220           ((eq days 'never)
221            ;; This isn't an expirable group.
222           (setq days -1))
223           ((eq days 'immediate)
224           (setq days 0)))
225
226
227     ;; build article string
228     (setq art-string (concat days " " (nndb-build-article-string articles)))
229     (nntp-send-command "^\.\r?\n\\|^[345].*\n" "X-EXPIRE" art-string)
230
231     (setq delete-list (nndb-get-remote-expire-response))
232     (setq num-delete (length delete-list))
233     (if (> num-delete 0)
234         (nnheader-message 5 "Deleting %s article(s) from %s"
235                           (int-to-string num-delete) group))
236
237     (nndb-build-expire-rest-list articles delete-list)))
238
239 (deffoo nndb-request-expire-articles
240     (articles &optional group server force)
241   "Expires ARTICLES from GROUP on SERVER.
242 If FORCE, delete regardless of exiration date, otherwise use normal
243 expiry mechanism."
244   (if nndb-server-side-expiry
245       (nndb-request-expire-articles-remote articles group server force)
246     (nndb-request-expire-articles-local articles group server force)))
247
248 (deffoo nndb-request-move-article
249     (article group server accept-form &optional last move-is-internal)
250   "Move ARTICLE (a number) from GROUP on SERVER.
251 Evals ACCEPT-FORM in current buffer, where the article is.
252 Optional LAST is ignored."
253   ;; we guess that the second arg in accept-form is the new group,
254   ;; which it will be for nndb, which is all that matters anyway
255   (let ((new-group (nth 1 accept-form)) result)
256     (nntp-possibly-change-group group server)
257
258     ;; use the move command for nndb-to-nndb moves
259     (if (string-match "^nndb" new-group)
260         (let ((new-group-name (gnus-group-real-name new-group)))
261           (nntp-send-command "^[23].*\n" "X-MOVE" article new-group-name)
262           (cons new-group article))
263       ;; else move normally
264       (let ((artbuf (get-buffer-create " *nndb move*")))
265       (and
266        (nndb-request-article article group server artbuf)
267        (save-excursion
268          (set-buffer artbuf)
269          (insert-buffer-substring nntp-server-buffer)
270          (setq result (eval accept-form))
271          (kill-buffer (current-buffer))
272          result)
273        (nndb-request-expire-articles (list article)
274                                      group
275                                      server
276                                      t))
277       result)
278       )))
279
280 (deffoo nndb-request-accept-article (group server &optional last)
281   "The article in the current buffer is put into GROUP."
282   (nntp-possibly-change-group group server)
283   (let (art msg)
284     (when (nntp-send-command "^[23].*\r?\n" "ACCEPT" group)
285       (nnheader-insert "")
286       (nntp-send-buffer "^[23].*\n"))
287
288     (set-buffer nntp-server-buffer)
289     (setq msg (buffer-string))
290     (or (string-match "^\\([0-9]+\\)" msg)
291         (error "nndb: %s" msg))
292     (setq art (substring msg (match-beginning 1) (match-end 1)))
293     (nnheader-message 5 "nndb: accepted %s" art)
294     (list art)))
295
296 (deffoo nndb-request-replace-article (article group buffer)
297   "ARTICLE is the number of the article in GROUP to be replaced with the contents of the BUFFER."
298   (set-buffer buffer)
299   (when (nntp-send-command "^[23].*\r?\n" "X-REPLACE" (int-to-string article))
300     (nnheader-insert "")
301     (nntp-send-buffer "^[23.*\n")
302     (list (int-to-string article))))
303
304                             ; nndb-request-delete-group does not exist
305                                         ; todo -- maybe later
306
307                             ; nndb-request-rename-group does not exist
308                                         ; todo -- maybe later
309
310 ;; -- standard compatability functions
311
312 (deffoo nndb-status-message (&optional server)
313   "Return server status as a string."
314   (set-buffer nntp-server-buffer)
315   (buffer-string))
316
317 ;; Import stuff from nntp
318
319 (nnoo-import nndb
320   (nntp))
321
322 (provide 'nndb)
323
324 ;;; arch-tag: 83bd6fb4-58d9-4fed-a901-c6c625ad5f8a
325 ;;; nndb.el ends here