vicious/mbox.lua

46 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)
2009-10-05 00:11:44 +02:00
-- mbox could be huge, get a 30kb chunk from EOF
-- * attachments could be much bigger than this
2009-10-04 00:28:54 +02:00
local f = io.open(mbox)
2009-10-05 00:11:44 +02:00
f:seek("end", -30720)
2009-10-04 00:28:54 +02:00
local txt = f:read("*all")
f:close()
2009-10-05 00:11:44 +02:00
-- Default value
local subject = "N/A"
2009-10-04 00:28:54 +02:00
-- Find all Subject lines
2009-10-05 00:11:44 +02:00
for i in string.gfind(txt, "Subject: ([^\n]*)") do
subject = i
end
2009-10-05 00:11:44 +02:00
-- Spam sanitize only the last subject
subject = helpers.escape(subject)
2009-10-05 00:11:44 +02:00
-- Don't abuse the wibox, truncate
subject = helpers.truncate(subject, 22)
return {subject}
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })