aboutsummaryrefslogtreecommitdiff
path: root/widgets/mboxc.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
commit237470c8f45190b213e3a173ce6ae1a74b3e11fe (patch)
tree7f53c8144761947d4bde20715bcad34f4be0d6c0 /widgets/mboxc.lua
parent9a82d4113a8271b7dfc7506f2b07379e3ede89a8 (diff)
downloadvicious-legacy-237470c8f45190b213e3a173ce6ae1a74b3e11fe.tar.xz
API: transform widgets namespace table to a directory
Diffstat (limited to 'widgets/mboxc.lua')
-rw-r--r--widgets/mboxc.lua55
1 files changed, 55 insertions, 0 deletions
diff --git a/widgets/mboxc.lua b/widgets/mboxc.lua
new file mode 100644
index 0000000..c5f694c
--- /dev/null
+++ b/widgets/mboxc.lua
@@ -0,0 +1,55 @@
+---------------------------------------------------
+-- Licensed under the GNU General Public License v2
+-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
+---------------------------------------------------
+
+-- {{{ Grab environment
+local io = { open = io.open }
+local setmetatable = setmetatable
+local string = { find = string.find }
+-- }}}
+
+
+-- Mboxc: provides the count of total, old and new messages in mbox files
+module("vicious.widgets.mboxc")
+
+
+-- {{{ Mbox count widget type
+local function worker(format, warg)
+ -- Initialise counters
+ local count = { old = 0, total = 0, new = 0 }
+
+ -- Get data from mbox files
+ for i=1, #warg do
+ local f = io.open(warg[i])
+
+ while true do
+ -- Read the mbox line by line, if we are going to read
+ -- some *HUGE* folders then switch to reading chunks
+ 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
+ end
+ f:close()
+ end
+
+ -- Substract total from old to get the new count
+ count.new = count.total - count.old
+
+ return {count.total, count.old, count.new}
+end
+-- }}}
+
+setmetatable(_M, { __call = function(_, ...) return worker(...) end })