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

some fixes
[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 markup       = require("lain.util.markup")
10 local helpers      = require("lain.helpers")
11
12 local awful        = require("awful")
13 local beautiful    = require("beautiful")
14 local naughty      = require("naughty")
15 local wibox        = require("wibox")
16
17 local io           = io
18 local tonumber     = tonumber
19 local string       = string
20
21 local setmetatable = setmetatable
22
23 -- Mail imap check
24 -- lain.widgets.imap
25 local imap = {} 
26
27 function worker(args)
28     local args = args or {}
29
30     local server = args.server
31     local mail = args.mail
32     local password = args.password
33
34     local port = args.port or "993"
35     local refresh_timeout = args.refresh_timeout or 60
36     local header = args.header or " Mail "
37     local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
38     local color = args.color or beautiful.fg_focus or "#FFFFFF"
39     local mail_encoding = args.mail_encoding or nil
40     local maxlen = args.maxlen or 200
41     local app = args.app or "mutt"
42     local is_plain = args.is_plain or false
43     local shadow = args.shadow or false
44
45     helpers.set_map(mail, true)
46     helpers.set_map(mail .. " count", "0")
47
48     local checkmail = helpers.scripts_dir .. "checkmail"
49
50     if not is_plain
51     then
52         local f = io.popen(password)
53         password = f:read("*all"):gsub("\n", ""):gsub("\r", "")
54         f:close()
55     end
56
57     local myimapcheck = wibox.widget.textbox()
58
59     local myimapcheckupdate = function()
60         function set_nomail()
61             if shadow
62             then
63                 myimapcheck:set_text('')
64             else
65                 myimapcheck:set_markup(markup(color, " no mail "))
66             end
67         end
68
69         conn = io.popen("ip link show")
70         check_conn = conn:read("*all") 
71         conn:close()
72
73         if not check_conn:find("state UP") then
74                set_nomail()
75                return
76         end
77
78         to_execute = checkmail .. ' -s ' .. server ..
79                      ' -u ' .. mail .. ' -p ' .. password
80                      .. ' --port ' .. port
81
82         if mail_encoding ~= nil
83         then
84             to_execute = to_execute .. ' --encoding '
85                          .. mail_encoding
86         end
87
88         f = io.popen(to_execute)
89         ws = f:read("*all")
90         f:close()
91
92         if ws:find("No new messages") ~= nil
93         then
94             helpers.set_map(mail, true)
95             set_nomail()
96         elseif ws:find("CheckMailError: invalid credentials") ~= nil
97         then
98             helpers.set_map(mail, true)
99             myimapcheck:set_markup(markup(header_color, header) ..
100                                    markup(color, "invalid credentials "))
101         else
102             mailcount = ws:match("%d") or "?"
103
104             if helpers.get_map(mail .. " count") ~= mailcount and mailcount ~= "?"
105             then
106                 helpers.set_map(mail, true)
107                 helpers.set_map(mail .. " count", mailcount)
108             end
109
110             myimapcheck:set_markup(markup(header_color, header) ..
111                                    markup(color, mailcount) .. " ")
112
113             if helpers.get_map(mail)
114             then
115                 if mailcount == "?"
116                 -- May happens sometimes using keyrings or other password fetchers.
117                 -- Since this should be automatically fixed in short times, we threat
118                 -- this exception delaying the update to the next timeout.
119                 then
120                     set_nomail()
121                     return
122                 elseif tonumber(mailcount) >= 1
123                 then
124                     notify_title = ws:match(mail .. " has %d new message.?")
125                     ws = ws:gsub(notify_title, "", 1):gsub("\n", "", 2)
126
127                     ws = ws:gsub("--Content.%S+.-\n", "")
128                     ws = ws:gsub("--%d+.-\n", "")
129
130                     if string.len(ws) > maxlen
131                     then
132                         ws = ws:sub(1, maxlen) .. "[...]"
133                     end
134
135                     notify_title = notify_title:gsub("\n", "")
136                 end
137
138                 naughty.notify({ title = notify_title,
139                                  fg = color,
140                                  text = ws,
141                                  icon = beautiful.lain_mail_notify or
142                                         helpers.icons_dir .. "mail.png",
143                                  timeout = 8,
144                                  position = "top_left" })
145
146                 helpers.set_map(mail, false)
147             end
148         end
149     end
150
151     local myimapchecktimer = timer({ timeout = refresh_timeout })
152     myimapchecktimer:connect_signal("timeout", myimapcheckupdate)
153     myimapchecktimer:start()
154     myimapcheck:buttons(awful.util.table.join(
155         awful.button({}, 0,
156
157             function()
158                 helpers.run_in_terminal(app)
159             end)
160     ))
161
162     return myimapcheck
163 end
164
165 return setmetatable(imap, { __call = function(_, ...) return worker(...) end })