1 ;; This file is free software; you can redistribute it and/or modify
2 ;; it under the terms of the GNU General Public License as published
3 ;; by the Free Software Foundation; either version 2, or (at your
4 ;; option) any later version.
6 ;; This program is distributed in the hope that it will be useful,
7 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
8 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 ;; GNU General Public License for more details.
11 ;; You should have received a copy of the GNU General Public License
12 ;; along with GNU Emacs; see the file COPYING. If not, write to the
13 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
14 ;; Boston, MA 02110-1301, USA.
16 ;; To use this as the fcc handler for message-mode,
17 ;; customize the notmuch-fcc-dirs variable
19 (eval-when-compile (require 'cl))
22 (require 'notmuch-lib)
24 (defvar notmuch-maildir-fcc-count 0)
26 (defcustom notmuch-fcc-dirs "sent"
27 "Determines the maildir directory in which to save outgoing mail.
29 Three types of values are permitted:
31 - nil: no Fcc header is added,
33 - a string: the value of `notmuch-fcc-dirs' is the name of the
36 - a list: the folder is chosen based on the From address of the
37 current message using a list of regular expressions and
38 corresponding folders:
40 ((\"Sebastian@SSpaeth.de\" . \"privat\")
41 (\"spaetz@sspaeth.de\" . \"OUTBOX.OSS\")
42 (\".*\" . \"defaultinbox\"))
44 If none of the regular expressions match the From address, no
45 Fcc header will be added.
47 In all cases, a relative FCC directory will be understood to
48 specify a directory within the notmuch mail store, (as set by
49 the database.path option in the notmuch configuration file).
51 You will be prompted to create the directory if it does not exist
52 yet when sending a mail."
55 (const :tag "No FCC header" nil)
56 (string :tag "A single folder")
57 (repeat :tag "A folder based on the From header"
58 (cons regexp (string :tag "Folder"))))
59 :require 'notmuch-fcc-initialization
62 (defun notmuch-fcc-handler (destdir)
63 "Write buffer to `destdir', marking it as sent
65 Intended to be dynamically bound to `message-fcc-handler-function'"
66 (notmuch-maildir-fcc-write-buffer-to-maildir destdir t))
68 (defun notmuch-fcc-header-setup ()
69 "Add an Fcc header to the current message buffer.
71 Sets the Fcc header based on the values of `notmuch-fcc-dirs'.
73 Originally intended to be use a hook function, but now called directly
78 ((or (not notmuch-fcc-dirs)
79 (message-field-value "Fcc"))
80 ;; Nothing set or an existing header.
83 ((stringp notmuch-fcc-dirs)
86 ((and (listp notmuch-fcc-dirs)
87 (stringp (car notmuch-fcc-dirs)))
88 ;; Old style - no longer works.
89 (error "Invalid `notmuch-fcc-dirs' setting (old style)"))
91 ((listp notmuch-fcc-dirs)
92 (let* ((from (message-field-value "From"))
95 (dolist (re-folder notmuch-fcc-dirs)
96 (when (string-match-p (car re-folder) from)
97 (throw 'first-match re-folder))))))
100 (message "No Fcc header added.")
104 (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
110 ;; If the resulting directory is not an absolute path,
111 ;; prepend the standard notmuch database path.
112 (if (= (elt subdir 0) ?/)
114 (concat (notmuch-database-path) "/" subdir)))))
116 ;; finally test if fcc points to a valid maildir
117 (let ((fcc-header (message-field-value "Fcc")))
118 (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
119 (cond ((not (file-writable-p fcc-header))
120 (error (format "No permission to create %s, which does not exist"
122 ((y-or-n-p (format "%s is not a maildir. Create it? "
124 (notmuch-maildir-fcc-create-maildir fcc-header))
126 (error "Message not sent"))))))))
128 (defun notmuch-maildir-fcc-host-fixer (hostname)
129 (replace-regexp-in-string "/\\|:"
131 (cond ((string-equal s "/") "\\057")
132 ((string-equal s ":") "\\072")
138 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
139 (let* ((ftime (float-time))
140 (microseconds (mod (* 1000000 ftime) 1000000))
141 (hostname (notmuch-maildir-fcc-host-fixer system-name)))
142 (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
143 (format "%d.%d_%d_%d.%s"
147 notmuch-maildir-fcc-count
150 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
151 (and (file-exists-p (concat dir "/cur/"))
152 (file-exists-p (concat dir "/new/"))
153 (file-exists-p (concat dir "/tmp/"))))
155 (defun notmuch-maildir-fcc-create-maildir (path)
156 (cond ((or (not (file-exists-p path)) (file-directory-p path))
157 (make-directory (concat path "/cur/") t)
158 (make-directory (concat path "/new/") t)
159 (make-directory (concat path "/tmp/") t))
160 ((file-regular-p path)
161 (error "%s is a file. Can't create maildir." path))
163 (error "I don't know how to create a maildir here"))))
165 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
166 "Returns the msg id of the message written to the temp directory
167 if successful, nil if not."
168 (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
169 (while (file-exists-p (concat destdir "/tmp/" msg-id))
170 (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
171 (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
172 (write-file (concat destdir "/tmp/" msg-id))
175 (error (format "Can't write to %s. Not a maildir."
179 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
181 (concat destdir "/tmp/" msg-id)
182 (concat destdir "/new/" msg-id ":2,")))
184 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
186 (concat destdir "/tmp/" msg-id)
187 (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
189 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
190 "Writes the current buffer to maildir destdir. If mark-seen is
191 non-nil, it will write it to cur/, and mark it as read. It should
192 return t if successful, and nil otherwise."
193 (let ((orig-buffer (buffer-name)))
195 (insert-buffer-substring orig-buffer)
197 (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
201 (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
203 (throw 'link-error nil))))
206 (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
208 (throw 'link-error nil))))))
209 (delete-file (concat destdir "/tmp/" msg-id))))
212 (provide 'notmuch-maildir-fcc)