imapfilter
imapfilter - filter mail in your IMAP account
メールをIMAPサーバー上で整理してくれるツールを捜していたらみつかった。Luaで書かれていて、設定ファイルもLuaだったのでLuaの勉強を開始。まあ、それなりに整理ができるようになった。いままでprocmailでやっていたことが大体できるようになったかな。MLの整理が問題であったのだが、ごにょごにょ関数を書いてできるようになりました。
function match_ml(account, field, contain, match_reg, path_base, m_t)
result = account1.INBOX:contain_field(field, contain)
messages = account1.INBOX:fetch_fields({field},result)
if messages then
for msgid, msgtxt in pairs(messages) do
success, capture = regex_search('^'..field..': '.. match_reg, msgtxt)
if success then
folder = path_base .. capture
if not m_t[folder] then
m_t[folder] = {}
end
m_t[folder][msgid] = true
end
end
end
end
--
-- main
--
m_t = {}
-- debian
match_ml(account1, 'X-Mailing-List', '@lists.debian.org',
'<([^@]+)', "tmp.debian.", m_t)
match_ml(account1, 'Recent-From', '@lists.debian.org',
'<([^@]+)', "tmp.debian.", m_t)
match_ml(account1, 'Recent-To', '@lists.debian.org',
'<([^@]+)', "tmp.debian.", m_t)
-- fml
match_ml(account1, 'X-ML-Name', '', '([a-zA-Z0-9_-]+)',"tmp.misc-ml.", m_t)
-- mailman
match_ml(account1, 'X-BeenThere', '', '([^@]+)',"tmp.misc-ml.", m_t)
match_ml(account1, 'X-Apparently-To', '', '([^@]+)',"tmp.misc-ml.", m_t)
for folder, result in pairs(m_t) do
account1.INBOX:move_messages(account1[folder], result)
-- account1.INBOX:unmark_seen(result)
end
苦労したのは附属のサンプルが間違っていたこと。Webサイトのほうは更新されていてドキュメントの通りにやったら動いた。table.insert(result,msgid)とやっては駄目。result[msgid] = trueとやる。

Debian QA
Facebook (taniguchi.takaki)
Twitter (@takaki_t)