-
Notifications
You must be signed in to change notification settings - Fork 4
/
himalaya-message.el
366 lines (327 loc) · 12.5 KB
/
himalaya-message.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
352
353
354
355
356
357
358
359
360
361
362
363
364
;;; himalaya-message.el --- Message management of email client Himalaya CLI -*- lexical-binding: t -*-
;; Copyright (C) 2021 Dante Catalfamo
;; Copyright (C) 2022-2024 soywod <[email protected]>
;; Author: Dante Catalfamo
;; soywod <[email protected]>
;; Maintainer: soywod <[email protected]>
;; Dante Catalfamo
;; Version: 1.0
;; Package-Requires: ((emacs "27.1"))
;; URL: https://github.com/dantecatalfamo/himalaya-emacs
;; Keywords: mail comm
;; This file is not part of GNU Emacs
;; This program 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.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Interface for the email client Himalaya CLI
;; <https://github.com/soywod/himalaya>
;;; Code:
(require 'subr-x)
(require 'mailheader)
(require 'message)
(require 'himalaya-process)
(require 'himalaya-account)
(require 'himalaya-folder)
(require 'himalaya-envelope-mark)
(require 'himalaya-flag)
(require 'himalaya-attachment)
(require 'himalaya-template)
(defun himalaya--extract-headers (msg)
"Extract MESSAGE headers."
(with-temp-buffer
(insert msg)
(goto-char (point-min))
(mail-header-extract-no-properties)))
(defun himalaya--generate-write-buffer (buffer-name tpl)
"Setup to be used to write a message."
(switch-to-buffer (generate-new-buffer buffer-name))
(insert (plist-get tpl :content))
(goto-line (plist-get (plist-get tpl :cursor) :row))
(move-to-column (plist-get (plist-get tpl :cursor) :col))
(himalaya-message-write-mode)
(set-buffer-modified-p nil))
(defun himalaya--read-message (id preview callback)
"Return the contents of message matching the envelope ID from
current folder on current account. If RAW is non-nil, return the raw
contents of the message including headers."
(message "Reading message %s…" id)
(himalaya--run
(lambda (msg) (funcall callback (replace-regexp-in-string "" "" msg)))
nil
"message"
"read"
(when himalaya-account (list "--account" himalaya-account))
(when himalaya-folder (list "--folder" himalaya-folder))
(when preview "--preview")
(format "%s" id))) ; force id as a string
(defun himalaya--read-message-raw (id callback)
"Return the contents of message matching the envelope ID from
current folder on current account. If RAW is non-nil, return the raw
contents of the message including headers."
(message "Reading raw message %s…" id)
(himalaya--run-plain
(lambda (msg) (funcall callback (replace-regexp-in-string "" "" msg)))
nil
"message"
"export"
(when himalaya-account (list "--account" himalaya-account))
(when himalaya-folder (list "--folder" himalaya-folder))
"--full"
(format "%s" id))) ; force id as a string
(defun himalaya--copy-messages (ids folder callback)
"Copy message(s) matching envelope IDS from current folder of
current account to target FOLDER."
(message "Copying message(s) to %s…" folder)
(himalaya--run
callback
nil
"message"
"copy"
(when himalaya-account (list "--account" himalaya-account))
(when himalaya-folder (list "--folder" himalaya-folder))
folder
ids))
(defun himalaya--move-messages (ids folder callback)
"Move message(s) matching envelope IDS from current folder of
current account to target FOLDER."
(message "Moving message(s) to %s…" folder)
(himalaya--run
callback
nil
"message"
"move"
(when himalaya-account (list "--account" himalaya-account))
(when himalaya-folder (list "--folder" himalaya-folder))
folder
ids))
(defun himalaya--delete-messages (ids callback)
"Delete message(s) matching envelope IDS from current folder of
current account."
(message "Deleting message(s)…")
(himalaya--run
callback
nil
"message"
"delete"
(when himalaya-account (list "--account" himalaya-account))
(when himalaya-folder (list "--folder" himalaya-folder))
ids))
(defun himalaya--read-current-message (&optional preview pre-hook)
"Read message matching current envelope id in current folder from
current account."
(himalaya--read-message
himalaya-id
preview
(lambda (msg)
(when pre-hook (funcall pre-hook))
(let* ((headers (himalaya--extract-headers msg))
(subject (alist-get 'subject headers)))
(switch-to-buffer (format "*%s*" subject))
(erase-buffer)
(insert msg)
(set-buffer-modified-p nil)
(himalaya-read-message-mode)
(goto-char (point-min))
(setq buffer-read-only t)
(setq himalaya-subject subject)))))
(defun himalaya--read-current-message-raw (&optional pre-hook)
"Read raw message matching current envelope id in current folder
from current account."
(himalaya--read-message-raw
himalaya-id
(lambda (msg)
(when pre-hook (funcall pre-hook))
(let* ((headers (himalaya--extract-headers msg))
(subject (alist-get 'subject headers)))
(switch-to-buffer (format "*Raw: %s*" subject))
(let ((inhibit-read-only t))
(erase-buffer)
(insert msg)
(set-buffer-modified-p nil)
(himalaya-read-message-raw-mode)
(goto-char (point-min))
(setq himalaya-subject subject))))))
(defun himalaya-read-current-message-plain (&optional preview)
"Read message matching current envelope id in current folder from
current account. If called with \\[universal-argument], enable
PREVIEW, which means that the flag Seen will not be applied to its
envelope."
(interactive "P")
(himalaya--read-current-message preview #'kill-current-buffer))
(defun himalaya-read-current-message-raw ()
"Read raw message matching current envelope id in current folder
from current account."
(interactive)
(himalaya--read-current-message-raw #'kill-current-buffer))
(defun himalaya-reply-to-current-message (&optional reply-all)
"Open a new buffer with a reply template to the current email.
If called with \\[universal-argument], email will be REPLY-ALL."
(interactive "P")
(himalaya--reply-template
himalaya-id
(lambda (tpl)
(setq himalaya-reply t)
(himalaya--generate-write-buffer (format "*Reply: %s*" himalaya-subject) tpl))
reply-all))
(defun himalaya-forward-current-message ()
"Open a new buffer with a forward template to the current email."
(interactive)
(himalaya--forward-template
himalaya-id
(lambda (tpl)
(setq himalaya-reply nil)
(himalaya--generate-write-buffer (format "*Forward: %s*" himalaya-subject) tpl))))
(defun himalaya-next-message ()
"Go to the next email."
(interactive)
(setq himalaya-id (prin1-to-string (1+ (string-to-number himalaya-id))))
(condition-case
nil (himalaya--read-current-message)
(t (user-error "At end of folder"))))
(defun himalaya-prev-message ()
"Go to the previous message."
(interactive)
(when (string= himalaya-id "1")
(user-error "At beginning of folder"))
(setq himalaya-id (prin1-to-string (max 1 (1- (string-to-number himalaya-id)))))
(himalaya--read-current-message))
(defun himalaya-read-message-at-point (&optional preview)
"Pick the envelope at point and read its associated message. If
called with \\[universal-argument], enable PREVIEW, which means that
the flag Seen will not be applied to its envelope."
(interactive "P")
(setq himalaya-id (tabulated-list-get-id))
(himalaya--read-current-message preview))
(defun himalaya-write-new-message ()
"Compose a new message in a buffer."
(interactive)
(himalaya--write-template
(lambda (tpl)
(setq himalaya-reply nil)
(himalaya--generate-write-buffer "*Himalaya New Message*" tpl))))
(defun himalaya-reply-to-message-at-point (&optional reply-all)
"Pick the envelope at point then reply to its associated message.
If called with \\[universal-argument], message will be REPLY-ALL."
(interactive "P")
(let* ((id (tabulated-list-get-id))
(subject (substring-no-properties (elt (tabulated-list-get-entry) 2))))
(setq himalaya-id id)
(setq himalaya-subject subject)
(himalaya-read-message-reply reply-all)))
(defun himalaya-forward-message-at-point ()
"Pick the envelope at point then forward its associated message."
(interactive)
(let* ((id (tabulated-list-get-id))
(subject (substring-no-properties (elt (tabulated-list-get-entry) 2))))
(setq himalaya-id id)
(setq himalaya-subject subject)
(himalaya-read-message-forward)))
(defun himalaya-copy-marked-messages ()
"Copy message(s) matching marked envelope(s) (or envelope at point)
from current folder of current account to selected folder."
(interactive)
(himalaya--pick-folder
"Copy to folder: "
(lambda (folder)
(himalaya--copy-messages
(or himalaya-marked-ids (list (tabulated-list-get-id)))
folder
(lambda (status)
(message "%s" (string-trim status))
(himalaya-unmark-all-envelopes t))))))
(defun himalaya-move-marked-messages ()
"Move message(s) matching marked envelope(s) (or envelope at point)
from current folder of current account to selected folder."
(interactive)
(himalaya--pick-folder
"Move to folder: "
(lambda (folder)
(let ((prev-point (point))
(ids (or himalaya-marked-ids (list (tabulated-list-get-id)))))
(himalaya--move-messages
ids
folder
(lambda (status)
(message "%s" (string-trim status))
(himalaya-unmark-all-envelopes t)
(revert-buffer)
(goto-char prev-point)))))))
(defun himalaya-delete-marked-messages ()
"Delete message(s) matching marked envelope(s) (or envelope at
point) from current folder of current account."
(interactive)
(let* ((prev-point (point))
(envelope (tabulated-list-get-entry))
(subject (substring-no-properties (elt envelope 2)))
(subject-or-ids (if himalaya-marked-ids (string-join himalaya-marked-ids ", ") subject)))
(when (y-or-n-p (format "Delete message(s) %s? " subject-or-ids))
(himalaya--delete-messages
(or himalaya-marked-ids (tabulated-list-get-id))
(lambda (status)
(message "%s" (string-trim status))
(himalaya-unmark-all-envelopes t)
(revert-buffer)
(goto-char prev-point))))))
(defun himalaya-send-buffer ()
"Send the current buffer."
(interactive)
(himalaya--send-template
(buffer-string)
(lambda (status)
(if himalaya-reply
(himalaya--add-flag
himalaya-id
"Answered"
(lambda (_)
(message "%s" (string-trim status))
(set-buffer-modified-p nil)
(kill-current-buffer)
(himalaya-list-envelopes)))
(message "%s" (string-trim status))
(set-buffer-modified-p nil)
(kill-current-buffer)))))
(defvar himalaya-read-message-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "a") #'himalaya-download-current-attachments)
(define-key map (kbd "R") #'himalaya-read-current-message-raw)
(define-key map (kbd "r") #'himalaya-reply-to-current-message)
(define-key map (kbd "f") #'himalaya-forward-current-message)
(define-key map (kbd "q") #'kill-current-buffer)
(define-key map (kbd "n") #'himalaya-next-message)
(define-key map (kbd "p") #'himalaya-prev-message)
map))
(define-derived-mode himalaya-read-message-mode message-mode "Himalaya-Read"
"Message reading mode."
(setq mail-header-separator ""))
(defvar himalaya-read-message-raw-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "a") #'himalaya-download-current-attachments)
(define-key map (kbd "R") #'himalaya-read-current-message-plain)
(define-key map (kbd "r") #'himalaya-reply-to-current-message)
(define-key map (kbd "f") #'himalaya-forward-current-message)
(define-key map (kbd "q") #'kill-current-buffer)
(define-key map (kbd "n") #'himalaya-next-message)
(define-key map (kbd "p") #'himalaya-prev-message)
map))
(define-derived-mode himalaya-read-message-raw-mode message-mode "Himalaya-Read-Raw"
"Himalaya raw message reading mode."
(setq mail-header-separator ""))
(defvar himalaya-message-write-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'himalaya-send-buffer)
map))
(define-derived-mode himalaya-message-write-mode message-mode "Himalaya-Write"
"Himalaya message writing mode."
(setq mail-header-separator ""))
(provide 'himalaya-message)
;;; himalaya-message.el ends here