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

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