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

febff2344979cfe27c589bdffd159036dd4225a1
[etc/awesome.git] / widgets / imap.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6                                                   
7 --]]
8
9 local helpers      = require("lain.helpers")
10
11 local naughty      = require("naughty")
12 local wibox        = require("wibox")
13
14 local io           = { popen  = io.popen }
15 local string       = { format = string.format,
16                        gsub   = string.gsub }
17 local tonumber     = tonumber
18
19 local setmetatable = setmetatable
20
21 -- Mail IMAP check
22 -- lain.widgets.imap
23 local imap = {}
24
25 local function worker(args)
26     local args     = args or {}
27
28     local server   = args.server
29     local mail     = args.mail
30     local password = args.password
31
32     local port     = args.port or 993
33     local timeout  = args.timeout or 60
34     local is_plain = args.is_plain or false
35     local settings = args.settings or function() end
36
37     local head_command  = "curl --connect-timeout 1 -fsm 3"
38     local request = "-X 'SEARCH (UNSEEN)'"
39
40     helpers.set_map(mail, 0)
41
42     if not is_plain
43     then
44         local f = io.popen(password)
45         password = f:read("*all"):gsub("\n", "")
46         f:close()
47     end
48
49     imap.widget = wibox.widget.textbox('')
50
51     function update()
52         mail_notification_preset = {
53             icon     = helpers.icons_dir .. "mail.png",
54             position = "top_left"
55         }
56
57         curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%s %s -k",
58                head_command, server, port, mail, password, request)
59
60         f = io.popen(curl)
61         ws = f:read("*all")
62         f:close()
63
64         _, mailcount = string.gsub(ws, "%d+", "")
65         _ = nil
66
67         widget = imap.widget
68         settings()
69
70         if mailcount >= 1 and mailcount > helpers.get_map(mail)
71         then
72             if mailcount == 1 then
73                 nt = mail .. " has one new message"
74             else
75                 nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
76             end
77             naughty.notify({ preset = mail_notification_preset, text = nt })
78         end
79
80         helpers.set_map(mail, mailcount)
81     end
82
83     helpers.newtimer(mail, timeout, update, true)
84     return imap.widget
85 end
86
87 return setmetatable(imap, { __call = function(_, ...) return worker(...) end })