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

2c7067c9458df12f7fc5ba3902c0195aa0c47c2f
[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 tonumber     = tonumber
16 local string       = { len    = string.len,
17                        format = string.format }
18
19 local setmetatable = setmetatable
20
21 -- Mail IMAP check
22 -- lain.widgets.imap
23 local imap = { stored = nil }
24
25 function worker(args)
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 encoding = args.encoding or nil
35     local maxlen   = args.maxlen or 200
36     local is_plain = args.is_plain or false
37     local settings = args.settings or function() end
38
39     local checkmail = helpers.scripts_dir .. "checkmail"
40
41     helpers.set_map(mail, true)
42     helpers.set_map(mail .. " count", "0")
43
44     if not is_plain
45     then
46         if not imap.stored
47         then
48             local f = io.popen(password)
49             password = f:read("*all"):gsub("\n", ""):gsub("\r", "")
50             f:close()
51             imap.stored = password
52         else
53             password = imap.stored
54         end
55     end
56
57     widget = wibox.widget.textbox('')
58
59     function update()
60         to_execute = string.format("%s -s %s -u %s -p %s --port %s",
61                      checkmail, server, mail, password, port) 
62
63         if encoding ~= nil
64         then
65             to_execute = string.format("%s --encoding %s",
66                          to_execute, encoding)
67         end
68
69         f = io.popen(to_execute)
70         ws = f:read("*all")
71         f:close()
72
73         mailcount = "0"
74
75         if ws:find("No new messages") ~= nil
76         then
77             helpers.set_map(mail, true)
78         elseif ws:find("CheckMailError: invalid credentials") ~= nil
79         then
80             helpers.set_map(mail, true)
81             mailcount = "invalid credentials"
82         else
83             mailcount = ws:match("%d") or "0"
84             if helpers.get_map(mail .. " count") ~= mailcount and mailcount ~= "0"
85             then
86                 helpers.set_map(mail, true)
87                 helpers.set_map(mail .. " count", mailcount)
88             end
89         end
90
91         notification_preset = {
92             icon     = helpers.icons_dir .. "mail.png",
93             timeout  = 8,
94             position = "top_left"
95         }
96
97         settings()
98
99
100         if helpers.get_map(mail) and tonumber(mailcount) >= 1
101         then
102             notify_title = ws:match(mail .. " has %d new message.?")
103             ws = ws:gsub(notify_title, "", 1):gsub("\n", "", 2)
104
105             -- trying to remove useless infos
106             ws = ws:gsub("--Content.%S+.-\n", "")
107             ws = ws:gsub("--%d+.-\n", "")
108
109             if string.len(ws) > maxlen
110             then
111                 ws = ws:sub(1, maxlen) .. "[...]"
112             end
113
114             notify_title = notify_title:gsub("\n", "")
115
116             naughty.notify({
117                 preset = notification_preset,
118                 title = notify_title,
119                 text = ws
120             })
121
122             helpers.set_map(mail, false)
123         end
124     end
125
126     helpers.newtimer(mail, timeout, update, true)
127
128     return widget
129 end
130
131 return setmetatable(imap, { __call = function(_, ...) return worker(...) end })