1 ;;; notmuch-maildir-fcc.el ---
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.
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.
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.
20 ;; To use this as the fcc handler for message-mode,
21 ;; customize the notmuch-fcc-dirs variable
25 (eval-when-compile (require 'cl))
28 (require 'notmuch-lib)
30 (defvar notmuch-maildir-fcc-count 0)
32 (defcustom notmuch-fcc-dirs "sent"
33 "Determines the Fcc Header which says where to save outgoing mail.
35 Three types of values are permitted:
37 - nil: no Fcc header is added,
39 - a string: the value of `notmuch-fcc-dirs' is the Fcc header to
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:
46 ((\"Sebastian@SSpaeth.de\" . \"privat\")
47 (\"spaetz@sspaeth.de\" . \"OUTBOX.OSS\")
48 (\".*\" . \"defaultinbox\"))
50 If none of the regular expressions match the From address, no
51 Fcc header will be added.
53 If `notmuch-maildir-use-notmuch-insert' is set (the default) then
54 the header should be of the form \"folder +tag1 -tag2\" where
55 folder is the folder (relative to the notmuch mailstore) to store
56 the message in, and tag1 and tag2 are tag changes to apply to the
57 stored message. This string is split using `split-string-and-unquote',
58 so a folder name containing spaces can be specified by
59 quoting each space with an immediately preceding backslash
60 or surrounding the entire folder name in double quotes.
62 If `notmuch-maildir-use-notmuch-insert' is nil then the Fcc
63 header should be the directory where the message should be
64 saved. A relative directory will be understood to specify a
65 directory within the notmuch mail store, (as set by the
66 database.path option in the notmuch configuration file).
68 In all cases you will be prompted to create the folder or
69 directory if it does not exist yet when sending a mail."
72 (const :tag "No FCC header" nil)
73 (string :tag "A single folder")
74 (repeat :tag "A folder based on the From header"
75 (cons regexp (string :tag "Folder"))))
76 :require 'notmuch-fcc-initialization
79 (defcustom notmuch-maildir-use-notmuch-insert 't
80 "Should fcc use notmuch insert instead of simple fcc"
81 :type '(choice :tag "Fcc Method"
82 (const :tag "Use notmuch insert" t)
83 (const :tag "Use simple fcc" nil))
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87 ;; Functions which set up the fcc header in the message buffer.
89 (defun notmuch-fcc-header-setup ()
90 "Add an Fcc header to the current message buffer.
92 Sets the Fcc header based on the values of `notmuch-fcc-dirs'.
94 Originally intended to be use a hook function, but now called directly
99 ((or (not notmuch-fcc-dirs)
100 (message-field-value "Fcc"))
101 ;; Nothing set or an existing header.
104 ((stringp notmuch-fcc-dirs)
107 ((and (listp notmuch-fcc-dirs)
108 (stringp (car notmuch-fcc-dirs)))
109 ;; Old style - no longer works.
110 (error "Invalid `notmuch-fcc-dirs' setting (old style)"))
112 ((listp notmuch-fcc-dirs)
113 (let* ((from (message-field-value "From"))
116 (dolist (re-folder notmuch-fcc-dirs)
117 (when (string-match-p (car re-folder) from)
118 (throw 'first-match re-folder))))))
121 (message "No Fcc header added.")
125 (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
128 (if notmuch-maildir-use-notmuch-insert
129 (notmuch-maildir-add-notmuch-insert-style-fcc-header subdir)
130 (notmuch-maildir-add-file-style-fcc-header subdir)))))
132 (defun notmuch-maildir-add-notmuch-insert-style-fcc-header (subdir)
133 ;; Notmuch insert does not accept absolute paths, so check the user
134 ;; really want this header inserted.
136 (when (or (not (= (elt subdir 0) ?/))
137 (y-or-n-p (format "Fcc header %s is an absolute path and notmuch insert is requested.\nInsert header anyway? "
139 (message-add-header (concat "Fcc: " subdir))))
141 (defun notmuch-maildir-add-file-style-fcc-header (subdir)
145 ;; If the resulting directory is not an absolute path,
146 ;; prepend the standard notmuch database path.
147 (if (= (elt subdir 0) ?/)
149 (concat (notmuch-database-path) "/" subdir))))))
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;; Functions for saving a message either using notmuch insert or file
153 ;; fcc. First functions common to the two cases.
155 (defmacro with-temporary-notmuch-message-buffer (&rest body)
156 "Set-up a temporary copy of the current message-mode buffer."
157 `(let ((case-fold-search t)
158 (buf (current-buffer))
159 (mml-externalize-attachments message-fcc-externalize-attachments))
160 (with-current-buffer (get-buffer-create " *message temp*")
162 (insert-buffer-substring buf)
165 (defun notmuch-maildir-setup-message-for-saving ()
166 "Setup message for saving. Should be called on a temporary copy.
168 This is taken from the function message-do-fcc."
169 (message-encode-message-body)
171 (message-narrow-to-headers)
172 (let ((mail-parse-charset message-default-charset))
173 (mail-encode-encoded-word-buffer)))
174 (goto-char (point-min))
175 (when (re-search-forward
176 (concat "^" (regexp-quote mail-header-separator) "$")
178 (replace-match "" t t )))
180 (defun notmuch-maildir-message-do-fcc ()
181 "Process Fcc headers in the current buffer.
183 This is a rearranged version of message mode's message-do-fcc."
187 (message-narrow-to-headers)
188 (setq file (message-fetch-field "fcc" t)))
190 (with-temporary-notmuch-message-buffer
192 (message-narrow-to-headers)
193 (while (setq file (message-fetch-field "fcc" t))
195 (message-remove-header "fcc" nil t)))
196 (notmuch-maildir-setup-message-for-saving)
197 ;; Process FCC operations.
199 (setq file (pop list))
200 (notmuch-fcc-handler file))
201 (kill-buffer (current-buffer)))))))
203 (defun notmuch-fcc-handler (fcc-header)
204 "Store message with notmuch insert or normal (file) fcc.
206 If `notmuch-maildir-use-notmuch-insert` is set then store the
207 message using notmuch insert. Otherwise store the message using
209 (message "Doing Fcc...")
210 (if notmuch-maildir-use-notmuch-insert
211 (notmuch-maildir-fcc-with-notmuch-insert fcc-header)
212 (notmuch-maildir-fcc-file-fcc fcc-header)))
214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
215 ;; Functions for saving a message using notmuch insert.
217 (defun notmuch-maildir-notmuch-insert-current-buffer (folder &optional create tags)
218 "Use notmuch insert to put the current buffer in the database.
220 This inserts the current buffer as a message into the notmuch
221 database in folder FOLDER. If CREATE is non-nil it will supply
222 the --create-folder flag to create the folder if necessary. TAGS
223 should be a list of tag changes to apply to the inserted message."
224 (let* ((args (append (when create (list "--create-folder"))
225 (list (concat "--folder=" folder))
227 (apply 'notmuch-call-notmuch-process
228 :stdin-string (buffer-string) "insert" args)))
230 (defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create)
231 "Store message with notmuch insert.
233 The fcc-header should be of the form \"folder +tag1 -tag2\" where
234 folder is the folder (relative to the notmuch mailstore) to store
235 the message in, and tag1 and tag2 are tag changes to apply to the
236 stored message. This string is split using `split-string-and-unquote',
237 so a folder name containing spaces can be specified by
238 quoting each space with an immediately preceding backslash
239 or surrounding the entire folder name in double quotes.
241 If CREATE is non-nil then create the folder if necessary."
242 (let* ((args (split-string-and-unquote fcc-header))
246 (notmuch-maildir-notmuch-insert-current-buffer folder create tags)
247 ;; Since there are many reasons notmuch insert could fail, e.g.,
248 ;; locked database, non-existent folder (which could be due to a
249 ;; typo, or just the user want a new folder, let the user decide
250 ;; how to deal with it.
252 (let ((response (notmuch-read-char-choice
253 "Insert failed: (r)etry, (c)reate folder, (i)gnore, or (e)dit the header? "
256 (?r (notmuch-maildir-fcc-with-notmuch-insert fcc-header))
257 (?c (notmuch-maildir-fcc-with-notmuch-insert fcc-header 't))
259 (?e (notmuch-maildir-fcc-with-notmuch-insert
260 (read-from-minibuffer "Fcc header: " fcc-header)))))))))
263 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
264 ;; Functions for saving a message using file fcc.
266 (defun notmuch-maildir-fcc-host-fixer (hostname)
267 (replace-regexp-in-string "/\\|:"
269 (cond ((string-equal s "/") "\\057")
270 ((string-equal s ":") "\\072")
276 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
277 (let* ((ftime (float-time))
278 (microseconds (mod (* 1000000 ftime) 1000000))
279 (hostname (notmuch-maildir-fcc-host-fixer (system-name))))
280 (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
281 (format "%d.%d_%d_%d.%s"
285 notmuch-maildir-fcc-count
288 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
289 (and (file-exists-p (concat dir "/cur/"))
290 (file-exists-p (concat dir "/new/"))
291 (file-exists-p (concat dir "/tmp/"))))
293 (defun notmuch-maildir-fcc-create-maildir (path)
294 (cond ((or (not (file-exists-p path)) (file-directory-p path))
295 (make-directory (concat path "/cur/") t)
296 (make-directory (concat path "/new/") t)
297 (make-directory (concat path "/tmp/") t))
298 ((file-regular-p path)
299 (error "%s is a file. Can't create maildir." path))
301 (error "I don't know how to create a maildir here"))))
303 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
304 "Returns the msg id of the message written to the temp directory
305 if successful, nil if not."
306 (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
307 (while (file-exists-p (concat destdir "/tmp/" msg-id))
308 (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
309 (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
310 (write-file (concat destdir "/tmp/" msg-id))
313 (error (format "Can't write to %s. Not a maildir."
317 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
319 (concat destdir "/tmp/" msg-id)
320 (concat destdir "/new/" msg-id ":2,")))
322 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
324 (concat destdir "/tmp/" msg-id)
325 (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
327 (defun notmuch-maildir-fcc-file-fcc (fcc-header)
328 "Write the message to the file specified by FCC-HEADER.
330 It offers the user a chance to correct the header, or filesystem,
332 (if (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
333 (notmuch-maildir-fcc-write-buffer-to-maildir fcc-header 't)
334 ;; The fcc-header is not a valid maildir see if the user wants to
335 ;; fix it in some way.
336 (let* ((prompt (format "Fcc %s is not a maildir: (r)etry, (c)reate folder, (i)gnore, or (e)dit the header? "
338 (response (notmuch-read-char-choice prompt '(?r ?c ?i ?e))))
340 (?r (notmuch-maildir-fcc-file-fcc fcc-header))
341 (?c (if (file-writable-p fcc-header)
342 (notmuch-maildir-fcc-create-maildir fcc-header)
343 (message "No permission to create %s." fcc-header)
345 (notmuch-maildir-fcc-file-fcc fcc-header))
347 (?e (notmuch-maildir-fcc-file-fcc
348 (read-from-minibuffer "Fcc header: " fcc-header)))))))
350 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
351 "Writes the current buffer to maildir destdir. If mark-seen is
352 non-nil, it will write it to cur/, and mark it as read. It should
353 return t if successful, and nil otherwise."
354 (let ((orig-buffer (buffer-name)))
356 (insert-buffer-substring orig-buffer)
358 (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
362 (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
364 (throw 'link-error nil))))
367 (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
369 (throw 'link-error nil))))))
370 (delete-file (concat destdir "/tmp/" msg-id))))
373 (provide 'notmuch-maildir-fcc)
375 ;;; notmuch-maildir-fcc.el ends here