| 1 | -- new e-mails count and last e-mail subject on Gmail |
| 2 | -- Copyright (C) 2010 Adrian C. <[email protected]> |
| 3 | -- Copyright (C) 2017 mutlusun <[email protected]> |
| 4 | -- Copyright (C) 2017 starenka <[email protected]> |
| 5 | -- Copyright (C) 2018-2019 Nguyễn Gia Phong <[email protected]m> |
| 6 | -- |
| 7 | -- This file is part of Vicious. |
| 8 | -- |
| 9 | -- Vicious is free software: you can redistribute it and/or modify |
| 10 | -- it under the terms of the GNU General Public License as |
| 11 | -- published by the Free Software Foundation, either version 2 of the |
| 12 | -- License, or (at your option) any later version. |
| 13 | -- |
| 14 | -- Vicious is distributed in the hope that it will be useful, |
| 15 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | -- GNU General Public License for more details. |
| 18 | -- |
| 19 | -- You should have received a copy of the GNU General Public License |
| 20 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 21 | |
| 22 | -- {{{ Grab environment |
| 23 | local type = type |
| 24 | local tonumber = tonumber |
| 25 | local string = { match = string.match } |
| 26 | |
| 27 | local helpers = require("vicious.helpers") |
| 28 | local spawn = require("vicious.spawn") |
| 29 | -- }}} |
| 30 | |
| 31 | -- Gmail: provides count of new and subject of last e-mail on Gmail |
| 32 | -- vicious.widgets.gmail |
| 33 | local gmail_all = {} |
| 34 | |
| 35 | -- {{{ Gmail widget type |
| 36 | local function parse(warg, stdout, stderr, exitreason, exitcode) |
| 37 | local count = -- Count comes before messages and matches at least 0 |
| 38 | tonumber(string.match(stdout, "<fullcount>([%d]+)</fullcount>")) or 0 |
| 39 | |
| 40 | -- Find subject tag |
| 41 | local title = string.match(stdout, "<entry>.-<title>(.-)</title>") or "N/A" |
| 42 | |
| 43 | -- Check if we should scroll, or maybe truncate |
| 44 | if type(warg) == "number" then |
| 45 | title = helpers.truncate(title, warg) |
| 46 | elseif type(warg) == "table" then |
| 47 | title = helpers.scroll(title, warg[1], warg[2]) |
| 48 | end |
| 49 | |
| 50 | return { ["{count}"] = count, ["{subject}"] = title } |
| 51 | end |
| 52 | |
| 53 | function gmail_all.async(format, warg, callback) |
| 54 | -- Get info from the Gmail atom feed using curl --netrc. |
| 55 | -- With username 'user' and password 'pass' |
| 56 | -- $HOME/.netrc should look similar to: |
| 57 | -- machine mail.google.com login user password pass |
| 58 | -- BE AWARE THAT MAKING THESE SETTINGS IS A SECURITY RISK! |
| 59 | spawn.easy_async("curl -fsn https://mail.google.com/mail/feed/atom", |
| 60 | function (...) callback(parse(warg, ...)) end) |
| 61 | end |
| 62 | -- }}} |
| 63 | |
| 64 | return helpers.setasyncall(gmail_all) |