]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
emacs: make modifications to message Fcc vars buffer-local
[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-handler (destdir)
63   "Write buffer to `destdir', marking it as sent
64
65 Intended to be dynamically bound to `message-fcc-handler-function'"
66     (notmuch-maildir-fcc-write-buffer-to-maildir destdir t))
67
68 (defun notmuch-fcc-header-setup ()
69   "Add an Fcc header to the current message buffer.
70
71 Sets the Fcc header based on the values of `notmuch-fcc-dirs'.
72
73 Originally intended to be use a hook function, but now called directly
74 by notmuch-mua-mail"
75
76   (let ((subdir
77          (cond
78           ((or (not notmuch-fcc-dirs)
79                (message-field-value "Fcc"))
80            ;; Nothing set or an existing header.
81            nil)
82
83           ((stringp notmuch-fcc-dirs)
84            notmuch-fcc-dirs)
85
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)"))
90
91           ((listp notmuch-fcc-dirs)
92            (let* ((from (message-field-value "From"))
93                   (match
94                    (catch 'first-match
95                      (dolist (re-folder notmuch-fcc-dirs)
96                        (when (string-match-p (car re-folder) from)
97                          (throw 'first-match re-folder))))))
98              (if match
99                  (cdr match)
100                (message "No Fcc header added.")
101                nil)))
102
103           (t
104            (error "Invalid `notmuch-fcc-dirs' setting (neither string nor list)")))))
105
106     (when subdir
107       (message-add-header
108        (concat "Fcc: "
109                (file-truename
110                 ;; If the resulting directory is not an absolute path,
111                 ;; prepend the standard notmuch database path.
112                 (if (= (elt subdir 0) ?/)
113                     subdir
114                   (concat (notmuch-database-path) "/" subdir)))))
115       
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"
121                                 fcc-header)))
122                 ((y-or-n-p (format "%s is not a maildir. Create it? "
123                                    fcc-header))
124                  (notmuch-maildir-fcc-create-maildir fcc-header))
125                 (t
126                  (error "Message not sent"))))))))
127  
128 (defun notmuch-maildir-fcc-host-fixer (hostname)
129   (replace-regexp-in-string "/\\|:"
130                             (lambda (s)
131                               (cond ((string-equal s "/") "\\057")
132                                     ((string-equal s ":") "\\072")
133                                     (t s)))
134                             hostname
135                             t
136                             t))
137
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"
144              ftime
145              (emacs-pid)
146              microseconds
147              notmuch-maildir-fcc-count
148              hostname)))
149
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/"))))
154
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))
162         (t
163          (error "I don't know how to create a maildir here"))))
164
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))
173            msg-id)
174           (t
175            (error (format "Can't write to %s. Not a maildir."
176                           destdir))
177            nil))))
178
179 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
180   (add-name-to-file
181    (concat destdir "/tmp/" msg-id)
182    (concat destdir "/new/" msg-id ":2,")))
183
184 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
185   (add-name-to-file
186    (concat destdir "/tmp/" msg-id)
187    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
188
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)))
194     (with-temp-buffer
195       (insert-buffer-substring orig-buffer)
196       (catch 'link-error
197         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
198           (when msg-id
199             (cond (mark-seen
200                    (condition-case err
201                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
202                      (file-already-exists
203                       (throw 'link-error nil))))
204                   (t
205                    (condition-case err
206                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
207                      (file-already-exists
208                       (throw 'link-error nil))))))
209           (delete-file (concat destdir "/tmp/" msg-id))))
210       t)))
211
212 (provide 'notmuch-maildir-fcc)
213