#!/bin/bash

# my160by2 - 2008 
# Copyright (C) <2008>  <Vaivaswatha N>
# 
# 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 <http://www.gnu.org/licenses/>.
# 
# Contact: vaivaswatha@puttu.net

declare username
declare password
declare mNumbers
declare message
maxNumbers=10
tmpFile1='/tmp/my160by21.temp'
tmpFile2='/tmp/my160by22.temp'
tmpFile3='/tmp/my160by23.temp'

function cleanUp ()
{
	rm contacts.csv contacts.xls cookies.txt *aspx*
	exit;
}

function mainLogin ()
{
#get the username and password from the user
	username=`kdialog --title "my160by2" --inputbox "Username (NOT NUMBER): "`
	if [ $? -ne 0 ]
	then
		cleanUp;
	fi
	password=`kdialog --title "my160by2" --password "Password: "`
	if [ $? -ne 0 ]
	then
		cleanUp;
	fi
	echo "$username" > auth.info
	echo "$password" >> auth.info #for future use to remember login info

#login to 160by2 with your usename and password
	wget --keep-session-cookies --save-cookies cookies.txt \
		--post-data "htxt_UserName=$username&txt_Passwd=$password&namenumber=0" \
		http://160by2.com/LoginCheck.aspx

	rm contacts.csv contacts.xls 

#this is to download the contact list fromm 160by2.
	wget -d --load-cookies cookies.txt \
		--post-data "__EVENTTARGET=download&__EVENTARGUMENT=0" \
		http://160by2.com/contacts.aspx -O ./contacts.xls

#convert the .xls file they provide into a comma seperated file
	sed -n 's/[\t ]\+<td>\([a-zA-Z ()_-]\+\)<\/td><td>\([0-9]\+\)<\/td>.\+/\1;\2/p' \
		./contacts.xls > contacts.csv
	sendMessage;
}

function setmNumbers ()
{
	if [ ! -f "contacts.csv" ]
	then
		kdialog --title "my160by2" --sorry "Please login: "
		mainLogin;
	fi

#read the csv contact list and put them in a format suitable for sending
#as argument to kdialog

	sed -n 's/\([a-zA-Z ()_-]\+\);\([0-9]\+\)/\2 "\1" off /p' contacts.csv | \
	sed 's/ //g' | tr '"' ' ' > $tmpFile1

#all spaces in the names are omitted. I haven't been able to overcome this yet
#in case i retain the spaces, the shell treats each part as a different argument
#can overcome by putting whole command and its argument in an another shell script
#and running that as a sub shell script. should try this out.


#display the contacts to the user	
	kdialog --title "my160by2" --separate-output --checklist "Select recepients: " \
	`cat $tmpFile1` > $tmpFile2
#kdialog puts the selected user's names in tmpFile2
	if [ $? -ne 0 ]; then
		cleanUp;
	fi
	noOfLines=`wc -l $tmpFile2 | cut -d ' ' -f 1`
	
	if [ $noOfLines -eq 0 ]; then
		kdialog --title "my160by2" --sorry "Please select atleast one recepient: "
		sendMessage;
		return;
	fi
#160by2 allows only 10 messages at a time
	if [ $noOfLines -gt $maxNumbers ]
	then
		kdialog --title "my160by2" --sorry "Maximum of $maxNumbers numbers: "
		setmNumbers; #recursive call, so return after it finishes
		return
	fi
	noOfChars=`wc -c $tmpFile2 | cut -d ' ' -f 1`
#noOfChars is decremented by 1 to remove the trailing newline
	noOfChars=$(($noOfChars-1))
#160by2 needs a list of numbers seperated by ','
	mNumbers=`cat $tmpFile2 | tr '\n' ',' | head -c $noOfChars`
	return
}
	
function sendMessage ()
{
	setmNumbers;
#read the message to be sent ensuring its not more than 80 characters
	while true 
	do
		kdialog --textinputbox "Enter SMS (max 80 characters): " > $tmpFile3
		if [ $? -ne 0 ]; then
			sendMessage;
			return
		fi
		charCount=`wc -c $tmpFile3 | cut -d ' ' -f 1`
		if [ $charCount -le 80 ]; then
			break;
		else
			kdialog --title "my160by2" --sorry "80 Characters exceeded. Please Correct: "
		fi
	done
	message=`cat $tmpFile3`
#send the message and the number list to 160by2	
	wget -d --load-cookies cookies.txt \
		--post-data "act_mnos=$mNumbers&txt_msg=$message" \
		http://160by2.com/postview_latest.aspx
	
	if [ ! -f "postview_latest.aspx" ]; then #160by2 sends some other file if session expired etc...
		kdialog --title "my160by2" --error "Sending failed. Restart my160by2 and try: "
		cleanUp;
	else
		kdialog --title "my160by2" --msgbox "Message sent. For confirmation you can 
check out the outbox in your account"
#this isn't the best way to test if the message was sent. for example, if the user
#exceeds his limits for messages, the same page is downloaded and this script cannot
#yet detect the error message 'within' the page to know that the message wasn't sent
		rm *aspx*
	fi
	sendMessage;
}

mainLogin;
cleanUp;

