Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_maildir.lua
blobbff1d70eb2843d2a7b0a30a82fdae3f448fc6b8c
1 -- Authors: Brett Parker <iDunno@sommitrealweird.co.uk>
2 -- License: GPL, version 2 or later
3 -- Last Changed: 2005
4 --
5 -- statusd_maildir.lua - Gets new and total counts of mails in a Maildir structure
6 -- Copyright (c) 2005 Brett Parker <iDunno@sommitrealweird.co.uk>
7 --
8 -- This program is free software; you can redistribute it and/or modify
9 -- it under the terms of the GNU General Public License as published by
10 -- the Free Software Foundation; either version 2 of the License, or
11 -- (at your option) any later version.
12 --
13 -- This program is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 -- GNU General Public License for more details.
17 --
18 -- You should have received a copy of the GNU General Public License
19 -- along with this program; if not, write to the Free Software
20 -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 -- This exports the variables %maildir_MAILDIRNAME, %maildir_MAILDIRNAME_new
23 -- and %maildir_MAILDIRNAME_total to the status bar where MAILDIRNAME is
24 -- the key in the maildirs setting.
26 -- The 2 settings available in the cfg_statusbar.lua are:
27 -- interval - this is the number of milliseconds between each check
28 -- maildirs - this is a key value list of Maildirs, the key is used
29 -- for MAILDIRNAME above.
31 -- The defaults update every 10 seconds with a maildir of ~/Maildir/
33 if not statusd_maildir then
34 statusd_maildir={
35 interval=10000,
36 maildirs = {INBOX="~/Maildir/"},
38 end
40 local settings = table.join (statusd.get_config("maildir"), statusd_maildir)
42 local function get_num_files(directory)
43 local f = io.popen('/bin/ls -U1 '..directory, 'r')
44 local count = 0
45 local line = f:read()
46 if line then
47 repeat
48 count = count + 1
49 line = f:read()
50 until not line
51 end
52 f:close()
53 return count
54 end
56 local function get_maildir_counts(maildir)
57 local newcount = get_num_files(maildir..'/new/')
58 local curcount = get_num_files(maildir..'/cur/')
59 return newcount, newcount + curcount
60 end
62 local maildir_timer
64 local function update_maildir()
65 for key, maildir in pairs(settings.maildirs) do
66 local new, total = get_maildir_counts(maildir)
67 statusd.inform("maildir_"..key, new.."/"..total)
68 statusd.inform("maildir_"..key.."_new", tostring(new))
69 statusd.inform("maildir_"..key.."_total", tostring(total))
70 if new>0 then
71 statusd.inform("maildir_"..key.."_hint", "important")
72 statusd.inform("maildir_"..key.."_new_hint", "important")
73 statusd.inform("maildir_"..key.."_total_hint", "important")
74 else
75 statusd.inform("maildir_"..key.."_hint", "normal")
76 statusd.inform("maildir_"..key.."_new_hint", "normal")
77 statusd.inform("maildir_"..key.."_total_hint", "normal")
78 end
79 end
80 maildir_timer:set(settings.interval, update_maildir)
81 end
83 maildir_timer = statusd.create_timer()
84 update_maildir()