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

#290 forgot fs
[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 = helpers.make_widget_textbox()
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 cmd          = args.cmd
30
31     function maildir.update()
32         if cmd then helpers.async({ awful.util.shell, "-c", cmd }, function() end) end
33
34         -- Find pathes to mailboxes.
35         local p = io.popen(string.format("find %s -mindepth 1 -maxdepth 2 -type d -not -name .git", mailpath))
36         local boxes = {}
37         repeat
38             line = p:read("*l")
39             if line then
40                 -- Find all files in the "new" subdirectory. For each
41                 -- file, print a single character (no newline). Don't
42                 -- match files that begin with a dot.
43                 -- Afterwards the length of this string is the number of
44                 -- new mails in that box.
45                 local mailstring = helpers.read_pipe(string.format("find %s /new -mindepth 1 -type f -not -name '.*' -printf a", line))
46
47                 -- Strip off leading mailpath.
48                 local box      = string.match(line, mailpath .. "/(.*)")
49                 local nummails = #mailstring
50
51                 if nummails > 0 then
52                     boxes[box] = nummails
53                 end
54             end
55         until not line
56         p:close()
57
58         local newmail = "no mail"
59         local total = 0
60
61         for box, number in helpers.spairs(boxes) do
62             -- Add this box only if it's not to be ignored.
63             if not helpers.element_in_table(box, ignore_boxes) then
64                 total = total + number
65                 if newmail == "no mail" then
66                     newmail = string.format("%s(%s)", box, number)
67                 else
68                     newmail = string.format("%s, %s(%s)", newmail, box, number)
69                 end
70             end
71         end
72
73         widget = maildir.widget
74         settings()
75     end
76
77     maildir.timer = helpers.newtimer(mailpath, timeout, maildir.update, true, true)
78
79     return maildir
80 end
81
82 return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })