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