Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / gnus / nndraft.el
CommitLineData
eec82323 1;;; nndraft.el --- draft article access for Gnus
23f87bed 2
73b0cd50 3;; Copyright (C) 1995-2011 Free Software Foundation, Inc.
eec82323 4
6748645f 5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
eec82323
LMI
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
22
23;;; Commentary:
24
25;;; Code:
26
8db5f662
KY
27;; For Emacs <22.2 and XEmacs.
28(eval-and-compile
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30
eec82323 31(require 'nnheader)
6748645f
LMI
32(require 'nnmail)
33(require 'gnus-start)
8db5f662 34(require 'gnus-group)
eec82323
LMI
35(require 'nnmh)
36(require 'nnoo)
16409b0b 37(require 'mm-util)
23f87bed 38(eval-when-compile (require 'cl))
eec82323 39
8db5f662
KY
40(declare-function nndraft-request-list "nnmh" (&rest args))
41
6748645f
LMI
42(nnoo-declare nndraft
43 nnmh)
eec82323 44
6748645f
LMI
45(defvoo nndraft-directory (nnheader-concat gnus-directory "drafts/")
46 "Where nndraft will store its files."
47 nnmh-directory)
eec82323 48
01c52d31
MB
49(defvar nndraft-required-headers '(Date)
50 "*Headers to be generated when saving a draft message.
51The headers in this variable and the ones in `message-required-headers'
52are generated if and only if they are also in `message-draft-headers'.")
53
eec82323
LMI
54\f
55
6748645f
LMI
56(defvoo nndraft-current-group "" nil nnmh-current-group)
57(defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail)
58(defvoo nndraft-current-directory nil nil nnmh-current-directory)
59
eec82323 60(defconst nndraft-version "nndraft 1.0")
6748645f 61(defvoo nndraft-status-string "" nil nnmh-status-string)
eec82323
LMI
62
63\f
64
65;;; Interface functions.
66
67(nnoo-define-basics nndraft)
68
6748645f
LMI
69(deffoo nndraft-open-server (server &optional defs)
70 (nnoo-change-server 'nndraft server defs)
71 (cond
72 ((not (file-exists-p nndraft-directory))
73 (nndraft-close-server)
74 (nnheader-report 'nndraft "No such file or directory: %s"
75 nndraft-directory))
76 ((not (file-directory-p (file-truename nndraft-directory)))
77 (nndraft-close-server)
78 (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
79 (t
80 (nnheader-report 'nndraft "Opened server %s using directory %s"
81 server nndraft-directory)
82 t)))
83
eec82323 84(deffoo nndraft-retrieve-headers (articles &optional group server fetch-old)
6748645f 85 (nndraft-possibly-change-group group)
20a673b2 86 (with-current-buffer nntp-server-buffer
eec82323 87 (erase-buffer)
758845a0 88 (let (article lines chars)
eec82323
LMI
89 ;; We don't support fetching by Message-ID.
90 (if (stringp (car articles))
91 'headers
92 (while articles
16409b0b 93 (narrow-to-region (point) (point))
eec82323
LMI
94 (when (nndraft-request-article
95 (setq article (pop articles)) group server (current-buffer))
96 (goto-char (point-min))
97 (if (search-forward "\n\n" nil t)
98 (forward-line -1)
99 (goto-char (point-max)))
758845a0
LMI
100 (setq lines (count-lines (point) (point-max))
101 chars (- (point-max) (point)))
eec82323 102 (delete-region (point) (point-max))
16409b0b 103 (goto-char (point-min))
eec82323 104 (insert (format "221 %d Article retrieved.\n" article))
758845a0 105 (insert (format "Lines: %d\nChars: %d\n" lines chars))
16409b0b
GM
106 (widen)
107 (goto-char (point-max))
eec82323
LMI
108 (insert ".\n")))
109
110 (nnheader-fold-continuation-lines)
111 'headers))))
112
eec82323 113(deffoo nndraft-request-article (id &optional group server buffer)
6748645f 114 (nndraft-possibly-change-group group)
eec82323
LMI
115 (when (numberp id)
116 ;; We get the newest file of the auto-saved file and the
117 ;; "real" file.
118 (let* ((file (nndraft-article-filename id))
119 (auto (nndraft-auto-save-file-name file))
120 (newest (if (file-newer-than-file-p file auto) file auto))
121 (nntp-server-buffer (or buffer nntp-server-buffer)))
122 (when (and (file-exists-p newest)
16409b0b
GM
123 (let ((nnmail-file-coding-system
124 (if (file-newer-than-file-p file auto)
23f87bed 125 (if (member group '("drafts" "delayed"))
16409b0b
GM
126 message-draft-coding-system
127 mm-text-coding-system)
128 mm-auto-save-coding-system)))
129 (nnmail-find-file newest)))
20a673b2 130 (with-current-buffer nntp-server-buffer
eec82323
LMI
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
23f87bed 135 (concat "^" (regexp-quote mail-header-separator) "$") nil t)
eec82323
LMI
136 (replace-match "" t t)))
137 t))))
138
139(deffoo nndraft-request-restore-buffer (article &optional group server)
140 "Request a new buffer that is restored to the state of ARTICLE."
6748645f
LMI
141 (nndraft-possibly-change-group group)
142 (when (nndraft-request-article article group server (current-buffer))
143 (message-remove-header "xref")
144 (message-remove-header "lines")
23f87bed
MB
145 ;; Articles in nndraft:queue are considered as sent messages. The
146 ;; Date field should be the time when they are sent.
147 ;;(message-remove-header "date")
6748645f 148 t))
eec82323
LMI
149
150(deffoo nndraft-request-update-info (group info &optional server)
6748645f
LMI
151 (nndraft-possibly-change-group group)
152 (gnus-info-set-read
153 info
154 (gnus-update-read-articles (gnus-group-prefixed-name group '(nndraft ""))
155 (nndraft-articles) t))
16409b0b
GM
156 (let ((marks (nth 3 info)))
157 (when marks
158 ;; Nix out all marks except the `unsend'-able article marks.
6748645f
LMI
159 (setcar (nthcdr 3 info)
160 (if (assq 'unsend marks)
161 (list (assq 'unsend marks))
162 nil))))
eec82323
LMI
163 t)
164
23f87bed
MB
165(defun nndraft-generate-headers ()
166 (save-excursion
167 (message-generate-headers
168 (message-headers-to-generate
01c52d31 169 nndraft-required-headers message-draft-headers nil))))
23f87bed 170
30b0f250
KY
171(defun nndraft-update-unread-articles ()
172 "Update groups' unread articles in the group buffer."
173 (nndraft-request-list)
174 (with-current-buffer gnus-group-buffer
bb9e218d
KY
175 (let* ((groups (mapcar (lambda (elem)
176 (gnus-group-prefixed-name (car elem)
177 (list 'nndraft "")))
178 (nnmail-get-active)))
179 (gnus-group-marked (copy-sequence groups))
39397fad
KY
180 ;; Don't send delayed articles.
181 (gnus-get-new-news-hook nil)
bb9e218d
KY
182 (inhibit-read-only t))
183 (gnus-group-get-new-news-this-group nil t)
9937bef4
G
184 (save-excursion
185 (dolist (group groups)
186 (unless (and gnus-permanently-visible-groups
187 (string-match gnus-permanently-visible-groups
188 group))
189 (gnus-group-goto-group group)
190 (when (zerop (gnus-group-group-unread))
191 (gnus-delete-line))))))))
30b0f250 192
eec82323
LMI
193(deffoo nndraft-request-associate-buffer (group)
194 "Associate the current buffer with some article in the draft group."
6748645f
LMI
195 (nndraft-open-server "")
196 (nndraft-request-group group)
197 (nndraft-possibly-change-group group)
198 (let ((gnus-verbose-backends nil)
199 (buf (current-buffer))
16409b0b
GM
200 article file)
201 (with-temp-buffer
202 (insert-buffer-substring buf)
6748645f 203 (setq article (nndraft-request-accept-article
16409b0b
GM
204 group (nnoo-current-server 'nndraft) t 'noinsert)
205 file (nndraft-article-filename article)))
206 (setq buffer-file-name (expand-file-name file)
207 buffer-auto-save-file-name (make-auto-save-file-name))
eec82323 208 (clear-visited-file-modtime)
4a43ee9b
MB
209 (let ((hook (if (boundp 'write-contents-functions)
210 'write-contents-functions
211 'write-contents-hooks)))
212 (gnus-make-local-hook hook)
213 (add-hook hook 'nndraft-generate-headers nil t))
30b0f250
KY
214 (gnus-make-local-hook 'after-save-hook)
215 (add-hook 'after-save-hook 'nndraft-update-unread-articles nil t)
216 (message-add-action '(nndraft-update-unread-articles)
217 'exit 'postpone 'kill)
eec82323
LMI
218 article))
219
286c4fc2 220(deffoo nndraft-request-group (group &optional server dont-check info)
23f87bed
MB
221 (nndraft-possibly-change-group group)
222 (unless dont-check
223 (let* ((pathname (nnmail-group-pathname group nndraft-directory))
224 (file-name-coding-system nnmail-pathname-coding-system)
225 dir file)
226 (nnheader-re-read-dir pathname)
e9bd5782 227 (setq dir (mapcar (lambda (name) (string-to-number (substring name 1)))
23f87bed
MB
228 (ignore-errors (directory-files
229 pathname nil "^#[0-9]+#$" t))))
230 (dolist (n dir)
231 (unless (file-exists-p
232 (setq file (expand-file-name (int-to-string n) pathname)))
233 (rename-file (nndraft-auto-save-file-name file) file)))))
234 (nnoo-parent-function 'nndraft
235 'nnmh-request-group
236 (list group server dont-check)))
237
c9fc72fa 238(deffoo nndraft-request-move-article (article group server accept-form
01c52d31 239 &optional last move-is-internal)
23f87bed
MB
240 (nndraft-possibly-change-group group)
241 (let ((buf (get-buffer-create " *nndraft move*"))
242 result)
243 (and
244 (nndraft-request-article article group server)
20a673b2 245 (with-current-buffer buf
23f87bed
MB
246 (erase-buffer)
247 (insert-buffer-substring nntp-server-buffer)
248 (setq result (eval accept-form))
249 (kill-buffer (current-buffer))
250 result)
251 (null (nndraft-request-expire-articles (list article) group server 'force))
252 result)))
253
6748645f
LMI
254(deffoo nndraft-request-expire-articles (articles group &optional server force)
255 (nndraft-possibly-change-group group)
256 (let* ((nnmh-allow-delete-final t)
4478e074
G
257 (nnmail-expiry-target
258 (or (gnus-group-find-parameter
229b59da 259 (gnus-group-prefixed-name group (list 'nndraft server))
4478e074
G
260 'expiry-target t)
261 nnmail-expiry-target))
6748645f
LMI
262 (res (nnoo-parent-function 'nndraft
263 'nnmh-request-expire-articles
264 (list articles group server force)))
265 article)
eec82323
LMI
266 ;; Delete all the "state" files of articles that have been expired.
267 (while articles
268 (unless (memq (setq article (pop articles)) res)
6748645f 269 (let ((auto (nndraft-auto-save-file-name
eec82323 270 (nndraft-article-filename article))))
eec82323 271 (when (file-exists-p auto)
16409b0b
GM
272 (funcall nnmail-delete-file-function auto)))
273 (dolist (backup
274 (let ((kept-new-versions 1)
275 (kept-old-versions 0))
276 (find-backup-file-name
277 (nndraft-article-filename article))))
278 (when (file-exists-p backup)
279 (funcall nnmail-delete-file-function backup)))))
eec82323
LMI
280 res))
281
282(deffoo nndraft-request-accept-article (group &optional server last noinsert)
6748645f
LMI
283 (nndraft-possibly-change-group group)
284 (let ((gnus-verbose-backends nil))
285 (nnoo-parent-function 'nndraft 'nnmh-request-accept-article
286 (list group server last noinsert))))
eec82323 287
16409b0b
GM
288(deffoo nndraft-request-replace-article (article group buffer)
289 (nndraft-possibly-change-group group)
290 (let ((nnmail-file-coding-system
23f87bed
MB
291 (if (member group '("drafts" "delayed"))
292 message-draft-coding-system
16409b0b
GM
293 mm-text-coding-system)))
294 (nnoo-parent-function 'nndraft 'nnmh-request-replace-article
295 (list article group buffer))))
296
eec82323 297(deffoo nndraft-request-create-group (group &optional server args)
6748645f
LMI
298 (nndraft-possibly-change-group group)
299 (if (file-exists-p nndraft-current-directory)
300 (if (file-directory-p nndraft-current-directory)
eec82323
LMI
301 t
302 nil)
303 (condition-case ()
304 (progn
6748645f 305 (gnus-make-directory nndraft-current-directory)
eec82323
LMI
306 t)
307 (file-error nil))))
308
309\f
310;;; Low-Level Interface
311
6748645f
LMI
312(defun nndraft-possibly-change-group (group)
313 (when (and group
314 (not (equal group nndraft-current-group)))
315 (nndraft-open-server "")
316 (setq nndraft-current-group group)
317 (setq nndraft-current-directory
318 (nnheader-concat nndraft-directory group))))
eec82323
LMI
319
320(defun nndraft-article-filename (article &rest args)
321 (apply 'concat
6748645f 322 (file-name-as-directory nndraft-current-directory)
eec82323
LMI
323 (int-to-string article)
324 args))
325
326(defun nndraft-auto-save-file-name (file)
327 (save-excursion
328 (prog1
329 (progn
330 (set-buffer (get-buffer-create " *draft tmp*"))
331 (setq buffer-file-name file)
332 (make-auto-save-file-name))
333 (kill-buffer (current-buffer)))))
334
6748645f
LMI
335(defun nndraft-articles ()
336 "Return the list of messages in the group."
337 (gnus-make-directory nndraft-current-directory)
338 (sort
e9bd5782 339 (mapcar 'string-to-number
6748645f
LMI
340 (directory-files nndraft-current-directory nil "\\`[0-9]+\\'" t))
341 '<))
342
343(nnoo-import nndraft
344 (nnmh
345 nnmh-retrieve-headers
346 nnmh-request-group
347 nnmh-close-group
16409b0b 348 nnmh-request-list
23f87bed 349 nnmh-request-newsgroups))
6748645f 350
eec82323
LMI
351(provide 'nndraft)
352
353;;; nndraft.el ends here