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
|
|
|
---------------------------------------------------
|
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 }
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
2009-10-01 11:46:28 +02:00
|
|
|
-- Mboxc: provides the count of total, old and new messages in mbox files
|
2012-06-15 18:07:05 +02:00
|
|
|
-- vicious.widgets.mboxc
|
2017-01-25 17:53:03 +01:00
|
|
|
local mboxc_all = {}
|
2009-07-31 07:49:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Mbox count widget type
|
2009-11-03 01:38:43 +01:00
|
|
|
local function worker(format, warg)
|
2010-03-14 03:37:40 +01:00
|
|
|
if not warg then return end
|
|
|
|
|
2010-04-02 01:08:12 +02:00
|
|
|
-- Initialize counters
|
2009-10-04 00:31:12 +02:00
|
|
|
local count = { old = 0, total = 0, new = 0 }
|
2009-10-01 11:46:28 +02:00
|
|
|
|
|
|
|
-- Get data from mbox files
|
2009-11-03 01:38:43 +01:00
|
|
|
for i=1, #warg do
|
|
|
|
local f = io.open(warg[i])
|
2009-10-01 11:46:28 +02:00
|
|
|
|
|
|
|
while true do
|
2010-01-29 16:41:40 +01:00
|
|
|
-- Read the mbox line by line, if we are going to read
|
|
|
|
-- some *HUGE* folders then switch to reading chunks
|
2009-10-04 00:31:12 +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 count.total = count.total + 1 end
|
|
|
|
|
|
|
|
-- Read messages have the Status header
|
|
|
|
local _, status = string.find(lines, "^Status:[%s]RO$")
|
|
|
|
if status ~= nil then count.old = count.old + 1 end
|
|
|
|
|
|
|
|
-- Skip the folder internal data
|
|
|
|
local _, int = string.find(lines, "^Subject:[%s].*FOLDER[%s]INTERNAL[%s]DATA")
|
|
|
|
if int ~= nil then count.total = count.total - 1 end
|
2009-10-01 11:46:28 +02:00
|
|
|
end
|
|
|
|
f:close()
|
2009-07-31 07:49:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Substract total from old to get the new count
|
2009-10-01 11:46:28 +02:00
|
|
|
count.new = count.total - count.old
|
2009-07-31 07:49:28 +02:00
|
|
|
|
2009-10-01 11:46:28 +02:00
|
|
|
return {count.total, count.old, count.new}
|
2009-07-31 07:49:28 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
2017-01-25 17:53:03 +01:00
|
|
|
return setmetatable(mboxc_all, { __call = function(_, ...) return worker(...) end })
|