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 "If set to non-nil, this will cause message mode to file (fcc) your mail in the specified directory, depending on your From address.
26 The first entry (a list) is used as a default fallback
27 when nothing matches. So in the easiest case notmuch-fcc-dirs is
28 just something like ((\"INBOX.Sent\"))
30 If you need a more fancy setup, where you want different Outboxes depending
31 on your From address, you use something like this:
34 (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
35 (\"Sebastian Spaeth <SSpaeth@ethz.ch>\" . \"uni\")
38 This will constructs a path, concatenating the content of the
39 variable 'message-directory' (a message mode variable
41 customize-variable<RET>message-directory<RET>) and the second
43 :require 'notmuch-fcc-initialization
47 (defun notmuch-fcc-initialization ()
48 "If notmuch-fcc-directories is set,
49 hook them into the message-fcc-handler-function"
50 (if (not (eq notmuch-fcc-dirs nil)) (progn
51 ;Set up the message-fcc-handler to move mails to the maildir in Fcc
52 ;The parameter is hardcoded to mark messages as "seen"
53 (setq message-fcc-handler-function
55 (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
56 ;add a hook to actually insert the Fcc header when sending
57 ;(preferrably we would use message-header-setup-up, but notmuch-reply
58 ; munges headers after that is run, so it won't work for replies within
60 (add-hook 'message-send-hook 'notmuch-fcc-header-setup))))
62 (defun notmuch-fcc-header-setup ()
63 "Can be added to message-send-hook and will set the FCC header
64 based on the values of notmuch-fcc-directories (see the
65 variable customization there for examples). It uses the
66 first entry as default fallback if no From address
68 ; only do something if notmuch-fcc-dirs is set
71 (cdr (assoc-string (message-fetch-field "from") notmuch-fcc-dirs t))))
72 (if (eq subdir nil) (setq subdir (car (car notmuch-fcc-dirs))))
73 (unless (message-fetch-field "fcc")
74 (message-add-header (concat "Fcc: "
75 (file-name-as-directory message-directory)
77 (let ((fcc-header (message-fetch-field "fcc")))
78 (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
79 (cond ((not (file-writable-p fcc-header))
80 (error (format "%s is not a maildir, but you don't have permission to create one." fcc-header)))
81 ((y-or-n-p (format "%s is not a maildir. Create it? "
83 (notmuch-maildir-fcc-create-maildir fcc-header))
85 (error "Not sending message."))))))))
87 (defun notmuch-maildir-fcc-host-fixer (hostname)
88 (replace-regexp-in-string "/\\|:"
90 (cond ((string-equal s "/") "\\057")
91 ((string-equal s ":") "\\072")
97 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
98 (let* ((ct (current-time))
99 (timeid (+ (* (car ct) 65536) (cadr ct)))
100 (microseconds (car (cdr (cdr ct))))
101 (hostname (notmuch-maildir-fcc-host-fixer system-name)))
102 (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
103 (format "%d.%d_%d_%d.%s"
107 notmuch-maildir-fcc-count
110 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
111 (and (file-exists-p (concat dir "/cur/"))
112 (file-exists-p (concat dir "/new/"))
113 (file-exists-p (concat dir "/tmp/"))))
115 (defun notmuch-maildir-fcc-create-maildir (path)
116 (cond ((or (not (file-exists-p path)) (file-directory-p path))
117 (make-directory (concat path "/cur/") t)
118 (make-directory (concat path "/new/") t)
119 (make-directory (concat path "/tmp/") t))
120 ((file-regular-p path)
121 (error "%s is a file. Can't creat maildir." path))
123 (error "I don't know how to create a maildir here"))))
125 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
126 "Returns the msg id of the message written to the temp directory
127 if successful, nil if not."
128 (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
129 (while (file-exists-p (concat destdir "/tmp/" msg-id))
130 (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
131 (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
132 (write-file (concat destdir "/tmp/" msg-id))
135 (error (format "Can't write to %s. Not a maildir."
139 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
141 (concat destdir "/tmp/" msg-id)
142 (concat destdir "/new/" msg-id ":2,")))
144 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
146 (concat destdir "/tmp/" msg-id)
147 (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
149 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
150 "Writes the current buffer to maildir destdir. If mark-seen is
151 non-nil, it will write it to cur/, and mark it as read. It should
152 return t if successful, and nil otherwise."
153 (let ((orig-buffer (buffer-name)))
155 (insert-buffer-substring orig-buffer)
157 (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
161 (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
163 (throw 'link-error nil))))
166 (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
168 (throw 'link-error nil))))))
169 (delete-file (concat destdir "/tmp/" msg-id))))
172 (notmuch-fcc-initialization)
173 (provide 'notmuch-maildir-fcc)