]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-maildir-fcc.el
emacs: simplify our local copy of message-do-fcc
[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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;; Functions for saving a message either using notmuch insert or file
125 ;; fcc. First functions common to the two cases.
126
127 (defun notmuch-maildir-message-do-fcc ()
128   "Process Fcc headers in the current buffer.
129
130 This is a rearranged version of message mode's message-do-fcc."
131   (let ((case-fold-search t)
132         (buf (current-buffer))
133         list file
134         (mml-externalize-attachments message-fcc-externalize-attachments))
135     (save-excursion
136       (save-restriction
137         (message-narrow-to-headers)
138         (setq file (message-fetch-field "fcc" t)))
139       (when file
140         (set-buffer (get-buffer-create " *message temp*"))
141         (erase-buffer)
142         (insert-buffer-substring buf)
143         (message-encode-message-body)
144         (save-restriction
145           (message-narrow-to-headers)
146           (while (setq file (message-fetch-field "fcc" t))
147             (push file list)
148             (message-remove-header "fcc" nil t))
149           (let ((mail-parse-charset message-default-charset))
150             (mail-encode-encoded-word-buffer)))
151         (goto-char (point-min))
152         (when (re-search-forward
153                (concat "^" (regexp-quote mail-header-separator) "$")
154                nil t)
155           (replace-match "" t t ))
156         ;; Process FCC operations.
157         (while list
158           (setq file (pop list))
159           (notmuch-fcc-handler file))
160         (kill-buffer (current-buffer))))))
161
162 (defun notmuch-fcc-handler (fcc-header)
163   "Store message with file fcc."
164   (notmuch-maildir-fcc-file-fcc fcc-header))
165
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;; Functions for saving a message using file fcc.
168
169 (defun notmuch-maildir-fcc-host-fixer (hostname)
170   (replace-regexp-in-string "/\\|:"
171                             (lambda (s)
172                               (cond ((string-equal s "/") "\\057")
173                                     ((string-equal s ":") "\\072")
174                                     (t s)))
175                             hostname
176                             t
177                             t))
178
179 (defun notmuch-maildir-fcc-make-uniq-maildir-id ()
180    (let* ((ftime (float-time))
181           (microseconds (mod (* 1000000 ftime) 1000000))
182           (hostname (notmuch-maildir-fcc-host-fixer system-name)))
183      (setq notmuch-maildir-fcc-count (+ notmuch-maildir-fcc-count 1))
184      (format "%d.%d_%d_%d.%s"
185              ftime
186              (emacs-pid)
187              microseconds
188              notmuch-maildir-fcc-count
189              hostname)))
190
191 (defun notmuch-maildir-fcc-dir-is-maildir-p (dir)
192   (and (file-exists-p (concat dir "/cur/"))
193        (file-exists-p (concat dir "/new/"))
194        (file-exists-p (concat dir "/tmp/"))))
195
196 (defun notmuch-maildir-fcc-create-maildir (path)
197   (cond ((or (not (file-exists-p path)) (file-directory-p path))
198          (make-directory (concat path "/cur/") t)
199          (make-directory (concat path "/new/") t)
200          (make-directory (concat path "/tmp/") t))
201         ((file-regular-p path)
202          (error "%s is a file. Can't create maildir." path))
203         (t
204          (error "I don't know how to create a maildir here"))))
205
206 (defun notmuch-maildir-fcc-save-buffer-to-tmp (destdir)
207   "Returns the msg id of the message written to the temp directory
208 if successful, nil if not."
209   (let ((msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
210     (while (file-exists-p (concat destdir "/tmp/" msg-id))
211       (setq msg-id (notmuch-maildir-fcc-make-uniq-maildir-id)))
212     (cond ((notmuch-maildir-fcc-dir-is-maildir-p destdir)
213            (write-file (concat destdir "/tmp/" msg-id))
214            msg-id)
215           (t
216            (error (format "Can't write to %s. Not a maildir."
217                           destdir))
218            nil))))
219
220 (defun notmuch-maildir-fcc-move-tmp-to-new (destdir msg-id)
221   (add-name-to-file
222    (concat destdir "/tmp/" msg-id)
223    (concat destdir "/new/" msg-id ":2,")))
224
225 (defun notmuch-maildir-fcc-move-tmp-to-cur (destdir msg-id &optional mark-seen)
226   (add-name-to-file
227    (concat destdir "/tmp/" msg-id)
228    (concat destdir "/cur/" msg-id ":2," (when mark-seen "S"))))
229
230 (defun notmuch-maildir-fcc-file-fcc (fcc-header)
231   "Write the message to the file specified by FCC-HEADER.
232
233 It offers the user a chance to correct the header, or filesystem,
234 if needed."
235   (if (notmuch-maildir-fcc-dir-is-maildir-p fcc-header)
236       (notmuch-maildir-fcc-write-buffer-to-maildir fcc-header 't)
237     ;; The fcc-header is not a valid maildir see if the user wants to
238     ;; fix it in some way.
239     (let* ((prompt (format "Fcc %s is not a maildir: (r)etry, (c)reate folder, (i)gnore, or  (e)dit the header? "
240                            fcc-header))
241             (response (read-char-choice prompt '(?r ?c ?i ?e))))
242          (case response
243                (?r (notmuch-maildir-fcc-file-fcc fcc-header))
244                (?c (if (file-writable-p fcc-header)
245                        (notmuch-maildir-fcc-create-maildir fcc-header)
246                      (message "No permission to create %s." fcc-header)
247                      (sit-for 2))
248                    (notmuch-maildir-fcc-file-fcc fcc-header))
249                (?i 't)
250                (?e (notmuch-maildir-fcc-file-fcc
251                     (read-from-minibuffer "Fcc header: " fcc-header)))))))
252
253 (defun notmuch-maildir-fcc-write-buffer-to-maildir (destdir &optional mark-seen)
254   "Writes the current buffer to maildir destdir. If mark-seen is
255 non-nil, it will write it to cur/, and mark it as read. It should
256 return t if successful, and nil otherwise."
257   (let ((orig-buffer (buffer-name)))
258     (with-temp-buffer
259       (insert-buffer-substring orig-buffer)
260       (catch 'link-error
261         (let ((msg-id (notmuch-maildir-fcc-save-buffer-to-tmp destdir)))
262           (when msg-id
263             (cond (mark-seen
264                    (condition-case err
265                        (notmuch-maildir-fcc-move-tmp-to-cur destdir msg-id t)
266                      (file-already-exists
267                       (throw 'link-error nil))))
268                   (t
269                    (condition-case err
270                        (notmuch-maildir-fcc-move-tmp-to-new destdir msg-id)
271                      (file-already-exists
272                       (throw 'link-error nil))))))
273           (delete-file (concat destdir "/tmp/" msg-id))))
274       t)))
275
276 (provide 'notmuch-maildir-fcc)
277
278 ;;; notmuch-maildir-fcc.el ends here