Released version 3-2015061300
[notion.git] / contrib / statusd / statusd_nmaild.lua
blob0e2777d15e7ec82819f8e241534150a4afe80776
1 -- Authors: Mario García H. <drosophila@nmental.com>
2 -- License: GPL, version 2
3 -- Last Changed: 2006-11-18
4 --
5 --------------------------------------------------------------------------------------------
6 --
7 -- PURPOSE:
8 -- Flexible Maildir monitor with configurable alarms and optional launcher for external
9 -- commands. The name "nmaild" stands for "aNy-mail-directories".
11 --------------------------------------------------------------------------------------------
12 --> Meters: %nmaild_new, %nmaild_read, %nmaild_allnew and %nmaild_allread <--
13 --------------------------------------------------------------------------------------------
15 -- If you do not customize default settings, this script detects the environment
16 -- variable "$MAILDIR". If that variable is not set, then ~/Maildir will be used as
17 -- default path. Otherwise, %nmaild_new and %nmaild_read are counters for new and
18 -- read emails into the first maildir found in settings. %nmaild_allnew and
19 -- %nmaild_allread are counters of new and read emails into the other mail directories
20 -- specified.
22 -- (Example: ~/Maildir/home, ~/Maildir/work, ~/Maildir/forum_lists, ~/Mail/FSCK,
23 -- spam, etc. where ~Maild/home is the main directory, others are counted as all*)
25 -- USAGE:
26 -- * If the directory ~/.ion3 does not exist create it and copy cfg_statusbar.lua
27 -- (the file is located somewhere in your /usr/share/ion3 or /usr/local/share/ion3
28 -- or /etc/X11/ion3 depending on ion3 installation). We need that file located on
29 -- our $HOME to edit and override Ion3 system configurations.
31 -- ** The file should look like this:
32 -- template = "[Mail %nmaild_new:%nmaild_all|%nmaild_allnew%nmaild_allread]"
33 --
34 -- After you restart ion3 (killall -USR1 ion3) or (session/restart on Ion menu),
35 -- the statusbar will look like this:
36 --
37 -- [Mail: 1:1|4:23] where |1:1| is meaning one mail new and one mail read in first dir.
39 -- You can change this script behavior by writting something like this in
40 -- cfg_statusbar.lua:
42 -----> CONFIGURATION EXAMPLE: ------------------------------------------------------------
44 -- nmaild = { Values not in config will be assumed from defaults
45 -- update_interval = 15*1000, --> Miliseconds (default is 15 seconds)
46 -- check = { --> Put here the list of maildirs to 'check' for.
47 -- "~/Maildir", Please, pay attention to the logical structure.
48 -- "~/Maildir/work",
49 -- "~/Maildir/copies",
50 -- "~/Security/spam",
51 -- "~/Maildir/lists",
52 -- "/xmail/office/susan",
53 -- "/Mail/common",
54 -- },
56 -- new = {"critical", 1}, --> Alarms: For new, read, allnew and allread.
57 -- read = {"important", 5}, --------------------------
58 -- allnew = {"critical", 5}, Syntax: mailfilter = {"hint", value},
59 -- allread = {"important", 10},
60 -- exec_on_new = "play ~/mew_wmail.mp3" --> Execute something on new email arrival.
61 -- If you want to deactivate exec_on_new,
62 -- just erase it from settings.
63 -- If you need to specify very complex commands
64 -- the best way is to replace the quotes for
65 -- [[ at start and ]] at the end.
67 -- }, --> Take care, write correct config endings.
68 --
69 -- Meanings:
70 -- --------------------------
71 -- "hint" means the color of alarm: If you are
72 -- daredevil, edit the file lookcommon_XXX.lua
73 -- (You must have a copy into ~/.ion3 directory)
74 -- to change colors or to add more (xcolors).
75 -- If 'value' reaches (is >= than) the number
76 -- you put here, alarms will be displayed !!.
78 -------------------------------------------------------------------------------------------
80 -- Internal cycles are provided by string.gsub() so, hopefully, we avoid unnecesary
81 -- use of lines and variables. This script will do only four (4) callings, no matters
82 -- the number of maildirs specified in cfg_statusbar.
84 -- VERSION: 1.a
85 --
86 -- LAST CHANGES:
87 -- Added "exec_on_new" function. You don't need to use it, but it does more fancy the
88 -- script. Now, it can do audio advices, launch your email client or show [g-k-x]message
89 -- when new emails are detected.
91 -- TO-DO:
92 -- To show the amounts of disk used. If someone finds interesting that addition...
94 -- LICENCE:
95 -- GPL2 Copyright (C) 2006 Mario García H.
96 -- See http://www.gnu.org/licenses/gpl.html to read complete licence)
98 -- T.STAMP: sat nov 18 01:03:50 COT 2006
100 -- CONTACT:
101 -- <drosophila (at) Nmental (dot) com>
103 ------ DEFAULT CONFIGURATION : -----------------------------------------------------------
105 local nmaild_timer
106 local last_count = { new = 0, allnew = 0 }
107 local defaults = {
108 update_interval = 5000,
109 check = { os.getenv("MAILDIR") or "~/Maildir" },
110 exec_on_new = false, --> Use this to play sounds , to execute an
111 new = { "critical", 1 }, -- email client, etc. on new email arrival.
112 read = { "important", 5 },
113 allnew = { "critical", 5 },
114 allread = {"important", 20 }
116 local settings = table.join(statusd.get_config("nmaild"), defaults)
118 ------ SCRIPT : --------------------------------------------------------------------------
120 local function exec_on_new()
121 if settings.exec_on_new then
122 os.execute(settings.exec_on_new.." 2>/dev/null &")
124 return
127 local get_count = function(nmaild, label)
128 local read_dirs = io.popen("du -a " ..nmaild.. " 2>/dev/null", "r")
129 local count = read_dirs:read("*a"); read_dirs:close()
130 _, count = string.gsub(count, "%d+%.%C+%.%C+", "") --> Simple filter.
132 local hint = count >= settings[label][2] and settings[label][1] or "normal"
133 statusd.inform("nmaild_" ..label, tostring(count))
134 statusd.inform("nmaild_" ..label.. "_hint", hint)
136 if (label == "new" or label == "allnew") and (count > last_count[label]) then
137 last_count[label] = count
138 exec_on_new()
139 return
141 if count == 0 and (label == "new" or label == "allnew") then
142 last_count[label] = 0
143 return
147 local function plan_count()
148 get_count(settings.check[1] .."/new", "new"); coroutine.yield()
149 get_count(settings.check[1] .."/cur", "read"); coroutine.yield()
150 get_count(table.concat(settings.check, "/new ", 2) .."/new", "allnew"); coroutine.yield()
151 get_count(table.concat(settings.check, "/cur ", 2) .."/cur", "allread")
152 return
155 local function update_nmaild()
156 local threads = coroutine.create(plan_count) --> Trying to avoid read bottlenecks ,>
157 while coroutine.resume(threads) do end --> Without threads the timer will die :(
158 nmaild_timer:set(settings.update_interval, update_nmaild)
161 nmaild_timer = statusd.create_timer()
162 update_nmaild()