]> git.madduck.net Git - etc/awesome.git/blob - widgets/maildir.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

wiki updated
[etc/awesome.git] / widgets / maildir.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local newtimer        = require("lain.helpers").newtimer
11 local read_pipe       = require("lain.helpers").read_pipe
12
13 local wibox           = require("wibox")
14
15 local util            = require("lain.util")
16
17 local io              = { popen  = io.popen }
18 local os              = { getenv = os.getenv }
19 local pairs           = pairs
20 local string          = { len    = string.len,
21                           match  = string.match }
22 local table           = { sort   = table.sort }
23
24 local setmetatable    = setmetatable
25
26 -- Maildir check
27 -- lain.widgets.maildir
28 local maildir = { total = 0 }
29
30 local function worker(args)
31     local args         = args or {}
32     local timeout      = args.timeout or 60
33     local mailpath     = args.mailpath or os.getenv("HOME") .. "/Mail"
34     local ignore_boxes = args.ignore_boxes or {}
35     local settings     = args.settings or function() end
36
37     maildir.widget = wibox.widget.textbox('')
38
39     function update()
40         -- Find pathes to mailboxes.
41         local p = io.popen("find " .. mailpath ..
42                            " -mindepth 1 -maxdepth 1 -type d" ..
43                            " -not -name .git")
44         local boxes = {}
45         repeat
46             line = p:read("*l")
47             if line ~= nil
48             then
49                 -- Find all files in the "new" subdirectory. For each
50                 -- file, print a single character (no newline). Don't
51                 -- match files that begin with a dot.
52                 -- Afterwards the length of this string is the number of
53                 -- new mails in that box.
54                 local mailstring = read_pipe("find " .. line ..
55                                     "/new -mindepth 1 -type f " ..
56                                     "-not -name '.*' -printf a")
57
58                 -- Strip off leading mailpath.
59                 local box = string.match(line, mailpath .. "/*([^/]+)")
60                 local nummails = string.len(mailstring)
61                 if nummails > 0
62                 then
63                     boxes[box] = nummails
64                 end
65             end
66         until line == nil
67
68         p:close()
69         table.sort(boxes)
70
71         newmail = "no mail"
72         -- Count the total number of mails irrespective of where it was found
73         total = 0
74
75         for box, number in pairs(boxes)
76         do
77             -- Add this box only if it's not to be ignored.
78             if not util.element_in_table(box, ignore_boxes)
79             then
80                 total = total + number
81                 if newmail == "no mail"
82                 then
83                     newmail = box .. "(" .. number .. ")"
84                 else
85                     newmail = newmail .. ", " ..
86                               box .. "(" .. number .. ")"
87                 end
88             end
89         end
90
91         if maildir.total ~= total then
92             widget = maildir.widget
93             settings()
94         end
95     end
96
97     newtimer(mailpath, timeout, update, true)
98     return maildir.widget
99 end
100
101 return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })