2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
|
|
|
-- * (c) 2009, Adrian C. <anrxc.sysphere.org>
|
|
|
|
---------------------------------------------------
|
2009-07-31 07:49:28 +02:00
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local io = { open = io.open }
|
2009-08-01 23:11:41 +02:00
|
|
|
local setmetatable = setmetatable
|
2009-07-31 07:49:28 +02:00
|
|
|
local string = { find = string.find }
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Mboxc: provides the count of total, old and new messages in a mbox
|
|
|
|
module("vicious.mboxc")
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Mbox count widget type
|
2009-08-07 17:41:10 +02:00
|
|
|
local function worker(format, mbox)
|
2009-07-31 07:49:28 +02:00
|
|
|
-- Initialise counters
|
|
|
|
local old = 0
|
|
|
|
local total = 0
|
|
|
|
|
|
|
|
-- Open the mbox
|
|
|
|
local f = io.open(mbox)
|
|
|
|
|
|
|
|
while true do
|
|
|
|
-- Read the mbox line by line, if we are going to read some
|
2009-09-14 17:25:23 +02:00
|
|
|
-- *HUGE* folders then switch to reading chunks
|
2009-07-31 07:49:28 +02:00
|
|
|
local lines = f:read("*line")
|
|
|
|
if not lines then break end
|
|
|
|
|
|
|
|
-- Find all messages
|
|
|
|
-- * http://www.jwz.org/doc/content-length.html
|
|
|
|
local _, from = string.find(lines, "^From[%s]")
|
|
|
|
if from ~= nil then total = total + 1 end
|
|
|
|
|
|
|
|
-- Read messages have the Status header
|
|
|
|
local _, status = string.find(lines, "^Status:[%s]RO$")
|
|
|
|
if status ~= nil then old = old + 1 end
|
|
|
|
|
|
|
|
-- Skip the folder internal data
|
|
|
|
local _, intdata = string.find(lines, "^Subject:[%s].*FOLDER[%s]INTERNAL[%s]DATA")
|
|
|
|
if intdata ~= nil then total = total -1 end
|
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
|
|
|
|
-- Substract total from old to get the new count
|
|
|
|
local new = total - old
|
|
|
|
|
|
|
|
return {total, old, new}
|
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
|
|
|
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
|