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