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

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