vicious/mbox.lua

44 lines
1.1 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2009, Adrian C. <anrxc.sysphere.org>
---------------------------------------------------
-- {{{ Grab environment
local io = { open = io.open }
local setmetatable = setmetatable
local string = { gfind = string.gfind }
local helpers = require("vicious.helpers")
-- }}}
-- Mbox: provides the subject of last e-mail in a mbox file
module("vicious.mbox")
-- {{{ Mailbox widget type
2009-08-07 17:41:10 +02:00
local function worker(format, mbox)
local f = io.open(mbox)
-- mbox could be huge, get a 15kb chunk from EOF
-- * attachments could be much bigger than this
f:seek("end", -15360)
local text = f:read("*all")
f:close()
for match in string.gfind(text, "Subject: ([^\n]*)") do
subject = match
end
if subject then
-- Spam sanitize only the last subject
subject = helpers.escape(subject)
-- Don't abuse the wibox, truncate
subject = helpers.truncate(subject, 22)
return {subject}
end
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })