As there is no "time protocol" available in the GSM network, some trick should be used.
When a SMS is sent, time of sending is written to the file. When a status report is requested, and the report is received, there is a timestamp which tells when SMSC received the message for delivery. At least with messages containing single part only, those timestamps are almost the same, or at least very near to each other.
Here is an eventhandler which is based on the
scripts/eventhandler_report sample. When a report is received, the script determines the difference of times, and then adjusts system time if the difference is too big. A command
super is used for adjusting, because only root can change the time. A line
date /bin/date smsd is required in the file
/etc/super.tab, when smsd is running as smsd:dialout.
The variable ADJUST_TIME defines how many seconds of difference is acceptable. Value 0 disables the adjusting.
#!/bin/bash
# SMS Server Tools 3.
# Sample eventhandler script for storing delivery timestamps.
ADJUST_TIME=10
#--------------------------------------------------------------------------
date2stamp()
{
date --utc --date "$1" +%s
}
dateDiff()
{
local sec=0
case $1 in
-s) sec=1; shift;;
-m) sec=60; shift;;
-h) sec=3600; shift;;
-d) sec=86400; shift;;
*) sec=86400;;
esac
local dte1=$(date2stamp "$1")
local dte2=$(date2stamp "$2")
local diffSec=$((dte2-dte1))
echo $((diffSec/sec))
}
set_header()
{
# $1 = filename
# $2 = header
local tmp=`mktemp /tmp/smsd_XXXXXX`
cp "$1" $tmp
formail -f -I "$2" < $tmp > "$1"
unlink $tmp
}
#--------------------------------------------------------------------------
# The following code stores delivery timestamp to the sent message
if [ "$1" = "REPORT" ]; then
SENTDIR=/var/spool/sms/sent
if grep "Status: 0" $2 >/dev/null; then
FROM=`formail -zx From: < $2`
TMPFILE=`mktemp /tmp/smsd_XXXXXX`
formail -I "" < $2 | sed -e"1,2d" > $TMPFILE
MESSAGE_ID=`formail -zX Message_id: < $TMPFILE`
RECEIVED=`formail -zx Discharge_timestamp: < $TMPFILE`
grep -lx "$MESSAGE_ID" $SENTDIR/* > $TMPFILE
cat $TMPFILE | while read FNAME; do
OLDRECEIVED=`formail -zx Received: < ${FNAME}`
if [ "$OLDRECEIVED" = "" ]; then
TO=`formail -zx To: < ${FNAME}`
if [ "$TO" = "$FROM" ]; then
set_header "${FNAME}" "Received: $RECEIVED"
if [ $ADJUST_TIME -gt 0 ]; then
SENT=`formail -zx Sent: < ${FNAME}`
TIME_DIFF=`dateDiff -s "$SENT" "$RECEIVED"`
set_header "${FNAME}" "Time_diff: $TIME_DIFF"
AGO=""
if [ $TIME_DIFF -lt 0 ]; then
AGO=" ago"
TIME_DIFF=$(($TIME_DIFF * -1))
fi
if [ $TIME_DIFF -ge $ADJUST_TIME ]; then
super date -s "$TIME_DIFF seconds$AGO"
fi
fi
fi
fi
done
unlink $TMPFILE
fi
fi
exit 0
'bash' Syntax Highlight powered by GeSHi