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

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