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

readme updated
[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 = {}
24
25 local 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     imap.widget = wibox.widget.textbox('')
58
59     notification_preset = {
60         icon     = helpers.icons_dir .. "mail.png",
61         timeout  = 8,
62         position = "top_left"
63     }
64
65     function imap.update()
66         to_execute = string.format("%s -s %s -u %s -p %s --port %s",
67                      checkmail, server, mail, password, port) 
68
69         if encoding ~= nil
70         then
71             to_execute = string.format("%s --encoding %s",
72                          to_execute, encoding)
73         end
74
75         f = io.popen(to_execute)
76         ws = f:read("*all")
77         f:close()
78
79         mailcount = "0"
80
81         if ws:find("No new messages") ~= nil
82         then
83             helpers.set_map(mail, true)
84         elseif ws:find("CheckMailError: invalid credentials") ~= nil
85         then
86             helpers.set_map(mail, true)
87             mailcount = "invalid credentials"
88         else
89             mailcount = ws:match("%d") or "0"
90             if helpers.get_map(mail .. " count") ~= mailcount and mailcount ~= "0"
91             then
92                 helpers.set_map(mail, true)
93                 helpers.set_map(mail .. " count", mailcount)
94             end
95         end
96
97         widget = imap.widget
98         settings()
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, imap.update, true)
127
128     return imap.widget
129 end
130
131 return setmetatable(imap, { __call = function(_, ...) return worker(...) end })