vicious/mbox.lua

51 lines
1.3 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
2010-01-02 21:21:54 +01:00
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- {{{ Grab environment
local type = type
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
local function worker(format, warg)
if type(warg) ~= "table" then mbox = warg end
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
local f = io.open(mbox or warg[1])
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
-- Check if we should scroll, or maybe truncate
if type(warg) == "table" then
if warg[3] ~= nil then
subject = helpers.scroll(subject, warg[2], warg[3])
else
subject = helpers.truncate(subject, warg[2])
end
end
2009-10-05 00:11:44 +02:00
return {helpers.escape(subject)}
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })