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

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