/
primary.lua.tmpl
108 lines (88 loc) · 4.19 KB
/
primary.lua.tmpl
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
-- crontab -e
-- 0 * * * * /usr/local/bin/imapfilter
function main ()
-- See 'man imapfilter_config' for an explanation of
-- imapfilter options and functions.
-- Some config examples:
-- https://github.com/lefcha/imapfilter/blob/master/samples/config.lua
-- https://gist.github.com/dylanwh/408810
-- http://www.npcglib.org/~stathis/blog/2012/07/09/linux-task-sorting-mail-with-imapfilter/
-- General Options
options.timeout = 120
options.subscribe = true
-- Accounts
{{ if .personal }}
{{- if or (eq .private true) (eq .currVaultID "1001") }}
{{- (vault (printf "kv/users/%s/imapfilter_primary" .currVaultID)).data.data.imapfilter_primary }}
{{- end }}
{{- end }}
-- Debug to view underlying mailbox structure
-- test = second:list_all('*', '*')
-- for key,value in pairs(test) do print(key, value) end
-- GMAIL
-- Gmail behaves differently to normal imap. With a normal imap acccount
-- you can simply use move_messages(). Google stores both inbox
-- and sent email in the "[Gmail]/All Mail" folder. An email sent to yourself
-- from the same email address exists as a single email and is visible in
-- the Inbox and Sent box. If you delete the inbox mail then the sent mail
-- is also deleted. Thus it is only safe to delete mail once both the Inbox
-- and Sent box is copied.
-- Only go ahead and copy inbox/sent/spam from gmail if I have received
-- some Inbox email.
if (second['Inbox']:check_status() >= 0) then
inbox_mail = second["Inbox"]:select_all()
inbox_copy_success = inbox_mail:copy_messages(first['INBOX'])
sent_mail = second['[Gmail]/Sent Mail']:select_all()
sent_copy_success = sent_mail:copy_messages(first['SENT'])
spam_mail = second["[Gmail]/Spam"]:select_all()
spam_mail:move_messages(first['SPAM'])
-- Only clear the All Mail folder if we were successful in moving
-- our Inbox and Sent mail. Note: move_messages is supposed to return
-- booleans for copying the messages and then marking them as deleted.
-- For some reason the mark for deletion return value is 'nil'.
-- This may need to be [Gmail]/Bin or [Gmail]/Trash check your account!
if (inbox_copy_success and sent_copy_success) then
print('Safe to delete both inbox and sent')
move_inbox = inbox_mail:move_messages(second['[Gmail]/Bin'])
move_sent = sent_mail:move_messages(second['[Gmail]/Bin'])
-- if (move_inbox and move_sent) then
-- print('Emptying bin');
-- -- bin_mail = second['[Gmail]/Trash']:select_all()
-- bin_mail = second['[Gmail]/Bin']:select_all()
-- bin_mail:delete_messages()
-- end
end
end
if (third['Inbox']:check_status() >= 0) then
inbox_results = third['Inbox']:select_all()
inbox_results:move_messages(first['INBOX'])
sent_results = third['Sent']:select_all()
sent_results:move_messages(first['INBOX'])
spam_results = third['Junk']:select_all()
spam_results:move_messages(first['INBOX'])
end
if (second['Inbox']:check_status() >= 0) then
inbox_results = second['Inbox']:select_all()
inbox_results:move_messages(first['INBOX'])
sent_results = second['Sent']:select_all()
sent_results:move_messages(first['INBOX'])
spam_results = second['Junk']:select_all()
spam_results:move_messages(first['INBOX'])
end
end
-- -- Escapes problematic characters in passwords.
-- function sanitize_pwd(pwd)
-- -- Chomp off newline character that is sucked in from Password Store.
-- pwd = string.gsub(pwd, "\n", "")
-- -- Escape backslash characters that exist in passwords. Need to escape the
-- -- escape characters at each 'level' where the password string is used
-- -- otherwise escape characters are lost.
-- --
-- -- The slashes are escaped first. If they were escaped later then
-- -- characters escaped later would be escaped again.
-- pwd = string.gsub(pwd, '%\\', '\\\\')
-- -- Escape double quote characters that exist in passwords.
-- pwd = string.gsub(pwd, '%"', '\\"')
-- return pwd
-- end
main()