2011-03-06 06:58:58 -05:00
|
|
|
--
|
|
|
|
-- This file contains examples on how IMAPFilter can be extended using
|
|
|
|
-- the Lua programming language.
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
|
|
-- 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 = myaccount.mymailbox:is_old()
|
2011-08-06 08:16:07 -04:00
|
|
|
results:move_messages(myaccount.myothermailbox)
|
2011-03-06 06:58:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
become_daemon(600, forever)
|
|
|
|
|
|
|
|
|
|
|
|
-- 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:
|
|
|
|
|
|
|
|
all = myaccount.mymailbox:select_all()
|
|
|
|
|
|
|
|
results = Set {}
|
|
|
|
for _, mesg in ipairs(all) do
|
2012-02-05 07:29:43 -05:00
|
|
|
mbox, uid = table.unpack(mesg)
|
2011-03-06 06:58:58 -05:00
|
|
|
text = mbox[uid]:fetch_message()
|
|
|
|
if (pipe_to('bayesian-spam-filter', text) == 1) then
|
|
|
|
table.insert(results, mesg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results:delete_messages()
|
|
|
|
|
|
|
|
|
|
|
|
-- One might want to run the bayesian filter only in those parts (attachments)
|
|
|
|
-- of the message that are of type text/plain and smaller than 1024 bytes.
|
|
|
|
-- This is possible using the fetch_structure() and fetch_part() functions:
|
|
|
|
|
|
|
|
all = myaccount.mymailbox:select_all()
|
|
|
|
|
|
|
|
results = Set {}
|
|
|
|
for _, mesg in ipairs(all) do
|
2012-02-05 07:29:43 -05:00
|
|
|
mbox, uid = table.unpack(mesg)
|
2011-03-06 06:58:58 -05:00
|
|
|
structure = mbox[uid]:fetch_structure()
|
|
|
|
for partid, partinf in pairs(structure) do
|
|
|
|
if partinf.type:lower() == 'text/plain' and partinf.size < 1024 then
|
|
|
|
part = mbox[uid]:fetch_part(partid)
|
|
|
|
if (pipe_to('bayesian-spam-filter', part) == 1) then
|
|
|
|
table.insert(results, mesg)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results:delete_messages()
|
|
|
|
|
|
|
|
|
2011-08-06 08:16:07 -04:00
|
|
|
-- Messages can be appended to a mailbox. One can fetch a message from a
|
|
|
|
-- mailbox, optionally process it, and then upload it to the same or different
|
|
|
|
-- mailbox, at the same or different mail servers. In the following example a
|
|
|
|
-- header field is added to all messages, and the processed messages are then
|
|
|
|
-- appended to a different mailbox.
|
|
|
|
|
|
|
|
all = myaccount.mymailbox:select_all()
|
|
|
|
|
|
|
|
for _, mesg in ipairs(all) do
|
2012-02-05 07:29:43 -05:00
|
|
|
mbox, uid = table.unpack(all)
|
2011-08-06 08:16:07 -04:00
|
|
|
header = mbox[uid]:fetch_header()
|
|
|
|
body = mbox[uid]:fetch_body()
|
|
|
|
message = header:gsub('[\r\n]+$', '\r\n') ..
|
|
|
|
'My-Header: My-Content\r\n' .. '\r\n' .. body
|
|
|
|
myaccount.myothermaibox:append_message(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-03-06 06:58:58 -05:00
|
|
|
-- 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:
|
|
|
|
--
|
2012-01-18 13:38:26 -05:00
|
|
|
-- secret1
|
|
|
|
-- secret2
|
2011-03-06 06:58:58 -05:00
|
|
|
--
|
|
|
|
-- ... is encrypted and saved to a file named "passwords.enc" with the
|
|
|
|
-- command:
|
|
|
|
--
|
2011-10-30 08:28:49 -04:00
|
|
|
-- $ openssl bf -in passwords.txt -out passwords.enc
|
2011-03-06 06:58:58 -05:00
|
|
|
--
|
|
|
|
-- 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:
|
|
|
|
|
2011-10-30 08:28:49 -04:00
|
|
|
status, output = pipe_from('openssl bf -d -in ~/passwords.enc')
|
2011-03-06 06:58:58 -05:00
|
|
|
|
2012-01-18 13:38:26 -05:00
|
|
|
_, _, password1, password2 = string.find(output, '([%w%p]+)\n([%w%p]+)')
|
2011-03-06 06:58:58 -05:00
|
|
|
|
|
|
|
account1 = IMAP {
|
|
|
|
server = 'imap1.mail.server',
|
|
|
|
username = 'user1',
|
|
|
|
password = password1
|
|
|
|
}
|
|
|
|
|
|
|
|
account2 = IMAP {
|
|
|
|
server = 'imap2.mail.server',
|
|
|
|
username = 'user2',
|
|
|
|
password = password2
|
|
|
|
}
|
|
|
|
|