* gnus-art.el (gnus-treat-hide-citation-maybe): Add more doc to the string.
[gnus] / lisp / nndraft.el
1 ;;; nndraft.el --- draft article access for Gnus
2
3 ;; Copyright (C) 1995-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30
31 (require 'nnheader)
32 (require 'nnmail)
33 (require 'gnus-start)
34 (require 'gnus-group)
35 (require 'nnmh)
36 (require 'nnoo)
37 (require 'mm-util)
38 (eval-when-compile (require 'cl))
39
40 (declare-function nndraft-request-list "nnmh" (&rest args))
41
42 (nnoo-declare nndraft
43   nnmh)
44
45 (defvoo nndraft-directory (nnheader-concat gnus-directory "drafts/")
46   "Where nndraft will store its files."
47   nnmh-directory)
48
49 (defvar nndraft-required-headers '(Date)
50   "*Headers to be generated when saving a draft message.
51 The headers in this variable and the ones in `message-required-headers'
52 are generated if and only if they are also in `message-draft-headers'.")
53
54 \f
55
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
60 (defconst nndraft-version "nndraft 1.0")
61 (defvoo nndraft-status-string "" nil nnmh-status-string)
62
63 \f
64
65 ;;; Interface functions.
66
67 (nnoo-define-basics nndraft)
68
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
84 (deffoo nndraft-retrieve-headers (articles &optional group server fetch-old)
85   (nndraft-possibly-change-group group)
86   (with-current-buffer nntp-server-buffer
87     (erase-buffer)
88     (let (article lines chars)
89       ;; We don't support fetching by Message-ID.
90       (if (stringp (car articles))
91           'headers
92         (while articles
93           (narrow-to-region (point) (point))
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)))
100             (setq lines (count-lines (point) (point-max))
101                   chars (- (point-max) (point)))
102             (delete-region (point) (point-max))
103             (goto-char (point-min))
104             (insert (format "221 %d Article retrieved.\n" article))
105             (insert (format "Lines: %d\nChars: %d\n" lines chars))
106             (widen)
107             (goto-char (point-max))
108             (insert ".\n")))
109
110         (nnheader-fold-continuation-lines)
111         'headers))))
112
113 (deffoo nndraft-request-article (id &optional group server buffer)
114   (nndraft-possibly-change-group group)
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)
123                  (let ((nnmail-file-coding-system
124                         (if (file-newer-than-file-p file auto)
125                             (if (member group '("drafts" "delayed"))
126                                 message-draft-coding-system
127                               mm-text-coding-system)
128                           mm-auto-save-coding-system)))
129                    (nnmail-find-file newest)))
130         (with-current-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 "^" (regexp-quote mail-header-separator) "$") nil t)
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."
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")
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")
148     t))
149
150 (deffoo nndraft-request-update-info (group info &optional server)
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))
156   (let ((marks (nth 3 info)))
157     (when marks
158       ;; Nix out all marks except the `unsend'-able article marks.
159       (setcar (nthcdr 3 info)
160               (if (assq 'unsend marks)
161                   (list (assq 'unsend marks))
162                 nil))))
163   t)
164
165 (defun nndraft-generate-headers ()
166   (save-excursion
167     (message-generate-headers
168      (message-headers-to-generate
169       nndraft-required-headers message-draft-headers nil))))
170
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
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))
180            ;; Don't send delayed articles.
181            (gnus-get-new-news-hook nil)
182            (inhibit-read-only t))
183       (gnus-group-get-new-news-this-group nil t)
184       (dolist (group groups)
185         (unless (and gnus-permanently-visible-groups
186                      (string-match gnus-permanently-visible-groups
187                                    group))
188           (gnus-group-goto-group group)
189           (when (zerop (gnus-group-group-unread))
190             (gnus-delete-line)))))))
191
192 (deffoo nndraft-request-associate-buffer (group)
193   "Associate the current buffer with some article in the draft group."
194   (nndraft-open-server "")
195   (nndraft-request-group group)
196   (nndraft-possibly-change-group group)
197   (let ((gnus-verbose-backends nil)
198         (buf (current-buffer))
199         article file)
200     (with-temp-buffer
201       (insert-buffer-substring buf)
202       (setq article (nndraft-request-accept-article
203                      group (nnoo-current-server 'nndraft) t 'noinsert)
204             file (nndraft-article-filename article)))
205     (setq buffer-file-name (expand-file-name file)
206           buffer-auto-save-file-name (make-auto-save-file-name))
207     (clear-visited-file-modtime)
208     (let ((hook (if (boundp 'write-contents-functions)
209                     'write-contents-functions
210                   'write-contents-hooks)))
211       (gnus-make-local-hook hook)
212       (add-hook hook 'nndraft-generate-headers nil t))
213     (gnus-make-local-hook 'after-save-hook)
214     (add-hook 'after-save-hook 'nndraft-update-unread-articles nil t)
215     (message-add-action '(nndraft-update-unread-articles)
216                         'exit 'postpone 'kill)
217     article))
218
219 (deffoo nndraft-request-group (group &optional server dont-check info)
220   (nndraft-possibly-change-group group)
221   (unless dont-check
222     (let* ((pathname (nnmail-group-pathname group nndraft-directory))
223            (file-name-coding-system nnmail-pathname-coding-system)
224            dir file)
225       (nnheader-re-read-dir pathname)
226       (setq dir (mapcar (lambda (name) (string-to-number (substring name 1)))
227                         (ignore-errors (directory-files
228                                         pathname nil "^#[0-9]+#$" t))))
229       (dolist (n dir)
230         (unless (file-exists-p
231                  (setq file (expand-file-name (int-to-string n) pathname)))
232           (rename-file (nndraft-auto-save-file-name file) file)))))
233   (nnoo-parent-function 'nndraft
234                         'nnmh-request-group
235                         (list group server dont-check)))
236
237 (deffoo nndraft-request-move-article (article group server accept-form
238                                       &optional last move-is-internal)
239   (nndraft-possibly-change-group group)
240   (let ((buf (get-buffer-create " *nndraft move*"))
241         result)
242     (and
243      (nndraft-request-article article group server)
244      (with-current-buffer buf
245        (erase-buffer)
246        (insert-buffer-substring nntp-server-buffer)
247        (setq result (eval accept-form))
248        (kill-buffer (current-buffer))
249        result)
250      (null (nndraft-request-expire-articles (list article) group server 'force))
251      result)))
252
253 (deffoo nndraft-request-expire-articles (articles group &optional server force)
254   (nndraft-possibly-change-group group)
255   (let* ((nnmh-allow-delete-final t)
256          (nnmail-expiry-target
257           (or (gnus-group-find-parameter
258                (gnus-group-prefixed-name group (list 'nndraft server))
259                'expiry-target t)
260               nnmail-expiry-target))
261          (res (nnoo-parent-function 'nndraft
262                                     'nnmh-request-expire-articles
263                                     (list articles group server force)))
264          article)
265     ;; Delete all the "state" files of articles that have been expired.
266     (while articles
267       (unless (memq (setq article (pop articles)) res)
268         (let ((auto (nndraft-auto-save-file-name
269                      (nndraft-article-filename article))))
270           (when (file-exists-p auto)
271             (funcall nnmail-delete-file-function auto)))
272         (dolist (backup
273                  (let ((kept-new-versions 1)
274                        (kept-old-versions 0))
275                    (find-backup-file-name
276                     (nndraft-article-filename article))))
277           (when (file-exists-p backup)
278             (funcall nnmail-delete-file-function backup)))))
279     res))
280
281 (deffoo nndraft-request-accept-article (group &optional server last noinsert)
282   (nndraft-possibly-change-group group)
283   (let ((gnus-verbose-backends nil))
284     (nnoo-parent-function 'nndraft 'nnmh-request-accept-article
285                           (list group server last noinsert))))
286
287 (deffoo nndraft-request-replace-article (article group buffer)
288   (nndraft-possibly-change-group group)
289   (let ((nnmail-file-coding-system
290          (if (member group '("drafts" "delayed"))
291              message-draft-coding-system
292            mm-text-coding-system)))
293     (nnoo-parent-function 'nndraft 'nnmh-request-replace-article
294                           (list article group buffer))))
295
296 (deffoo nndraft-request-create-group (group &optional server args)
297   (nndraft-possibly-change-group group)
298   (if (file-exists-p nndraft-current-directory)
299       (if (file-directory-p nndraft-current-directory)
300           t
301         nil)
302     (condition-case ()
303         (progn
304           (gnus-make-directory nndraft-current-directory)
305           t)
306       (file-error nil))))
307
308 \f
309 ;;; Low-Level Interface
310
311 (defun nndraft-possibly-change-group (group)
312   (when (and group
313              (not (equal group nndraft-current-group)))
314     (nndraft-open-server "")
315     (setq nndraft-current-group group)
316     (setq nndraft-current-directory
317           (nnheader-concat nndraft-directory group))))
318
319 (defun nndraft-article-filename (article &rest args)
320   (apply 'concat
321          (file-name-as-directory nndraft-current-directory)
322          (int-to-string article)
323          args))
324
325 (defun nndraft-auto-save-file-name (file)
326   (save-excursion
327     (prog1
328         (progn
329           (set-buffer (get-buffer-create " *draft tmp*"))
330           (setq buffer-file-name file)
331           (make-auto-save-file-name))
332       (kill-buffer (current-buffer)))))
333
334 (defun nndraft-articles ()
335   "Return the list of messages in the group."
336   (gnus-make-directory nndraft-current-directory)
337   (sort
338    (mapcar 'string-to-number
339            (directory-files nndraft-current-directory nil "\\`[0-9]+\\'" t))
340    '<))
341
342 (nnoo-import nndraft
343   (nnmh
344    nnmh-retrieve-headers
345    nnmh-request-group
346    nnmh-close-group
347    nnmh-request-list
348    nnmh-request-newsgroups))
349
350 (provide 'nndraft)
351
352 ;;; nndraft.el ends here