-- flaw, a Lua OO management framework for Awesome WM widgets. -- Copyright (C) 2010,2011 David Soulayrol -- 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 . -- 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. -- --

This module contains a provider for GMail contents and a text -- gadget.

-- --

Gadget

-- --

The gmail gadget can be instantiated by indexing the gadget -- module with text.gmail. The ID parameter has no -- meaning for this gadget, and it takes no particular parameters. By -- default, the gadget pattern is '$count messages'. See -- the gadget module documentation to -- learn about standard gadgets parameters.

-- --
-- g = flaw.gadget.text.gmail() --
-- --

The gmail gadget is updated every delay seconds, -- like any other gadget. However, clicking on the gadget triggers -- immediate mail check.

-- --

Provider

-- --

The gmail provider gets it information by downloading with -- curl the feed at -- https://mail.google.com/mail/feed/atom/unread. Of -- course, this URL needs authentication, so the file -- $HOME/.netrc should at least have a line with the -- following format:

-- --
-- machine mail.google.com login my_login password my_password --
-- --

This file is read by curl to proceed to -- authentication when asked. Since it keeps unobfuscated passwords, -- this file shall be readable only by the user.

-- --

The gmail provider data is composed of the following fields.

-- -- -- -- -- @author David Soulayrol <david.soulayrol AT gmail DOT com> -- @copyright 2010, David Soulayrol module('flaw.gmail') --- The gmail provider prototype. -- --

The gmail provider type is set to gmail._NAME.

-- -- @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("") ~= nil then depth = states.ENTRY else pattern = line:match("(.*)") if pattern ~= nil then current = current .. ' (' .. flaw.helper.strings.escape(pattern) .. ')' end end elseif depth == states.ENTRY then if line:match("") ~= nil then self.data.mails = self.data.mails .. current .. "\n" depth = states.ROOT elseif line:match("") ~= nil then depth = states.AUTHOR else pattern = line:match("(.*)") 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 = "" .. current .. "" end end elseif depth == states.ROOT then if line:match("") ~= nil then depth = states.ENTRY else self.data.count = line:match("([%d]+)") or self.data.count end end end end f:close() end --- A factory for GMail provider. -- --

Only one provider is built. It is stored in the global provider -- cache.

-- -- @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)