#!/bin/sh # Authors: # Enzo Michelangeli # Simon G # Krystian Kowalczuk (kryskow (monkey) wp.pl) # MurDok # Uwe Gellhaus # Theo Markettos version=20080625 # ***************************** BIG SETTING !!! ******************************* # Test of domain-name to ip address. # Needed with certain services that resolve to different ip addresses # that are un-connected with each other. # Report if this works on your system or not. Simon G # yes or no BETA_USER=yes # **************************************************************************** # June 24th 2008 (Theo Markettos) # New function smsbalance. Send your balance as an SMS. # # New system to connect to ip address instead of hostname(domain). # This solves a problem of connecting to different unconnected servers # that have the same hostname. # June 22nd 2008 (Uwe Gellhaus) # New .com clone (intervoip), but different url base. # April 15th 2008 (MurDoK) # New websms2.php in use. Ability to set CallerID.(Those you have registered) # NOTE: When sending delayed SMSes the command line options have changed. (callerid) # ALSO: Floating point numbers and awk with non-english locales (LANG!=en_US) fixed. # March 07th 2008 (Krystian Kowalczuk) # fix for new betamax billing format (for counting used free minutes and sms-es in last 7 days) # Feb 28th 2008 (SimonG) # Special creditcents and freedays for nonoh.net # Added freedays function, taken from the PHP script. # Help from "Uwe Gellhaus" ugell at yahoo dot de # January 28th 2008 (Krystian Kowalczuk) # Added the possibility to count free # sms-es in last 7 days. # July 23rd 2007 # Added a patch to fix a CR problem with voipcheap. Thanks to # Peter Walser for this patch. # May 5th 2007 # Added a patch to the usedminutes function to search for # € 0.039 instead of FREE. when using the voipbusterpro.com service. # Janurary 19th 2007 # Added date feature to SMS sending. Simon G # 2 October 2006 # New features: Make call, send SMS by Simon G usage() { echo >&2 "usage: $(basename $0) [creditcents|usedminutes|freedays|last7daysminutes|last7dayssms|call|sms|smsbalance] service username password [monthsago|daysago|sourcenumber|smstext] [destinationnumber]" echo >&2 "" echo >&2 " Extra options when sending an sms. After [destinationnumber] there is [callerid] [monthtosend] [daytosend] [hourtosend] [minutetosend] [gmt difference in hours]." echo >&2 "" echo >&2 " Omit [smstext] when using smsbalance." exit 255 } if [ $# -lt 4 ]; then usage; fi OP="$1" SERVICE="$2" USERNAME="$3" PASSWORD="$4" MONTHSAGO="$5" DAYSAGO="$5" SOURCENUM=`echo "$5" | sed "s/+/00/"` # Convert + to 00 SMSTEXT="$5" DESTNUM=`echo "$6" | sed "s/+/00/"` # Convert + to 00 CALLERID="$7" #sms (MurDoK) SMSMONTH="$8" SMSDAY="$9" # cost of a call connected through site CONNECTCOST="0.050" # We need to shift to get more than 9 command line options shift SMSHOUR="$9" shift SMSMINUTE="$9" shift SMSGMT="$9" # Where to store temporary data COOKIEJAR="/tmp/cj.$$" CALLRECORDS="/tmp/cr.$$" LAST7TMPFILE="/tmp/rozmowy" # umask 077 # protect cookie jar from indiscreet eyes # uncomment the following line if usage is accounted for on "per minute or part thereof" basis TOTALIZER='{totalsecs +=(3600 * $1 + 60 * ($2 + int(($3 + 59)/60)))} END {print totalsecs/60}' # uncomment the following line if usage is accounted for on per second basis #TOTALIZER='{totalsecs += (3600 * $1 + 60 * $2 + $3)} END {print int((totalsecs+59)/60)}' Curl() { curl "$@" local STATUS="$?" if [ ! $STATUS ]; then rm -f ${COOKIEJAR}; exit $STATUS; fi } case _"$OP" in _call) ;; _sms) ;; _smsbalance) ;; _creditcents) ;; _freedays) ;; _usedminutes) ;; _last7daysminutes) ;; _last7dayssms) NOW=$(awk 'BEGIN{print systime()}') if [ $? != 0 ]; then echo >&2 "This script requires awk installed". exit 255 fi ;; *) usage ;; esac case _"$PASSWORD" in _) usage ;; esac gomainpage() { LOGGEDIN=$(Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}"'/index.php?part=menu&justloggedin=true') } sendsmsnow() { echo "Sending direct sms to [${DESTNUM}]" if [ "X$CALLERID" = "X" ]; then # Caller ID is not set CALLERID="" fi OUTPUT=$(Curl -s -k -b ${COOKIEJAR} -d action=send -d panel=true \ -d message="${SMSTEXT}" -d callerid="${CALLERID}" -d bnrphonenumber="${DESTNUM}" \ -d sendscheduled=no "${BASEURL}/websms2.php") } # BASEURL noncomtest=`echo $SERVICE | sed -n '/\./p'` # Look for dots in the service name if [ -z "$noncomtest" ]; then # Normal dot com if [ $SERVICE = "intervoip" ]; then # Special for intervoip HOSTNAME="www.${SERVICE}.com" BASEURL="https://SEDDOMAIN/myaccount" # Another tld else # Normal dot coms HOSTNAME="myaccount.${SERVICE}.com" BASEURL="https://SEDDOMAIN/clx" fi else # Other Non- .com s HOSTNAME="myaccount.${SERVICE}" BASEURL="https://SEDDOMAIN/clx" # Another tld fi # some sites (eg smslisto.com) use multiple servers and they don't share # session cookies, so we must pick one to login to and always use that IP # address if [ "X$BETA_USER" = "Xyes" ]; then echo >&2 "Thanks for beta testing me. Set BETA_USER=no to switch this off. Reports please to Simon G " # TODO Find a better way to do this # Alt 1 using standard bash tools. host is not used as my default openwrt does not that command. IP=`nslookup $HOSTNAME | grep "Address: " | tail -n 1 | sed 's/.* \([[:digit:]]\{1,3\}\(\.[[:digit:]]\{1,3\}\)\{3\}\).*/\1/'` # Alt. 2 using perl # IP=`perl -e 'use Socket; print inet_ntoa(inet_aton("$ARGV[0]") or die "Couldnt resolve $ARGV[0]: $!\n") or die "Couldnt resolve $ARGV[0]: $!\n";' $HOSTNAME` else IP=$HOSTNAME fi BASEURL=`echo $BASEURL | sed s/SEDDOMAIN/$IP/` # #### Login with username and password LOGGEDIN=$(Curl -s -k -c ${COOKIEJAR} -d username="${USERNAME}" -d password="${PASSWORD}" ${BASEURL}/ | grep -i 'Refresh') if [ _"$LOGGEDIN" = "_" ]; then echo "Sorry could not login" exit 254 fi #### go to index page as required by META Refresh. This time will fail (?) gomainpage case _"$OP" in _creditcents) if [ $SERVICE = "nonoh.net" ]; then #echo "Special nonoh.net creditcents" # € 0.00 Curl -s -k -b ${COOKIEJAR} "${BASEURL}/contacts.php" \ | perl -e 'while( <> ) { s##\n#g; print; }' \ | sed -n -e \ 's|.*\&euro\;\ \;\([0-9.]\+\).*|\1|p' \ | LC_ALL=C awk '{print 100 * $1}' # NOTE: MurDok likes to remove the last line above with voipbuster. # It works for me though ?? /SimonG else #### go to index page as required by META Refresh. This time will work (?) gomainpage echo $LOGGEDIN \ | sed -n -e 's|.*Remaining credit[^0-9]\+\([0-9.]\+\).*|\1|p' \ | LC_ALL=C awk '{print 100 * $1}' fi ;; _freedays) if [ $SERVICE = "nonoh.net" ]; then #echo "Special nonoh.net freedays" # You have: 118 freedays FREEDAYS=`Curl -s -k -b ${COOKIEJAR} "${BASEURL}/contacts.php" \ | perl -e 'while( <> ) { s##\n#g; print; }' \ | grep freedays | sed -n -e 's|.*id=\"balanceid\">\([0-9]*\).*|\1|p'` else # From the PHP script # // Freedays remaining: # // $bResult = preg_match('/.*Freedays remaining\:\\([0-9]*).*\<\/span/', $sMainPage, $aMatches); // # Eg. Freedays remaining:
334 \([0-9]*\).*|\1|p'` fi if [ -z $FREEDAYS ]; then FREEDAYS="0"; fi echo $FREEDAYS ;; _usedminutes) CURYEAR=$(date +%Y) CURMONTH=$(date +%m | sed -e 's/^0//') if [ _"$MONTHSAGO" != _ ]; then CURABSMONTH=$((12 * $CURYEAR + $CURMONTH - 1)) THENABSMONTH=$(($CURABSMONTH - $MONTHSAGO)) CURYEAR=$(($THENABSMONTH / 12)) CURMONTH=$((($THENABSMONTH % 12) + 1)) fi if [ $SERVICE = "voipbusterpro" ]; then # Voipbusterpro.com # € 0.039 Curl -s -k -b ${COOKIEJAR} "${BASEURL}/calls.php?month=${CURMONTH}&year=${CURYEAR}" \ | perl -e 'while( <> ) { s##\n#g; print; }' \ | sed -n -e \ 's|[[:space:]]*.*\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\).*\&euro\;\ \;0\.039.*|\1 \2 \3|p' \ | awk "$TOTALIZER" else # Other services Curl -s -k -b ${COOKIEJAR} "${BASEURL}/calls.php?month=${CURMONTH}&year=${CURYEAR}" \ | perl -e 'while( <> ) { s##\n#g; print; }' \ | sed -n -e \ 's|[[:space:]]*.*\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\).*FREE.*|\1 \2 \3|p' \ | awk "$TOTALIZER" fi #### go to index page before logoff (?) gomainpage ;; _last7daysminutes) datedaysago() { # format days_ago [timestamp] local TS="$3" if [ _"$TS" = _ ]; then TS=$(awk 'BEGIN{print systime()}'); fi awk 'BEGIN{print strftime("'"$1"'",'"$TS"'-('"$2"'*24*3600))}' } if [ _"$DAYSAGO" = _ ]; then DAYSAGO=0 fi YEAREND=$(datedaysago "%Y" "$DAYSAGO" "$NOW") ZMONTHEND=$(datedaysago "%m" "$DAYSAGO" "$NOW") ZDAYEND=$(datedaysago "%d" "$DAYSAGO" "$NOW") MONTHEND=$(echo $ZMONTHEND | sed -e 's/^0//') DAYEND=$(echo $ZDAYEND | sed -e 's/^0//') YEARSTART=$(datedaysago "%Y" "$(($DAYSAGO + 6))" "$NOW") ZMONTHSTART=$(datedaysago "%m" "$(($DAYSAGO + 6))" "$NOW") ZDAYSTART=$(datedaysago "%d" "$(($DAYSAGO + 6))" "$NOW") MONTHSTART=$(echo $ZMONTHSTART | sed -e 's/^0//') DAYSTART=$(echo $ZDAYSTART | sed -e 's/^0//') { if [ _"$MONTHSTART" != _"$MONTHEND" ]; then #### get last month's data Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}/calls.php?month=${MONTHSTART}&year=${YEARSTART}" #### go to index page before getting this month's data (?) gomainpage fi Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}/calls.php?month=${MONTHEND}&year=${YEAREND}" } \ | perl -e 'while( <> ) { s#\n##g; print; }' \ | perl -e 'while( <> ) { s#\n##g; print; }' \ | sed -n -e \ '{ s/.*.*\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).*\(+[0-9]*\).*\([0-9][0-9]\):\([0-9]\{2\}\):\([0-9]\{2\}\)\(.*\)/\1 \2 \3 \4 \5 \6 \7 \8/p }' \ | sed -e 's/<[^>]*>//g' | sed -e 's/&[^;]*;//g' \ | while read YEAR MONTH DAY NUMBER DURH DURM DURS COST; do # printf '%s %20d %s\n' "$YEAR-$MONTH-$DAY $NUMBER $DURH:$DURM:$DURS $COST" if [ $YEAR$MONTH$DAY -ge $YEARSTART$ZMONTHSTART$ZDAYSTART -a $YEAR$MONTH$DAY -le $YEAREND$ZMONTHEND$ZDAYEND \ -a \( $COST = "FREE!" \) ]; then echo "$DURH $DURM $DURS" fi done \ | awk "$TOTALIZER" #### go to index page before logoff (?) gomainpage ;; _last7dayssms) datedaysago() { # format days_ago [timestamp] local TS="$3" if [ _"$TS" = _ ]; then TS=$(awk 'BEGIN{print systime()}'); fi awk 'BEGIN{print strftime("'"$1"'",'"$TS"'-('"$2"'*24*3600))}' } if [ _"$DAYSAGO" = _ ]; then DAYSAGO=0 fi YEAREND=$(datedaysago "%Y" "$DAYSAGO" "$NOW") ZMONTHEND=$(datedaysago "%m" "$DAYSAGO" "$NOW") ZDAYEND=$(datedaysago "%d" "$DAYSAGO" "$NOW") MONTHEND=$(echo $ZMONTHEND | sed -e 's/^0//') DAYEND=$(echo $ZDAYEND | sed -e 's/^0//') YEARSTART=$(datedaysago "%Y" "$(($DAYSAGO + 6))" "$NOW") ZMONTHSTART=$(datedaysago "%m" "$(($DAYSAGO + 6))" "$NOW") ZDAYSTART=$(datedaysago "%d" "$(($DAYSAGO + 6))" "$NOW") MONTHSTART=$(echo $ZMONTHSTART | sed -e 's/^0//') DAYSTART=$(echo $ZDAYSTART | sed -e 's/^0//') { if [ _"$MONTHSTART" != _"$MONTHEND" ]; then #### get last month's data Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}/calls.php?month=${MONTHSTART}&year=${YEARSTART}" #### go to index page before getting this month's data (?) gomainpage fi Curl -s -k -b ${COOKIEJAR} \ "${BASEURL}/calls.php?month=${MONTHEND}&year=${YEAREND}" } \ | perl -e 'while( <> ) { s#\n##g; print; }' \ | perl -e 'while( <> ) { s#\n##g; print; }' \ | sed -n -e \ '{ s/.*.*\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).*\(+[0-9]*\).*\([0-9][0-9]\):\([0-9]\{2\}\):\([0-9]\{2\}\)\(.*\)/\1 \2 \3 \4 \5 \6 \7 \8/p }' \ | sed -e 's/<[^>]*>//g' | sed -e 's/&[^;]*;//g' \ | while read YEAR MONTH DAY NUMBER DURH DURM DURS COST; do #printf '%s %20d %s\n' "$YEAR-$MONTH-$DAY $NUMBER $DURH:$DURM:$DURS $COST" >> $LAST7TMPFILE if [ $YEAR$MONTH$DAY -ge $YEARSTART$ZMONTHSTART$ZDAYSTART -a $YEAR$MONTH$DAY -le $YEAREND$ZMONTHEND$ZDAYEND \ -a \( $COST = "FREE!" \) ]; then echo "0 1 0" fi done \ | awk "$TOTALIZER" #### go to index page before logoff (?) gomainpage ;; # **** CALL **** _call) echo "Calling From [${SOURCENUM}] to [${DESTNUM}]" OUTPUT=$(Curl -s -k -b ${COOKIEJAR} -d action=initcall -d panel=true \ -d anrphonenr="${SOURCENUM}" -d bnrphonenr="${DESTNUM}" \ "${BASEURL}/webcalls2.php") #### go to index page before logoff (?) gomainpage ;; # **** SMS **** _sms) #SMSDAY="18" #SMSMONTH="01" #SMSHOUR="16" #SMSMINUTE="35" #SMSGMT="1" if [ ${#SMSGMT} -gt 0 ]; then # They have filled in the GMT option. Hopefully the others too echo "Sending delayed sms to [${DESTNUM}] at Month[${SMSMONTH}] Day[${SMSDAY}] Hour[${SMSHOUR}] Minute [${SMSMINUTE}] GMT[${SMSGMT}]" OUTPUT=$(Curl -s -k -b ${COOKIEJAR} -d action=send -d panel=true \ -d message="${SMSTEXT}" -d bnrphonenumber="${DESTNUM}" \ -d day="${SMSDAY}" -d month="${SMSMONTH}" -d hour="${SMSHOUR}" \ -d minute="${SMSMINUTE}" -d gmt="${SMSGMT}" "${BASEURL}/websms2.php") else # Use the unscheduled one sendsmsnow fi # echo "$OUTPUT" > debug.log #### go to index page before logoff (?) gomainpage ;; # send an SMS containing our account balance and free days _smsbalance) # we don't include the SMS text parameter, so shift params up one CALLERID=$DESTNUM DESTNUM=$SMSTEXT FREEDAYS=`echo $LOGGEDIN | sed -n -e 's|.*Freedays remaining\:
\([0-9]*\).*|\1|p'` if [ -z $FREEDAYS ]; then FREEDAYS="0"; fi CREDIT=`echo $LOGGEDIN | sed -n -e 's|.*Remaining credit[^0-9]\+\([0-9.]\+\).*|\1|p'` SMSTEXT="$SERVICE account: credit $CREDIT, remaining $FREEDAYS free days" sendsmsnow gomainpage ;; esac LOGGEDIN=$(Curl -s -k -b ${COOKIEJAR} "${BASEURL}/index.php?part=logoff") rm ${COOKIEJAR}