]> 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:

net.lua: support wifi/ethernet connection indicators
[etc/awesome.git] / widget / imap.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013, Luke Bonham
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   = { format = string.format,
13                    gsub   = string.gsub }
14 local type     = type
15 local tonumber = tonumber
16
17 -- Mail IMAP check
18 -- lain.widget.imap
19
20 local function factory(args)
21     local imap      = { widget = wibox.widget.textbox() }
22     local args      = args or {}
23     local server    = args.server
24     local mail      = args.mail
25     local password  = args.password
26     local port      = args.port or 993
27     local timeout   = args.timeout or 60
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 'SEARCH (UNSEEN)'"
35
36     if not server or not mail or not password then return end
37
38     helpers.set_map(mail, 0)
39
40     if not is_plain then
41         if type(password) == "string" or type(password) == "table" then
42             helpers.async(password, function(f) password = f:gsub("\n", "") end)
43         elseif type(password) == "function" then
44             local p = password()
45         end
46     end
47
48     function update()
49         mail_notification_preset = {
50             icon     = helpers.icons_dir .. "mail.png",
51             position = "top_left"
52         }
53
54         if followtag then
55             mail_notification_preset.screen = awful.screen.focused()
56         end
57
58         curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%q %s -k",
59                head_command, server, port, mail, password, request)
60
61         helpers.async(curl, function(f)
62             _, mailcount = string.gsub(f, "%d+", "")
63             widget = imap.widget
64             settings()
65
66             if notify == "on" and mailcount >= 1 and mailcount > helpers.get_map(mail) then
67                 if mailcount == 1 then
68                     nt = mail .. " has one new message"
69                 else
70                     nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
71                 end
72                 naughty.notify { preset = mail_notification_preset, text = nt }
73             end
74
75             helpers.set_map(mail, mailcount)
76         end)
77
78     end
79
80     imap.timer = helpers.newtimer(mail, timeout, update, true, true)
81
82     return imap
83 end
84
85 return factory