vicious/mbox.lua

44 lines
1.2 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)
-- mbox could be huge, get a 15kb chunk from EOF
-- * attachments could be much bigger than this
2009-10-04 00:28:54 +02:00
local f = io.open(mbox)
f:seek("end", -15360)
2009-10-04 00:28:54 +02:00
local txt = f:read("*all")
f:close()
2009-10-04 00:28:54 +02:00
-- Find all Subject lines
for s in string.gfind(txt, "Subject: ([^\n]*)") do subject = s 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}
2009-10-04 00:28:54 +02:00
else
return {"N/A"}
end
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })