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
21 (defvar notmuch-maildir-fcc-count 0)
23 (defcustom notmuch-fcc-dirs nil
24 "Determines the maildir directory to save outgoing mails in.
26 If set to non-nil, this will cause message mode to file your
27 mail in the specified directory (fcc).
29 It is either a string if you only need one fcc directory or a
30 list if they depend on your From address (see example).
32 In the former case it is a string such as \"INBOX.Sent\".
34 In the fancy setup, where you want different outboxes depending
35 on your From address, you supply a list like this:
38 (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
39 (\"Sebastian Spaeth <spaetz@sspaeth.de>\" . \"OUTBOX.OSS\")
42 The outbox that matches a key (case insensitive) will be
43 used. The first entry is used as a default fallback when nothing
46 In all cases, the complete FCC directory will be constructed by
47 concatenating the content of the variable 'message-directory'
48 ('~/Mail/' by default and customizable via M-x
49 customize-variable<RET>message-directory<RET>) and this value.
51 You will be prompted to create the directory if it does not exist yet when
54 This function will not modify the headers if there is a FCC
55 header, but will check that the target directory exists."
57 :require 'notmuch-fcc-initialization
61 (defun notmuch-fcc-initialization ()
62 "If notmuch-fcc-directories is set,
63 hook them into the message-fcc-handler-function"
64 ;; Set up the message-fcc-handler to move mails to the maildir in Fcc
65 ;; The parameter is set to mark messages as "seen"
66 (setq message-fcc-handler-function
68 (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
69 ;;add a hook to actually insert the Fcc header when sending
70 (add-hook 'message-send-hook 'notmuch-fcc-header-setup))
72 (defun notmuch-fcc-header-setup ()
73 "Adds an appropriate fcc header to the current mail buffer
75 Can be added to message-send-hook and will set the FCC header
76 based on the values of notmuch-fcc-directories (see the
77 variable customization there for examples). It uses the first
78 entry as default fallback if no From address matches."
79 ;; only do something if notmuch-fcc-dirs is set
80 (when notmuch-fcc-dirs
82 (if (stringp notmuch-fcc-dirs)
83 ;; notmuch-fcc-dirs is a string, just use it as subdir
84 (setq subdir notmuch-fcc-dirs)
85 ;; else: it's a list of alists (("sent") ("name1" . "sent1"))
86 (setq subdir (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t)))
87 ;; if we found no hit, use the first entry as default fallback
88 (unless subdir (setq subdir (car (car notmuch-fcc-dirs)))))
90 ;; if there is no fcc header yet, add ours
91 (unless (message-fetch-field "fcc")
92 (message-add-header (concat "Fcc: "
93 (file-name-as-directory message-directory)
96 ;; finally test if fcc points to a valid maildir
97 (let ((fcc-header (message-fetch-field "fcc")))
98 (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
99 (cond ((not (file-writable-p fcc-header))
100 (error (format "%s is not a maildir, but you don't have permission to create one." fcc-header)))
101 ((y-or-n-p (format "%s is not a maildir. Create it? "
103 (notmuch-maildir-fcc-create-maildir fcc-header))
105 (error "Not sending message."))))))))
107 (defun notmuch-maildir-fcc-host-fixer (hostname)
108 (replace-regexp-in-string "/\\|:"
110 (cond ((string-equal s "/") "\\057")
111 ((string-equal s ":") "\\072")
117 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
118 (let* ((ct (current-time))
119 (timeid (+ (* (car ct) 65536) (cadr ct)))
120 (microseconds (car (cdr (cdr ct))))
121 (hostname (notmuch-maildir-fcc-host-fixer system-name)))
122 (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
123 (format "%d.%d_%d_%d.%s"
127 notmuch-maildir-fcc-count
130 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
131 (and (file-exists-p (concat dir "/cur/"))
132 (file-exists-p (concat dir "/new/"))
133 (file-exists-p (concat dir "/tmp/"))))
135 (defun notmuch-maildir-fcc-create-maildir (path)
136 (cond ((or (not (file-exists-p path)) (file-directory-p path))
137 (make-directory (concat path "/cur/") t)
138 (make-directory (concat path "/new/") t)
139 (make-directory (concat path "/tmp/") t))
140 ((file-regular-p path)
141 (error "%s is a file. Can't creat maildir." path))
143 (error "I don't know how to create a maildir here"))))
145 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
146 "Returns the msg id of the message written to the temp directory
147 if successful, nil if not."
148 (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
149 (while (file-exists-p (concat destdir "/tmp/" msg-id))
150 (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
151 (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
152 (write-file (concat destdir "/tmp/" msg-id))
155 (error (format "Can't write to %s. Not a maildir."
159 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
161 (concat destdir "/tmp/" msg-id)
162 (concat destdir "/new/" msg-id ":2,")))
164 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
166 (concat destdir "/tmp/" msg-id)
167 (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
169 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
170 "Writes the current buffer to maildir destdir. If mark-seen is
171 non-nil, it will write it to cur/, and mark it as read. It should
172 return t if successful, and nil otherwise."
173 (let ((orig-buffer (buffer-name)))
175 (insert-buffer-substring orig-buffer)
177 (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
181 (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
183 (throw 'link-error nil))))
186 (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
188 (throw 'link-error nil))))))
189 (delete-file (concat destdir "/tmp/" msg-id))))
192 (notmuch-fcc-initialization)
193 (provide 'notmuch-maildir-fcc)