Skip to content

Instantly share code, notes, and snippets.

@KangOl
Created January 14, 2013 21:58
Show Gist options
  • Save KangOl/4533912 to your computer and use it in GitHub Desktop.
Save KangOl/4533912 to your computer and use it in GitHub Desktop.
imapfilter: auto sort mailing list
bool = function(o)
if not o then
return false
elseif o == '' then
return false
elseif o == 0 then
return false
elseif #o == 0 then
return false
end
return true
end
pyor = function(o)
if not bool(o) then
return false
end
return o
end
function process()
ML = 'MailingLists/'
AUTO_ML = ML..'Auto/'
-- now make a generic filtering on mailing lists...
messages = mbox:contain_field('List-Id', '')
mls = {}
for msgid, listid in pairs(mbox:fetch_fields({'List-Id'}, messages) or {}) do
if bool(listid) then
-- imapfilter bug: the search is also done inside the message body
-- and thus also match the forwarded emails.
s, m1, m2 = regex_search('^List-Id: (.*) ?<(.*)>$', listid)
if s then
m = pyor(m1) or pyor(m2)
else
m2 = string.sub(listid, 10)
m = m2
end
if bool(m2) then
-- get the first part of a split on .
s, _ = m:find('.', 1, true)
if s then
m = m:sub(1, s-1)
end
mls[m2] = m:gsub('"', ''):gsub('/', '-')
end
end
end
for listid, listtitle in pairs(mls) do
dest = AUTO_ML..listtitle
account:create_mailbox(dest)
account:subscribe_mailbox(dest)
messages:contain_field('List-Id', listid):move_messages(account[dest])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment