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

6574276221e54de1f138dc1e6389ddf9d781f576
[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
18 local setmetatable = setmetatable
19
20 -- Mail IMAP check
21 -- lain.widgets.imap
22 local imap = {}
23
24 local function worker(args)
25     local args     = args or {}
26
27     local server   = args.server
28     local mail     = args.mail
29     local password = args.password
30
31     local port     = args.port or 993
32     local timeout  = args.timeout or 60
33     local is_plain = args.is_plain or false
34     local settings = args.settings or function() end
35
36     local head_command  = "curl --connect-timeout 1 -fsm 3"
37     local request = "-X 'SEARCH (UNSEEN)'"
38
39     helpers.set_map(mail, 0)
40
41     if not is_plain
42     then
43         local f = io.popen(password)
44         password = f:read("*all"):gsub("\n", "")
45         f:close()
46     end
47
48     imap.widget = wibox.widget.textbox('')
49
50     function update()
51         mail_notification_preset = {
52             icon     = helpers.icons_dir .. "mail.png",
53             position = "top_left"
54         }
55         
56         curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%s %s -k",
57                head_command, server, port, mail, password, request)
58
59         f = io.popen(curl)
60         ws = f:read("*all")
61         f:close()
62
63         t, mailcount = string.gsub(ws, "%d", "")
64         t = nil -- because it's useless
65
66         widget = imap.widget
67         settings()
68
69         if mailcount > helpers.get_map(mail) and mailcount >= 1
70         then
71             if mailcount == 1 then
72                 nt = mail .. " has one new message"
73             else
74                 nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
75             end
76             naughty.notify({ preset = mail_notification_preset, text = nt })
77         end
78
79         helpers.set_map(mail, mailcount)
80     end
81
82     helpers.newtimer(mail, timeout, update, true)
83     return imap.widget
84 end
85
86 return setmetatable(imap, { __call = function(_, ...) return worker(...) end })