Gmail Widget-2

From awesome
Jump to: navigation, search
The two scripts below are alternative approaches to Gmail statusbar widget for Awesome-2.3. They do not require python feed parsers or other such programs.

Requires:

  • wget, found in almost all distros.
  • ca-certificates for security. If you can't get the package or don't want to, then comment out the wget command line and use the one above it, which disables security checks.
  • /dev/shm, a tmpfs ram filesystem found in most distros, or you can change the file strings to another location.

Usage:

See the comments at the top of each script. You can run multiple instances of the scripts to check multiple accounts and have all alerts go to the same textbox. Just stagger the start as shown in the comments. You can mix the regular and "nag" versions - they are compatible. Both versions sit in the background and will not complain if there is no internet connection, which is useful if you run it at startup on a notebook or other box that is not always connected. They will start working once a connection is established. The regular version tries to connect every minute until it connects and then reverts to whatever interval you put on the command line. The "nag" version only tries at the command line interval.

You stop the alert by left-clicking on the text box, or you can set up a keyboard shortcut create the file.

Optional Extras:

If you want audible alerts, then install a program like beep and uncomment the line in the alert section. Note that beep does not work on the EeePC, so try a program like tones which is part of the siggen package. You could also modify the scripts to play an audio file or operate a solenoid-controlled fog horn, or whatever you want.

It is fairly easy to modify the scripts to work with dzen2 or zenity if you want.

Awesome-Gmail-Nag:

The "nag" version assumes that you are very well disciplined and read all new mail immediately. If you don't, the "nag" version will alert you each time it checks. This version simply checks the first digit of the unread message count and alerts you if that digit is not zero.

#!/bin/sh
# awesome-gmail-nag for awesome-2.3 by ljcohen 04 July 2008
# lines like the one below are test code - uncomment for debugging
## test commands ## any comments about test commands
#
# You need the following in your .awesomerc section "statusbar mystatusbar"
# textbox gmail {
#     text_align = "center"  mouse { button = "1" command = "spawn" arg = "exec touch /dev/shm/gmail-ack-$USER" }
#               }
# You can run multiple account checks in the same textbox. Just offset them in your
# .xinitrc or .xprofile like this (see usage below):
# /usr/bin/awesome-gmail-nag firstaccount password 6 & # start first account 
# ( sleep 3m ; /usr/bin/awesome-gmail-nag nextaccount password 6 & ) & # start next account
#
############ code starts here ###########
 if [ $# -ne 3 ]; then # 
    echo Usage: $(basename $0) username password interval-in-minutes
    exit 1
 fi
#
 until [ -S ~/.awesome_ctl.0 ] ; do  # wait until awesome starts (if started in .xinitrc for example)
   sleep 2s
 done
###### real stuff starts here ############
 GMUSER=$1
 PASSWD=$2
 INTERVAL=$3
 CFILE=/dev/shm/gmail-ack-$USER	# file exists only when alert acknowledged
 GURL="https://mail.google.com/mail/feed/atom"  # feed url
#  
 while [ -S ~/.awesome_ctl.0 ] ; do  # exit if awesome is not running
  # use line below if package ca-certificates is not installed - MAY BE SECURITY RISK!
  # MCOUNT=$(wget -qO- --no-check-certificate --http-user=$GMUSER --http-password=$PASSWD $GURL | grep fullcount)
  MCOUNT=$(wget -qO- --http-user=$GMUSER --http-password=$PASSWD $GURL | grep fullcount)
  ## echo $MCOUNT ## looks like <fullcount>36</fullcount>
  MCOUNT=`expr substr "$MCOUNT" 12 1` # just get first digit
  ## echo Count=$MCOUNT ##
  if [ $MCOUNT -ne 0 2>/dev/null ] ; then # you got mail!
     rm -f $CFILE		  # loose it (false acknowledgement)
     	while [ ! -f $CFILE ] ; do # flash the statusbar textbox
	     echo 0 widget_tell mystatusbar gmail text " [*******] " | awesome-client
	     # beep -f 1000
	     sleep 2
	     echo 0 widget_tell mystatusbar gmail text " [$GMUSER] " | awesome-client
	     sleep 2
        done
      rm -f $CFILE # get rid of alert acknowledgement file
  fi
  ##  date ## for checking sleep interval
  # line below clears textbox
  echo 0 widget_tell mystatusbar gmail text "" | awesome-client
  sleep $INTERVAL"m"  # wait for next check
 done
#
 exit 0

Awesome-Gmail:

The regular version is for people like me, who do not always read new emails as they arrive, and often have many unread emails in the box. This version creates a file in your home directory for each email account you use. The file contains the id number (explained in the script comments) of the newest message. It is read on startup and only written to when a new message arrives. The script only alerts you if the first email has a higher id then the one stored. If the message count is zero, the id variable and file are reset to zero.

If you are using a box with an SSD and are paranoid about writes, than delete the sections that refer to $IFILE. The script stores the working variable in memory, so the only downside is that you will get an alert each time the program starts (normally at reboot) when the default id is zero. All other files are stored in /dev/shm, which is a ram file system.

A warning: I do not fully understand the id, but I think it is a form of a time stamp. A new email seems to "always" be a higher number than the one before, though they are not sequential. It is possible that at some point the portion of the id that I use may rollover to a lower value sequence. If that happens you will have new mail and no alerts, which you will notice. Just stop the script, delete the ~/.gmail-lid-"your-account" file and restart the program. I have been using this script for weeks and not had any problems, but you never know.

#!/bin/sh
# awesome-gmail for awesome-2.3 by ljcohen 04 July 2008
# lines like the one below are test code - uncomment for debugging
## test commands ## any comments about test commands
#
# You need the following in your .awesomerc section "statusbar mystatusbar"
# textbox gmail {
#     text_align = "center"  mouse { button = "1" command = "spawn" arg = "exec touch /dev/shm/gmail-ack-$USER" }
#               }
# You can run multiple account checks in the same textbox. Just offset them in your
# .xinitrc or .xprofile like this (see usage below):
# /usr/bin/awesome-gmail firstaccount password 6 & # start first account 
# ( sleep 3m ; /usr/bin/awesome-gmail nextaccount password 6 & ) & # start next account
#
############ code starts here ###########
 if [ $# -ne 3 ]; then # 
    echo Usage: $(basename $0) username password interval-in-minutes
    exit 1
 fi
#
 until [ -S ~/.awesome_ctl.0 ] ; do  # wait until awesome starts (if started in .xinitrc for example)
   sleep 2s
 done
# 
############### real stuff starts here #################
 GMUSER=$1
 PASSWD=$2
 INTERVAL=$3
# NOTE - /dev/shm is temporary ram file system. files disappear at reboot 
 CFILE=/dev/shm/gmail-ack-$USER	# file exists only when alert acknowledged
 GFILE=/dev/shm/gmail-$GMUSER	# working copy of latest feed contents
 IFILE=~/.gmail-lid-$GMUSER	# contains last id for after reboot/restart
 LASTID=0			# default last id
 GURL="https://mail.google.com/mail/feed/atom"  # feed url
#    
 if [ -f $IFILE ] ; then	# check for saved count file
     LASTID=$(cat $IFILE)	# and retrive last id 
  else
     echo 0 >$IFILE		# otherwise create one with id of 0 
 fi
#
 while [ -S ~/.awesome_ctl.0 ] ; do  # exit if awesome is not running
 GOGET=1
 until [ $GOGET -eq 0 ] ; do # wait for connection
  # use line below if package ca-certificates is not installed - MAY BE SECURITY RISK!
  # MCOUNT=$(wget -qO- --no-check-certificate --http-user=$GMUSER --http-password=$PASSWD $GURL | grep fullcount)
  wget -qO $GFILE --http-user=$GMUSER --http-password=$PASSWD $GURL
  GOGET=$?
  ## echo goget=$GOGET ##
  if [ $GOGET -eq 1 ] ; then
     echo 0 widget_tell mystatusbar gmail text "" | awesome-client
     sleep 1m
    else
     GOGET=0
  fi 
  done  
  MCOUNT=$(cat $GFILE | grep fullcount)
  ## echo $MCOUNT ## looks like <fullcount>36</fullcount>
  MCOUNT=${MCOUNT#<*>} # strips <fullcount>
  MCOUNT=${MCOUNT%<*>} # strips </fullcount> and leaves just number of emails
  ## echo Count=$MCOUNT ##
  if [ $MCOUNT -ne 0 ] ; then # there is an id, so process it
     ID=$(cat $GFILE | grep "<id>")
     # each ID looks like <id>tag:gmail.google.com,2004:1274193124735397341</id>
     # the string may contain many, we just take the first one (newest always first)
     ID=`expr substr "$ID" 31 19` 
     # ID now looks like 1274193124735397341
     ## echo lastid=$LASTID ; echo now-id=$ID ##
   else # there are no unread emails at all
     ID=0 ; LASTID=0
       if [ `cat $IFILE` -ne 0 ] ; then 
          echo 0 >$IFILE  # clear the saved id
       fi
  fi  
#
  if [ $ID -gt $LASTID ] ; then # a new email is on top
     LASTID=$ID			# update pointer
     echo $LASTID >$IFILE	# and update file
     rm -f $CFILE		  # loose (false) acknowledgement file
     	while [ ! -f $CFILE ] ; do # flash the statusbar textbox
	     echo 0 widget_tell mystatusbar gmail text " [*******] " | awesome-client
	     # beep -f 1000
	     sleep 2
	     echo 0 widget_tell mystatusbar gmail text " [$GMUSER] " | awesome-client
	     sleep 2
        done
     rm -f $CFILE # get rid of alert acknowledgement file
  fi
  ##  date ## for checking sleep interval
# line below shows account checked, number of unread emails and time of last check
#  echo 0 widget_tell mystatusbar gmail text " [$GMUSER($MCOUNT)@`date +\"%H:%M\"`] " | awesome-client
  echo 0 widget_tell mystatusbar gmail text " [$GMUSER-$MCOUNT] " | awesome-client
  sleep $INTERVAL"m"  # wait for next check
 done
#
 exit 0

--Krell46 16:44, 6 July 2008 (UTC)

Personal tools