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

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