2013-09-07 12:06:42 +02:00
|
|
|
--[[
|
2017-09-04 12:43:00 +02:00
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
2017-09-11 06:51:52 +02:00
|
|
|
* (c) 2013, Luca CPZ
|
2017-09-04 12:43:00 +02:00
|
|
|
|
2013-09-07 12:06:42 +02:00
|
|
|
--]]
|
|
|
|
|
2023-09-05 22:29:10 +02:00
|
|
|
local requirePrefix = tostring(...):match(".*lain") or ""
|
|
|
|
if requirePrefix then
|
|
|
|
requirePrefix = requirePrefix .. "."
|
|
|
|
end
|
|
|
|
|
|
|
|
local helpers = require(requirePrefix .. "helpers")
|
2017-02-08 20:45:11 +01:00
|
|
|
local naughty = require("naughty")
|
|
|
|
local wibox = require("wibox")
|
2017-12-09 15:28:25 +01:00
|
|
|
local awful = require("awful")
|
2018-08-30 16:02:41 +02:00
|
|
|
local string = string
|
2017-02-08 20:45:11 +01:00
|
|
|
local type = type
|
|
|
|
local tonumber = tonumber
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2013-09-10 23:02:11 +02:00
|
|
|
-- Mail IMAP check
|
2017-02-08 14:15:48 +01:00
|
|
|
-- lain.widget.imap
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2017-02-08 20:45:11 +01:00
|
|
|
local function factory(args)
|
2020-11-29 22:48:32 +01:00
|
|
|
args = args or {}
|
|
|
|
|
2020-10-31 18:11:29 +01:00
|
|
|
local imap = { widget = args.widget or wibox.widget.textbox() }
|
2018-11-24 15:08:04 +01:00
|
|
|
local server = args.server
|
|
|
|
local mail = args.mail
|
|
|
|
local password = args.password
|
|
|
|
local port = args.port or 993
|
|
|
|
local timeout = args.timeout or 60
|
|
|
|
local pwdtimeout = args.pwdtimeout or 10
|
|
|
|
local is_plain = args.is_plain or false
|
|
|
|
local followtag = args.followtag or false
|
|
|
|
local notify = args.notify or "on"
|
|
|
|
local settings = args.settings or function() end
|
2017-01-21 00:29:31 +01:00
|
|
|
|
|
|
|
local head_command = "curl --connect-timeout 3 -fsm 3"
|
2018-11-24 15:08:04 +01:00
|
|
|
local request = "-X 'STATUS INBOX (MESSAGES RECENT UNSEEN)'"
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2017-01-23 22:27:37 +01:00
|
|
|
if not server or not mail or not password then return end
|
|
|
|
|
2018-11-24 15:08:04 +01:00
|
|
|
mail_notification_preset = {
|
|
|
|
icon = helpers.icons_dir .. "mail.png",
|
|
|
|
position = "top_left"
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:26:48 +02:00
|
|
|
helpers.set_map(mail, 0)
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2016-12-04 12:42:39 +01:00
|
|
|
if not is_plain then
|
2017-01-25 21:06:20 +01:00
|
|
|
if type(password) == "string" or type(password) == "table" then
|
|
|
|
helpers.async(password, function(f) password = f:gsub("\n", "") end)
|
|
|
|
elseif type(password) == "function" then
|
2018-11-24 15:08:04 +01:00
|
|
|
imap.pwdtimer = helpers.newtimer(mail .. "-password", pwdtimeout, function()
|
|
|
|
local retrieved_password, try_again = password()
|
|
|
|
if not try_again then
|
|
|
|
imap.pwdtimer:stop() -- stop trying to retrieve
|
|
|
|
password = retrieved_password or "" -- failsafe
|
|
|
|
end
|
|
|
|
end, true, true)
|
2017-01-25 21:06:20 +01:00
|
|
|
end
|
2013-09-10 23:02:11 +02:00
|
|
|
end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2018-11-24 15:08:04 +01:00
|
|
|
function imap.update()
|
|
|
|
-- do not update if the password has not been retrieved yet
|
|
|
|
if type(password) ~= "string" then return end
|
2015-07-29 13:21:59 +02:00
|
|
|
|
2018-11-24 15:08:04 +01:00
|
|
|
local curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:'%s' %s -k",
|
2018-08-31 12:11:38 +02:00
|
|
|
head_command, server, port, mail, password, request)
|
2013-09-12 02:26:48 +02:00
|
|
|
|
2017-01-20 20:58:22 +01:00
|
|
|
helpers.async(curl, function(f)
|
2018-11-24 15:08:04 +01:00
|
|
|
imap_now = { ["MESSAGES"] = 0, ["RECENT"] = 0, ["UNSEEN"] = 0 }
|
|
|
|
|
2018-11-28 20:43:04 +01:00
|
|
|
for s,d in f:gmatch("(%w+)%s+(%d+)") do imap_now[s] = tonumber(d) end
|
2018-11-24 15:08:04 +01:00
|
|
|
mailcount = imap_now["UNSEEN"] -- backwards compatibility
|
2015-10-20 15:17:44 +02:00
|
|
|
widget = imap.widget
|
2018-11-24 15:08:04 +01:00
|
|
|
|
2015-10-20 15:17:44 +02:00
|
|
|
settings()
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2018-08-31 12:11:38 +02:00
|
|
|
if notify == "on" and mailcount and mailcount >= 1 and mailcount > helpers.get_map(mail) then
|
2018-11-24 15:08:04 +01:00
|
|
|
if followtag then mail_notification_preset.screen = awful.screen.focused() end
|
|
|
|
naughty.notify {
|
|
|
|
preset = mail_notification_preset,
|
|
|
|
text = string.format("%s has <b>%d</b> new message%s", mail, mailcount, mailcount == 1 and "" or "s")
|
|
|
|
}
|
2014-08-05 12:59:19 +02:00
|
|
|
end
|
2018-11-28 20:43:04 +01:00
|
|
|
|
|
|
|
helpers.set_map(mail, imap_now["UNSEEN"])
|
2014-08-05 12:59:19 +02:00
|
|
|
end)
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2018-11-24 15:08:04 +01:00
|
|
|
imap.timer = helpers.newtimer(mail, timeout, imap.update, true, true)
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2017-01-25 17:13:14 +01:00
|
|
|
return imap
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2017-02-08 20:45:11 +01:00
|
|
|
return factory
|