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

60a55090bbd79597c7d6dab22b5e475921b585e6
[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 shell        = require("awful.util").shell
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      = helpers.make_widget_textbox()
26     local args      = args or {}
27     local server    = args.server
28     local mail      = args.mail
29     local password  = args.password
30     local port      = args.port or 993
31     local timeout   = args.timeout or 60
32     local is_plain  = args.is_plain or false
33     local followtag = args.followtag or false
34     local settings  = args.settings or function() end
35
36     local head_command = "curl --connect-timeout 3 -fsm 3"
37     local request = "-X 'SEARCH (UNSEEN)'"
38
39     helpers.set_map(mail, 0)
40
41     if not is_plain then
42         helpers.async(string.format("%s -s '%s'", shell, password), function(f)
43             password = f:gsub("\n", "")
44         end)
45     end
46
47     function update()
48         mail_notification_preset = {
49             icon     = helpers.icons_dir .. "mail.png",
50             position = "top_left"
51         }
52
53         if followtag then
54             mail_notification_preset.screen = awful.screen.focused()
55         end
56
57         curl = string.format("%s -c '%s --url imaps://%s:%s/INBOX -u %s:%q %s -k'",
58                shell, head_command, server, port, mail, password, request)
59
60         helpers.async(curl, function(f)
61             _, mailcount = string.gsub(f, "%d+", "")
62
63             widget = imap.widget
64             settings()
65
66             if 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     helpers.newtimer(mail, timeout, update)
81
82     return setmetatable(imap, { __index = imap.widget })
83 end
84
85 return setmetatable({}, { __call = function(_, ...) return worker(...) end })