#!/bin/sh
# This is an example how to use an eventhandler with smsd.
# $1 is the type of the event wich can be SENT, RECEIVED, FAILED or REPORT.
# $2 is the filename of the sms.
# $3 is the message id. Only used for SENT messages with status report.

#The following lines report an event to the console
echo "smsd reports an event:"
echo "type: $1"
echo "file: $2"
echo "id:   $3"

#The next line changes the file attributes so that everybody can read
#received SM
if [ "$1" = "RECEIVED" ]; then
  chmod a+r $2
fi


#This sends all received SM to an eMail receiver:
if [ "$1" = "RECEIVED" ]; then
  /usr/sbin/sendmail root@localhost <$2
fi

#This sends all received SM to eMail receiver. The recipient address 
#must be the first word of the SM.

if [ "$1" = "RECEIVED" ]; then
  receiver=`cat $2 | grep '^*.@.*' | sed -n 1p | cut -f1 -d' '`
  if [ $receiver ]; then
    /usr/sbin/sendmail $receiver < $2
  fi
fi

#This forwards all received SM to another mobile phone:
if [ "$1" = "RECEIVED" ]; then
  FROM=`formail -zx From: <$2`
  formail -f -I "To: 491721234567" <$2 >$2.forward
  echo "from $FROM" >> $2.forward
  mv $2.forward /var/spool/sms/outgoing
fi

