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

pulsebar: corrected colors typo; closes #251
[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 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
24 local function worker(args)
25     local imap        = {}
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 followtag   = args.followtag or false
36     local settings    = args.settings or function() end
37
38     local head_command  = "curl --connect-timeout 3 -fsm 3"
39     local request = "-X 'SEARCH (UNSEEN)'"
40
41     helpers.set_map(mail, 0)
42
43     if not is_plain then
44         password = helpers.read_pipe(password):gsub("\n", "")
45     end
46
47     imap.widget = wibox.widget.textbox('')
48
49     function update()
50         mail_notification_preset = {
51             icon     = helpers.icons_dir .. "mail.png",
52             position = "top_left"
53         }
54
55         if followtag then
56             mail_notification_preset.screen = awful.screen.focused()
57         end
58
59         curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%q %s -k",
60                head_command, server, port, mail, password, request)
61
62         async.request(curl, function(f)
63             _, mailcount = string.gsub(f, "%d+", "")
64             _ = nil
65
66             widget = imap.widget
67             settings()
68
69             if mailcount >= 1 and mailcount > helpers.get_map(mail)
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({
77                     preset = mail_notification_preset,
78                     text = nt
79                 })
80             end
81
82             helpers.set_map(mail, mailcount)
83         end)
84
85     end
86
87     helpers.newtimer(mail, timeout, update, true)
88
89     return setmetatable(imap, { __index = imap.widget })
90 end
91
92 return setmetatable({}, { __call = function(_, ...) return worker(...) end })