aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2009-07-31 07:49:28 +0200
committerAdrian C. (anrxc) <anrxc@sysphere.org>2009-07-31 07:49:28 +0200
commit475da04ed1199d87121be886e1636a1cbbf7f0d2 (patch)
treebf22e47a49ded85acb1a1083e0e3da19af2ce0c8
parentdad393b94b508e4ce94b66c4c209e29f89830472 (diff)
downloadvicious-legacy-475da04ed1199d87121be886e1636a1cbbf7f0d2.tar.xz
Mbox mail count widget included.
The, mboxc, widget supplements the mbox widget. The mbox widget returns the subject of the last e-mail in a mbox, while the mboxc widget returns the count of total, old and new messages. Dealing with potentially *HUGE* files is not easy, I tried to find some middle ground for now, comments in the file discuss it in detail. Having the LuaFileSystem lib would be nice, so we could do caching and avoid 90% of the reads, but I didn't rely on external libraries to this point so I'm not going to start now.
-rw-r--r--README4
-rw-r--r--mboxc.lua57
2 files changed, 61 insertions, 0 deletions
diff --git a/README b/README
index 1a33232..1206353 100644
--- a/README
+++ b/README
@@ -122,6 +122,10 @@ vicious.widgets.mbox
- provides the subject of last e-mail in a mbox file
- takes the full path to the mbox as an argument
+vicious.widgets.mboxc
+ - provides the count of total, old and new messages in a mbox
+ - takes the full path to the mbox as an argument
+
vicious.widgets.mdir
- provides a number of new and unread messages in a Maildir
structure
diff --git a/mboxc.lua b/mboxc.lua
new file mode 100644
index 0000000..b549b69
--- /dev/null
+++ b/mboxc.lua
@@ -0,0 +1,57 @@
+----------------------------------------------------------
+-- Licensed under the GNU General Public License version 2
+-- * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
+----------------------------------------------------------
+
+-- {{{ Grab environment
+local io = { open = io.open }
+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
+function worker(format, mbox)
+ -- Initialise counters
+ local old = 0
+ local total = 0
+
+ -- Open the mbox
+ --
+ -- If we had LuaFileSystem we could check the mtime and size of
+ -- the file and if they didn't change since the last read we could
+ -- return the old, cached, count. However, we didn't rely on extra
+ -- libraries to this point so we won't start now.
+ local f = io.open(mbox)
+
+ while true do
+ -- Read the mbox line by line, if we are going to read some
+ -- *HUGE* folders then switch to reading chunks. It's why we are
+ -- not reading the whole file at once in the first place.
+ 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
+-- }}}