]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
Modify our local copy of message-do-fcc
[notmuch] / emacs / notmuch-maildir-fcc.el
1 ;;; notmuch-maildir-fcc.el ---
2
3 ;; This file is free software; you can redistribute it and/or modify
4 ;; it under the terms of the GNU General Public License as published
5 ;; by the Free Software Foundation; either version 2, or (at your
6 ;; option) any later version.
7
8 ;; This program is distributed in the hope that it will be useful,
9 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ;; GNU General Public License for more details.
12
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
15 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 ;; Boston, MA 02110-1301, USA.
17
18 ;;; Commentary:
19
20 ;; To use this as the fcc handler for message-mode,
21 ;; customize the notmuch-fcc-dirs variable
22
23 ;;; Code:
24
25 (eval-when-compile (require 'cl))
26 (require 'message)
27
28 (require 'notmuch-lib)
29
30 (defvar notmuch-maildir-fcc-count 0)
31
32 (defcustom notmuch-fcc-dirs "sent"
33  "Determines the maildir directory in which to save outgoing mail.
34
35 Three types of values are permitted:
36
37 - nil: no Fcc header is added,
38
39 - a string: the value of `notmuch-fcc-dirs' is the name of the
40   folder to use,
41
42 - a list: the folder is chosen based on the From address of the
43   current message using a list of regular expressions and
44   corresponding folders:
45
46      ((\"Sebastian@SSpaeth.de\" . \"privat\")
47       (\"spaetz@sspaeth.de\" . \"OUTBOX.OSS\")
48       (\".*\" . \"defaultinbox\"))
49
50   If none of the regular expressions match the From address, no
51   Fcc header will be added.
52
53 In all cases, a relative FCC directory will be understood to
54 specify a directory within the notmuch mail store, (as set by
55 the database.path option in the notmuch configuration file).
56
57 You will be prompted to create the directory if it does not exist
58 yet when sending a mail."
59
60  :type '(choice
61          (const :tag "No FCC header" nil)
62          (string :tag "A single folder")
63          (repeat :tag "A folder based on the From header"
64                  (cons regexp (string :tag "Folder"))))
65  :require 'notmuch-fcc-initialization
66  :group 'notmuch-send)
67
68
69 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 ;; Functions which set up the fcc header in the message buffer.
71
72 (defun notmuch-fcc-header-setup ()
73   "Add an Fcc header to the current message buffer.
74
75 Sets the Fcc header based on the values of `notmuch-fcc-dirs'.
76
77 Originally intended to be use a hook function, but now called directly
78 by notmuch-mua-mail"
79
80   (let ((subdir
81          (cond
82           ((or (not notmuch-fcc-dirs)
83                (message-field-value "Fcc"))
84            ;; Nothing set or an existing header.
85            nil)
86
87           ((stringp notmuch-fcc-dirs)
88            notmuch-fcc-dirs)
89
90           ((and (listp notmuch-fcc-dirs)
91                 (stringp (car notmuch-fcc-dirs)))
92            ;; Old style - no longer works.
93            (error "Invalid `notmuch-fcc-dirs' setting (old style)"))
94
95           ((listp notmuch-fcc-dirs)
96            (let* ((from (message-field-value "From"))
97                   (match
98                    (catch 'first-match
99                      (dolist (re-folder notmuch-fcc-dirs)
100                        (when (string-match-p (car re-folder) from)
101                          (throw 'first-match re-folder))))))
102              (if match
103                  (cdr match)
104                (message "No Fcc header added.")
105                nil)))
106
107           (t
108            (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
109
110     (when subdir
111       (notmuch-maildir-add-file-style-fcc-header subdir))))
112
113 (defun notmuch-maildir-add-file-style-fcc-header (subdir)
114   (message-add-header
115    (concat "Fcc: "
116            (file-truename
117             ;; If the resulting directory is not an absolute path,
118             ;; prepend the standard notmuch database path.
119             (if (= (elt subdir 0) ?/)
120                 subdir
121               (concat (notmuch-database-path) "/" subdir))))))
122
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;; Functions for saving a message either using notmuch insert or file
125 ;; fcc. First functions common to the two cases.
126
127 (defmacro with-temporary-notmuch-message-buffer (&rest body)
128   "Set-up a temporary copy of the current message-mode buffer."
129   `(let ((case-fold-search t)
130          (buf (current-buffer))
131          (mml-externalize-attachments message-fcc-externalize-attachments))
132      (with-current-buffer (get-buffer-create " *message temp*")
133        (erase-buffer)
134        (insert-buffer-substring buf)
135        ,@body)))
136
137 (defun notmuch-maildir-setup-message-for-saving ()
138   "Setup message for saving. Should be called on a temporary copy.
139
140 This is taken from the function message-do-fcc."
141   (message-encode-message-body)
142   (save-restriction
143     (message-narrow-to-headers)
144     (let ((mail-parse-charset message-default-charset))
145       (mail-encode-encoded-word-buffer)))
146   (goto-char (point-min))
147   (when (re-search-forward
148          (concat "^" (regexp-quote mail-header-separator) "$")
149          nil t)
150     (replace-match "" t t )))
151
152 (defun notmuch-maildir-message-do-fcc ()
153   "Process Fcc headers in the current buffer.
154
155 This is a rearranged version of message mode's message-do-fcc."
156   (let (list file)
157     (save-excursion
158       (save-restriction
159         (message-narrow-to-headers)
160         (setq file (message-fetch-field "fcc" t)))
161       (when file
162         (with-temporary-notmuch-message-buffer
163          (save-restriction
164            (message-narrow-to-headers)
165            (while (setq file (message-fetch-field "fcc" t))
166              (push file list)
167              (message-remove-header "fcc" nil t)))
168          (notmuch-maildir-setup-message-for-saving)
169          ;; Process FCC operations.
170          (while list
171            (setq file (pop list))
172            (notmuch-fcc-handler file))
173          (kill-buffer (current-buffer)))))))
174
175 (defun notmuch-fcc-handler (fcc-header)
176   "Store message with file fcc."
177   (notmuch-maildir-fcc-file-fcc fcc-header))
178
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;; Functions for saving a message using file fcc.
181
182 (defun notmuch-maildir-fcc-host-fixer (hostname)
183   (replace-regexp-in-string "/\\|:"
184                             (lambda (s)
185                               (cond ((string-equal s "/") "\\057")
186                                     ((string-equal s ":") "\\072")
187                                     (t s)))
188                             hostname
189                             t
190                             t))
191
192 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
193    (let* ((ftime (float-time))
194           (microseconds (mod (* 1000000 ftime) 1000000))
195           (hostname (notmuch-maildir-fcc-host-fixer system-name)))
196      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
197      (format "%d.%d_%d_%d.%s"
198              ftime
199              (emacs-pid)
200              microseconds
201              notmuch-maildir-fcc-count
202              hostname)))
203
204 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
205   (and (file-exists-p (concat dir "/cur/"))
206        (file-exists-p (concat dir "/new/"))
207        (file-exists-p (concat dir "/tmp/"))))
208
209 (defun notmuch-maildir-fcc-create-maildir (path)
210   (cond ((or (not (file-exists-p path)) (file-directory-p path))
211          (make-directory (concat path "/cur/") t)
212          (make-directory (concat path "/new/") t)
213          (make-directory (concat path "/tmp/") t))
214         ((file-regular-p path)
215          (error "%s is a file. Can't create maildir." path))
216         (t
217          (error "I don't know how to create a maildir here"))))
218
219 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
220   "Returns the msg id of the message written to the temp directory
221 if successful, nil if not."
222   (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
223     (while (file-exists-p (concat destdir "/tmp/" msg-id))
224       (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
225     (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
226            (write-file (concat destdir "/tmp/" msg-id))
227            msg-id)
228           (t
229            (error (format "Can't write to %s. Not a maildir."
230                           destdir))
231            nil))))
232
233 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
234   (add-name-to-file
235    (concat destdir "/tmp/" msg-id)
236    (concat destdir "/new/" msg-id ":2,")))
237
238 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
239   (add-name-to-file
240    (concat destdir "/tmp/" msg-id)
241    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
242
243 (defun notmuch-maildir-fcc-file-fcc (fcc-header)
244   "Write the message to the file specified by FCC-HEADER.
245
246 It offers the user a chance to correct the header, or filesystem,
247 if needed."
248   (if (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
249       (notmuch-maildir-fcc-write-buffer-to-maildir fcc-header 't)
250     ;; The fcc-header is not a valid maildir see if the user wants to
251     ;; fix it in some way.
252     (let* ((prompt (format "Fcc %s is not a maildir: (r)etry, (c)reate folder, (i)gnore, or  (e)dit the header? "
253                            fcc-header))
254             (response (read-char-choice prompt '(?r ?c ?i ?e))))
255          (case response
256                (?r (notmuch-maildir-fcc-file-fcc fcc-header))
257                (?c (if (file-writable-p fcc-header)
258                        (notmuch-maildir-fcc-create-maildir fcc-header)
259                      (message "No permission to create %s." fcc-header)
260                      (sit-for 2))
261                    (notmuch-maildir-fcc-file-fcc fcc-header))
262                (?i 't)
263                (?e (notmuch-maildir-fcc-file-fcc
264                     (read-from-minibuffer "Fcc header: " fcc-header)))))))
265
266 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
267   "Writes the current buffer to maildir destdir. If mark-seen is
268 non-nil, it will write it to cur/, and mark it as read. It should
269 return t if successful, and nil otherwise."
270   (let ((orig-buffer (buffer-name)))
271     (with-temp-buffer
272       (insert-buffer-substring orig-buffer)
273       (catch 'link-error
274         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
275           (when msg-id
276             (cond (mark-seen
277                    (condition-case err
278                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
279                      (file-already-exists
280                       (throw 'link-error nil))))
281                   (t
282                    (condition-case err
283                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
284                      (file-already-exists
285                       (throw 'link-error nil))))))
286           (delete-file (concat destdir "/tmp/" msg-id))))
287       t)))
288
289 (provide 'notmuch-maildir-fcc)
290
291 ;;; notmuch-maildir-fcc.el ends here