]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
emacs: fcc should fail at the right time if it doesn't point to a maildir
[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  "If set to non-nil, this will cause message mode to file (fcc) your mail in the specified directory, depending on your From address.
25
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\"))
29
30  If you need a more fancy setup, where you want different Outboxes depending
31  on your From address, you use something like this:
32
33      (   (\"defaultinbox\")
34          (\"Sebastian Spaeth <Sebastian@SSpaeth.de>\" . \"privat\")
35          (\"Sebastian Spaeth <SSpaeth@ethz.ch>\" . \"uni\")
36      )
37
38  This will constructs a path, concatenating the content of the
39  variable 'message-directory' (a message mode variable
40  customizable via m-x
41  customize-variable<RET>message-directory<RET>) and the second
42  part in the alist."
43  :require 'notmuch-fcc-initialization
44  :group 'notmuch
45 )
46
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
54           '(lambda (destdir)
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
59     ; notmuch)
60     (add-hook 'message-send-hook 'notmuch-fcc-header-setup))))
61
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
67       matches."
68   ; only do something if notmuch-fcc-dirs is set
69   (if notmuch-fcc-dirs
70    (let ((subdir
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: " message-directory subdir)))
75      (unless (notmuch-maildir-fcc-dir-is-maildir-p 
76               (message-fetch-field "fcc"))
77        (error (format "%s is not a maildir." (message-fetch-field "fcc")))))))
78
79 (defun notmuch-maildir-fcc-host-fixer (hostname)
80   (replace-regexp-in-string "/\\|:"
81                             '(lambda (s)
82                                (cond ((string-equal s "/") "\\057")
83                                      ((string-equal s ":") "\\072")
84                                      (t s)))
85                             hostname
86                             t
87                             t))
88
89 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
90    (let* ((ct (current-time))
91           (timeid (+ (* (car ct) 65536) (cadr ct)))
92           (microseconds (car (cdr (cdr ct))))
93           (hostname (notmuch-maildir-fcc-host-fixer system-name)))
94      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
95      (format "%d.%d_%d_%d.%s"
96              timeid
97              (emacs-pid)
98              microseconds
99              notmuch-maildir-fcc-count
100              hostname)))
101
102 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
103   (and (file-exists-p (concat dir "/cur/"))
104        (file-exists-p (concat dir "/new/"))
105        (file-exists-p (concat dir "/tmp/"))))
106
107 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
108   "Returns the msg id of the message written to the temp directory
109 if successful, nil if not."
110   (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
111     (while (file-exists-p (concat destdir "/tmp/" msg-id))
112       (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
113     (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
114            (write-file (concat destdir "/tmp/" msg-id))
115            msg-id)
116           (t
117            (error (format "Can't write to %s. Not a maildir."
118                           destdir))
119            nil))))
120
121 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
122   (add-name-to-file
123    (concat destdir "/tmp/" msg-id)
124    (concat destdir "/new/" msg-id ":2,")))
125
126 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
127   (add-name-to-file
128    (concat destdir "/tmp/" msg-id)
129    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
130
131 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
132   "Writes the current buffer to maildir destdir. If mark-seen is
133 non-nil, it will write it to cur/, and mark it as read. It should
134 return t if successful, and nil otherwise."
135   (let ((orig-buffer (buffer-name)))
136     (with-temp-buffer
137       (insert-buffer-substring orig-buffer)
138       (catch 'link-error
139         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
140           (when msg-id
141             (cond (mark-seen
142                    (condition-case err
143                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
144                      (file-already-exists
145                       (throw 'link-error nil))))
146                   (t
147                    (condition-case err
148                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
149                      (file-already-exists
150                       (throw 'link-error nil))))))
151           (delete-file (concat destdir "/tmp/" msg-id))))
152       t)))
153
154 (notmuch-fcc-initialization)
155 (provide 'notmuch-maildir-fcc)