Insert Lines and Chars headers for prettier summary display.
[gnus] / lisp / nndraft.el
1 ;;; nndraft.el --- draft article access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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 of the License, or
14 ;; (at your option) any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'nnheader)
29 (require 'nnmail)
30 (require 'gnus-start)
31 (require 'nnmh)
32 (require 'nnoo)
33 (require 'mm-util)
34 (eval-when-compile (require 'cl))
35
36 (nnoo-declare nndraft
37   nnmh)
38
39 (defvoo nndraft-directory (nnheader-concat gnus-directory "drafts/")
40   "Where nndraft will store its files."
41   nnmh-directory)
42
43 (defvar nndraft-required-headers '(Date)
44   "*Headers to be generated when saving a draft message.
45 The headers in this variable and the ones in `message-required-headers'
46 are generated if and only if they are also in `message-draft-headers'.")
47
48 \f
49
50 (defvoo nndraft-current-group "" nil nnmh-current-group)
51 (defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail)
52 (defvoo nndraft-current-directory nil nil nnmh-current-directory)
53
54 (defconst nndraft-version "nndraft 1.0")
55 (defvoo nndraft-status-string "" nil nnmh-status-string)
56
57 \f
58
59 ;;; Interface functions.
60
61 (nnoo-define-basics nndraft)
62
63 (deffoo nndraft-open-server (server &optional 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   (nndraft-possibly-change-group group)
80   (with-current-buffer nntp-server-buffer
81     (erase-buffer)
82     (let (article lines chars)
83       ;; We don't support fetching by Message-ID.
84       (if (stringp (car articles))
85           'headers
86         (while articles
87           (narrow-to-region (point) (point))
88           (when (nndraft-request-article
89                  (setq article (pop articles)) group server (current-buffer))
90             (goto-char (point-min))
91             (if (search-forward "\n\n" nil t)
92                 (forward-line -1)
93               (goto-char (point-max)))
94             (setq lines (count-lines (point) (point-max))
95                   chars (- (point-max) (point)))
96             (delete-region (point) (point-max))
97             (goto-char (point-min))
98             (insert (format "221 %d Article retrieved.\n" article))
99             (insert (format "Lines: %d\nChars: %d\n" lines chars))
100             (widen)
101             (goto-char (point-max))
102             (insert ".\n")))
103
104         (nnheader-fold-continuation-lines)
105         'headers))))
106
107 (deffoo nndraft-request-article (id &optional group server buffer)
108   (nndraft-possibly-change-group group)
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                  (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)))
124         (with-current-buffer nntp-server-buffer
125           (goto-char (point-min))
126           ;; If there's a mail header separator in this file,
127           ;; we remove it.
128           (when (re-search-forward
129                  (concat "^" (regexp-quote mail-header-separator) "$") nil t)
130             (replace-match "" t t)))
131         t))))
132
133 (deffoo nndraft-request-restore-buffer (article &optional group server)
134   "Request a new buffer that is restored to the state of ARTICLE."
135   (nndraft-possibly-change-group group)
136   (when (nndraft-request-article article group server (current-buffer))
137     (message-remove-header "xref")
138     (message-remove-header "lines")
139     ;; Articles in nndraft:queue are considered as sent messages.  The
140     ;; Date field should be the time when they are sent.
141     ;;(message-remove-header "date")
142     t))
143
144 (deffoo nndraft-request-update-info (group info &optional server)
145   (nndraft-possibly-change-group group)
146   (gnus-info-set-read
147    info
148    (gnus-update-read-articles (gnus-group-prefixed-name group '(nndraft ""))
149                               (nndraft-articles) t))
150   (let ((marks (nth 3 info)))
151     (when marks
152       ;; Nix out all marks except the `unsend'-able article marks.
153       (setcar (nthcdr 3 info)
154               (if (assq 'unsend marks)
155                   (list (assq 'unsend marks))
156                 nil))))
157   t)
158
159 (defun nndraft-generate-headers ()
160   (save-excursion
161     (message-generate-headers
162      (message-headers-to-generate
163       nndraft-required-headers message-draft-headers nil))))
164
165 (deffoo nndraft-request-associate-buffer (group)
166   "Associate the current buffer with some article in the draft group."
167   (nndraft-open-server "")
168   (nndraft-request-group group)
169   (nndraft-possibly-change-group group)
170   (let ((gnus-verbose-backends nil)
171         (buf (current-buffer))
172         article file)
173     (with-temp-buffer
174       (insert-buffer-substring buf)
175       (setq article (nndraft-request-accept-article
176                      group (nnoo-current-server 'nndraft) t 'noinsert)
177             file (nndraft-article-filename article)))
178     (setq buffer-file-name (expand-file-name file)
179           buffer-auto-save-file-name (make-auto-save-file-name))
180     (clear-visited-file-modtime)
181     (let ((hook (if (boundp 'write-contents-functions)
182                     'write-contents-functions
183                   'write-contents-hooks)))
184       (gnus-make-local-hook hook)
185       (add-hook hook 'nndraft-generate-headers nil t))
186     article))
187
188 (deffoo nndraft-request-group (group &optional server dont-check info)
189   (nndraft-possibly-change-group group)
190   (unless dont-check
191     (let* ((pathname (nnmail-group-pathname group nndraft-directory))
192            (file-name-coding-system nnmail-pathname-coding-system)
193            dir file)
194       (nnheader-re-read-dir pathname)
195       (setq dir (mapcar (lambda (name) (string-to-number (substring name 1)))
196                         (ignore-errors (directory-files
197                                         pathname nil "^#[0-9]+#$" t))))
198       (dolist (n dir)
199         (unless (file-exists-p
200                  (setq file (expand-file-name (int-to-string n) pathname)))
201           (rename-file (nndraft-auto-save-file-name file) file)))))
202   (nnoo-parent-function 'nndraft
203                         'nnmh-request-group
204                         (list group server dont-check)))
205
206 (deffoo nndraft-request-move-article (article group server accept-form
207                                       &optional last move-is-internal)
208   (nndraft-possibly-change-group group)
209   (let ((buf (get-buffer-create " *nndraft move*"))
210         result)
211     (and
212      (nndraft-request-article article group server)
213      (with-current-buffer buf
214        (erase-buffer)
215        (insert-buffer-substring nntp-server-buffer)
216        (setq result (eval accept-form))
217        (kill-buffer (current-buffer))
218        result)
219      (null (nndraft-request-expire-articles (list article) group server 'force))
220      result)))
221
222 (deffoo nndraft-request-expire-articles (articles group &optional server force)
223   (nndraft-possibly-change-group group)
224   (let* ((nnmh-allow-delete-final t)
225          (res (nnoo-parent-function 'nndraft
226                                     'nnmh-request-expire-articles
227                                     (list articles group server force)))
228          article)
229     ;; Delete all the "state" files of articles that have been expired.
230     (while articles
231       (unless (memq (setq article (pop articles)) res)
232         (let ((auto (nndraft-auto-save-file-name
233                      (nndraft-article-filename article))))
234           (when (file-exists-p auto)
235             (funcall nnmail-delete-file-function auto)))
236         (dolist (backup
237                  (let ((kept-new-versions 1)
238                        (kept-old-versions 0))
239                    (find-backup-file-name
240                     (nndraft-article-filename article))))
241           (when (file-exists-p backup)
242             (funcall nnmail-delete-file-function backup)))))
243     res))
244
245 (deffoo nndraft-request-accept-article (group &optional server last noinsert)
246   (nndraft-possibly-change-group group)
247   (let ((gnus-verbose-backends nil))
248     (nnoo-parent-function 'nndraft 'nnmh-request-accept-article
249                           (list group server last noinsert))))
250
251 (deffoo nndraft-request-replace-article (article group buffer)
252   (nndraft-possibly-change-group group)
253   (let ((nnmail-file-coding-system
254          (if (member group '("drafts" "delayed"))
255              message-draft-coding-system
256            mm-text-coding-system)))
257     (nnoo-parent-function 'nndraft 'nnmh-request-replace-article
258                           (list article group buffer))))
259
260 (deffoo nndraft-request-create-group (group &optional server args)
261   (nndraft-possibly-change-group group)
262   (if (file-exists-p nndraft-current-directory)
263       (if (file-directory-p nndraft-current-directory)
264           t
265         nil)
266     (condition-case ()
267         (progn
268           (gnus-make-directory nndraft-current-directory)
269           t)
270       (file-error nil))))
271
272 \f
273 ;;; Low-Level Interface
274
275 (defun nndraft-possibly-change-group (group)
276   (when (and group
277              (not (equal group nndraft-current-group)))
278     (nndraft-open-server "")
279     (setq nndraft-current-group group)
280     (setq nndraft-current-directory
281           (nnheader-concat nndraft-directory group))))
282
283 (defun nndraft-article-filename (article &rest args)
284   (apply 'concat
285          (file-name-as-directory nndraft-current-directory)
286          (int-to-string article)
287          args))
288
289 (defun nndraft-auto-save-file-name (file)
290   (save-excursion
291     (prog1
292         (progn
293           (set-buffer (get-buffer-create " *draft tmp*"))
294           (setq buffer-file-name file)
295           (make-auto-save-file-name))
296       (kill-buffer (current-buffer)))))
297
298 (defun nndraft-articles ()
299   "Return the list of messages in the group."
300   (gnus-make-directory nndraft-current-directory)
301   (sort
302    (mapcar 'string-to-number
303            (directory-files nndraft-current-directory nil "\\`[0-9]+\\'" t))
304    '<))
305
306 (nnoo-import nndraft
307   (nnmh
308    nnmh-retrieve-headers
309    nnmh-request-group
310    nnmh-close-group
311    nnmh-request-list
312    nnmh-request-newsgroups))
313
314 (provide 'nndraft)
315
316 ;;; nndraft.el ends here