-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskedemail.el
124 lines (111 loc) · 4.73 KB
/
maskedemail.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
(require 's)
(require 'cl)
(require 'dash)
(defgroup maskedemail nil
"Handle MaskedEmail provided by FastMail"
:group 'tools)
(defcustom maskedemail-cli-path "maskedemail"
"path of maskedemail cli binary"
:type 'string
:group 'maskedemail
)
(cl-defstruct maskedemail "Masked Email Struct" email domain state)
(defvar all-maskedemails nil "All Masked Emails (served as a cache)")
(defun maskedemail-list ()
"List all Masked Emails"
(interactive)
(with-temp-buffer
(let* ((list-output (shell-command-to-string (format "%s -e list" maskedemail-cli-path)))
(list-output (string-trim list-output))
(list-output-lines (s-split "\n" list-output)))
;; (message "%s" list-output-lines)
(setq all-maskedemails nil)
(dolist (line list-output-lines)
(let* ((fields (s-split " " line))
(fields-with-key (-interleave '(:email :domain :state) fields))
(m (apply 'make-maskedemail fields-with-key)))
(push m all-maskedemails)))
all-maskedemails)))
(defun maskedemail-refresh ()
"Refresh Masked Email list"
(interactive)
(setq all-maskedemails nil)
(maskedemail-list))
(defun maskedemail-create ()
(interactive)
(with-temp-buffer
(let* ((list-output (shell-command-to-string (format "%s -e create %s"
maskedemail-cli-path
(read-string "Create for domain: "))))
(list-output (string-trim list-output)))
(let* ((fields (s-split " " list-output))
(fields-with-key (-interleave '(:email :domain :state) fields))
(m (apply 'make-maskedemail fields-with-key)))
(push m all-maskedemails)
(message "%s" (maskedemail-email m))))))
(defun maskedemail-select ()
"Selete MaskedEmail based on domain and copy to kill ring (clipboard)"
(interactive)
(unless all-maskedemails (maskedemail-list))
(let* ((domains (-map (lambda (m)
`(,(maskedemail-domain m) . ,m))
all-maskedemails))
(chosen (alist-get
(completing-read "Domain: " domains)
domains nil nil 'equal))
(chosen-email (maskedemail-email chosen))
(chosen-domain (maskedemail-domain chosen))
(chosen-state (maskedemail-state chosen)))
(kill-new chosen-email)
chosen-email))
(defun maskedemail-enable ()
"Enable MaskedEmail"
(interactive)
(unless all-maskedemails (maskedemail-list))
(let* ((enabled-maskedemails (-filter (lambda (m)
(member (maskedemail-state m)
'("pending" "disabled")))
all-maskedemails))
(emails (-map (lambda (m)
`(,(maskedemail-email m) . ,m))
enabled-maskedemails))
(chosen (alist-get
(completing-read "Email: " emails)
emails nil nil 'equal))
(chosen-email (maskedemail-email chosen))
(chosen-domain (maskedemail-domain chosen))
(chosen-state (maskedemail-state chosen)))
(shell-command (format "%s -e enable %s"
maskedemail-cli-path
chosen-email))
;; NOTE: can be optimized if use a hashmap for all-maskedemails
(dolist (m all-maskedemails)
(when (s-equals? (maskedemail-email m) chosen-email)
(setf (maskedemail-state m) "enabled")))
chosen-email))
(defun maskedemail-disable ()
"Disable MaskedEmail"
(interactive)
(unless all-maskedemails (maskedemail-list))
(let* ((disabled-maskedemails (-filter (lambda (m)
(member (maskedemail-state m)
'("enabled")))
all-maskedemails))
(emails (-map (lambda (m)
`(,(maskedemail-email m) . ,m))
disabled-maskedemails))
(chosen (alist-get
(completing-read "Email: " emails)
emails nil nil 'equal))
(chosen-email (maskedemail-email chosen))
(chosen-domain (maskedemail-domain chosen))
(chosen-state (maskedemail-state chosen)))
(shell-command (format "%s -e disable %s"
maskedemail-cli-path
chosen-email))
;; NOTE: can be optimized if use a hashmap for all-maskedemails
(dolist (m all-maskedemails)
(when (s-equals? (maskedemail-email m) chosen-email)
(setf (maskedemail-state m) "disabled")))
chosen-email))
(provide 'maskedemail)