]> git.madduck.net Git - etc/awesome.git/blob - .config/awesome/lain/widget/imap.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:

fix virt-manager tagging
[etc/awesome.git] / .config / awesome / lain / widget / imap.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013, Luca CPZ
5
6 --]]
7
8 local helpers  = require("lain.helpers")
9 local naughty  = require("naughty")
10 local wibox    = require("wibox")
11 local awful    = require("awful")
12 local string   = string
13 local type     = type
14 local tonumber = tonumber
15
16 -- Mail IMAP check
17 -- lain.widget.imap
18
19 local function factory(args)
20     local imap       = { widget = wibox.widget.textbox() }
21     local args       = args or {}
22     local server     = args.server
23     local mail       = args.mail
24     local password   = args.password
25     local port       = args.port or 993
26     local timeout    = args.timeout or 60
27     local pwdtimeout = args.pwdtimeout or 10
28     local is_plain   = args.is_plain or false
29     local followtag  = args.followtag or false
30     local notify     = args.notify or "on"
31     local settings   = args.settings or function() end
32
33     local head_command = "curl --connect-timeout 3 -fsm 3"
34     local request = "-X 'STATUS INBOX (MESSAGES RECENT UNSEEN)'"
35
36     if not server or not mail or not password then return end
37
38     mail_notification_preset = {
39         icon     = helpers.icons_dir .. "mail.png",
40         position = "top_left"
41     }
42
43     helpers.set_map(mail, 0)
44
45     if not is_plain then
46         if type(password) == "string" or type(password) == "table" then
47             helpers.async(password, function(f) password = f:gsub("\n", "") end)
48         elseif type(password) == "function" then
49             imap.pwdtimer = helpers.newtimer(mail .. "-password", pwdtimeout, function()
50                 local retrieved_password, try_again = password()
51                 if not try_again then
52                     imap.pwdtimer:stop() -- stop trying to retrieve
53                     password = retrieved_password or "" -- failsafe
54                 end
55             end, true, true)
56         end
57     end
58
59     function imap.update()
60         -- do not update if the password has not been retrieved yet
61         if type(password) ~= "string" then return end
62
63         local curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:'%s' %s -k",
64                      head_command, server, port, mail, password, request)
65
66         helpers.async(curl, function(f)
67             imap_now = { ["MESSAGES"] = 0, ["RECENT"] = 0, ["UNSEEN"] = 0 }
68
69             for s,d in f:gmatch("(%w+)%s+(%d+)") do imap_now[s] = tonumber(d) end
70             mailcount = imap_now["UNSEEN"] -- backwards compatibility
71             widget = imap.widget
72
73             settings()
74
75             if notify == "on" and mailcount and mailcount >= 1 and mailcount > helpers.get_map(mail) then
76                 if followtag then mail_notification_preset.screen = awful.screen.focused() end
77                 naughty.notify {
78                     preset = mail_notification_preset,
79                     text   = string.format("%s has <b>%d</b> new message%s", mail, mailcount, mailcount == 1 and "" or "s")
80                 }
81             end
82
83             helpers.set_map(mail, imap_now["UNSEEN"])
84         end)
85
86     end
87
88     imap.timer = helpers.newtimer(mail, timeout, imap.update, true, true)
89
90     return imap
91 end
92
93 return factory