Code:
#FUNCTIONS
InvalidKeyword()
{
#arg1 must be shortcode, arg2 must be subscriber msisdn
message="Invalid Keyword, contact us on !!!"
log "error; subscriber [$getmsisdn] has requested an invalid keyword [$getkeyword]" $log
$mysqlpath/mysql -u wirels -e "INSERT INTO dB2.outbox VALUES ('NULL','$1',$2, '$message', '', 'N',NOW())"
$mysqlpath/mysql -u wirels -e "UPDATE dB.dB_table set SMS_Sent=1 WHERE sender='$2'"
#touch tmp invalid keyword file to show sub requested invalid keyword
touch $tmpdir/$2.keyword.invalid
}
ScheduleSend()
{
getmsisdn=$1
getkeyword=$2
getshortcode=$3
alloctbl="dB.contentallocation"
keywordtbl="`grep -i "^$getkeyword" $keywordcfg | awk '{print $2}'`"
#if keyword does not exist
if [ -z "$keywordtbl" ];then
InvalidKeyword $getshortcode $getmsisdn
return
fi
#check if subscriber has entry in contentallocation for their keyword but not in keyword table
$mysqlpath/mysql -u wirels -e "SELECT b.ID FROM $keywordtbl b LEFT JOIN $alloctbl t2 ON b.ID = t2.ID and t2.MSIDN='$getmsisdn' and t2.Cont='$getkeyword' where t2.ID IS NULL" | grep -v ID > $tmpdir/alloc.$getmsisdn.tmp
#proceed
if [ `cat $tmpdir/alloc.$getmsisdn.tmp | grep [0-9] | wc -l | awk '{print $1}'` -gt 0 ];then
#set message to send to subscriber
gettargetdatadir="`grep -i "^$getkeyword" $keywordcfg | awk '{print $3}'`"
log "info; begin processing for subscriber [$getmsisdn] with keyword [$getkeyword] to shortcode [$getshortcode]" $log
if [ -d $gettargetdatadir ];then
#construct path, choose random file to send
$mysqlpath/mysql -u wirels -e "SELECT Description,Folder,Format,ID FROM $keywordtbl WHERE Keyword LIKE '%$getkeyword%' ORDER BY RAND() LIMIT 1" | grep -v Description > $tmpdir/getdata.$getmsisdn.tmp
for line in "`cat $tmpdir/getdata.$getmsisdn.tmp`"
do
filename="`echo $line | awk '{print $1}'`"
subdir="`echo $line | awk '{print $2}'`"
newkeywordID="`echo $line | awk '{print $4}'`"
done
if [ ! -f "$gettargetdatadir/$subdir/$filename" ];then
log "error; unable to find link to send [$gettargetdatadir/$subdir/$filename] for subscriber [$getmsisdn]"
InvalidKeyword $getshortcode $getmsisdn
return
fi
type="`basename $gettargetdatadir`"
url="`grep -i "^$getkeyword" $keywordcfg | awk '{print $4}'`"
message="$urldownloadtxt $url/$type/$subdir/$filename"
else
#get from db
$mysqlpath/mysql -u wirels -e "SELECT ID,Description FROM $keywordtbl WHERE Keyword LIKE '%$getkeyword%' ORDER BY RAND() LIMIT 1" | grep -v Description > $tmpdir/getdata.$getmsisdn.tmp
for line in "`cat $tmpdir/getdata.$getmsisdn.tmp`"
do
newkeywordID="`echo $line | awk '{print $1}'`"
message="`echo $line | sed s/"^[0-9]*."/""/g`"
done
fi
if [ "$message" == "" ];then
InvalidKeyword $getshortcode $getmsisdn
return
fi
#encode message string
#message="`echo $message | sed s/"'"/"\\\'"/g`"
echo $message | sed s/"'"/"\\\'"/g > $tmpdir/message.$getmsisdn.tmp
message="`cat $tmpdir/message.$getmsisdn.tmp`"
rm -f $tmpdir/message.$getmsisdn.tmp
log "info; preparing content [$message] content id [$newkeywordID] for subscriber [$getmsisdn] " $log
#update contentallocation tbl
$mysqlpath/mysql -u wirels -e "INSERT INTO $alloctbl VALUES (null,'$newkeywordID','$getmsisdn','$getkeyword','',NOW())"
#echo $alloctbl VALUES null $newkeywordID $getmsisdn $getkeyword "" now
log "info; contentallocation tbl updated for subscriber[$getmsisdn]" $log
#update outbox
#VALUES NULL, SMS_FROM which is shortcode , SMS_TO which is sub's number, SMS_TEXT which is message , '', 'N', NOW()
$mysqlpath/mysql -u wirels -e "INSERT INTO dB2.outbox (id,SMS_FROM,SMS_TO,SMS_TEXT,SMS_UDH,SMS_SENT,SMS_TIME) VALUES ('NULL','$getshortcode','$getmsisdn', '$message','', 'N',NOW())"
#echo INSERT INTO dB2.outbox VALUES NULL $getshortcode $getmsisdn $message 'null' N
log "info; outbox tbl updated for subscriber[$getmsisdn]" $log
#update inbox
$mysqlpath/mysql -u wirels -e "UPDATE dB.dB_table set SMS_Sent=1 WHERE sender='$getmsisdn'"
log "info; dB_table tbl updated for subscriber[$getmsisdn]" $log
else
log "warn; subscriber [$getmsisdn] has requested all their content for keyword [$getkeyword]" $log
fi
return
}
log()
{
echo "`date '+%Y%m%d %H%M%S'` $1" >> $log
}
i=0
accountcntry="$1" #e.g. drc za
mysqlpath="/usr/bin"
#include variable definitions file
. /home/wirels/checksmsrcvd/env/env.checksmsrcvd.sh
#defaults
log="/home/wirels/checksmsrcvd/$accountcntry/log/keywordrcv.log"
tmpdir="/home/wirels/checksmsrcvd/$accountcntry/tmp"
lockfile="$tmpdir/my.lck"
if [ ! -f $lockfile ];then
touch $lockfile
else
log "system; another instance for script already running, will exit" $log
exit
fi
$mysqlpath/mysql -u wirels -e "SELECT * FROM dB.dB_table where SMS_Sent = 0" | grep -wv id > $tmpdir/inbox.tmp
if [ "`cat $tmpdir/inbox.tmp | wc -l | awk '{print $1}'`" -eq 0 ];then
log "info; nothing to process in db_table $log
fi
cat $tmpdir/inbox.tmp | while read entry
do
msisdn=`echo $entry | awk '{print $2}'`
keyword=`echo $entry | awk '{print $4}'`
shortcode=`echo $entry | awk '{print $3}'`
subscriptions_count="`grep $accountcntry $subscriptionscfg | egrep -w "\+$shortcode|$shortcode" | awk '{print $3}'`"
[ -z $subscriptions_count ] && subscriptions_count=0
subscriptions_period="`grep $accountcntry $subscriptionscfg | egrep -w "\+$shortcode|$shortcode" | awk '{print $4}'`"
ScheduleSend $msisdn $keyword $shortcode $accountcntry
#update subscriptions table if subscriber has sent to a repeated content shortcode and keyword valid
if [ ! -f $tmpdir/$msisdn.keyword.invalid ];then
if [ $subscriptions_count -gt 0 ];then
$subscriptions_count=`expr $subscriptions_count - 1`
$mysqlpath/mysql -u wirels -e "INSERT INTO dB.subscriptions (msisdn,shortcode,keyword,counts,receivedtime,lastscheduled,period) VALUES ('$getmsisdn','$getshortcode','$getkeyword','$subscriptions_count',NOW(),NOW(),'$subscriptions_period')"
fi
fi
done
#clean up
rm -f $tmpdir/alloc*.tmp $tmpdir/getdata*.tmp $tmpdir/message*.tmp $tmpdir/*keyword.invalid
rm -f $lockfile
exit