2 -- ion/mod_statusbar/ion-statusd/statusd_mail.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2009.
6 -- See the included file LICENSE for details.
9 -- The keyword for this monitor
13 update_interval
=10*1000,
14 retry_interval
=60*10*1000,
15 mbox
= os
.getenv("MAIL"),
19 local settings
=table.join(statusd
.get_config(mon
), defaults
)
21 local function TR(s
, ...)
22 return string.format(statusd
.gettext(s
), unpack(arg
))
25 local function check_spool()
26 if not settings
.mbox
then
27 statusd
.warn(TR("MAIL environment variable not set "..
28 "and no spool given."))
32 if not settings
.files
["spool"] then
34 settings
.files
["spool"] = settings
.mbox
35 elseif not(settings
.files
["spool"] == mbox
) then
36 statusd
.warn(TR("%s.mbox does not match %s.files['spool']; using %s.mbox",
39 settings
.files
["spool"] = settings
.mbox
42 local function calcmail(fname
)
43 local f
, err
=io
.open(fname
, 'r')
44 local total
, read, old
=0, 0, 0
46 local in_headers
=false
47 local had_status
=false
55 if had_blank
and string.find(l
, '^From ') then
67 elseif in_headers
and not had_status
then
68 local st
, en
, stat
=string.find(l
, '^Status:(.*)')
71 if string.find(l
, 'R') then
74 if string.find(l
, 'O') then
84 return total
, total
-read, total
-old
89 local mail_timestamps
= {}
90 function init_timestamps ()
91 for key
, val
in pairs(settings
.files
) do
92 mail_timestamps
[key
]=-2.0
97 local function update_mail()
99 for key
, mbox
in pairs(settings
.files
) do
101 error(TR(key
.." not set"))
104 local old_tm
=mail_timestamps
[key
]
105 mail_timestamps
[key
]=statusd
.last_modified(mbox
)
107 if mail_timestamps
[key
]>old_tm
then
108 local mail_total
, mail_unread
, mail_new
=calcmail(mbox
)
109 if failed
== nil then
110 failed
= not mail_total
112 failed
= failed
and (not mail_total
)
115 if key
== "spool" then
121 statusd
.inform(meter
.."_new", tostring(mail_new
))
122 statusd
.inform(meter
.."_unread", tostring(mail_unread
))
123 statusd
.inform(meter
.."_total", tostring(mail_total
))
126 statusd
.inform(meter
.."_new_hint", "important")
128 statusd
.inform(meter
.."_new_hint", "normal")
131 if mail_unread
>0 then
132 statusd
.inform(meter
.."_unread_hint", "important")
134 statusd
.inform(meter
.."_unread_hint", "normal")
141 statusd
.warn(TR("Disabling mail monitor for %d seconds.",
142 settings
.retry_interval
/1000))
144 mail_timer
:set(settings
.retry_interval
, update_mail
)
148 mail_timer
:set(settings
.update_interval
, update_mail
)
151 mail_timer
=statusd
.create_timer()