-- statusd_maildir.lua -- -- Gets new and total counts of mails in a Maildir structure -- Written by Brett Parker -- -- This exports the variables %maildir_MAILDIRNAME, %maildir_MAILDIRNAME_new -- and %maildir_MAILDIRNAME_total to the status bar where MAILDIRNAME is -- the key in the maildirs setting. -- -- The settings available in the cfg_statusbar.lua are: -- interval - this is the number of milliseconds between each check -- maildirs - this is a key value list of Maildirs, the key is used -- for MAILDIRNAME above. -- important - this is a key value list, the key is the same as used for -- the maildirs key, the value is the number of mails to count -- as important, anything over this threshold sets the hint to -- important -- critical - same as important, but sets the hint to be critical -- -- The defaults update every 10 seconds with a maildir of ~/Maildir/, important -- of 0 and critical of 10. -- -- You are free to distribute this software under the terms of the GNU -- General Public License Version 2. -- local defaults={ interval=10000, maildirs = {INBOX="~/Maildir/"}, important = { default=0 }, critical = { default=10 }, } local settings = table.join (statusd.get_config("maildir"), defaults) local function get_num_files(directory) local f = io.popen('/bin/ls -U1 '..directory, 'r') local count = 0 local line = f:read() if line then repeat count = count + 1 line = f:read() until not line end f:close() return count end local function get_maildir_counts(maildir) local newcount = get_num_files(maildir..'/new/') local curcount = get_num_files(maildir..'/cur/') return newcount, newcount + curcount end local function get_maildir_status(maildir,newcount) local important = settings.important[maildir] local critical = settings.critical[maildir] if important == nil then important = settings.important.default end if critical == nil then critical = settings.critical.default end if newcount > critical then return "critical" elseif newcount > important then return "important" end return "normal" end local maildir_timer local function update_maildir() for key, maildir in settings.maildirs do local new, total = get_maildir_counts(maildir) statusd.inform("maildir_"..key, new.."/"..total) statusd.inform("maildir_"..key.."_new", tostring(new)) statusd.inform("maildir_"..key.."_total", tostring(total)) statusd.inform("maildir_"..key.."_hint", get_maildir_status(key, new)) statusd.inform("maildir_"..key.."_new_hint", get_maildir_status(key, new)) statusd.inform("maildir_"..key.."_total_hint", get_maildir_status(key, new)) end maildir_timer:set(settings.interval, update_maildir) end if settings.important.default == nil then settings.important.default = defaults.important.default end if settings.critical.default == nil then settings.critical.default = defaults.critical.default end maildir_timer = statusd.create_timer() update_maildir()