1 ;;; nndraft.el --- draft article access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
36 (eval-when-compile (require 'cl
))
41 (defvoo nndraft-directory
(nnheader-concat gnus-directory
"drafts/")
42 "Where nndraft will store its files."
45 (defvar nndraft-required-headers
'(Date)
46 "*Headers to be generated when saving a draft message.
47 The headers in this variable and the ones in `message-required-headers'
48 are generated if and only if they are also in `message-draft-headers'.")
52 (defvoo nndraft-current-group
"" nil nnmh-current-group
)
53 (defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail
)
54 (defvoo nndraft-current-directory nil nil nnmh-current-directory
)
56 (defconst nndraft-version
"nndraft 1.0")
57 (defvoo nndraft-status-string
"" nil nnmh-status-string
)
61 ;;; Interface functions.
63 (nnoo-define-basics nndraft
)
65 (deffoo nndraft-open-server
(server &optional defs
)
66 (nnoo-change-server 'nndraft server defs
)
68 ((not (file-exists-p nndraft-directory
))
69 (nndraft-close-server)
70 (nnheader-report 'nndraft
"No such file or directory: %s"
72 ((not (file-directory-p (file-truename nndraft-directory
)))
73 (nndraft-close-server)
74 (nnheader-report 'nndraft
"Not a directory: %s" nndraft-directory
))
76 (nnheader-report 'nndraft
"Opened server %s using directory %s"
77 server nndraft-directory
)
80 (deffoo nndraft-retrieve-headers
(articles &optional group server fetch-old
)
81 (nndraft-possibly-change-group group
)
83 (set-buffer nntp-server-buffer
)
86 ;; We don't support fetching by Message-ID.
87 (if (stringp (car articles
))
90 (narrow-to-region (point) (point))
91 (when (nndraft-request-article
92 (setq article
(pop articles
)) group server
(current-buffer))
93 (goto-char (point-min))
94 (if (search-forward "\n\n" nil t
)
96 (goto-char (point-max)))
97 (delete-region (point) (point-max))
98 (goto-char (point-min))
99 (insert (format "221 %d Article retrieved.\n" article
))
101 (goto-char (point-max))
104 (nnheader-fold-continuation-lines)
107 (deffoo nndraft-request-article
(id &optional group server buffer
)
108 (nndraft-possibly-change-group group
)
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 (let ((nnmail-file-coding-system
118 (if (file-newer-than-file-p file auto
)
119 (if (member group
'("drafts" "delayed"))
120 message-draft-coding-system
121 mm-text-coding-system
)
122 mm-auto-save-coding-system
)))
123 (nnmail-find-file newest
)))
125 (set-buffer nntp-server-buffer
)
126 (goto-char (point-min))
127 ;; If there's a mail header separator in this file,
129 (when (re-search-forward
130 (concat "^" (regexp-quote mail-header-separator
) "$") nil t
)
131 (replace-match "" t t
)))
134 (deffoo nndraft-request-restore-buffer
(article &optional group server
)
135 "Request a new buffer that is restored to the state of ARTICLE."
136 (nndraft-possibly-change-group group
)
137 (when (nndraft-request-article article group server
(current-buffer))
138 (message-remove-header "xref")
139 (message-remove-header "lines")
140 ;; Articles in nndraft:queue are considered as sent messages. The
141 ;; Date field should be the time when they are sent.
142 ;;(message-remove-header "date")
145 (deffoo nndraft-request-update-info
(group info
&optional server
)
146 (nndraft-possibly-change-group group
)
149 (gnus-update-read-articles (gnus-group-prefixed-name group
'(nndraft ""))
150 (nndraft-articles) t
))
151 (let ((marks (nth 3 info
)))
153 ;; Nix out all marks except the `unsend'-able article marks.
154 (setcar (nthcdr 3 info
)
155 (if (assq 'unsend marks
)
156 (list (assq 'unsend marks
))
160 (defun nndraft-generate-headers ()
162 (message-generate-headers
163 (message-headers-to-generate
164 nndraft-required-headers message-draft-headers nil
))))
166 (deffoo nndraft-request-associate-buffer
(group)
167 "Associate the current buffer with some article in the draft group."
168 (nndraft-open-server "")
169 (nndraft-request-group group
)
170 (nndraft-possibly-change-group group
)
171 (let ((gnus-verbose-backends nil
)
172 (buf (current-buffer))
175 (insert-buffer-substring buf
)
176 (setq article
(nndraft-request-accept-article
177 group
(nnoo-current-server 'nndraft
) t
'noinsert
)
178 file
(nndraft-article-filename article
)))
179 (setq buffer-file-name
(expand-file-name file
)
180 buffer-auto-save-file-name
(make-auto-save-file-name))
181 (clear-visited-file-modtime)
182 (let ((hook (if (boundp 'write-contents-functions
)
183 'write-contents-functions
184 'write-contents-hooks
)))
185 (gnus-make-local-hook hook
)
186 (add-hook hook
'nndraft-generate-headers nil t
))
189 (deffoo nndraft-request-group
(group &optional server dont-check
)
190 (nndraft-possibly-change-group group
)
192 (let* ((pathname (nnmail-group-pathname group nndraft-directory
))
193 (file-name-coding-system nnmail-pathname-coding-system
)
195 (nnheader-re-read-dir pathname
)
196 (setq dir
(mapcar (lambda (name) (string-to-number (substring name
1)))
197 (ignore-errors (directory-files
198 pathname nil
"^#[0-9]+#$" t
))))
200 (unless (file-exists-p
201 (setq file
(expand-file-name (int-to-string n
) pathname
)))
202 (rename-file (nndraft-auto-save-file-name file
) file
)))))
203 (nnoo-parent-function 'nndraft
205 (list group server dont-check
)))
207 (deffoo nndraft-request-move-article
(article group server accept-form
208 &optional last move-is-internal
)
209 (nndraft-possibly-change-group group
)
210 (let ((buf (get-buffer-create " *nndraft move*"))
213 (nndraft-request-article article group server
)
217 (insert-buffer-substring nntp-server-buffer
)
218 (setq result
(eval accept-form
))
219 (kill-buffer (current-buffer))
221 (null (nndraft-request-expire-articles (list article
) group server
'force
))
224 (deffoo nndraft-request-expire-articles
(articles group
&optional server force
)
225 (nndraft-possibly-change-group group
)
226 (let* ((nnmh-allow-delete-final t
)
227 (res (nnoo-parent-function 'nndraft
228 'nnmh-request-expire-articles
229 (list articles group server force
)))
231 ;; Delete all the "state" files of articles that have been expired.
233 (unless (memq (setq article
(pop articles
)) res
)
234 (let ((auto (nndraft-auto-save-file-name
235 (nndraft-article-filename article
))))
236 (when (file-exists-p auto
)
237 (funcall nnmail-delete-file-function auto
)))
239 (let ((kept-new-versions 1)
240 (kept-old-versions 0))
241 (find-backup-file-name
242 (nndraft-article-filename article
))))
243 (when (file-exists-p backup
)
244 (funcall nnmail-delete-file-function backup
)))))
247 (deffoo nndraft-request-accept-article
(group &optional server last noinsert
)
248 (nndraft-possibly-change-group group
)
249 (let ((gnus-verbose-backends nil
))
250 (nnoo-parent-function 'nndraft
'nnmh-request-accept-article
251 (list group server last noinsert
))))
253 (deffoo nndraft-request-replace-article
(article group buffer
)
254 (nndraft-possibly-change-group group
)
255 (let ((nnmail-file-coding-system
256 (if (member group
'("drafts" "delayed"))
257 message-draft-coding-system
258 mm-text-coding-system
)))
259 (nnoo-parent-function 'nndraft
'nnmh-request-replace-article
260 (list article group buffer
))))
262 (deffoo nndraft-request-create-group
(group &optional server args
)
263 (nndraft-possibly-change-group group
)
264 (if (file-exists-p nndraft-current-directory
)
265 (if (file-directory-p nndraft-current-directory
)
270 (gnus-make-directory nndraft-current-directory
)
275 ;;; Low-Level Interface
277 (defun nndraft-possibly-change-group (group)
279 (not (equal group nndraft-current-group
)))
280 (nndraft-open-server "")
281 (setq nndraft-current-group group
)
282 (setq nndraft-current-directory
283 (nnheader-concat nndraft-directory group
))))
285 (defun nndraft-article-filename (article &rest args
)
287 (file-name-as-directory nndraft-current-directory
)
288 (int-to-string article
)
291 (defun nndraft-auto-save-file-name (file)
295 (set-buffer (get-buffer-create " *draft tmp*"))
296 (setq buffer-file-name file
)
297 (make-auto-save-file-name))
298 (kill-buffer (current-buffer)))))
300 (defun nndraft-articles ()
301 "Return the list of messages in the group."
302 (gnus-make-directory nndraft-current-directory
)
304 (mapcar 'string-to-number
305 (directory-files nndraft-current-directory nil
"\\`[0-9]+\\'" t
))
310 nnmh-retrieve-headers
314 nnmh-request-newsgroups
))
318 ;; arch-tag: 3ce26ca0-41cb-48b1-8703-4dad35e188aa
319 ;;; nndraft.el ends here