-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail.lua
200 lines (180 loc) · 6.25 KB
/
gmail.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
-- flaw, a Lua OO management framework for Awesome WM widgets.
-- Copyright (C) 2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- Grab environment.
local io = io
local os = os
local awful = {
button = require("awful.button"),
util = require("awful.util"),
}
local flaw = {
helper = require('flaw.helper'),
gadget = require('flaw.gadget'),
provider = require('flaw.provider')
}
--- GMail report gadget and provider.
--
-- <p>This module contains a provider for GMail contents and a text
-- gadget.</p>
--
-- <h2>Gadget</h2>
--
-- <p>The gmail gadget can be instantiated by indexing the gadget
-- module with <code>text.gmail</code>. The ID parameter has no
-- meaning for this gadget, and it takes no particular parameters. By
-- default, the gadget pattern is <code>'$count messages'</code>. See
-- the <a href="flaw.gadget.html">gadget</a> module documentation to
-- learn about standard gadgets parameters.</p>
--
-- <div class='example'>
-- g = flaw.gadget.text.gmail()
-- </div>
--
-- <p>The gmail gadget is updated every <code>delay</code> seconds,
-- like any other gadget. However, clicking on the gadget triggers
-- immediate mail check.</p>
--
-- <h2>Provider</h2>
--
-- <p>The gmail provider gets it information by downloading with
-- <code>curl</code> the feed at
-- <code>https://mail.google.com/mail/feed/atom/unread</code>. Of
-- course, this URL needs authentication, so the file
-- <code>$HOME/.netrc</code> should at least have a line with the
-- following format:</p>
--
-- <div class='example'>
-- machine mail.google.com login my_login password my_password
-- </div>
--
-- <p>This file is read by <code>curl</code> to proceed to
-- authentication when asked. Since it keeps unobfuscated passwords,
-- this file shall be readable only by the user.</p>
--
-- <p>The gmail provider data is composed of the following fields.</p>
--
-- <ul>
--
-- <li><code>timestamp</code>
--
-- <p>The date of the last check.</p></li>
--
-- <li><code>count</code>
--
-- <p>The current number of unread messages.</p></li>
--
-- <li><code>mails</code>
--
-- <p>The unread threads. This text block is intended to be used in a
-- notification popup or a tooltip, like this:</p>
--
-- <div class='example'>
-- gmail_gadget:set_tooltip('Unread threads at $timestamp\n$mails')
-- </div></li>
--
-- </ul>
--
--
-- @author David Soulayrol <david.soulayrol AT gmail DOT com>
-- @copyright 2010, David Soulayrol
module('flaw.gmail')
--- The gmail provider prototype.
--
-- <p>The gmail provider type is set to gmail._NAME.</p>
--
-- @class table
-- @name GMailProvider
GMailProvider = flaw.provider.CyclicProvider:new{ type = _NAME }
--- Callback for provider refresh.
function GMailProvider:do_refresh()
local states = { ROOT = 0, ENTRY = 1, AUTHOR = 2 }
local depth = states.ROOT
local current = ''
local pattern = ''
local feed = 'https://mail.google.com/mail/feed/atom/unread'
local f = io.popen("curl --connect-timeout 1 -m 3 -fsn " .. feed)
if 0 ~= f:seek("end") then
self.data.timestamp = os.date('%H:%M')
self.data.count = '0'
self.data.mails = ''
f:seek("set")
for line in f:lines() do
if depth == states.AUTHOR then
if line:match("</author>") ~= nil then
depth = states.ENTRY
else
pattern = line:match("<name>(.*)</name>")
if pattern ~= nil then
current = current .. ' (' .. flaw.helper.strings.escape(pattern) .. ')'
end
end
elseif depth == states.ENTRY then
if line:match("</entry>") ~= nil then
self.data.mails = self.data.mails .. current .. "\n"
depth = states.ROOT
elseif line:match("<author>") ~= nil then
depth = states.AUTHOR
else
pattern = line:match("<title>(.*)</title>")
if pattern ~= nil then
current = flaw.helper.strings.crop(
flaw.helper.strings.escape(pattern), 42)
-- Remove HTML entities that would be now truncated.
local i = current:find("&")
if i ~= nil and current:find(";") == nil then
current = current:sub(0, i - 1) .. "..."
end
current = "<i>" .. current .. "</i>"
end
end
elseif depth == states.ROOT then
if line:match("<entry>") ~= nil then
depth = states.ENTRY
else
self.data.count =
line:match("<fullcount>([%d]+)</fullcount>") or self.data.count
end
end
end
end
f:close()
end
--- A factory for GMail provider.
--
-- <p>Only one provider is built. It is stored in the global provider
-- cache.</p>
--
-- @return a brand new gmail provider, or the existing one if found in
-- the providers cache.
function provider_factory()
local p = flaw.provider.get(_NAME, '')
-- Create the provider if necessary.
if p == nil then
p = GMailProvider:new{
id = '', data = { timestamp = 'N/A', count = "0", mails = '' } }
flaw.provider.add(p)
end
return p
end
-- A Text gadget prototype for GMail status display.
GMailTextGadget = flaw.gadget.TextGadget:new{}
-- Create the wrapped gadget.
function GMailTextGadget:create(wopt)
flaw.gadget.TextGadget.create(self, wopt)
self.widget:buttons(
awful.util.table.join(
awful.button({ }, 1, function() self.provider:refresh(self) end)))
end
-- A Text gadget prototype for GMail summary display.
flaw.gadget.register.text(
_M, { delay = 300, pattern = '$count messages' }, {}, GMailTextGadget)