1 ;;; notmuch-maildir-fcc.el --- inserting using a fcc handler -*- lexical-binding: t -*-
3 ;; Copyright © Jesse Rosenthal
5 ;; This file is part of Notmuch.
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch. If not, see <https://www.gnu.org/licenses/>.
20 ;; Authors: Jesse Rosenthal <jrosenthal@jhu.edu>
28 (require 'notmuch-lib)
30 (defvar notmuch-maildir-fcc-count 0)
34 (defcustom notmuch-fcc-dirs "sent"
35 "Determines the Fcc Header which says where to save outgoing mail.
37 Three types of values are permitted:
39 - nil: no Fcc header is added,
41 - a string: the value of `notmuch-fcc-dirs' is the Fcc header to
44 - an alist: the folder is chosen based on the From address of
45 the current message according to an alist mapping regular
46 expressions to folders or nil:
48 ((\"Sebastian@SSpaeth.de\" . \"privat\")
49 (\"spaetz@sspaeth.de\" . \"OUTBOX.OSS\")
50 (\".*\" . \"defaultinbox\"))
52 If none of the regular expressions match the From address, or
53 if the cdr of the matching entry is nil, then no Fcc header
56 If `notmuch-maildir-use-notmuch-insert' is set (the default) then
57 the header should be of the form \"folder +tag1 -tag2\" where
58 folder is the folder (relative to the notmuch mailstore) to store
59 the message in, and tag1 and tag2 are tag changes to apply to the
60 stored message. This string is split using `split-string-and-unquote',
61 so a folder name containing spaces can be specified by
62 quoting each space with an immediately preceding backslash
63 or surrounding the entire folder name in double quotes.
65 If `notmuch-maildir-use-notmuch-insert' is nil then the Fcc
66 header should be the directory where the message should be
67 saved. A relative directory will be understood to specify a
68 directory within the notmuch mail store, (as set by the
69 database.path option in the notmuch configuration file).
71 In all cases you will be prompted to create the folder or
72 directory if it does not exist yet when sending a mail."
75 (const :tag "No FCC header" nil)
76 (string :tag "A single folder")
77 (repeat :tag "A folder based on the From header"
78 (cons regexp (choice (const :tag "No FCC header" nil)
79 (string :tag "Folder")))))
80 :require 'notmuch-fcc-initialization
83 (defcustom notmuch-maildir-use-notmuch-insert t
84 "Should fcc use notmuch insert instead of simple fcc."
85 :type '(choice :tag "Fcc Method"
86 (const :tag "Use notmuch insert" t)
87 (const :tag "Use simple fcc" nil))
90 ;;; Functions which set up the fcc header in the message buffer.
92 (defun notmuch-fcc-header-setup ()
93 "Add an Fcc header to the current message buffer.
95 If the Fcc header is already set, then keep it as-is.
96 Otherwise set it according to `notmuch-fcc-dirs'."
99 ((or (not notmuch-fcc-dirs)
100 (message-field-value "Fcc"))
101 ;; Nothing set or an existing header.
103 ((stringp notmuch-fcc-dirs)
105 ((and (listp notmuch-fcc-dirs)
106 (stringp (car notmuch-fcc-dirs)))
107 ;; Old style - no longer works.
108 (error "Invalid `notmuch-fcc-dirs' setting (old style)"))
109 ((listp notmuch-fcc-dirs)
110 (if-let ((match (seq-some (let ((from (message-field-value "From")))
111 (pcase-lambda (`(,regexp . ,folder))
112 (and (string-match-p regexp from)
116 (message "No Fcc header added.")
119 (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
121 (if notmuch-maildir-use-notmuch-insert
122 (notmuch-maildir-add-notmuch-insert-style-fcc-header subdir)
123 (notmuch-maildir-add-file-style-fcc-header subdir)))))
125 (defun notmuch-maildir-add-notmuch-insert-style-fcc-header (subdir)
126 ;; Notmuch insert does not accept absolute paths, so check the user
127 ;; really want this header inserted.
128 (when (or (not (= (elt subdir 0) ?/))
129 (y-or-n-p (format "Fcc header %s is an absolute path %s %s" subdir
130 "and notmuch insert is requested."
131 "Insert header anyway? ")))
132 (message-add-header (concat "Fcc: " subdir))))
134 (defun notmuch-maildir-add-file-style-fcc-header (subdir)
138 ;; If the resulting directory is not an absolute path,
139 ;; prepend the standard notmuch database path.
140 (if (= (elt subdir 0) ?/)
142 (concat (notmuch-database-path) "/" subdir))))))
144 ;;; Functions for saving a message using either method.
146 (defmacro with-temporary-notmuch-message-buffer (&rest body)
147 "Set-up a temporary copy of the current message-mode buffer."
150 (let ((case-fold-search t)
151 (buf (current-buffer))
152 (mml-externalize-attachments message-fcc-externalize-attachments))
153 (with-current-buffer (get-buffer-create " *message temp*")
154 (message-clone-locals buf) ;; for message-encoded-mail-cache
156 (insert-buffer-substring buf)
159 (defun notmuch-maildir-setup-message-for-saving ()
160 "Setup message for saving.
162 This should be called on a temporary copy.
163 This is taken from the function message-do-fcc."
164 (if (not message-encoded-mail-cache)
165 (message-encode-message-body)
167 (insert message-encoded-mail-cache))
169 (message-narrow-to-headers)
170 (mail-encode-encoded-word-buffer))
171 (goto-char (point-min))
172 (when (re-search-forward
173 (concat "^" (regexp-quote mail-header-separator) "$")
175 (replace-match "" t t )))
177 (defun notmuch-maildir-message-do-fcc ()
178 "Process Fcc headers in the current buffer.
180 This is a rearranged version of message mode's message-do-fcc."
184 (message-narrow-to-headers)
185 (setq file (message-fetch-field "fcc" t)))
187 (with-temporary-notmuch-message-buffer
188 (notmuch-maildir-setup-message-for-saving)
190 (message-narrow-to-headers)
191 (while (setq file (message-fetch-field "fcc" t))
193 (message-remove-header "fcc" nil t)))
194 ;; Process FCC operations.
195 (mapc #'notmuch-fcc-handler files)
196 (kill-buffer (current-buffer)))))))
198 (defun notmuch-fcc-handler (fcc-header)
199 "Store message with notmuch insert or normal (file) fcc.
201 If `notmuch-maildir-use-notmuch-insert' is set then store the
202 message using notmuch insert. Otherwise store the message using
204 (message "Doing Fcc...")
205 (if notmuch-maildir-use-notmuch-insert
206 (notmuch-maildir-fcc-with-notmuch-insert fcc-header)
207 (notmuch-maildir-fcc-file-fcc fcc-header))
208 (message "Doing Fcc...done"))
210 ;;; Functions for saving a message using notmuch insert.
212 (defun notmuch-maildir-notmuch-insert-current-buffer (folder &optional create tags)
213 "Use notmuch insert to put the current buffer in the database.
215 This inserts the current buffer as a message into the notmuch
216 database in folder FOLDER. If CREATE is non-nil it will supply
217 the --create-folder flag to create the folder if necessary. TAGS
218 should be a list of tag changes to apply to the inserted message."
219 (apply 'notmuch-call-notmuch-process
220 :stdin-string (buffer-string) "insert"
221 (append (and create (list "--create-folder"))
222 (list (concat "--folder=" folder))
225 (defun notmuch-maildir-fcc-with-notmuch-insert (fcc-header &optional create)
226 "Store message with notmuch insert.
228 The fcc-header should be of the form \"folder +tag1 -tag2\" where
229 folder is the folder (relative to the notmuch mailstore) to store
230 the message in, and tag1 and tag2 are tag changes to apply to the
231 stored message. This string is split using `split-string-and-unquote',
232 so a folder name containing spaces can be specified by
233 quoting each space with an immediately preceding backslash
234 or surrounding the entire folder name in double quotes.
236 If CREATE is non-nil then create the folder if necessary."
237 (pcase-let ((`(,folder . ,tags)
238 (split-string-and-unquote fcc-header)))
240 (notmuch-maildir-notmuch-insert-current-buffer folder create tags)
241 ;; Since there are many reasons notmuch insert could fail, e.g.,
242 ;; locked database, non-existent folder (which could be due to a
243 ;; typo, or just the user want a new folder, let the user decide
244 ;; how to deal with it.
246 (let ((response (read-char-choice "Insert failed: \
247 \(r)etry, (c)reate folder, (i)gnore, or (e)dit the header? " '(?r ?c ?i ?e))))
249 (?r (notmuch-maildir-fcc-with-notmuch-insert fcc-header))
250 (?c (notmuch-maildir-fcc-with-notmuch-insert fcc-header t))
252 (?e (notmuch-maildir-fcc-with-notmuch-insert
253 (read-from-minibuffer "Fcc header: " fcc-header)))))))))
255 ;;; Functions for saving a message using file fcc.
257 (defun notmuch-maildir-fcc-host-fixer (hostname)
258 (replace-regexp-in-string "/\\|:"
260 (cond ((string-equal s "/") "\\057")
261 ((string-equal s ":") "\\072")
267 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
268 (let* ((ftime (float-time))
269 (microseconds (mod (* 1000000 ftime) 1000000))
270 (hostname (notmuch-maildir-fcc-host-fixer (system-name))))
271 (cl-incf notmuch-maildir-fcc-count)
272 (format "%d.%d_%d_%d.%s"
276 notmuch-maildir-fcc-count
279 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
280 (and (file-exists-p (concat dir "/cur/"))
281 (file-exists-p (concat dir "/new/"))
282 (file-exists-p (concat dir "/tmp/"))))
284 (defun notmuch-maildir-fcc-create-maildir (path)
285 (cond ((or (not (file-exists-p path)) (file-directory-p path))
286 (make-directory (concat path "/cur/") t)
287 (make-directory (concat path "/new/") t)
288 (make-directory (concat path "/tmp/") t))
289 ((file-regular-p path)
290 (error "%s is a file. Can't create maildir." path))
292 (error "I don't know how to create a maildir here"))))
294 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
295 "Returns the msg id of the message written to the temp directory
296 if successful, nil if not."
297 (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
298 (while (file-exists-p (concat destdir "/tmp/" msg-id))
299 (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
300 (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
301 (write-file (concat destdir "/tmp/" msg-id))
304 (error "Can't write to %s. Not a maildir." destdir)))))
306 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
308 (concat destdir "/tmp/" msg-id)
309 (concat destdir "/new/" msg-id ":2,")))
311 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
313 (concat destdir "/tmp/" msg-id)
314 (concat destdir "/cur/" msg-id ":2," (and mark-seen "S"))))
316 (defun notmuch-maildir-fcc-file-fcc (fcc-header)
317 "Write the message to the file specified by FCC-HEADER.
319 If that fails, then offer the user a chance to correct the header
321 (if (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
322 (notmuch-maildir-fcc-write-buffer-to-maildir fcc-header t)
323 ;; The fcc-header is not a valid maildir see if the user wants to
324 ;; fix it in some way.
325 (let* ((prompt (format "Fcc %s is not a maildir: \
326 \(r)etry, (c)reate folder, (i)gnore, or (e)dit the header? " fcc-header))
327 (response (read-char-choice prompt '(?r ?c ?i ?e))))
329 (?r (notmuch-maildir-fcc-file-fcc fcc-header))
330 (?c (if (file-writable-p fcc-header)
331 (notmuch-maildir-fcc-create-maildir fcc-header)
332 (message "No permission to create %s." fcc-header)
334 (notmuch-maildir-fcc-file-fcc fcc-header))
336 (?e (notmuch-maildir-fcc-file-fcc
337 (read-from-minibuffer "Fcc header: " fcc-header)))))))
339 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
340 "Write the current buffer to maildir destdir.
342 If mark-seen is non-nil, then write it to \"cur/\", and mark it
343 as read, otherwise write it to \"new/\". Return t if successful,
345 (let ((orig-buffer (buffer-name)))
347 (insert-buffer-substring orig-buffer)
349 (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
353 (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
354 (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id))
356 (throw 'link-error nil))))
357 (delete-file (concat destdir "/tmp/" msg-id))))
362 (provide 'notmuch-maildir-fcc)
364 ;;; notmuch-maildir-fcc.el ends here