2 -- ion/mod_statusbar/ion-statusd/statusd_mail.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2008.
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
12 -- The keyword for this monitor
16 update_interval=10*1000,
17 retry_interval=60*10*1000,
18 mbox = os.getenv("MAIL"),
22 local settings=table.join(statusd.get_config(mon), defaults)
24 local function TR(s, ...)
25 return string.format(statusd.gettext(s), unpack(arg))
28 local function check_spool()
29 if not settings.mbox then
30 statusd.warn(TR("MAIL environment variable not set "..
31 "and no spool given."))
35 if not settings.files["spool"] then
37 settings.files["spool"] = settings.mbox
38 elseif not(settings.files["spool"] == mbox) then
39 statusd.warn(TR("%s.mbox does not match %s.files['spool']; using %s.mbox",
42 settings.files["spool"] = settings.mbox
45 local function calcmail(fname)
46 local f, err=io.open(fname, 'r')
47 local total, read, old=0, 0, 0
49 local in_headers=false
50 local had_status=false
58 if had_blank and string.find(l, '^From ') then
70 elseif in_headers and not had_status then
71 local st, en, stat=string.find(l, '^Status:(.*)')
74 if string.find(l, 'R') then
77 if string.find(l, 'O') then
87 return total, total-read, total-old
92 local mail_timestamps = {}
93 function init_timestamps ()
94 for key, val in pairs(settings.files) do
95 mail_timestamps[key]=-2.0
100 local function update_mail()
102 for key, mbox in pairs(settings.files) do
104 error(TR(key.." not set"))
107 local old_tm=mail_timestamps[key]
108 mail_timestamps[key]=statusd.last_modified(mbox)
110 if mail_timestamps[key]>old_tm then
111 local mail_total, mail_unread, mail_new=calcmail(mbox)
112 if failed == nil then
113 failed = not mail_total
115 failed = failed and (not mail_total)
118 if key == "spool" then
124 statusd.inform(meter.."_new", tostring(mail_new))
125 statusd.inform(meter.."_unread", tostring(mail_unread))
126 statusd.inform(meter.."_total", tostring(mail_total))
129 statusd.inform(meter.."_new_hint", "important")
131 statusd.inform(meter.."_new_hint", "normal")
134 if mail_unread>0 then
135 statusd.inform(meter.."_unread_hint", "important")
137 statusd.inform(meter.."_unread_hint", "normal")
144 statusd.warn(TR("Disabling mail monitor for %d seconds.",
145 settings.retry_interval/1000))
147 mail_timer:set(settings.retry_interval, update_mail)
151 mail_timer:set(settings.update_interval, update_mail)
154 mail_timer=statusd.create_timer()