lain/widgets/maildir.lua

94 lines
3.2 KiB
Lua
Raw Normal View History

2014-01-29 11:56:32 +01:00
2013-09-07 12:06:42 +02:00
--[[
Licensed under GNU General Public License v2
* (c) 2013, Luke Bonham
* (c) 2010-2012, Peter Hofmann
--]]
2017-01-09 20:21:56 +01:00
local spawn = require("awful").spawn
local wibox = require("wibox")
2013-09-07 12:06:42 +02:00
2017-01-09 20:21:56 +01:00
local element_in_table = require("lain.helpers").element_in_table
local newtimer = require("lain.helpers").newtimer
local read_pipe = require("lain.helpers").read_pipe
local spairs = require("lain.helpers").spairs
2013-09-07 12:06:42 +02:00
2017-01-09 20:21:56 +01:00
local io = { popen = io.popen }
local os = { getenv = os.getenv }
local pairs = pairs
local string = { format = string.format,
len = string.len,
match = string.match }
2017-01-09 20:21:56 +01:00
local setmetatable = setmetatable
2013-09-07 12:06:42 +02:00
-- Maildir check
-- lain.widgets.maildir
2015-10-20 15:17:44 +02:00
local maildir = {}
2013-09-07 12:06:42 +02:00
2013-09-11 19:39:14 +02:00
local function worker(args)
2013-09-10 23:02:11 +02:00
local args = args or {}
local timeout = args.timeout or 60
local mailpath = args.mailpath or os.getenv("HOME") .. "/Mail"
2013-09-07 12:06:42 +02:00
local ignore_boxes = args.ignore_boxes or {}
2013-09-10 23:02:11 +02:00
local settings = args.settings or function() end
local ext_mail_cmd = args.external_mail_cmd
2013-09-10 23:02:11 +02:00
2013-09-11 19:39:14 +02:00
maildir.widget = wibox.widget.textbox('')
2013-09-10 23:02:11 +02:00
2013-09-13 00:07:44 +02:00
function update()
2017-01-09 20:21:56 +01:00
if ext_mail_cmd then awful.spawn(ext_mail_cmd) end
2015-11-22 09:16:35 +01:00
2013-09-07 12:06:42 +02:00
-- Find pathes to mailboxes.
2017-01-09 20:21:56 +01:00
local p = io.popen(string.format("find %s -mindepth 1 -maxdepth 2 -type d -not -name .git", mailpath))
2013-09-07 12:06:42 +02:00
local boxes = {}
repeat
line = p:read("*l")
2017-01-09 20:21:56 +01:00
if line then
2013-09-07 12:06:42 +02:00
-- Find all files in the "new" subdirectory. For each
-- file, print a single character (no newline). Don't
-- match files that begin with a dot.
-- Afterwards the length of this string is the number of
-- new mails in that box.
2017-01-09 20:21:56 +01:00
local mailstring = read_pipe(string.format("find %s /new -mindepth 1 -type f -not -name '.*' -printf a", line))
2013-09-07 12:06:42 +02:00
-- Strip off leading mailpath.
2017-01-09 20:21:56 +01:00
local box = string.match(line, mailpath .. "/(.*)")
2013-09-07 12:06:42 +02:00
local nummails = string.len(mailstring)
2017-01-09 20:21:56 +01:00
if nummails > 0 then
2013-09-07 12:06:42 +02:00
boxes[box] = nummails
end
end
2017-01-09 20:21:56 +01:00
until not line
2015-11-18 09:02:16 +01:00
p:close()
2013-09-07 12:06:42 +02:00
2017-01-09 20:21:56 +01:00
local newmail = "no mail"
local total = 0
2016-12-04 12:42:39 +01:00
2017-01-09 20:21:56 +01:00
for box, number in spairs(boxes) do
2013-09-07 12:06:42 +02:00
-- Add this box only if it's not to be ignored.
2017-01-09 20:21:56 +01:00
if not util.element_in_table(box, ignore_boxes) then
total = total + number
2017-01-09 20:21:56 +01:00
if newmail == "no mail" then
newmail = string.format("%s(%s)", box, number)
2013-09-07 12:06:42 +02:00
else
2017-01-09 20:21:56 +01:00
newmail = string.format("%s, %s(%s)", newmail, box, number)
2013-09-07 12:06:42 +02:00
end
end
end
2015-10-20 15:17:44 +02:00
widget = maildir.widget
2017-01-09 20:21:56 +01:00
2015-10-20 15:17:44 +02:00
settings()
2013-09-07 12:06:42 +02:00
end
2013-09-13 00:07:44 +02:00
newtimer(mailpath, timeout, update, true)
2017-01-09 20:21:56 +01:00
return setmetatable(maildir, { __index = maildir.widget })
2013-09-07 12:06:42 +02:00
end
return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })