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

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