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
|
--
-- This file contains examples on how IMAPFilter can be extended using the Lua
-- programming language.
--
-- When filtering based on a message's date, the argument must be of the
-- strftime(3) form "%d-%b-%Y". Sometimes it is useful to generate the date
-- argument, based on the system's current date (a kind of "older than" or
-- "newer than" functionality).
--
-- The auxiliary function date_before() is supplied for conveniency. For
-- example the following filter matches messages newer than 30 days:
myfilter = {
'since ' .. date_before(30),
}
-- Passwords could be supplied during execution time.
--
-- The auxiliary function get_pass() is supplied for conveniency. The
-- following example displays a prompt and reads the password from the user:
myaccount = {
server = 'imap.mail.server',
username = 'me',
password = get_pass('Enter password for me@imap.mail.server: ')
}
-- One can list all the available mailboxes residing on a server recursively.
--
-- This can be accomplished by use of the list() and lsub() commands. An
-- example function that traverses the folder tree, finds the available
-- mailboxes, inserts them in a table and prints the result, is demonstrated:
mailboxes = {}
function list_all(account, path)
mboxes, folders = list(account, path)
for _, mbox in ipairs(mboxes) do
table.insert(mailboxes, mbox)
end
for _, folder in ipairs(folders) do
if (folder ~= path .. '/') then
list_all(account, folder)
end
end
end
list_all(myaccount, '')
print(unpack(mailboxes))
-- IMAPFilter can be detached from the controlling terminal and run in the
-- background as a system daemon.
--
-- The auxiliary function daemon_mode() is supplied for conveniency. The
-- following example puts imapfilter in the background and runs endlessly,
-- executing the commands in the forever() function and sleeping for 600
-- seconds between intervals:
function forever()
results = match(myaccount, 'mymailbox', myfilter)
delete(myaccount, 'mymailbox', results)
end
daemon_mode(600, forever)
-- On certain occasions, the IMAP search mechanism is too restrictive; it
-- matches strings ignoring case distinction and does not understand regular
-- expressions. In some other cases, the mail server administrator may have
-- restricted or disabled the IMAP search functionality (although this is an
-- IMAP protocol violation).
--
-- Therefore, it may be a better (or the only) solution, to download the
-- messages (or parts of them) and perform the searching locally. Thus, using
-- the fetch*() family of functions, the header, body, text (header and body)
-- or specific header lines of a message can be downloaded and then processed
-- using Lua's builtin string functions.
--
-- The following example matches messages based on the "From" and "Subject"
-- headers, inserts their sequence number in a table and requests from the
-- server to delete them:
results = match(myaccount, 'mymailbox', {})
fields = fetchfields(myaccount, 'mymailbox', { 'from', 'subject' }, results)
results = {}
for msgid, msghdrs in pairs(fields) do
if (string.find(msghdrs, 'From: .*user1@domain1') or
string.find(msghdrs, 'From: .*user2@domain2') and not
string.find(msghdrs, 'Subject: [imapfilter-devel] .*')) then
table.insert(results, msgid)
end
end
delete(myaccount, 'mymailbox', results)
-- IMAPFilter can take advantage of all those filtering utilities that are
-- available and use a wide range of heuristic tests, text analysis,
-- internet-based realtime blacklists, advanced learning algorithms, etc. to
-- classify mail. IMAPFilter can pipe a message to a program and act on the
-- message based on the program's exit status.
--
-- The auxiliary function pipe_to() is supplied for conveniency. For example
-- if there was a utility named "bayesian-spam-filter", which returned 1 when
-- it considered the message "spam" and 0 otherwise:
results = match(myaccount, 'mymailbox', {})
messages = fetchmessage(myaccount, 'mymailbox', results)
results = {}
for msgid, msgtxt in pairs(messages) do
if (pipe_to('bayesian-spam-filter', msgtxt) == 1) then
table.insert(results, msgid)
end
end
delete(myaccount, 'mymailbox', results)
-- Passwords could be extracted during execution time from an encrypted file.
--
-- The file is encrypted using the openssl(1) command line tool. For example
-- the "passwords.txt" file:
--
-- secret1
-- secret2
--
-- ... is encrypted and saved to a file named "passwords.enc" with the command:
--
-- $ openssl bf -salt -in passwords.txt -out passwords.enc
--
-- The auxiliary function pipe_from() is supplied for conveniency. The user is
-- prompted to enter the decryption password, the file is decrypted and the
-- account passwords are set accordingly:
account1 = {
server = 'imap1.mail.server',
username = 'user1',
}
account2 = {
server = 'imap2.mail.server',
username = 'user2',
}
status, output = pipe_from('openssl bf -d -salt -in ~/passwords.enc')
_, _, account1.password, account2.password = string.find(output,
'(%w+)\n(%w+)\n')
|