Nov 25, 2008

Bash Script: Dhiraagu WebSMS

I've been noticing a lot of problems with Dhiraagu's WebSMS site, mostly that annoying "Session Expired" Errors. So I decided it was time for a major overhaul of my websms script. So here it is. enjoy! Don't forget to comment, and send bug-reports, suggestions, hate-mail etc.

first, a change-log

REV 0 : Initial Release

REV 1 :
+ Verbosity
+ Checking message length
* Cleaned up regex

REV 2 :
+ Directory (alias)
+ Error checking numbers

Ver 2.0:
25-Nov-2008
Major overhaul, re-written from scratch

* Moved directory out of script to external file
* Moved cookie to /tmp (changeable)
+ Added argument processing
* NUMBER must now be given as an argument to -n option
+ Added option to silence output
+ Added option to display help
+ Added option to override default user and pass
+ Added option to override default directory
* Will detect old cookie-and try to re-use instead of logging in every-time.
+ Error checking of username and password
+ Handles "Session expired" error to an extent.It will keep re-trying until success

* Some more small changes that I can't remember


Installation is pretty standard. It uses cURL to do the heavy lifting so you'll need that. The directory file now defaults to ~/.wsmsdir. This is a comma seperated text file with name-number pairs

eg:
hotchick1,123943
hotchick2,123432
daddy,12432
wife,1498052
Change the default values for user and pass. Although this is not required anymore because you can give username and password info to the script using the -u and -p options now. It is not recommended if you share you computer with snoopy people. The script will leave a cookie file in /tmp by default (change "cookiebase" to whatever you like if you want to change this) name USERNAME.wsmscookie (this can be changed on the fourth line from the bottom).If you consider this feature a security risk or something, uncomment the last line to delete the cookie everytime after you send a message.
#!/usr/bin/env bash

##########################################
##                                      ##
##               \|/                    ##
##              '-D                     ##
##             BDWSMS 2.0               ##
##       (Bash Dhiraagu Web-Sms)        ##
##                                      ##
##########################################
##                                      
##  Written By, kudanai [2008]                 
##  http://kudanai.blogspot.com         
## 
##  This script is released as-is and 
##  without any liability on my behalf.
##
##  You are free to make modifications  
##  and redistribute. Credits where they
##  are due are appreciated, but not 
##  necessary.
##
##  Please submit feature requests and 
##  bug reports to moc.liamg@ianaduk
##  (email address is written backwards)
##
##########################################

user=DEFAULT-USER
pass=DEFAULT-PASS

#---leave these if you don't know what they mean --##

dirlist=~/.wsmsdir
cookiebase=/tmp

## ----no need to edit beyond this point --##
version=2.0
cookie=0
number=0
msg=0
verbose=1 #change to 0 if you want silence as default
uflag=0
pflag=0

main()
{
 if [ -e ${dirlist} ];then dcheck=`cat ${dirlist} | grep -w ${number} | cut -f2 -d","`;fi 

 if [ -n "${dcheck}" ]
 then
  number=${dcheck}
 fi

 if [ -z `echo ${number} | grep -E "^7[6-9][0-9]{5}$"` ]
 then
  echo "ERROR: Invalid Number or unknown alias"
  exit 1
 fi

 if [ $verbose -gt 0 ]
 then
  echo "Sending to: ${number}"
  if [ `expr length "${msg}"` -gt 140 ]
   then 
       echo "WARNING: Message will be truncated at ...${msg:130:10}"
  fi
 fi

 if [ -e ${cookie} ]
 then
  if [ $verbose -gt 0 ];then echo "Found cookie file - will try to re-use";fi
  sendsms
 else
  login
 fi

}

login()
{
 if [ $verbose -gt 0 ];then echo "Authenticating ... Getting cookie";fi
 ret=`curl -s --compressed -c ${cookie} -d "username=${user}" -d "password=${pass}" \
  http://websms.dhimobile.com.mv/cgi-bin/websms/index.pl`

 if [ -n "`echo ${ret} | grep -i "password is incorrect"`" ]
 then
  echo "ERROR: Incorrect password"
  exit
 elif [ -n "`echo ${ret} | grep -i "you are not the"`" ]
 then
  echo "ERROR: Incorrect username"
  exit
 elif [ -n "`echo ${ret} | sed -n \"s/.*\( 0 more \).*/\1/p\"`" ]
 then
     echo "ERROR: Daily quota reached"
     exit
 else
     sendsms
 fi
}

sendsms()
{
 if [ $verbose -gt 0 ];then echo "Attempting to send message... ";fi
 ret=`curl -s --compressed -b ${cookie}  -d "mobilenumber=${number}" -d "message=${msg:0:140}" \
  http://websms.dhimobile.com.mv/cgi-bin/websms/send_message.pl`
 
 rem=`echo ${ret} | sed -n 's/.*\([yY]ou .* Day\).*/\1/p'`
 
 if [ -n "${rem}" ]
 then
  echo ${rem}
  exit
 else
  if [ $verbose -gt 0 ];then echo "ERROR: session expired? trying again";fi
  rm ${cookie}
  login
 fi
  
}

printhelp()
{
 echo "BDWSMS - KudaNai (kudanai.blogspot.com)"
 echo "Version: $version"
 echo "USAGE: $0 [OPTIONS...] -n number 'message'"
 echo
 echo "OPTIONS"
 echo " -h  Print this help and exit"
 echo " -v  Print version information"
 echo " -s  Silent. Supress additional information."
 echo " -d  Overried default directory file. The Directory file"
 echo "   is a comma seperated file containing name,number pairs"
 echo " -u USERNAME Override default username. Must use with -p"
 echo " -p PASSWORD Override default password. Must use with -u"
 echo
 echo "Please note that the -n argument is MANDATORY"
 exit 1
}

while getopts 'vshu:p:n:d:' OPTION
do
 case $OPTION in
  s) verbose=0 
   ;;
  v) echo "BDWSMS Version: ${version} [2008]"
   exit 0
   ;;
  u) user="${OPTARG}"
   uflag=1
   ;;
  d) dirlist="${OPTARG}"
   ;;
  p) pass="${OPTARG}"
   pflag=1
   ;;
  n) number="${OPTARG}"
   ;;
  h) printhelp
   ;;
  ?) printhelp
   exit;;
 esac
done

shift $(( $OPTIND - 1 ))
msg=$1

if [[ -z ${number} ]] || [[ -z ${msg} ]]
then
 printhelp
elif [[ ${uflag} -ne ${pflag} ]]
then
 echo "ERROR: You must specify values for both -u and -p options or not at all"
 printhelp
else
 cookie="${cookiebase}/${user}.wsmscookie"
 main 
fi

#rm ${cookie}

5 Comments:

Typos said...

thnx...this is very handy...

Anonymous said...

how about an option to display the number of messages you have remaining?

Anonymous said...

The website is really screwed up isn't it? It took me some 15 retries to get the login working. Thanks for working the automatic retries into your script. But once it is logged in it seems to work, the problem appears to be with the initial login.

Anonymous said...

and I wonder how this work!!cud some one expalain it pls?

SoE said...

Sure just hit me on facebook or twitter or whatever.

Post a Comment