Author Topic: conky - Gmail new email information (bash version)  (Read 1901 times)

hakerdefo

  • Posts: 524
    • View Profile
    • Looking at Linux through the Windows of Life
conky - Gmail new email information (bash version)
« on: November 25, 2016, 04:04:18 PM »
As promised, ladies & gentlemen, here is a bash script to get new email info from your Gmail account for dislay in conky  8)

First we need to make sure that the script dependencies are installed,

Code: [Select]
sudo apt-get install xmlstarlet
sudo apt-get install curl
sudo apt-get install wget
sudo apt-get install coreutils

On VSIDO three of the above four should most likely be already installed  8)

Next open your favourite text-editor and copy the script,

Code: [Select]
#!/usr/bin/env bash
###the-script-begins###
###
type curl >/dev/null 2>&1 || { printf "\033c"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; echo -e '\e[38;5;82m'"A required dependency \"cURL\" is unavailable"; echo -e '\e[38;5;82m'"\"cURL\" is available for almost all linux distros."; echo -e '\e[38;5;82m'"Please install \"curl\" using your package manager."; echo -e '\e[38;5;82m'"Please visit the homepage of \"cURL\" for more info"; echo -e '\e[38;5;82m'"https://curl.haxx.se/"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; tput sgr0; exit 1; }
type wget >/dev/null 2>&1 || { printf "\033c"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; echo -e '\e[38;5;82m'"A required dependency \"Wget\" is unavailable"; echo -e '\e[38;5;82m'"\"Wget\" is available for almost all linux distros."; echo -e '\e[38;5;82m'"Please install \"wget\" using your package manager."; echo -e '\e[38;5;82m'"Please visit the homepage of \"Wget\" for more info"; echo -e '\e[38;5;82m'"https://www.gnu.org/software/wget/"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; tput sgr0; exit 1; }
type xmlstarlet >/dev/null 2>&1 || { printf "\033c"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; echo -e '\e[38;5;82m'"A required dependency \"XMLStarlet\" is unavailable"; echo -e '\e[38;5;82m'"\"XMLStarlet\" is available for almost all linux distros."; echo -e '\e[38;5;82m'"Please install \"xmlstarlet\" using your package manager."; echo -e '\e[38;5;82m'"Please visit the homepage of \"XMLStarlet\" for more info"; echo -e '\e[38;5;82m'"http://xmlstar.sourceforge.net/"; echo -e ""; for i in {16..51} {51..16}; do echo -en "\e[38;5;${i}m#\e[0m"; done; echo; echo -e ""; tput sgr0; exit 1; }
wget --spider --user-agent="Mozilla/5.0 Gecko/20100101" --timeout=30 -q "www.google.com" -O /dev/null
RETVAL=$?
if [[ ! "$RETVAL" == 0 ]]; then
echo -e ""
echo -e "Internet Connection Seems Down."
echo -e ""
exit
fi
local_user="gmail_username"
local_pass="gmail_password"
local_count="5"
local_rm=$(which rm)
curl --silent --connect-timeout 30 --user-agent "Mozilla/5.0 Gecko/20100101" "https://"$local_user":"$local_pass"@mail.google.com/mail/feed/atom" -o /tmp/cgmail.xml
grep "401" "/tmp/cgmail.xml"
RETVAL=$?
if [[ "$RETVAL" == 0 ]]; then
echo -e ""
echo -e "Incorrect username or password."
echo -e ""
"$local_rm" "/tmp/cgmail.xml"
exit
fi
mail_count=$(xmlstarlet sel -N x="http://purl.org/atom/ns#" -t -m '//x:feed' -v  x:fullcount -n /tmp/cgmail.xml)
if [[ "$local_count" == 0 ]]; then
echo -e ""
echo -e "$local_user :: No New Email."
echo -e ""
"$local_rm" "/tmp/cgmail.xml"
exit
fi
if (( "$local_count" < "$mail_count" )); then
final_count="$local_count"
else
final_count="$mail_count"
fi
local_subject=$(cat "/tmp/cgmail.xml" | xmlstarlet sel -N x="http://purl.org/atom/ns#" -t -m "//x:entry[position()<=$final_count]" -v x:title -n)
mail_date=$(cat "/tmp/cgmail.xml" | xmlstarlet sel -N x="http://purl.org/atom/ns#" -t -m "//x:entry[position()<=$final_count]" -v x:issued -n)
local_date=$(while IFS= read -r line ; do date -d"$line" +%y-%m-%d ; done <<< "$mail_date")
echo -e ""
echo -e "$local_user :: \"$mail_count\" New Emails."
echo -e ""
pr -m -t <(echo "$local_date") <(echo "$local_subject")
echo -e ""
"$local_rm" "/tmp/cgmail.xml"
exit 0
###
###the-script-ends###


Go to the line 15-16 in the script and change "gmail_username" &  "gmail_password" to reflect your Google account credentials,

Code: [Select]
local_user="gmail_username"
local_pass="gmail_password"

The script by default prints info of the "5" newest emails. You can change this number on line 17 to display as many new emails as you wish,

Code: [Select]
local_count="5"

Save the script somewhere in your $PATH with the name "check-gmail" and give it execute permissions,

Code: [Select]
chmod 755 /path/to/check-gmail

And finally add following line in your ".conkyrc" to call the script from conky,

Code: [Select]
execi 600 bash /path/to/check-gmail

Of-course "/path/to/check-gmail" above will have to be replaced with the actual path of the script.

It is very easy to colorize the output of the script. Look for the "echo" commands in the script and adjust them to print the output in your favourite colors. If you need any help regarding this script don't hesitate to post your query here  :)

Cheers!!!
You Can't Always Git What You Want

Fangammika

  • Posts: 2
    • View Profile
Re: conky - Gmail new email information (bash version)
« Reply #1 on: December 19, 2017, 10:31:35 AM »
I have read the content on this page and I like it very much because it is really helpful to readers.

su8

  • Posts: 1
    • View Profile
Re: conky - Gmail new email information (bash version)
« Reply #2 on: April 02, 2018, 03:50:03 PM »
Here's C version, compile with gcc mail.c -lX11 -o mail

Replace the code in the variables GMAIL_ACC and GMAIL_PASS with your gmail one and run the program.

Code: [Select]
/*
   04/02/2018 https://github.com/su8

   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 2 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, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include <curl/curl.h>

#define GMAIL_ACC "foo"
#define GMAIL_PASS "bar"

static size_t read_gmail_data_cb(char *, size_t size, size_t nmemb, char *);

static size_t
read_gmail_data_cb(char *data, size_t size, size_t nmemb, char *str1) {
  char *ptr = data;
  size_t sz = nmemb * size, x = 0;

  for (; *ptr; ptr++, x++) {
    if ((x+17) < sz) { /* Verifying up to *(ptr+17) */

      if ('f' == *ptr) { /* fullcount */
        if ('f' == *ptr && 'u' == *(ptr+1) && 'l' == *(ptr+2)) {
          *str1++ = *(ptr+10); /* 1 email */
          if (0 != (isdigit((unsigned char) *(ptr+11)))) {
            *str1++ = *(ptr+11); /* 10 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+12)))) {
            *str1++ = *(ptr+12); /* 100 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+13)))) {
            *str1++ = *(ptr+13); /* 1000 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+14)))) {
            *str1++ = *(ptr+14); /* 10 000 emails */
          }
          if (0 != (isdigit((unsigned char) *(ptr+15)))) {
            *str1++ = *(ptr+15); /* 100 000 emails */
          }
          *str1 = '\0';
          break;
        }
      }

    }
  }

  if ('\0' != *str1) {
    *str1++ = '\0';
  }
  return sz;
}


int main(void) {
  char str[1000] = "0";
  const char *const da_url = "https://mail.google.com/mail/feed/atom";

  CURL *curl = NULL;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_ALL);

  if (NULL == (curl = curl_easy_init())) {
    goto error;
  }

  curl_easy_setopt(curl, CURLOPT_USERNAME, GMAIL_ACC);
  curl_easy_setopt(curl, CURLOPT_PASSWORD, GMAIL_PASS);
  curl_easy_setopt(curl, CURLOPT_URL, da_url);
  curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
  curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, read_gmail_data_cb);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, str);

  res = curl_easy_perform(curl);
  if (CURLE_OK != res) {
    goto error;
  }

error:
  if (NULL != curl) {
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();

  printf("Unread Mails %s\n", str);
  return EXIT_SUCCESS;
}

VastOne

  •      v-ger
  • Posts: 4128
    • View Profile
    • VSIDO Community
Re: conky - Gmail new email information (bash version)
« Reply #3 on: April 07, 2018, 02:16:43 AM »
I built it using your github page here

I also had to:

Code: [Select]
sudo apt-get install build-essentials libcurl4-openssl-dev
Very clean and nice app, thank you!

One should also know that the ACC needed is just the name.. NOT the @gmail.com included
VSIDO      VSIDO Twitter     VSIDO FB     VSIDO Google+   

I dev VSIDO