forked from bennyandresen/mu4e-send-delay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmu4e-send-delay.el
351 lines (302 loc) · 14.2 KB
/
mu4e-send-delay.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
;;; mu4e-send-delay.el --- Delay sending of mails in mu4e -*- lexical-binding: t -*-
;; Copyright (C) 2016-2017 Benjamin Andresen <[email protected]>
;; Author: Benjamin Andresen <[email protected]>
;; Maintainer: Benjamin Andresen <[email protected]>
;; Version: 20170610.0636
;; URL: https://github.com/jleechpe/outorg-export
;; Package-Requires: ((emacs "26.1"))
;; This file is not part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Updated, properly formatted, and cleaned up version of Benjamin Andresen's
;; original version.
;;; Code:
(require 'cl-lib)
(require 'gnus-util)
(autoload 'parse-time-string "parse-time" nil nil)
(require 'mu4e-view)
(require 'mu4e-compose)
;; (require 'mu4e-draft)
(declare-function org-msg-edit-mode "org-msg" ())
;;;; Custom options
(defgroup mu4e-send-delay nil
"Customization for delayed sending of messages."
:group 'mu4e)
(defcustom mu4e-send-delay-header "X-Delay"
"Header name for storing info about delayed mails."
:type 'string
:group 'mu4e-delay)
(defcustom mu4e-send-delay-strip-header-before-send t
"If non-nil, remove `mu4e-send-delay-header' before sending mail."
:type 'boolean
:group 'mu4e-delay)
(defcustom mu4e-send-delay-include-header-in-draft t
"Whether to include the delay header when starting to draft a message.
If nil, add delay header only when sending the message."
:type 'boolean
:group 'mu4e-delay)
(defcustom mu4e-send-delay-default-delay "10m"
"Default length of delay."
:type 'string
:group 'mu4e-delay)
(defcustom mu4e-send-delay-default-hour "7"
"Default length of delay."
:type 'string
:group 'mu4e-delay)
(defcustom mu4e-send-delay-timer 120
"Number of seconds between checks for delayed mail to send."
:type 'integer
:group 'mu4e-delay)
(defcustom mu4e-send-delay-enable-org-msg nil
"Whether the user wants to enable compatibility with `org-msg'.
Toggle `mu4e-send-delay-setup' if you change the value of this
variable after `mu4e-send-delay-setup' has already been called"
:type 'boolean
:group 'mu4e-delay)
;;;; Functions
;;;;; Time strings and parsing
;; Copied from mu4e-delay which is from gnus-delay
(defun mu4e-send-delay-parse-delay-header-string (delay)
"Return due date for email delayed by or to DELAY.
DELAY is a string, giving the length of the time. Possible values
are:
,* <digits><units> for <units> in minutes (`m'), hours (`h'),
days (`d'), weeks (`w'), months (`M'), or years (`Y');
,* YYYY-MM-DD for a specific date. The time of day is given by
the variable `mu4e-send-delay-default-hour', with the minutes
and seconds value set to zero.
,* hh:mm for a specific time. Use 24h format. If it is later
than this time, then the deadline is tomorrow, else today."
(interactive
(list (read-string
"Target date (YYYY-MM-DD), time (hh:mm), or length of delay (units in [mhdwMY]): "
mu4e-send-delay-default-delay)))
;; Allow spell checking etc.
(let (num unit days year month day hour minute deadline)
(cond ((string-match
"\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
delay)
(setq year (string-to-number (match-string 1 delay))
month (string-to-number (match-string 2 delay))
day (string-to-number (match-string 3 delay)))
(setq deadline
(message-make-date
(encode-time 0 0 ; second and minute
mu4e-send-delay-default-hour
day month year))))
((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
(setq hour (string-to-number (match-string 1 delay))
minute (string-to-number (match-string 2 delay)))
;; Use current time, except...
(setq deadline (apply 'vector (decode-time (current-time))))
;; ... for minute and hour.
(aset deadline 1 minute)
(aset deadline 2 hour)
;; Convert to seconds.
(setq deadline (float-time (apply 'encode-time
(append deadline nil))))
;; If this time has passed already, add a day.
(when (< deadline (float-time))
(setq deadline (+ 86400 deadline))) ; 86400 secs/day
;; Convert seconds to date header.
(setq deadline (message-make-date
(seconds-to-time deadline))))
((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
(setq num (match-string 1 delay))
(setq unit (match-string 2 delay))
;; Start from seconds, then multiply into needed units.
(setq num (string-to-number num))
(cond ((string= unit "Y")
(setq delay (* num 60 60 24 365)))
((string= unit "M")
(setq delay (* num 60 60 24 30)))
((string= unit "w")
(setq delay (* num 60 60 24 7)))
((string= unit "d")
(setq delay (* num 60 60 24)))
((string= unit "h")
(setq delay (* num 60 60)))
(t
(setq delay (* num 60))))
(setq deadline (message-make-date
(seconds-to-time (+ (float-time) delay)))))
(t (error "Malformed delay `%s'" delay)))
deadline))
(defun mu4e-send-delay-return-delay-header-value (file-path)
"Return delay header value for email at FILE-PATH."
(if (file-exists-p file-path)
(let (found-value parsed-value)
(with-temp-buffer
(insert-file-contents file-path)
(setq found-value (message-fetch-field mu4e-send-delay-header))
(when found-value
(setq parsed-value (mu4e-send-delay-parse-delay-header-string found-value))
;; Make sure found-value is a parsed time string (a little janky,
;; but there is no real-world use case this would fail)
(when (eq (length found-value) (length parsed-value))
found-value))))
(user-error "[mu4e-send-delay-return-delay-header-value] %s does not exist" file-path)))
(defun mu4e-send-delay-elapsed-p (file-path)
"Return non-nil if the time string in email at FILE-PATH has passed."
(when-let* ((header-value (mu4e-send-delay-return-delay-header-value file-path))
(parsed-ts (parse-time-string header-value)))
(unless (cl-every #'null parsed-ts)
(let* ((delay-time (encode-time parsed-ts))
(time-passed (time-since delay-time)))
(and (>= (nth 0 time-passed) 0)
(>= (nth 1 time-passed) 0))))))
;;;;; Scheduling during email composition
(defun mu4e-send-delay-schedule-and-exit ()
"Schedule send email and exit current email composition buffer."
(condition-case err
(let* ((schedule-time
(mu4e-send-delay-parse-delay-header-string (or (message-fetch-field mu4e-send-delay-header) mu4e-send-delay-default-delay))))
;; Replace delay header value with a time string
(message-remove-header mu4e-send-delay-header nil)
(message-add-header (format "%s: %s" mu4e-send-delay-header schedule-time))
(when (buffer-file-name) (mu4e--set-parent-flags (buffer-file-name))) ; Set reply/forward flag
(message-dont-send)
(when message-kill-buffer-on-exit (kill-buffer (current-buffer)))
(mu4e-message "Mail scheduled to send at %s" schedule-time))
(error (princ (format "mu4e-send-delay: %s" err)))))
(defun mu4e-send-delay-send-and-exit (&optional delay)
"Send this email.
If DELAY, then delay sending this email."
(interactive "P")
(run-hooks 'message-send-hook)
(if delay
(mu4e-send-delay-schedule-and-exit)
(when mu4e-send-delay-strip-header-before-send
(message-remove-header mu4e-send-delay-header nil))
(message-send-and-exit)))
;;;;; Sending
(defmacro mu4e-send-delay-with-mu4e-context (context &rest body)
"Evaluate BODY under CONTEXT.
Sets `mu4e--context-current' to CONTEXT and evaluates with
`with-mu4e-context-vars'."
(declare (indent 2))
`(let* ((mu4e--context-current ,context))
(with-mu4e-context-vars ,context
,@body)))
(defun mu4e-send-delay-move-or-delete-draft (file-path)
"Move mail at FILE-PATH appropriately or delete stored draft.
Be aware that `mu4e-sent-messages-behavior' should be set to
`trash' or `delete' if using GMail, since GMail automatically
sends copies to the sent folder, meaning a value of `set' will
lead to duplicate emails that you will have to manually remove."
;; NOTE 2023-07-08: The FCC header is only added by mu4e for the values of
;; `trash' and `send'. See `mu4e~compose-setup-fcc-maybe'
(pcase mu4e-sent-messages-behavior
((or 'sent 'trash)
;; REVIEW 2023-07-08: Not sure if this is the correct behavior, since I
;; don't use `trash' or `sent', so I haven't tested it
(with-temp-buffer
(insert-file-contents file-path)
(when-let ((file (message-fetch-field "fcc")))
(message-remove-header "fcc" nil)
(message-remove-header mu4e-send-delay-header nil)
(write-file file)
(set-buffer-modified-p nil))
(delete-file file-path)))
('delete (delete-file file-path))))
(defun mu4e-send-delay-send-if-due (file-path)
"Send mail at FILE-PATH if it should be sent and is not currently open."
(when (and (mu4e-send-delay-elapsed-p file-path)
(not (get-file-buffer file-path))) ; Not opened
(condition-case err
(progn
(with-current-buffer (find-file-noselect file-path)
;; Force recode to fix character encoding issue
(set-buffer-file-coding-system 'utf-8 t)
(recode-region (point-min) (point-max) 'prefer-utf-8 'utf-8-unix)
(insert mu4e--header-separator)
(message-mode)
(when mu4e-send-delay-strip-header-before-send
(message-remove-header mu4e-send-delay-header nil))
(message-send))
;; Then deal with the original file (by moving it to the appropriate
;; folder or deleting it)
(mu4e-send-delay-move-or-delete-draft file-path)
t)
(error "mu4e-delay-send: %s" err))))
(defun mu4e-send-delay-send-due ()
"Send all delayed mail drafts that are due."
(interactive)
(when (mu4e-root-maildir)
(let* ((dirs (if mu4e-contexts
(mapcar (lambda (context)
(mu4e-send-delay-with-mu4e-context context
(expand-file-name "cur" (concat (mu4e-root-maildir) (mu4e-get-drafts-folder)))))
mu4e-contexts)
(list (expand-file-name "cur" (concat (mu4e-root-maildir) (mu4e-get-drafts-folder)))))))
(when (memq t
(mapcar (lambda (dir)
(cl-loop for file in (directory-files dir t "^[^\.]")
collect (mu4e-send-delay-send-if-due file)))
dirs))
;; Only update index if something was done
(mu4e-update-index)))))
;;;;; Timer
(defvar mu4e-send-delay-send-due-timer nil
"Timer to run `mu4e-send-delay-send-due'.")
(defun mu4e-send-delay-initialize-send-queue-timer ()
"Set up `mu4e-send-delay-send-due' to run on a timer."
(interactive)
(unless mu4e-send-delay-send-due-timer
(setq mu4e-send-delay-send-due-timer
(run-with-timer 0 mu4e-send-delay-timer 'mu4e-send-delay-send-due))))
;;;; org-msg advice
(defun mu4e-send-delay-org-msg-ctrl-c-ctrl-c ()
"Send message like `message-send-and-exit'.
Identical to the original `org-msg-ctrl-c-ctrl-c', but calls
`mu4e-send-delay-send-and-exit' instead as well as passes ARG to
it."
(when (eq major-mode 'org-msg-edit-mode)
(org-msg-sanity-check)
(if current-prefix-arg
(org-msg-mua-call 'send 'mu4e-send-delay-send-and-exit current-prefix-arg)
(org-msg-mua-call 'send-and-exit 'mu4e-send-delay-send-and-exit current-prefix-arg))))
;;;; Setup
(defun mu4e-send-delay-setup ()
"Run this command to set up `mu4e-send-delay'.
Make sure delay header is added when composing emails. Advise
`mu4e~draft-common-construct' since it is used by mu4e to insert
headers across all mu4e's email composition buffers.
Running this command more than once will advise
`mu4e~draft-common-construct' multiple times, leading to multiple
delay headers being inserted upon composition (which can be
manually removed afterward).
Also show delay info in `mu4e-headers-mode' and `mu4e-view-mode'."
(interactive)
(mu4e-send-delay-initialize-send-queue-timer)
(advice-add 'mu4e~draft-common-construct :around
#'(lambda (orig-fun &rest args)
(concat
(apply orig-fun args)
(when mu4e-send-delay-include-header-in-draft
(mu4e~draft-header mu4e-send-delay-header mu4e-send-delay-default-delay)))))
(add-to-list 'mu4e-header-info-custom
'(:send-delay . (:name "X-Delay"
:shortname "Delay"
:help "Date/time when mail is scheduled for sending"
:function (lambda (msg)
(or
(mu4e-send-delay-return-delay-header-value
(mu4e-message-field msg :path))
"")))))
(add-to-list 'mu4e-view-fields :send-delay t)
(if mu4e-send-delay-enable-org-msg
(advice-add 'org-msg-ctrl-c-ctrl-c :override #'mu4e-send-delay-org-msg-ctrl-c-ctrl-c)
(advice-remove 'org-msg-ctrl-c-ctrl-c :override #'mu4e-send-delay-org-msg-ctrl-c-ctrl-c)))
(provide 'mu4e-send-delay)
;;; mu4e-send-delay.el ends here