awesome/awesome-wm-widgets/email-widget/email.lua

101 lines
3.0 KiB
Lua
Raw Normal View History

2018-11-29 11:39:53 +01:00
local os = require("os")
2018-11-28 15:31:35 +01:00
local wibox = require("wibox")
local awful = require("awful")
local naughty = require("naughty")
local watch = require("awful.widget.watch")
local path_to_icons = "/usr/share/icons/Arc/actions/22/"
2018-11-29 11:39:53 +01:00
local home = os.getenv('HOME')
local email_widget = wibox.widget.textbox()
2018-11-28 15:31:35 +01:00
email_widget:set_font('Play 9')
2018-11-29 11:39:53 +01:00
local email_icon = wibox.widget.imagebox()
2018-11-28 15:31:35 +01:00
email_icon:set_image(path_to_icons .. "/mail-mark-new.png")
2018-11-29 11:39:53 +01:00
local previous_value = 0
2021-11-16 18:06:38 +01:00
local password1 = ''
local password2 = ''
local password3 = ''
2022-01-06 10:40:34 +01:00
awful.spawn.easy_async_with_shell('pass web/mail.ovh.com; pass web/webmail.gandi.net; pass web/cas.inp-toulouse.fr;', function(stdout)
2021-11-16 18:06:38 +01:00
s = split(stdout, '\n')
password1 = s[1]
password2 = s[2]
password3 = s[3]
watch(
2022-01-06 10:40:34 +01:00
"bash -c '" .. home .. "/.config/awesome/awesome-wm-widgets/email-widget/count_unread_emails.py \"" .. password1 .. "\" \"" .. password2 .. "\" \"" .. password3 .. "\"'", 60,
2021-11-16 18:06:38 +01:00
function(widget, stdout, stderr, exitreason, exitcode)
local unread_emails_num = tonumber(stdout) or 0
if previous_value < unread_emails_num then
previous_value = unread_emails_num
show_emails(10)
end
email_widget:set_text(stdout)
if (unread_emails_num > 0) then
email_icon:set_image(path_to_icons .. "/mail-mark-unread.png")
elseif (unread_emails_num == 0) then
email_icon:set_image(path_to_icons .. "/mail-message-new.png")
end
end
)
end)
2018-12-02 12:36:50 +01:00
function split(str, delimiter)
if str == nil then
return {}
end
local ret = {''}
count = 1
for i = 1, string.len(str) do
local c = str:sub(i,i)
if c == delimiter then
count = count + 1
ret[count] = ''
else
ret[count] = ret[count] .. c
end
end
return ret
end
2018-11-29 11:39:53 +01:00
function show_emails(timeout)
2022-01-06 10:40:34 +01:00
awful.spawn.easy_async_with_shell(home .. "/.config/awesome/awesome-wm-widgets/email-widget/read_unread_emails.py \"" .. password1 .. "\" \"" .. password2 .. "\" \"" .. password3 .."\"",
2018-11-29 11:39:53 +01:00
function(stdout, stderr, reason, exit_code)
2018-12-02 12:36:50 +01:00
s = split(stderr, '\n')
local actions = {}
if stderr ~= '' then
for index, line in pairs(s) do
if index > 1 then
actions["Check email from account " .. tostring(index - 1)] = function ()
2018-12-05 20:32:37 +01:00
awful.spawn.easy_async("firefox " .. s[index - 1], function() end)
2018-12-02 12:36:50 +01:00
end
end
end
end
2018-11-28 15:31:35 +01:00
naughty.notify{
text = stdout,
title = "Unread Emails",
2018-11-29 11:39:53 +01:00
timeout = timeout,
2018-11-28 15:31:35 +01:00
width = 400,
2018-12-02 12:36:50 +01:00
actions = actions,
2018-11-28 15:31:35 +01:00
}
end
)
end
2018-11-29 11:39:53 +01:00
email_icon:connect_signal("button::press", function() show_emails(5) end)
return {
icon = email_icon,
widget = email_widget
}