]> 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:

bef09f4a4527d22d2033064c6dd759b7933f64ad
[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 awful        = require("awful")
11 local wibox        = require("wibox")
12 local helpers      = require("lain.helpers")
13 local io           = { popen  = io.popen }
14 local os           = { getenv = os.getenv }
15 local string       = { format = string.format,
16                        match  = string.match }
17 local setmetatable = setmetatable
18
19 -- Maildir check (synchronous)
20 -- lain.widgets.maildir
21 local maildir = {}
22
23 local function worker(args)
24     local args         = args or {}
25     local timeout      = args.timeout or 60
26     local mailpath     = args.mailpath or os.getenv("HOME") .. "/Mail"
27     local ignore_boxes = args.ignore_boxes or {}
28     local settings     = args.settings or function() end
29     local ext_mail_cmd = args.external_mail_cmd
30
31     maildir.widget = wibox.widget.textbox()
32
33     function update()
34         if ext_mail_cmd then awful.spawn(ext_mail_cmd) end
35
36         -- Find pathes to mailboxes.
37         local p = io.popen(string.format("find %s -mindepth 1 -maxdepth 2 -type d -not -name .git", mailpath))
38         local boxes = {}
39         repeat
40             line = p:read("*l")
41             if line then
42                 -- Find all files in the "new" subdirectory. For each
43                 -- file, print a single character (no newline). Don't
44                 -- match files that begin with a dot.
45                 -- Afterwards the length of this string is the number of
46                 -- new mails in that box.
47                 local mailstring = helpers.read_pipe(string.format("find %s /new -mindepth 1 -type f -not -name '.*' -printf a", line))
48
49                 -- Strip off leading mailpath.
50                 local box      = string.match(line, mailpath .. "/(.*)")
51                 local nummails = #mailstring
52
53                 if nummails > 0 then
54                     boxes[box] = nummails
55                 end
56             end
57         until not line
58         p:close()
59
60         local newmail = "no mail"
61         local total = 0
62
63         for box, number in helpers.spairs(boxes) do
64             -- Add this box only if it's not to be ignored.
65             if not helpers.element_in_table(box, ignore_boxes) then
66                 total = total + number
67                 if newmail == "no mail" then
68                     newmail = string.format("%s(%s)", box, number)
69                 else
70                     newmail = string.format("%s, %s(%s)", newmail, box, number)
71                 end
72             end
73         end
74
75         widget = maildir.widget
76         settings()
77     end
78
79     helpers.newtimer(mailpath, timeout, update, true)
80
81     return setmetatable(maildir, { __index = maildir.widget })
82 end
83
84 return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })