*** 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* ((buf (get-buffer-create " *draft headers*"))
62            article)
63       (set-buffer buf)
64       (buffer-disable-undo (current-buffer))
65       (erase-buffer)
66       ;; We don't support fetching by Message-ID.
67       (if (stringp (car articles))
68           'headers
69         (while articles
70           (set-buffer buf)
71           (when (nndraft-request-article 
72                  (setq article (pop articles)) group server (current-buffer))
73             (goto-char (point-min))
74             (if (search-forward "\n\n" nil t)
75                 (forward-line -1)
76               (goto-char (point-max)))
77             (delete-region (point) (point-max))
78             (set-buffer nntp-server-buffer)
79             (goto-char (point-max))
80             (insert (format "221 %d Article retrieved.\n" article))
81             (insert-buffer-substring buf)
82             (insert ".\n")))
83
84         (nnheader-fold-continuation-lines)
85         'headers))))
86
87 (defun nndraft-open-server (server &optional defs)
88   (nnheader-change-server 'nndraft server defs)
89   (unless (assq 'nndraft-directory defs)
90     (setq nndraft-directory server))
91   (cond 
92    ((not (file-exists-p nndraft-directory))
93     (nndraft-close-server)
94     (nnheader-report 'nndraft "No such file or directory: %s"
95                      nndraft-directory))
96    ((not (file-directory-p (file-truename nndraft-directory)))
97     (nndraft-close-server)
98     (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
99    (t
100     (nnheader-report 'nndraft "Opened server %s using directory %s"
101                      server nndraft-directory)
102     t)))
103
104 (defun nndraft-close-server (&optional server)
105   (setq nndraft-current-server nil)
106   t)
107
108 (defun nndraft-server-opened (&optional server)
109   (and nntp-server-buffer
110        (get-buffer nntp-server-buffer)
111        nndraft-current-server
112        (equal nndraft-current-server server)))
113
114 (defun nndraft-status-message (&optional server)
115   nndraft-status-string)
116
117 (defun nndraft-request-article (id &optional group server buffer)
118   (when (numberp id)
119     ;; We get the newest file of the auto-saved file and the 
120     ;; "real" file.
121     (let* ((file (nndraft-article-filename id))
122            (auto (nndraft-auto-save-file-name file))
123            (newest (if (file-newer-than-file-p file auto) file auto))
124            (nntp-server-buffer (or buffer nntp-server-buffer)))
125       (when (and (file-exists-p newest)
126                  (nnmail-find-file newest))
127         (save-excursion 
128           (set-buffer nntp-server-buffer)
129           (goto-char (point-min))
130           ;; If there's a mail header separator in this file, 
131           ;; we remove it.
132           (when (re-search-forward
133                  (concat "^" mail-header-separator "$") nil t)
134             (replace-match "" t t)))
135         t))))
136
137 (defun nndraft-request-restore-buffer (article &optional group server)
138   "Request a new buffer that is restored to the state of ARTICLE."
139   (let ((file (nndraft-article-filename article ".state"))
140         nndraft-point nndraft-mode nndraft-buffer-name)
141     (when (file-exists-p file)
142       (load file t t t)
143       (when nndraft-buffer-name
144         (set-buffer (get-buffer-create
145                      (generate-new-buffer-name nndraft-buffer-name)))
146         (nndraft-request-article article group server (current-buffer))
147         (funcall nndraft-mode)
148         (let ((gnus-verbose-backends nil))
149           (nndraft-request-expire-articles (list article) group server t))
150         (goto-char nndraft-point))
151       nndraft-buffer-name)))
152
153 (defun nndraft-request-update-info (group info &optional server)
154   (setcar (cddr info) nil)
155   (when (nth 3 info)
156     (setcar (nthcdr 3 info) nil))
157   t)
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
163                         group nndraft-current-server t 'noinsert)))
164          (file (nndraft-article-filename article)))
165     (setq buffer-file-name file)
166     (setq buffer-auto-save-file-name (make-auto-save-file-name))
167     (clear-visited-file-modtime)
168     article))
169
170 (defun nndraft-request-group (group &optional server dont-check)
171   (prog1
172       (nndraft-execute-nnmh-command
173        `(nnmh-request-group group "" ,dont-check))
174     (nnheader-report 'nndraft nnmh-status-string)))
175
176 (defun nndraft-request-list (&optional server dir)
177   (nndraft-execute-nnmh-command
178    `(nnmh-request-list nil ,dir)))
179
180 (defun nndraft-request-newgroups (date &optional server)
181   (nndraft-execute-nnmh-command
182    `(nnmh-request-newgroups ,date ,server)))
183
184 (defun nndraft-request-expire-articles 
185   (articles group &optional server force)
186   (let ((res (nndraft-execute-nnmh-command
187               `(nnmh-request-expire-articles
188                 ',articles group ,server ,force)))
189         article)
190     ;; Delete all the "state" files of articles that have been expired.
191     (while articles
192       (unless (memq (setq article (pop articles)) res)
193         (let ((file (nndraft-article-filename article ".state"))
194               (auto (nndraft-auto-save-file-name
195                      (nndraft-article-filename article))))
196           (when (file-exists-p file)
197             (funcall nnmail-delete-file-function file))
198           (when (file-exists-p auto)
199             (funcall nnmail-delete-file-function auto)))))
200     res))
201
202 (defun nndraft-request-accept-article (group &optional server last noinsert)
203   (let* ((point (point))
204          (mode major-mode)
205          (name (buffer-name))
206          (gnus-verbose-backends nil)
207          (gart (nndraft-execute-nnmh-command
208                 `(nnmh-request-accept-article group ,server ,last noinsert)))
209          (state
210           (nndraft-article-filename (cdr gart) ".state")))
211     ;; Write the "state" file.
212     (save-excursion
213       (nnheader-set-temp-buffer " *draft state*")
214       (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
215                                     nndraft-point ,point
216                                     nndraft-buffer-name ,name)))
217       (write-region (point-min) (point-max) state nil 'silent)
218       (kill-buffer (current-buffer)))
219     gart))
220
221 (defun nndraft-close-group (group &optional server)
222   t)
223
224 (defun nndraft-request-create-group (group &optional server)
225   (if (file-exists-p nndraft-directory)
226       (if (file-directory-p nndraft-directory)
227           t
228         nil)
229     (condition-case ()
230         (progn
231           (make-directory nndraft-directory t)
232           t)
233       (file-error nil))))
234
235 \f
236 ;;; Low-Level Interface
237
238 (defun nndraft-execute-nnmh-command (command)
239   (let ((dir (expand-file-name nndraft-directory)))
240     (and (string-match "/$" dir)
241          (setq dir (substring dir 0 (match-beginning 0))))
242     (string-match "/[^/]+$" dir)
243     (let ((group (substring dir (1+ (match-beginning 0))))
244           (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
245           (nnmail-keep-last-article nil)
246           (nnmh-get-new-mail nil))
247       (eval command))))
248
249 (defun nndraft-article-filename (article &rest args)
250   (apply 'concat
251          (file-name-as-directory nndraft-directory)
252          (int-to-string article)
253          args))
254
255 (defun nndraft-auto-save-file-name (file)
256   (save-excursion
257     (prog1
258         (progn
259           (set-buffer (get-buffer-create " *draft tmp*"))
260           (setq buffer-file-name file)
261           (make-auto-save-file-name))
262       (kill-buffer (current-buffer)))))
263
264 (provide 'nndraft)
265
266 ;;; nndraft.el ends here