f75303d67fdf0d0d67e040e36b58cc847de4740b
[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
37 \f
38
39 (defconst nndraft-version "nndraft 1.0")
40 (defvar nndraft-status-string "")
41
42 \f
43
44 (defvar nndraft-current-server nil)
45 (defvar nndraft-server-alist nil)
46 (defvar nndraft-server-variables 
47   (list
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-init-server-buffer)
90   (if (equal server nndraft-current-server)
91       t
92     (if nndraft-current-server
93         (setq nndraft-server-alist 
94               (cons (list nndraft-current-server
95                           (nnheader-save-variables nndraft-server-variables))
96                     nndraft-server-alist)))
97     (let ((state (assoc server nndraft-server-alist)))
98       (if state 
99           (progn
100             (nnheader-restore-variables (nth 1 state))
101             (setq nndraft-server-alist (delq state nndraft-server-alist)))
102         (nnheader-set-init-variables nndraft-server-variables defs))
103       (or (assq 'nndraft-directory defs)
104           (setq nndraft-directory server)))
105     (setq nndraft-current-server server)))
106
107 (defun nndraft-close-server (&optional server)
108   t)
109
110 (defun nndraft-server-opened (&optional server)
111   (and nntp-server-buffer
112        (get-buffer nntp-server-buffer)
113        nndraft-current-server
114        (equal nndraft-current-server server)))
115
116 (defun nndraft-status-message (&optional server)
117   nndraft-status-string)
118
119 (defun nndraft-request-article (id &optional group server buffer)
120   (when (numberp id)
121     ;; We get the newest file of the auto-saved file and the 
122     ;; "real" file.
123     (let* ((file (nndraft-article-filename id))
124            (auto (nndraft-auto-save-file-name file))
125            (newest (if (file-newer-than-file-p file auto) file auto))
126            (nntp-server-buffer (or buffer nntp-server-buffer)))
127       (when (and (file-exists-p newest)
128                  (nnmail-find-file newest))
129         (save-excursion 
130           (set-buffer nntp-server-buffer)
131           (goto-char (point-min))
132           ;; If there's a mail header separator in this file, 
133           ;; we remove it.
134           (when (re-search-forward
135                  (concat "^" mail-header-separator "$") nil t)
136             (replace-match "" t t)))
137         t))))
138
139 (defun nndraft-request-restore-buffer (article &optional group server)
140   "Request a new buffer that is restored to the state of ARTICLE."
141   (let ((file (nndraft-article-filename article ".state"))
142         nndraft-point nndraft-mode nndraft-buffer-name)
143     (when (file-exists-p file)
144       (load file t t t)
145       (when nndraft-buffer-name
146         (set-buffer (get-buffer-create
147                      (generate-new-buffer-name nndraft-buffer-name)))
148         (nndraft-request-article article group server (current-buffer))
149         (funcall nndraft-mode)
150         (let ((gnus-verbose-backends nil))
151           (nndraft-request-expire-articles (list article) group server t))
152         (goto-char nndraft-point))
153       nndraft-buffer-name)))
154
155 (defun nndraft-request-update-info (group info &optional server)
156   (setcar (cdr (cdr info)) nil)
157   (when (nth 3 info)
158     (setcar (nthcdr 3 info) nil)))
159
160 (defun nndraft-request-associate-buffer (group)
161   "Associate the current buffer with some article in the draft group."
162   (let* ((gnus-verbose-backends nil)
163          (article (cdr (nndraft-request-accept-article group 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-post (&optional server)
185   (mail-send-and-exit nil))
186
187 (defun nndraft-request-expire-articles 
188   (articles group &optional server force)
189   (let ((res (nndraft-execute-nnmh-command
190               (` (nnmh-request-expire-articles
191                   (quote (, articles)) group (, server) (, force)))))
192         article)
193     ;; Delete all the "state" files of articles that have been expired.
194     (while articles
195       (unless (memq (setq article (pop articles)) res)
196         (let ((file (nndraft-article-filename article ".state"))
197               (auto (nndraft-auto-save-file-name
198                      (nndraft-article-filename article))))
199           (when (file-exists-p file)
200             (funcall nnmail-delete-file-function file))
201           (when (file-exists-p auto)
202             (funcall nnmail-delete-file-function auto)))))
203     res))
204
205 (defun nndraft-request-accept-article (group &optional last noinsert)
206   (let* ((point (point))
207          (mode major-mode)
208          (name (buffer-name))
209          (gnus-verbose-backends nil)
210          (gart (nndraft-execute-nnmh-command
211                 (` (nnmh-request-accept-article group (, last) noinsert))))
212          (state
213           (nndraft-article-filename (cdr gart) ".state")))
214     ;; Write the "state" file.
215     (save-excursion
216       (nnheader-set-temp-buffer " *draft state*")
217       (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
218                                     nndraft-point ,point
219                                     nndraft-buffer-name ,name)))
220       (write-region (point-min) (point-max) state nil 'silent)
221       (kill-buffer (current-buffer)))
222     gart))
223
224 (defun nndraft-close-group (group &optional server)
225   t)
226
227 (defun nndraft-request-create-group (group &optional server)
228   (if (file-exists-p nndraft-directory)
229       (if (file-directory-p nndraft-directory)
230           t
231         nil)
232     (condition-case ()
233         (progn
234           (make-directory nndraft-directory t)
235           t)
236       (file-error nil))))
237
238 \f
239 ;;; Low-Level Interface
240
241 (defun nndraft-execute-nnmh-command (command)
242   (let ((dir (expand-file-name nndraft-directory)))
243     (and (string-match "/$" dir)
244          (setq dir (substring dir 0 (match-beginning 0))))
245     (string-match "/[^/]+$" dir)
246     (let ((group (substring dir (1+ (match-beginning 0))))
247           (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
248           (nnmh-get-new-mail nil))
249       (eval command))))
250
251 (defun nndraft-article-filename (article &rest args)
252   (apply 'concat
253          (file-name-as-directory nndraft-directory)
254          (int-to-string article)
255          args))
256
257 (defun nndraft-auto-save-file-name (file)
258   (save-excursion
259     (prog1
260         (progn
261           (set-buffer (get-buffer-create " *draft tmp*"))
262           (setq buffer-file-name file)
263           (make-auto-save-file-name))
264       (kill-buffer (current-buffer)))))
265
266 (provide 'nndraft)
267
268 ;;; nndraft.el ends here