]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
emacs: maildir-fcc: prepare for use of notmuch insert
[notmuch] / emacs / notmuch-maildir-fcc.el
1 ;;; notmuch-maildir-fcc.el ---
2
3 ;; This file is free software; you can redistribute it and/or modify
4 ;; it under the terms of the GNU General Public License as published
5 ;; by the Free Software Foundation; either version 2, or (at your
6 ;; option) any later version.
7
8 ;; This program is distributed in the hope that it will be useful,
9 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ;; GNU General Public License for more details.
12
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
15 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 ;; Boston, MA 02110-1301, USA.
17
18 ;;; Commentary:
19
20 ;; To use this as the fcc handler for message-mode,
21 ;; customize the notmuch-fcc-dirs variable
22
23 ;;; Code:
24
25 (eval-when-compile (require 'cl))
26 (require 'message)
27
28 (require 'notmuch-lib)
29
30 (defvar notmuch-maildir-fcc-count 0)
31
32 (defcustom notmuch-fcc-dirs "sent"
33  "Determines the maildir directory in which to save outgoing mail.
34
35 Three types of values are permitted:
36
37 - nil: no Fcc header is added,
38
39 - a string: the value of `notmuch-fcc-dirs' is the name of the
40   folder to use,
41
42 - a list: the folder is chosen based on the From address of the
43   current message using a list of regular expressions and
44   corresponding folders:
45
46      ((\"Sebastian@SSpaeth.de\" . \"privat\")
47       (\"spaetz@sspaeth.de\" . \"OUTBOX.OSS\")
48       (\".*\" . \"defaultinbox\"))
49
50   If none of the regular expressions match the From address, no
51   Fcc header will be added.
52
53 In all cases, a relative FCC directory will be understood to
54 specify a directory within the notmuch mail store, (as set by
55 the database.path option in the notmuch configuration file).
56
57 You will be prompted to create the directory if it does not exist
58 yet when sending a mail."
59
60  :type '(choice
61          (const :tag "No FCC header" nil)
62          (string :tag "A single folder")
63          (repeat :tag "A folder based on the From header"
64                  (cons regexp (string :tag "Folder"))))
65  :require 'notmuch-fcc-initialization
66  :group 'notmuch-send)
67
68
69 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 ;; Functions which set up the fcc header in the message buffer.
71
72 (defun notmuch-fcc-header-setup ()
73   "Add an Fcc header to the current message buffer.
74
75 Sets the Fcc header based on the values of `notmuch-fcc-dirs'.
76
77 Originally intended to be use a hook function, but now called directly
78 by notmuch-mua-mail"
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       (notmuch-maildir-add-file-style-fcc-header subdir))))
112
113 (defun notmuch-maildir-add-file-style-fcc-header (subdir)
114   (message-add-header
115    (concat "Fcc: "
116            (file-truename
117             ;; If the resulting directory is not an absolute path,
118             ;; prepend the standard notmuch database path.
119             (if (= (elt subdir 0) ?/)
120                 subdir
121               (concat (notmuch-database-path) "/" subdir))))))
122
123 (defun notmuch-fcc-handler (fcc-header)
124   "Store message with file fcc."
125   (notmuch-maildir-fcc-file-fcc fcc-header))
126
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 ;; Functions for saving a message using file fcc.
129
130 (defun notmuch-maildir-fcc-host-fixer (hostname)
131   (replace-regexp-in-string "/\\|:"
132                             (lambda (s)
133                               (cond ((string-equal s "/") "\\057")
134                                     ((string-equal s ":") "\\072")
135                                     (t s)))
136                             hostname
137                             t
138                             t))
139
140 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
141    (let* ((ftime (float-time))
142           (microseconds (mod (* 1000000 ftime) 1000000))
143           (hostname (notmuch-maildir-fcc-host-fixer system-name)))
144      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
145      (format "%d.%d_%d_%d.%s"
146              ftime
147              (emacs-pid)
148              microseconds
149              notmuch-maildir-fcc-count
150              hostname)))
151
152 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
153   (and (file-exists-p (concat dir "/cur/"))
154        (file-exists-p (concat dir "/new/"))
155        (file-exists-p (concat dir "/tmp/"))))
156
157 (defun notmuch-maildir-fcc-create-maildir (path)
158   (cond ((or (not (file-exists-p path)) (file-directory-p path))
159          (make-directory (concat path "/cur/") t)
160          (make-directory (concat path "/new/") t)
161          (make-directory (concat path "/tmp/") t))
162         ((file-regular-p path)
163          (error "%s is a file. Can't create maildir." path))
164         (t
165          (error "I don't know how to create a maildir here"))))
166
167 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
168   "Returns the msg id of the message written to the temp directory
169 if successful, nil if not."
170   (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
171     (while (file-exists-p (concat destdir "/tmp/" msg-id))
172       (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
173     (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
174            (write-file (concat destdir "/tmp/" msg-id))
175            msg-id)
176           (t
177            (error (format "Can't write to %s. Not a maildir."
178                           destdir))
179            nil))))
180
181 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
182   (add-name-to-file
183    (concat destdir "/tmp/" msg-id)
184    (concat destdir "/new/" msg-id ":2,")))
185
186 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
187   (add-name-to-file
188    (concat destdir "/tmp/" msg-id)
189    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
190
191 (defun notmuch-maildir-fcc-file-fcc (fcc-header)
192   "Write the message to the file specified by FCC-HEADER.
193
194 It offers the user a chance to correct the header, or filesystem,
195 if needed."
196   (if (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
197       (notmuch-maildir-fcc-write-buffer-to-maildir fcc-header 't)
198     ;; The fcc-header is not a valid maildir see if the user wants to
199     ;; fix it in some way.
200     (let* ((prompt (format "Fcc %s is not a maildir: (r)etry, (c)reate folder, (i)gnore, or  (e)dit the header? "
201                            fcc-header))
202             (response (read-char-choice prompt '(?r ?c ?i ?e))))
203          (case response
204                (?r (notmuch-maildir-fcc-file-fcc fcc-header))
205                (?c (if (file-writable-p fcc-header)
206                        (notmuch-maildir-fcc-create-maildir fcc-header)
207                      (message "No permission to create %s." fcc-header)
208                      (sit-for 2))
209                    (notmuch-maildir-fcc-file-fcc fcc-header))
210                (?i 't)
211                (?e (notmuch-maildir-fcc-file-fcc
212                     (read-from-minibuffer "Fcc header: " fcc-header)))))))
213
214 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
215   "Writes the current buffer to maildir destdir. If mark-seen is
216 non-nil, it will write it to cur/, and mark it as read. It should
217 return t if successful, and nil otherwise."
218   (let ((orig-buffer (buffer-name)))
219     (with-temp-buffer
220       (insert-buffer-substring orig-buffer)
221       (catch 'link-error
222         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
223           (when msg-id
224             (cond (mark-seen
225                    (condition-case err
226                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
227                      (file-already-exists
228                       (throw 'link-error nil))))
229                   (t
230                    (condition-case err
231                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
232                      (file-already-exists
233                       (throw 'link-error nil))))))
234           (delete-file (concat destdir "/tmp/" msg-id))))
235       t)))
236
237 (provide 'notmuch-maildir-fcc)
238
239 ;;; notmuch-maildir-fcc.el ends here