*** empty log message ***
[gnus] / lisp / nndraft.el
1 ;;; nndraft.el --- draft article access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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 by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'nnheader)
29 (require 'nnmh)
30 (eval-and-compile (require 'cl))
31
32 (eval-and-compile
33   (autoload 'mail-send-and-exit "sendmail"))
34
35 (defvar nndraft-directory nil
36   "Where nndraft will store its directory.")
37
38 \f
39
40 (defconst nndraft-version "nndraft 1.0")
41 (defvar nndraft-status-string "")
42
43 \f
44
45 (defvar nndraft-current-server nil)
46 (defvar nndraft-server-alist nil)
47 (defvar nndraft-server-variables 
48   `((nndraft-directory nil)
49     (nndraft-status-string "")
50     (nndraft-group-alist)))
51
52 \f
53
54 ;;; Interface functions.
55
56
57 (defun nndraft-retrieve-headers (articles &optional group server fetch-old)
58   (save-excursion
59     (set-buffer nntp-server-buffer)
60     (erase-buffer)
61     (let* ((file nil)
62            (buf (get-buffer-create " *draft headers*"))
63            beg article)
64       (set-buffer buf)
65       (buffer-disable-undo (current-buffer))
66       (erase-buffer)
67       ;; We don't support fetching by Message-ID.
68       (if (stringp (car articles))
69           'headers
70         (while articles
71           (set-buffer buf)
72           (when (nndraft-request-article 
73                  (setq article (pop articles)) group server (current-buffer))
74             (goto-char (point-min))
75             (if (search-forward "\n\n" nil t)
76                 (forward-line -1)
77               (goto-char (point-max)))
78             (delete-region (point) (point-max))
79             (set-buffer nntp-server-buffer)
80             (goto-char (point-max))
81             (insert (format "221 %d Article retrieved.\n" article))
82             (insert-buffer-substring buf)
83             (insert ".\n")))
84
85         (nnheader-fold-continuation-lines)
86         'headers))))
87
88 (defun nndraft-open-server (server &optional defs)
89   (nnheader-change-server 'nndraft server defs)
90   (unless (assq 'nndraft-directory defs)
91     (setq nndraft-directory server))
92   (cond 
93    ((not (file-exists-p nndraft-directory))
94     (nndraft-close-server)
95     (nnheader-report 'nndraft "No such file or directory: %s"
96                      nndraft-directory))
97    ((not (file-directory-p (file-truename nndraft-directory)))
98     (nndraft-close-server)
99     (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
100    (t
101     (nnheader-report 'nndraft "Opened server %s using directory %s"
102                      server nndraft-directory)
103     t)))
104
105 (defun nndraft-close-server (&optional server)
106   (setq nndraft-current-server nil)
107   t)
108
109 (defun nndraft-server-opened (&optional server)
110   (and nntp-server-buffer
111        (get-buffer nntp-server-buffer)
112        nndraft-current-server
113        (equal nndraft-current-server server)))
114
115 (defun nndraft-status-message (&optional server)
116   nndraft-status-string)
117
118 (defun nndraft-request-article (id &optional group server buffer)
119   (when (numberp id)
120     ;; We get the newest file of the auto-saved file and the 
121     ;; "real" file.
122     (let* ((file (nndraft-article-filename id))
123            (auto (nndraft-auto-save-file-name file))
124            (newest (if (file-newer-than-file-p file auto) file auto))
125            (nntp-server-buffer (or buffer nntp-server-buffer)))
126       (when (and (file-exists-p newest)
127                  (nnmail-find-file newest))
128         (save-excursion 
129           (set-buffer nntp-server-buffer)
130           (goto-char (point-min))
131           ;; If there's a mail header separator in this file, 
132           ;; we remove it.
133           (when (re-search-forward
134                  (concat "^" mail-header-separator "$") nil t)
135             (replace-match "" t t)))
136         t))))
137
138 (defun nndraft-request-restore-buffer (article &optional group server)
139   "Request a new buffer that is restored to the state of ARTICLE."
140   (let ((file (nndraft-article-filename article ".state"))
141         nndraft-point nndraft-mode nndraft-buffer-name)
142     (when (file-exists-p file)
143       (load file t t t)
144       (when nndraft-buffer-name
145         (set-buffer (get-buffer-create
146                      (generate-new-buffer-name nndraft-buffer-name)))
147         (nndraft-request-article article group server (current-buffer))
148         (funcall nndraft-mode)
149         (let ((gnus-verbose-backends nil))
150           (nndraft-request-expire-articles (list article) group server t))
151         (goto-char nndraft-point))
152       nndraft-buffer-name)))
153
154 (defun nndraft-request-update-info (group info &optional server)
155   (setcar (cdr (cdr info)) nil)
156   (when (nth 3 info)
157     (setcar (nthcdr 3 info) nil)))
158
159 (defun nndraft-request-associate-buffer (group)
160   "Associate the current buffer with some article in the draft group."
161   (let* ((gnus-verbose-backends nil)
162          (article (cdr (nndraft-request-accept-article group t 'noinsert)))
163          (file (nndraft-article-filename article)))
164     (setq buffer-file-name file)
165     (setq buffer-auto-save-file-name (make-auto-save-file-name))
166     (clear-visited-file-modtime)
167     article))
168
169 (defun nndraft-request-group (group &optional server dont-check)
170   (prog1
171       (nndraft-execute-nnmh-command
172        `(nnmh-request-group group "" ,dont-check))
173     (nnheader-report 'nndraft nnmh-status-string)))
174
175 (defun nndraft-request-list (&optional server dir)
176   (nndraft-execute-nnmh-command
177    `(nnmh-request-list nil ,dir)))
178
179 (defun nndraft-request-newgroups (date &optional server)
180   (nndraft-execute-nnmh-command
181    `(nnmh-request-newgroups ,date ,server)))
182
183 (defun nndraft-request-expire-articles 
184   (articles group &optional server force)
185   (let ((res (nndraft-execute-nnmh-command
186               `(nnmh-request-expire-articles
187                 ',articles group ,server ,force)))
188         article)
189     ;; Delete all the "state" files of articles that have been expired.
190     (while articles
191       (unless (memq (setq article (pop articles)) res)
192         (let ((file (nndraft-article-filename article ".state"))
193               (auto (nndraft-auto-save-file-name
194                      (nndraft-article-filename article))))
195           (when (file-exists-p file)
196             (funcall nnmail-delete-file-function file))
197           (when (file-exists-p auto)
198             (funcall nnmail-delete-file-function auto)))))
199     res))
200
201 (defun nndraft-request-accept-article (group &optional last noinsert)
202   (let* ((point (point))
203          (mode major-mode)
204          (name (buffer-name))
205          (gnus-verbose-backends nil)
206          (gart (nndraft-execute-nnmh-command
207                 `(nnmh-request-accept-article group ,last noinsert)))
208          (state
209           (nndraft-article-filename (cdr gart) ".state")))
210     ;; Write the "state" file.
211     (save-excursion
212       (nnheader-set-temp-buffer " *draft state*")
213       (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
214                                     nndraft-point ,point
215                                     nndraft-buffer-name ,name)))
216       (write-region (point-min) (point-max) state nil 'silent)
217       (kill-buffer (current-buffer)))
218     gart))
219
220 (defun nndraft-close-group (group &optional server)
221   t)
222
223 (defun nndraft-request-create-group (group &optional server)
224   (if (file-exists-p nndraft-directory)
225       (if (file-directory-p nndraft-directory)
226           t
227         nil)
228     (condition-case ()
229         (progn
230           (make-directory nndraft-directory t)
231           t)
232       (file-error nil))))
233
234 \f
235 ;;; Low-Level Interface
236
237 (defun nndraft-execute-nnmh-command (command)
238   (let ((dir (expand-file-name nndraft-directory)))
239     (and (string-match "/$" dir)
240          (setq dir (substring dir 0 (match-beginning 0))))
241     (string-match "/[^/]+$" dir)
242     (let ((group (substring dir (1+ (match-beginning 0))))
243           (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
244           (nnmail-keep-last-article nil)
245           (nnmh-get-new-mail nil))
246       (eval command))))
247
248 (defun nndraft-article-filename (article &rest args)
249   (apply 'concat
250          (file-name-as-directory nndraft-directory)
251          (int-to-string article)
252          args))
253
254 (defun nndraft-auto-save-file-name (file)
255   (save-excursion
256     (prog1
257         (progn
258           (set-buffer (get-buffer-create " *draft tmp*"))
259           (setq buffer-file-name file)
260           (make-auto-save-file-name))
261       (kill-buffer (current-buffer)))))
262
263 (provide 'nndraft)
264
265 ;;; nndraft.el ends here