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