[imapfilter-devel] Re: iterate on defined filters
David DeSimone
fox at verio.net
Wed Aug 18 19:44:04 EEST 2004
Joel CARNAT <joel at carnat.net> wrote:
>
> I thought ALERTS[k] would say "spam", "virus", ... but no =) In fact,
> it give the k index of the array (filter) named "spam", "virus", ...
> That's not what I expected as I use the filter name to create an INBOX
> directory :)
I believe that LUA can do what you want, but you have to give some
thought to your data structure. :)
> -- Move admin_vc alerts
>
> LIST = {"mailer_daemon", "nagios", "spam", "virus"}
> for k,ALERTS in pairs({mailer_daemon, nagios, spam, virus}) do
> -- print("k is " .. k)
> -- print("ALERTS is " .. LIST[k])
> results = match(admin_vc, 'INBOX', ALERTS)
> headers = fetchheaders(admin_vc, 'INBOX', { 'date ' }, results)
> if headers ~= nil then
> for ID, HDR in pairs(headers) do
> _, _, month, year =
> string.find(HDR, "Date: %a+, %d+ (%a+) (%d+)")
> move(admin_vc, 'INBOX', admin_vc,
> year .. '.' .. month .. '.' .. LIST[k], results)
> end
> end
> end
I think creating a dependency that LIST and the anonymous table have the
same elements in the same order, is doing things in a less efficient
manner. There is also the fact that the "pairs()" iterator is not
guaranteed to return table items in the order you specified them. That
is what the "ipairs()" iterator will do.
I think it would be better to create a master table in which the KEY
value is the name of the list, and the VALUE is a sub-table which
ctonains the filter patterns you want. Something like this:
LIST =
{
"mailer_daemon" =
{
'to "postmaster"',
'subject "returned mail",
},
"nagios" =
{
'from "root"',
'subject "NAGIOS alert",
}
}
Of course, my query filters here are made-up, but I assume you have some
in use.
So, when you use the pairs() iterator, you will get both a useful key
(name of list), and a useful value (filter operator to be applied), and
you know they will always be correctly associated with each other. So
your function becomes:
for list, query in pairs(LIST)
do
-- print("list is " .. list)
-- print("query is:")
-- for k, v in ipairs(query)
-- do
print(" " .. v)
-- end
results = match(admin_vc, 'INBOX', query)
headers = fetchheaders(admin_vc, 'INBOX', { 'date ' }, results)
if headers ~= nil
then
for ID, HDR in pairs(headers)
do
_, _, month, year =
string.find(HDR, "Date: %a+, %d+ (%a+) (%d+)")
move(admin_vc, 'INBOX', admin_vc,
year .. '.' .. month .. '.' .. list, { ID })
end
end
end
Your code used "results" in the move() operation, but I substituted
anonymous table { ID } instead, because it seems that you want each
individual message to move to the correct folder due to its date, rather
than all messages... that is the point of iterating over each message's
header, right?
--
David DeSimone || Network Admin || fox at verio.net
"It took me fifteen years to discover that I had no
talent for writing, but I couldn't give it up because
by that time I was too famous. -- Robert Benchley
More information about the Imapfilter-devel
mailing list