2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
|
|
|
* (c) 2013, Luke Bonham
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local helpers = require("lain.helpers")
|
2014-08-05 12:59:19 +02:00
|
|
|
local async = require("lain.asyncshell")
|
2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
local naughty = require("naughty")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
|
2015-07-29 13:21:59 +02:00
|
|
|
local mouse = mouse
|
2013-09-12 02:26:48 +02:00
|
|
|
local string = { format = string.format,
|
|
|
|
gsub = string.gsub }
|
2013-09-13 18:15:25 +02:00
|
|
|
local tonumber = tonumber
|
2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
|
2013-09-10 23:02:11 +02:00
|
|
|
-- Mail IMAP check
|
2013-09-07 12:06:42 +02:00
|
|
|
-- lain.widgets.imap
|
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
local function worker(args)
|
2015-07-29 13:21:59 +02:00
|
|
|
local imap = {}
|
|
|
|
local args = args or {}
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2015-07-29 13:21:59 +02:00
|
|
|
local server = args.server
|
|
|
|
local mail = args.mail
|
|
|
|
local password = args.password
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2015-07-29 13:21:59 +02:00
|
|
|
local port = args.port or 993
|
|
|
|
local timeout = args.timeout or 60
|
|
|
|
local is_plain = args.is_plain or false
|
|
|
|
local followmouse = args.followmouse or false
|
|
|
|
local settings = args.settings or function() end
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2014-09-08 18:51:48 +02:00
|
|
|
local head_command = "curl --connect-timeout 3 -fsm 3"
|
2013-09-12 02:26:48 +02:00
|
|
|
local request = "-X 'SEARCH (UNSEEN)'"
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-12 02:26:48 +02:00
|
|
|
helpers.set_map(mail, 0)
|
2013-09-07 12:06:42 +02:00
|
|
|
|
|
|
|
if not is_plain
|
|
|
|
then
|
2015-08-13 01:56:37 +02:00
|
|
|
password = helpers.read_pipe(password):gsub("\n", "")
|
2013-09-10 23:02:11 +02:00
|
|
|
end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-11 19:39:14 +02:00
|
|
|
imap.widget = wibox.widget.textbox('')
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-13 00:07:44 +02:00
|
|
|
function update()
|
|
|
|
mail_notification_preset = {
|
2013-09-12 02:26:48 +02:00
|
|
|
icon = helpers.icons_dir .. "mail.png",
|
|
|
|
position = "top_left"
|
|
|
|
}
|
2013-09-13 18:15:25 +02:00
|
|
|
|
2015-07-29 13:21:59 +02:00
|
|
|
if followmouse then
|
|
|
|
mail_notification_preset.screen = mouse.screen
|
|
|
|
end
|
|
|
|
|
2014-11-25 22:14:56 +01:00
|
|
|
curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%q %s -k",
|
2013-09-12 02:26:48 +02:00
|
|
|
head_command, server, port, mail, password, request)
|
|
|
|
|
2014-08-08 14:25:21 +02:00
|
|
|
async.request(curl, function(f)
|
2015-08-05 12:28:54 +02:00
|
|
|
_, mailcount = string.gsub(f, "%d+", "")
|
2014-08-05 12:59:19 +02:00
|
|
|
_ = nil
|
|
|
|
|
2015-10-20 15:17:44 +02:00
|
|
|
widget = imap.widget
|
|
|
|
settings()
|
2014-08-05 12:59:19 +02:00
|
|
|
|
|
|
|
if mailcount >= 1 and mailcount > helpers.get_map(mail)
|
|
|
|
then
|
|
|
|
if mailcount == 1 then
|
|
|
|
nt = mail .. " has one new message"
|
|
|
|
else
|
|
|
|
nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
|
|
|
|
end
|
|
|
|
naughty.notify({
|
|
|
|
preset = mail_notification_preset,
|
2015-07-28 20:43:47 +02:00
|
|
|
text = nt
|
2014-08-05 12:59:19 +02:00
|
|
|
})
|
|
|
|
end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2014-08-05 12:59:19 +02:00
|
|
|
helpers.set_map(mail, mailcount)
|
|
|
|
end)
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2013-09-13 00:07:44 +02:00
|
|
|
helpers.newtimer(mail, timeout, update, true)
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2014-06-13 09:44:36 +02:00
|
|
|
return setmetatable(imap, { __index = imap.widget })
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2014-06-13 09:44:36 +02:00
|
|
|
return setmetatable({}, { __call = function(_, ...) return worker(...) end })
|