1 ;;; nndraft.el --- draft article access for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
33 (eval-when-compile (require 'cl))
38 (defvoo nndraft-directory (nnheader-concat gnus-directory "drafts/")
39 "Where nndraft will store its files."
40 nnmh-current-directory)
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)
48 (defconst nndraft-version "nndraft 1.0")
49 (defvoo nndraft-status-string "" nil nnmh-status-string)
53 ;;; Interface functions.
55 (nnoo-define-basics nndraft)
57 (deffoo nndraft-open-server (server &optional defs)
58 (push `(nndraft-current-group
59 ,(file-name-nondirectory (directory-file-name nndraft-directory)))
61 (push `(nndraft-top-directory
62 ,(file-name-directory (directory-file-name nndraft-directory)))
64 (nnoo-change-server 'nndraft server defs)
66 ((not (file-exists-p nndraft-directory))
67 (nndraft-close-server)
68 (nnheader-report 'nndraft "No such file or directory: %s"
70 ((not (file-directory-p (file-truename nndraft-directory)))
71 (nndraft-close-server)
72 (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
74 (nnheader-report 'nndraft "Opened server %s using directory %s"
75 server nndraft-directory)
78 (deffoo nndraft-retrieve-headers (articles &optional group server fetch-old)
80 (set-buffer nntp-server-buffer)
82 (let* ((buf (get-buffer-create " *draft headers*"))
85 (buffer-disable-undo (current-buffer))
87 ;; We don't support fetching by Message-ID.
88 (if (stringp (car articles))
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)
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)
105 (nnheader-fold-continuation-lines)
108 (deffoo nndraft-request-article (id &optional group server buffer)
110 ;; We get the newest file of the auto-saved file and the
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))
119 (set-buffer nntp-server-buffer)
120 (goto-char (point-min))
121 ;; If there's a mail header separator in this file,
123 (when (re-search-forward
124 (concat "^" mail-header-separator "$") nil t)
125 (replace-match "" t t)))
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))
135 (deffoo nndraft-request-update-info (group info &optional server)
138 (gnus-update-read-articles "nndraft:drafts" (nndraft-articles) t))
140 (when (setq marks (nth 3 info))
141 (setcar (nthcdr 3 info)
142 (if (assq 'unsend marks)
143 (list (assq 'unsend marks))
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))
152 (nnheader-temp-write nil
154 (setq article (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)
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)))
168 ;; Delete all the "state" files of articles that have been expired.
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)))))
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))))
182 (deffoo nndraft-request-create-group (group &optional server args)
183 (if (file-exists-p nndraft-directory)
184 (if (file-directory-p nndraft-directory)
189 (gnus-make-directory nndraft-directory)
194 ;;; Low-Level Interface
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))
204 (defun nndraft-article-filename (article &rest args)
206 (file-name-as-directory nndraft-directory)
207 (int-to-string article)
210 (defun nndraft-auto-save-file-name (file)
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)))))
219 (defun nndraft-articles ()
220 "Return the list of messages in the group."
221 (gnus-make-directory nndraft-directory)
223 (mapcar 'string-to-int
224 (directory-files nndraft-directory nil "\\`[0-9]+\\'" t))
227 (nnoo-map-functions nndraft
228 (nnmh-retrieve-headers 0 nndraft-current-group 0 0)
229 (nnmh-request-group nndraft-current-group 0 0)
230 (nnmh-close-group nndraft-current-group 0)
231 (nnmh-request-list (nnoo-current-server 'nndraft) nndraft-directory)
232 (nnmh-request-newsgroups (nnoo-current-server 'nndraft) nndraft-directory))
236 ;;; nndraft.el ends here