]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
version: bump to 0.15.1
[notmuch] / emacs / notmuch-maildir-fcc.el
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.
5
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.
10
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.
15 ;;
16 ;; To use this as the fcc handler for message-mode,
17 ;; customize the notmuch-fcc-dirs variable
18
19 (eval-when-compile (require 'cl))
20 (require 'message)
21
22 (require 'notmuch-lib)
23
24 (defvar notmuch-maildir-fcc-count 0)
25
26 (defcustom notmuch-fcc-dirs "sent"
27  "Determines the maildir directory in which to save outgoing mail.
28
29 Three types of values are permitted:
30
31 - nil: no Fcc header is added,
32
33 - a string: the value of `notmuch-fcc-dirs' is the name of the
34   folder to use,
35
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:
39
40      ((\"Sebastian@SSpaeth.de\" . \"privat\")
41       (\"spaetz@sspaeth.de\" . \"OUTBOX.OSS\")
42       (\".*\" . \"defaultinbox\"))
43
44   If none of the regular expressions match the From address, no
45   Fcc header will be added.
46
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).
50
51 You will be prompted to create the directory if it does not exist
52 yet when sending a mail."
53
54  :type '(choice
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
60  :group 'notmuch-send)
61
62 (defun notmuch-fcc-initialization ()
63   "If notmuch-fcc-directories is set,
64    hook them into the message-fcc-handler-function"
65     ;; Set up the message-fcc-handler to move mails to the maildir in Fcc
66     ;; The parameter is set to mark messages as "seen"
67     (setq message-fcc-handler-function
68           (lambda (destdir)
69             (notmuch-maildir-fcc-write-buffer-to-maildir destdir t)))
70     ;; add a hook to actually insert the Fcc header when sending
71     (add-hook 'message-header-setup-hook 'notmuch-fcc-header-setup))
72
73 (defun notmuch-fcc-header-setup ()
74   "Add an Fcc header to the current message buffer.
75
76 Can be added to `message-send-hook' and will set the Fcc header
77 based on the values of `notmuch-fcc-dirs'. An existing Fcc header
78 will NOT be removed or replaced."
79
80   (let ((subdir
81          (cond
82           ((or (not notmuch-fcc-dirs)
83                (message-field-value "Fcc"))
84            ;; Nothing set or an existing header.
85            nil)
86
87           ((stringp notmuch-fcc-dirs)
88            notmuch-fcc-dirs)
89
90           ((and (listp notmuch-fcc-dirs)
91                 (stringp (car notmuch-fcc-dirs)))
92            ;; Old style - no longer works.
93            (error "Invalid `notmuch-fcc-dirs' setting (old style)"))
94
95           ((listp notmuch-fcc-dirs)
96            (let* ((from (message-field-value "From"))
97                   (match
98                    (catch 'first-match
99                      (dolist (re-folder notmuch-fcc-dirs)
100                        (when (string-match-p (car re-folder) from)
101                          (throw 'first-match re-folder))))))
102              (if match
103                  (cdr match)
104                (message "No Fcc header added.")
105                nil)))
106
107           (t
108            (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
109
110     (when subdir
111       (message-add-header
112        (concat "Fcc: "
113                (file-truename
114                 ;; If the resulting directory is not an absolute path,
115                 ;; prepend the standard notmuch database path.
116                 (if (= (elt subdir 0) ?/)
117                     subdir
118                   (concat (notmuch-database-path) "/" subdir)))))
119       
120       ;; finally test if fcc points to a valid maildir
121       (let ((fcc-header (message-field-value "Fcc")))
122         (unless (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
123           (cond ((not (file-writable-p fcc-header))
124                  (error (format "No permission to create %s, which does not exist"
125                                 fcc-header)))
126                 ((y-or-n-p (format "%s is not a maildir. Create it? "
127                                    fcc-header))
128                  (notmuch-maildir-fcc-create-maildir fcc-header))
129                 (t
130                  (error "Message not sent"))))))))
131  
132 (defun notmuch-maildir-fcc-host-fixer (hostname)
133   (replace-regexp-in-string "/\\|:"
134                             (lambda (s)
135                               (cond ((string-equal s "/") "\\057")
136                                     ((string-equal s ":") "\\072")
137                                     (t s)))
138                             hostname
139                             t
140                             t))
141
142 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
143    (let* ((ftime (float-time))
144           (microseconds (mod (* 1000000 ftime) 1000000))
145           (hostname (notmuch-maildir-fcc-host-fixer system-name)))
146      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
147      (format "%d.%d_%d_%d.%s"
148              ftime
149              (emacs-pid)
150              microseconds
151              notmuch-maildir-fcc-count
152              hostname)))
153
154 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
155   (and (file-exists-p (concat dir "/cur/"))
156        (file-exists-p (concat dir "/new/"))
157        (file-exists-p (concat dir "/tmp/"))))
158
159 (defun notmuch-maildir-fcc-create-maildir (path)
160   (cond ((or (not (file-exists-p path)) (file-directory-p path))
161          (make-directory (concat path "/cur/") t)
162          (make-directory (concat path "/new/") t)
163          (make-directory (concat path "/tmp/") t))
164         ((file-regular-p path)
165          (error "%s is a file. Can't create maildir." path))
166         (t
167          (error "I don't know how to create a maildir here"))))
168
169 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
170   "Returns the msg id of the message written to the temp directory
171 if successful, nil if not."
172   (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
173     (while (file-exists-p (concat destdir "/tmp/" msg-id))
174       (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
175     (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
176            (write-file (concat destdir "/tmp/" msg-id))
177            msg-id)
178           (t
179            (error (format "Can't write to %s. Not a maildir."
180                           destdir))
181            nil))))
182
183 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
184   (add-name-to-file
185    (concat destdir "/tmp/" msg-id)
186    (concat destdir "/new/" msg-id ":2,")))
187
188 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
189   (add-name-to-file
190    (concat destdir "/tmp/" msg-id)
191    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
192
193 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
194   "Writes the current buffer to maildir destdir. If mark-seen is
195 non-nil, it will write it to cur/, and mark it as read. It should
196 return t if successful, and nil otherwise."
197   (let ((orig-buffer (buffer-name)))
198     (with-temp-buffer
199       (insert-buffer-substring orig-buffer)
200       (catch 'link-error
201         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
202           (when msg-id
203             (cond (mark-seen
204                    (condition-case err
205                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
206                      (file-already-exists
207                       (throw 'link-error nil))))
208                   (t
209                    (condition-case err
210                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
211                      (file-already-exists
212                       (throw 'link-error nil))))))
213           (delete-file (concat destdir "/tmp/" msg-id))))
214       t)))
215
216 (notmuch-fcc-initialization)
217 (provide 'notmuch-maildir-fcc)
218