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

widget.imap: ensure mailcount is not nil before notifying; closes #400
[etc/awesome.git] / 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 is_plain  = args.is_plain or false
28     local followtag = args.followtag or false
29     local notify    = args.notify or "on"
30     local settings  = args.settings or function() end
31
32     local head_command = "curl --connect-timeout 3 -fsm 3"
33     local request = "-X 'SEARCH (UNSEEN)'"
34
35     if not server or not mail or not password then return end
36
37     helpers.set_map(mail, 0)
38
39     if not is_plain then
40         if type(password) == "string" or type(password) == "table" then
41             helpers.async(password, function(f) password = f:gsub("\n", "") end)
42         elseif type(password) == "function" then
43             local p = password()
44         end
45     end
46
47     function update()
48         mail_notification_preset = {
49             icon     = helpers.icons_dir .. "mail.png",
50             position = "top_left"
51         }
52
53         if followtag then
54             mail_notification_preset.screen = awful.screen.focused()
55         end
56
57         local curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%q %s -k",
58                      head_command, server, port, mail, password, request)
59
60         helpers.async(curl, function(f)
61             mailcount = tonumber(f:match("UNSEEN (%d+)"))
62             widget = imap.widget
63             settings()
64
65             if notify == "on" and mailcount and mailcount >= 1 and mailcount > helpers.get_map(mail) then
66                 local nt = mail .. " has <b>" .. mailcount .. "</b> new message"
67                 if mailcount >= 1 then nt = nt .. "s" end
68                 naughty.notify { preset = mail_notification_preset, text = nt }
69             end
70
71             helpers.set_map(mail, mailcount)
72         end)
73
74     end
75
76     imap.timer = helpers.newtimer(mail, timeout, update, true, true)
77
78     return imap
79 end
80
81 return factory