IMAPFILTER_CONFIG(5) | File Formats Manual | IMAPFILTER_CONFIG(5) |
imapfilter_config
—
imapfilter configuration file
$HOME/.imapfilter/config.lua
imapfilter(1) uses the Lua programming language as a configuration and extension language, therefore the configuration file is a Lua script.
Although knowledge of Lua is not required to use imapfilter(1), it is nonetheless recommended, especially if one wants to extend it. For more information on Lua see http://www.lua.org/docs.html.
A brief description of the Lua values and types mentioned hereafter in the manual page follows:
Program's options are set using an already initialised table named “options”, in the following manner:
options.timeout = 120 options.namespace = false options.charset = 'ISO-8859-1'
Available options are:
enter_idle
().
Default is “true”.Accounts are initialized using the
IMAP
()
function, and the details of the connection are defined using an account
table:
myaccount = IMAP { server = 'imap.mail.server', username = 'me', password = 'secret', ssl = 'auto' }
An account table must have the following elements:
An account table can also have the following optional elements:
Note that due to Lua using backslash ‘\’ as an escape character for its strings, one has to use double backslashes in order to insert a single backslash, and thus a backslash character inside a password might require four backslashes.
Note that this requires that an OAuth client ID and client secret have been obtained, an OAuth2 token has been generated and authorized, a new access token has been generated using the refresh token if the last access token has expired, and an OAuth2 string has been generated from the access token. The aforementioned OAuth2 string is a Base64 encoded string that should be set here. For more information see https://developers.google.com/gmail/xoauth2_protocol.
Note that the latest versions of the OpenSSL library have deprecated version specific methods, and the actual protocol version used will be negotiated to be the highest version mutually supported by the client and the server. This is also what the “auto” value does.
The following methods can be used on an account to list mailboxes in a folder of an account:
list_all
(folder)list_subscribed
(folder)The following methods can be used on an account to list mailboxes, using wildcards, in a folder of an account. The ‘*’ wildcard, matches any character and the ‘%’ matches any character except the folder delimiter, ie. non-recursively:
list_all
(folder,
mailbox)list_subscribed
(folder,
mailbox)Examples:
mailboxes, folders = myaccount:list_subscribed('myfolder') mailboxes, folders = myaccount:list_all('myfolder/mysubfolder', '*')
The following methods can be used to manipulate mailboxes in an account:
create_mailbox
(name)delete_mailbox
(name)rename_mailbox
(oldname,
newname)subscribe_mailbox
(name)unsubscribe_mailbox
(name)Examples:
myaccount:create_mailbox('mymailbox') myaccount:subscribe_mailbox('mymailbox') myaccount:unsubscribe_mailbox('myfolder/mymailbox') myaccount:delete_mailbox('myfolder/mymailbox')
After an IMAP account has been initialized, mailboxes residing in that account can be accessed simply as elements of the account table:
myaccount.mymailbox
If mailbox names don't only include letters, digits and underscores, or begin with a digit, an alternative form must be used:
myaccount['mymailbox']
A mailbox inside a folder can be only accessed by using the alternative form:
myaccount['myfolder/mymailbox']
The methods that are available for an account (eg.
list_all
(),
create_mailbox
(), etc.) , are considered keywords
and must not be used as mailbox names, and the same also applies for any
string starting with an underscore, as they are considered reserved.
The following methods can be used to check the status of a mailbox:
check_status
()The
check_status
()
method gets the current status of a mailbox, and returns four values of
number type: the total number of messages, the
number of recent messages, the number of unseen messages in the mailbox,
and the next UID to be assigned to a new message in the mailbox.
enter_idle
()enter_idle
() method implements the IMAP IDLE
(RFC 2177) extension. By using this extension it's not necessary to poll
the server for changes to the selected mailbox (ie. using the
check_status
() method), but instead the server
sends an update when there is a change in the mailbox (eg. in case of new
mail). When the enter_idle
() method has been
called no more commands in the configuration file are executed until an
update is received, at which point the
enter_idle
() method returns. For the
enter_idle
() to work, the IDLE extension has to be
supported by the IMAP server.
The
enter_idle
()
method returns a value of type boolean:
“true” if the IDLE extension is supported and there was a
update in the mailbox, and “false” if the IDLE extension
is not supported, in which case the method returns immediately. When the
aforementioned return value was “true”, an additional
second value of type string is also returned,
indicating the event received from the server, which is useful when the
wakeonany option has been enabled.
Apart from an event received by the server,
the SIGUSR1 or SIGUSR2 signals can also interrupt the IDLE mode at any
time, and the execution of the configuration file will then continue
from the next line after the
enter_idle
().
In this case only the value “true” is returned.
Examples:
exist, unread, unseen, uidnext = myaccount.mymailbox:check_status() update = myaccount.mymailbox:enter_idle() update, event = myaccount.mymailbox:enter_idle()
The searching methods in this subsection can be applied to any mailbox. They return a special form of table, that contains the messages that match the searching method. This table can be combined with other tables using logic theory. There are three available operations, that implement logical “or”, logical “and” and logical “not”.
The logical “or” is implemented using the ‘+’ operator:
results = myaccount.mymailbox:is_unseen() + myaccount.mymailbox:is_larger(100000)
The logical “and” is implemented using the ‘*’ operator:
results = myaccount.mymailbox:is_unseen() * myaccount.mymailbox:is_larger(100000)
The logical “not” is implemented using the ‘-’ operator:
results = myaccount.mymailbox:is_unseen() - myaccount.mymailbox:is_larger(100000)
The three logical operators can be combined in the same expression. The logical “and” has higher precedence than the logical “or” and the logical “not”, with the latter two having the same precedence, and parentheses may be used to change this behaviour:
results = myaccount.mymailbox:is_unseen() + myaccount.mymailbox:is_larger(100000) * myaccount.mymailbox:contain_subject('test') results = ( myaccount.mymailbox:is_unseen() + myaccount.mymailbox:is_larger(100000) ) * myaccount.mymailbox:contain_subject('test')
The returned tables of the searching methods can also be stored in variables and then further processed:
unseen = myaccount.myaccount:is_unseen() larger = myaccount.mymailbox:is_larger(100000) subject = myaccount.mymailbox:contain_subject('test') results = unseen + larger * subject
A composite filter that includes one or more simple rules can be defined:
myfilter = function () return myaccount.mymailbox:is_unseen() + myaccount.mymailbox:is_larger(100000) * myaccount.mymailbox:contain_subject('test') end results = myfilter()
Composite filters can may be more dynamic by adding arguments:
myfilter = function (mailbox, size, subject) return mailbox:is_unseen() + mailbox:is_larger(size) * mailbox:contain_subject(subject) end results = myfilter(myaccount.mailbox, 100000, 'test')
It is also possible to combine the searching methods in different mailboxes, either at the same or different accounts, for example when the same actions will be executed on messages residing in different mailboxes or accounts.
results = myaccount.mymailbox:is_unseen() + myaccount.myothermailbox:is_larger(100000) + myotheraccount.myothermailbox:contain_subject('test')
And for those that want to know more about the return values of the following methods, it is a table which contains tables with two values: the mailbox (table) the message belongs to, and the message UID (number) which points to the matching message. For examples on iterating these returned tables, or creating new tables of this format (they are actually metatables implementing sets), see the samples/extend.lua file.
{ { <myaccount.mymailbox>, 1 }, { <myaccount.mymailbox>, 3 }, { <myaccount.myothermailbox>, 5 }, { <myothermailbox.myothermailbox>, 7}, { ... }, ... }
The following method can be used to get all messages in a mailbox:
select_all
()The following methods can be used to search for messages that are in a specific state:
is_answered
()is_deleted
()is_draft
()is_flagged
()is_new
()is_old
()is_recent
()is_seen
()is_unanswered
()is_undeleted
()is_undraft
()is_unflagged
()is_unseen
()The following method can be used to search for messages that have a specific keyword flag set:
has_keyword
(flag)has_unkeyword
(flag)The following methods can be used to search for messages based on their size:
is_larger
(size)is_smaller
(size)The following methods can be used to search for messages based on their age:
is_newer
(age)is_older
(age)The following methods can be used to search for messages based on their arrival or sent date, in the “day-month-year” form, where day is the day of the month as a decimal number (01-31), month is the abbreviated month ( “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”) and year is the year as decimal number including the century (eg. 2007):
arrived_before
(date)arrived_on
(date)arrived_since
(date)sent_before
(date)sent_on
(date)sent_since
(date)The following methods can be used to do case-insensitive searching, for messages that contain a specific word or phrase:
contain_bcc
(string)contain_cc
(string)contain_from
(string)contain_subject
(string)contain_to
(string)contain_field
(field,
string)contain_body
(string)contain_message
(string)The following methods can be used to do case-sensitive searching, for messages that match a specific regular expression pattern. The matching mechanism that is used to support this is based on the Perl-compatible regular expressions (PCRE), and more information about the patterns and modifiers that can be used, is available in the relevant documentation at http://pcre.org/original/doc/html/.
This way of searching is not supported by the IMAP protocol, and this means that what actually happens under the hood, is that the relevant parts of all the messages are downloaded and matched locally. It is therefore recommended to use these methods with meta-searching (see following section), in order to narrow down the set of messages that should be searched, and thus minimize what will be downloaded.
Note that due to Lua using backslash ‘\’ as an escape character for its strings, one has to use double backslashes in order to insert a single backslash inside a regular expression pattern:
match_bcc
(pattern)match_cc
(pattern)match_from
(pattern)match_subject
(pattern)match_to
(pattern)match_field
(field,
pattern)match_header
(pattern)match_body
(pattern)match_message
(pattern)The following method can be used to search for messages using user queries based on the IMAP specification (RFC 3501 Section 6.4.4):
send_query
(criteria)Examples:
results = myaccount.mymailbox:select_all() results = myaccount.mymailbox:is_new() results = myaccount.mymailbox:is_recent() results = myaccount.mymailbox:is_larger(100000) results = myaccount.mymailbox:is_older(10) results = myaccount.mymailbox:has_keyword('MyFlag') results = myaccount.mymailbox:arrived_before('01-Jan-2007') results = myaccount.mymailbox:sent_since('01-Jan-2007') results = myaccount.mymailbox:contain_subject('test') results = myaccount.mymailbox:contain_field('Sender', 'user@host') results = myaccount.mymailbox:contain_body('hello world') results = myaccount.mymailbox:match_from('.*(user1|user2)@host') results = myaccount.mymailbox:send_query('ALL') results = myaccount['mymailbox']:is_new() results = myaccount['myfolder/mymailbox']:is_recent()
After one of more searching methods have been applied to one or more mailboxes, the result contains all the necessary information, such as which messages matched in which mailboxes. Using this result these messages can be either searched further or processed in various way.
The results of the searching methods can be searched further on in the same way as searching is done in mailboxes. The difference is that instead of doing the search in the whole mailbox, ie. in all the messages, it is instead done only to those messages that were returned in a previous search.
Examples:
results:match_message('^[Hh]ello world!?$') myaccount.mymailbox:is_new():match_body('^[Ww]orld, hello!?$')
The processing methods are applied to the results that searching returned.
The following method can be used to delete messages in a mailbox:
delete_messages
()The following methods can be used to copy and move messages in a mailbox at the same or different accounts. If the destination mailbox is in a different account than the source mailbox, then the messages are downloaded and then uploaded to the destination:
copy_messages
(destination)move_messages
(destination)The following methods can be used to mark messages in a mailbox:
mark_answered
()mark_deleted
()mark_draft
()mark_flagged
()mark_seen
()unmark_answered
()unmark_deleted
()unmark_draft
()unmark_flagged
()unmark_seen
()The following methods can be used to flag messages in a mailbox. The standard system flags are “\Answered”, “\Deleted”, “\Draft”, “\Flagged”, “\Seen”, while if the server supports it, new user keywords may be defined:
add_flags
(flags)remove_flags
(flags)replace_flags
(flags)Examples:
results:delete_messages() results:copy_messages(myaccount.myothermailbox) results:move_messages(myotheraccount.mymailbox) results:mark_seen() results:unmark_flagged() results:add_flags({ 'MyFlag', '\\Seen' }) results:remove_flags({ '\\Seen' }) results:move_messages(myotheraccount['myfolder/mymailbox'])
The messages that are residing in any mailbox can be also accessed, as a whole or in parts. Messages can be accessed using their unique identifier (UID):
myaccount.mymailbox[22]
The UIDs of messages the user is interested in, are gained from the results of searching:
results = account.INBOX:is_unseen() for _, message in ipairs(results) do mailbox, uid = table.unpack(message) header = mailbox[uid]:fetch_header() end
The following methods can be used to fetch parts of messages. The methods return a string. The downloaded message parts are cached locally, so they can be reused inside the same program session:
fetch_message
()fetch_header
()fetch_body
()fetch_field
(field)fetch_part
(part)The following methods can be used to fetch details about the state of a message:
fetch_flags
()fetch_date
()fetch_size
()fetch_structure
()The following methods can be used to append a message to a mailbox:
append_message
(message)append_message
(message,
flags, date)fetch_flags
(), and
date (string), as returned by
fetch_date
().Examples:
myaccount.mymailbox[2]:fetch_message() myaccount.mymailbox[3]:fetch_field('subject') myaccount.mymailbox[5]:fetch_part('1.1') myaccount['mymailbox'][7]:fetch_message() myaccount['myfolder/mymailbox'][11]:fetch_message() myaccount.mymailbox:append_message(message)
The following auxiliary functions are also available for convenience:
form_date
(days)get_password
(prompt)become_daemon
(interval,
commands)become_daemon
(interval,
commands, nochdir,
noclose)If nochdir (boolean) is “true”, the current working directory is not changed to the root directory (/). If noclose (boolean) is “true”, the standard input, standard output and standard error are not redirected to /dev/null.
pipe_to
(command,
data)pipe_from
(command)regex_search
(pattern,
string)Examples:
date = form_date(14) password = get_password('Enter password: ') become_daemon(600, myfunction) status = pipe_to('mycommandline', 'mydata') status, data = pipe_from('mycommandline') success, capture = regex_search('^(?i)pcre: (\\w)$', 'mystring')
See samples/config.lua and samples/extend.lua in the source code distribution.
HOME
August 26, 2018 | Debian |