]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
Easier way to define a fcc directory
[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 (require 'message)
20
21 (defvar notmuch-maildir-fcc-count 0)
22
23 (defcustom notmuch-fcc-dirs nil
24  "Determines the maildir directory to save outgoing mails in.
25
26  If set to non-nil, this will cause message mode to file your
27  mail in the specified directory (fcc).
28
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).
31
32  In the former case it is a string such as \"INBOX.Sent\".
33
34  In the fancy setup, where you want different outboxes depending
35  on your From address, you supply a list like this:
36
37      ((\"defaultinbox\")
38       (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
39       (\"Sebastian Spaeth <spaetz@sspaeth.de>\" . \"OUTBOX.OSS\")
40      )
41
42  The outbox that matches a key (case insensitive) will be
43  used. The first entry is used as a default fallback when nothing
44  else matches.
45
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.
50
51  You will be prompted to create the directory if it does not exist yet when 
52  sending a mail.
53
54  This function will not modify the headers if there is a FCC
55  header, but will check that the target directory exists."
56
57  :require 'notmuch-fcc-initialization
58  :group 'notmuch
59 )
60
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
67           '(lambda (destdir)
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))
71
72 (defun notmuch-fcc-header-setup ()
73   "Adds an appropriate fcc header to the current mail buffer
74
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
81     (let (subdir)
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)))))
89
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)
94                                 subdir)))
95
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? "
102                                fcc-header))
103              (notmuch-maildir-fcc-create-maildir fcc-header))
104             (t
105              (error "Not sending message."))))))))
106  
107 (defun notmuch-maildir-fcc-host-fixer (hostname)
108   (replace-regexp-in-string "/\\|:"
109                             '(lambda (s)
110                                (cond ((string-equal s "/") "\\057")
111                                      ((string-equal s ":") "\\072")
112                                      (t s)))
113                             hostname
114                             t
115                             t))
116
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"
124              timeid
125              (emacs-pid)
126              microseconds
127              notmuch-maildir-fcc-count
128              hostname)))
129
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/"))))
134
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))
142         (t
143          (error "I don't know how to create a maildir here"))))
144
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))
153            msg-id)
154           (t
155            (error (format "Can't write to %s. Not a maildir."
156                           destdir))
157            nil))))
158
159 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
160   (add-name-to-file
161    (concat destdir "/tmp/" msg-id)
162    (concat destdir "/new/" msg-id ":2,")))
163
164 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
165   (add-name-to-file
166    (concat destdir "/tmp/" msg-id)
167    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
168
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)))
174     (with-temp-buffer
175       (insert-buffer-substring orig-buffer)
176       (catch 'link-error
177         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
178           (when msg-id
179             (cond (mark-seen
180                    (condition-case err
181                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
182                      (file-already-exists
183                       (throw 'link-error nil))))
184                   (t
185                    (condition-case err
186                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
187                      (file-already-exists
188                       (throw 'link-error nil))))))
189           (delete-file (concat destdir "/tmp/" msg-id))))
190       t)))
191
192 (notmuch-fcc-initialization)
193 (provide 'notmuch-maildir-fcc)
194