*** empty log message ***
[gnus] / lisp / nndraft.el
1 ;;; nndraft.el --- draft article access for Gnus
2 ;; Copyright (C) 1995,96,97 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 'nnmail)
30 (require 'gnus-start)
31 (require 'nnmh)
32 (require 'nnoo)
33 (eval-when-compile (require 'cl))
34
35 (nnoo-declare nndraft
36   nnmh)
37
38 (defvoo nndraft-directory (nnheader-concat message-directory "drafts/")
39   "Where nndraft will store its files."
40   nnmh-current-directory)
41
42 \f
43
44 (defvoo nndraft-current-group "" nil nnmh-current-group)
45 (defvoo nndraft-top-directory nil nil nnmh-directory)
46 (defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail)
47
48 (defconst nndraft-version "nndraft 1.0")
49 (defvoo nndraft-status-string "" nil nnmh-status-string)
50
51 \f
52
53 ;;; Interface functions.
54
55 (nnoo-define-basics nndraft)
56
57 (deffoo nndraft-open-server (server &optional defs)
58   (push `(nndraft-current-group
59           ,(file-name-nondirectory (directory-file-name nndraft-directory)))
60         defs)
61   (push `(nndraft-top-directory
62           ,(file-name-directory (directory-file-name nndraft-directory)))
63         defs)
64   (nnoo-change-server 'nndraft server defs)
65   (cond
66    ((not (file-exists-p nndraft-directory))
67     (nndraft-close-server)
68     (nnheader-report 'nndraft "No such file or directory: %s"
69                      nndraft-directory))
70    ((not (file-directory-p (file-truename nndraft-directory)))
71     (nndraft-close-server)
72     (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
73    (t
74     (nnheader-report 'nndraft "Opened server %s using directory %s"
75                      server nndraft-directory)
76     t)))
77
78 (deffoo nndraft-retrieve-headers (articles &optional group server fetch-old)
79   (save-excursion
80     (set-buffer nntp-server-buffer)
81     (erase-buffer)
82     (let* ((buf (get-buffer-create " *draft headers*"))
83            article)
84       (set-buffer buf)
85       (buffer-disable-undo (current-buffer))
86       (erase-buffer)
87       ;; We don't support fetching by Message-ID.
88       (if (stringp (car articles))
89           'headers
90         (while articles
91           (set-buffer buf)
92           (when (nndraft-request-article
93                  (setq article (pop articles)) group server (current-buffer))
94             (goto-char (point-min))
95             (if (search-forward "\n\n" nil t)
96                 (forward-line -1)
97               (goto-char (point-max)))
98             (delete-region (point) (point-max))
99             (set-buffer nntp-server-buffer)
100             (goto-char (point-max))
101             (insert (format "221 %d Article retrieved.\n" article))
102             (insert-buffer-substring buf)
103             (insert ".\n")))
104
105         (nnheader-fold-continuation-lines)
106         'headers))))
107
108 (deffoo nndraft-request-article (id &optional group server buffer)
109   (when (numberp id)
110     ;; We get the newest file of the auto-saved file and the
111     ;; "real" file.
112     (let* ((file (nndraft-article-filename id))
113            (auto (nndraft-auto-save-file-name file))
114            (newest (if (file-newer-than-file-p file auto) file auto))
115            (nntp-server-buffer (or buffer nntp-server-buffer)))
116       (when (and (file-exists-p newest)
117                  (nnmail-find-file newest))
118         (save-excursion
119           (set-buffer nntp-server-buffer)
120           (goto-char (point-min))
121           ;; If there's a mail header separator in this file,
122           ;; we remove it.
123           (when (re-search-forward
124                  (concat "^" mail-header-separator "$") nil t)
125             (replace-match "" t t)))
126         t))))
127
128 (deffoo nndraft-request-restore-buffer (article &optional group server)
129   "Request a new buffer that is restored to the state of ARTICLE."
130   (when (nndraft-request-article article group server (current-buffer))
131     (let ((gnus-verbose-backends nil))
132       (nndraft-request-expire-articles (list article) group server t))
133     t))
134
135 (deffoo nndraft-request-update-info (group info &optional server)
136   (gnus-info-set-read
137    info
138    (gnus-update-read-articles "nndraft:draft" (nndraft-articles) t))
139   (let (marks)
140     (when (setq marks (nth 3 info))
141       (setcar (nthcdr 3 info)
142               (if (assq 'unsend marks)
143                   (list (assq 'unsend marks))
144                 nil))))
145   t)
146
147 (deffoo nndraft-request-associate-buffer (group)
148   "Associate the current buffer with some article in the draft group."
149   (let ((gnus-verbose-backends nil)
150         (buf (current-buffer))
151          article file)
152     (nnheader-temp-write nil
153       (insert-buffer buf)
154       (setq article (cdr (nndraft-request-accept-article
155                           group (nnoo-current-server 'nndraft) t 'noinsert)))
156       (setq file (nndraft-article-filename article)))
157     (setq buffer-file-name file)
158     (setq buffer-auto-save-file-name (make-auto-save-file-name))
159     (clear-visited-file-modtime)
160     article))
161
162 (deffoo nndraft-request-expire-articles (articles group &optional server force)
163   (let* ((nnmh-allow-delete-final t)
164          (res (nndraft-execute-nnmh-command
165                `(nnmh-request-expire-articles
166                  ',articles group ,server ,force)))
167          article)
168     ;; Delete all the "state" files of articles that have been expired.
169     (while articles
170       (unless (memq (setq article (pop articles)) res)
171         (let ((auto (nndraft-auto-save-file-name
172                      (nndraft-article-filename article))))
173           (when (file-exists-p auto)
174             (funcall nnmail-delete-file-function auto)))))
175     res))
176
177 (deffoo nndraft-request-accept-article (group &optional server last noinsert)
178   (let ((gnus-verbose-backends nil))
179     (nndraft-execute-nnmh-command
180      `(nnmh-request-accept-article group ,server ,last noinsert))))
181
182 (deffoo nndraft-request-create-group (group &optional server args)
183   (if (file-exists-p nndraft-directory)
184       (if (file-directory-p nndraft-directory)
185           t
186         nil)
187     (condition-case ()
188         (progn
189           (gnus-make-directory nndraft-directory)
190           t)
191       (file-error nil))))
192
193 \f
194 ;;; Low-Level Interface
195
196 (defun nndraft-execute-nnmh-command (command)
197   (let* ((dir (directory-file-name (expand-file-name nndraft-directory)))
198          (group (file-name-nondirectory dir))
199          (nnmh-directory (file-name-directory dir))
200          (nnmail-keep-last-article nil)
201          (nnmh-get-new-mail nil))
202     (eval command)))
203
204 (defun nndraft-article-filename (article &rest args)
205   (apply 'concat
206          (file-name-as-directory nndraft-directory)
207          (int-to-string article)
208          args))
209
210 (defun nndraft-auto-save-file-name (file)
211   (save-excursion
212     (prog1
213         (progn
214           (set-buffer (get-buffer-create " *draft tmp*"))
215           (setq buffer-file-name file)
216           (make-auto-save-file-name))
217       (kill-buffer (current-buffer)))))
218
219 (defun nndraft-articles ()
220   "Return the list of messages in the group."
221   (sort
222    (mapcar
223     (lambda (file)
224       (string-to-int file))
225     (directory-files nndraft-directory nil "^[0-9]+$" t))
226    '<))
227
228 (nnoo-map-functions nndraft
229   (nnmh-retrieve-headers 0 nndraft-current-group 0 0)
230   (nnmh-request-group nndraft-current-group 0 0)
231   (nnmh-close-group nndraft-current-group 0)
232   (nnmh-request-list (nnoo-current-server 'nndraft) nndraft-directory)
233   (nnmh-request-newsgroups (nnoo-current-server 'nndraft) nndraft-directory))
234
235 (provide 'nndraft)
236
237 ;;; nndraft.el ends here