#!/bin/bash
#
# 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 .
#
# Copyright (C) 2007 Vino Fernando Crescini
# Version 0.2
# Quick and dirty script to send sms via the Exetel gateway
progname="sms_send"
progver="0.2"
function print_usage()
{
echo "Usage:"
echo " ${progname} -h"
echo " ${progname} -v"
echo " ${progname} [-c config-file] sender mobile-number[,mobile-number[,...]]"
}
# parse arguments
args=`getopt -q hvc: ${*}`
if [ ${?} != 0 ]; then
print_usage >&2
exit 1
fi
unset cfgfile
unset cfgflag
set -- ${args}
for i; do
case "${i}" in
-h)
print_usage
exit 0
;;
-v)
echo "${progname} ${progver}"
exit 0
;;
-c)
cfgfile=`eval echo ${2}`
cfgflag=1
shift
shift
;;
--)
shift
break
;;
esac
done
sender=`eval echo ${1}`
sendee=`eval echo ${2}`
if [ -z "${sender}" ] || [ -z "${sendee}" ]; then
print_usage >&2
exit 1
fi
# global config file
if [ -r /etc/sms_send.conf ]; then
. /etc/sms_send.conf
fi
# rc file
if [ -r ~/.sms_sendrc ]; then
. ~/.sms_sendrc
fi
# given config file
if [ -r "${cfgfile}" ]; then
. "${cfgfile}"
elif [ "${cfgflag}" = "1" ]; then
echo "cannot read config file ${cfgfile}" >&2
exit 2
fi
if [ -z "${SMS_CURL}" ]; then
echo "SMS_CURL not defined"
exit 3
fi
if [ -z "${SMS_URL}" ]; then
echo "SMS_URL not defined"
exit 3
fi
if [ -z "${SMS_USR}" ]; then
echo "SMS_USR not defined"
exit 3
fi
if [ -z "${SMS_PWD}" ]; then
echo "SMS_PWD not defined"
exit 3
fi
# check if curl is installed and whether it's usable
if ! [ -x ${SMS_CURL} ]; then
echo "sms_send requires curl" >&2
exit 4
fi
if ! curl -V | grep Protocols: | grep -q https || ! curl -V | grep Features: | grep -q SSL; then
echo "sms_send requires curl with SSL support" >&2
exit 5
fi
# read the first 160 characters from stdin, replacing newline with space
temp=`head -c 160`
# escape special characters
mesg=`echo -n -e ${temp} | sed -e "s/%/%25/g" | sed -e "s/&/%26/g" | sed -e "s/+/%2B/g" | sed -e "s/#/%23/g" | sed -e "s/=/%3D/g" | sed -e "s/\//%2F/g" | sed -e "s/,/%2C/g" | sed -e "s/:/%3A/g" | sed -e "s/;/%3B/g" | sed -e "s/@/%40/g" | sed -e "s/ /%20/g"`
# now do it
ref=`date +%Y%m%d%H%M%S`
res=`${SMS_CURL} -q -s -k "${SMS_URL}?username=${SMS_USR}&password=${SMS_PWD}&mobilenumber=${sendee}&sender=${sender}&messagetype=Text&referencenumber=${ref}&message=${mesg}"`
sta=${?}
# check what curl returned
if [ "${?}" != "0" ]; then
echo "curl returned ${?}" >&2
exit $[100 + ${?}]
fi
# check if the server returned something in the correct format
res=`echo ${res} | head -n 1 | grep "[0-9]|.*|.*|.*"`
if [ "${?}" != "0" ]; then
echo "incorrect format received from server" >&2
exit 6
fi
# check what the server returned
scode="${res%%|*}"
stext="${res##*|}"
if [ "${scode}" != "1" ]; then
echo "failed to send: ${stext}"
exit $[200 + ${scode}]
fi
exit 0