2019-09-14 05:33:40 +02:00
|
|
|
-- widget type providing the count of total, old and new messages in mbox files
|
|
|
|
-- Copyright (C) 2010 Adrian C. <anrxc@sysphere.org>
|
|
|
|
-- Copyright (C) 2017 mutlusun <mutlusun@github.com>
|
|
|
|
--
|
|
|
|
-- This file is part of Vicious.
|
|
|
|
--
|
|
|
|
-- Vicious is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as
|
|
|
|
-- published by the Free Software Foundation, either version 2 of the
|
|
|
|
-- License, or (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- Vicious is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
|
2009-07-31 07:49:28 +02:00
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local io = { open = io.open }
|
2019-09-14 05:33:40 +02:00
|
|
|
local helpers = require"vicious.helpers"
|
2009-07-31 07:49:28 +02:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Mbox count widget type
|
2019-10-16 09:42:31 +02:00
|
|
|
return helpers.setcall(function (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
|
2019-09-14 05:33:40 +02:00
|
|
|
local _, from = lines:find"^From[%s]"
|
2009-10-04 00:31:12 +02:00
|
|
|
if from ~= nil then count.total = count.total + 1 end
|
|
|
|
|
|
|
|
-- Read messages have the Status header
|
2019-09-14 05:33:40 +02:00
|
|
|
local _, status = lines:find"^Status:[%s]RO$"
|
2009-10-04 00:31:12 +02:00
|
|
|
if status ~= nil then count.old = count.old + 1 end
|
|
|
|
|
|
|
|
-- Skip the folder internal data
|
2019-09-14 05:33:40 +02:00
|
|
|
local _, int = lines:find"^Subject:[%s].*FOLDER[%s]INTERNAL[%s]DATA"
|
2009-10-04 00:31:12 +02:00
|
|
|
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}
|
2019-10-16 09:42:31 +02:00
|
|
|
end)
|
2009-07-31 07:49:28 +02:00
|
|
|
-- }}}
|