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

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