#!/bin/sh

# This shell script gets called by vgetty if a DTMF
# command is detected, the digits heard are passed
# in $1 (not including the leading * and trailing #).
#
# If a digit was typed during playback of the greeting
# message, it is treated as a 1-digit DTMF command string.
#
# ToDo: Fix the security hole !!!
#

######################################
# This can be tailored to your needs #

VOICE_DIR=/var/spool/voice
ZPLAY=/usr/local/bin/zplay
#BEEP="/usr/local/bin/zplay -St -b '[1320,0,3]'"
#PLAY="/usr/local/bin/zplay -St"
#REC="/usr/local/bin/zplay -Sr"

# ZPLAY=/usr/local/bin/vm
PLAY="/usr/local/bin/vm play -t"
REC="/usr/local/bin/vm record -t"
BEEP="/usr/local/bin/vm beep 1412 3"

PATH=$VOICE_DIR:/bin:/usr/bin:/usr/local/bin

### 
# Play the new messages if the code is correct.
# Required: a file called .code containing an ascii number,
# and the following voice files in the message directory.

CODE=`cat $VOICE_DIR/.code`
MSG_DIR=$VOICE_DIR/messages
DTMF_DIR=$VOICE_DIR/dtmf

M_GET_CODE=$MSG_DIR/get-code
M_INCORRECT=$MSG_DIR/incorrect
M_GOODBYE=$MSG_DIR/goodbye

# if no news are recieved play this one!
# make it relative to the dtmf_dir!

M_NONEW=../messages/NONEW

# name for the Alert_Message activated via DTMF
# should be start with a Zero to come FIRST !

M_ALERT=0_alert

MAXTRIES=3
TRIES=0

# append a line to the vgetty/mgetty logfile

# RING_LOG=/var/log/mgetty.modem.log
RING_LOG=/var/log/vgetty.modem.log

# Make a alert sound to wake up the User
# This _HAVE_ to be changed, due to your lack of this soundfile

ALERT () {
         for i in 0 1 2 3 4 5 6 7 8 9                  
           do   
           cat /usr/local/sounds/alert.wav > /dev/dsp 
         done
}

# after this point changes should be avoided, #
# unless you know what you are doing          #
###############################################

### debugging commands ###
DEBUG=false
EXEC=exec
if [ "$DEBUG" = true ]; then ZPLAY=zplay_sim; EXEC=exec_sim; fi
#zplay_sim () { echo -n $* " : " >/dev/stderr; read q; echo $q; }
zplay_sim () { echo Zplay $*  }
exec_sim () { echo Exec $*; exit 0; }

### useful subroutines to help handle caller hangups
# play a voice file, read dtmf digit(s) and store it in $dtmf,
# quit if the hanger hangs up

Get_dtmf_command () {
	if [ ! -f $1 ]; then msg=$VOICE_DIR/messages/beep; else msg=$1; fi
	dtmf=`$ZPLAY -St -C $msg`
	if [ $? = 2 ]; then exit 0; fi
}

Get_dtmf_digit () {
	if [ ! -f $1 ]; then msg=$VOICE_DIR/messages/beep; else msg=$1; fi
	dtmf=`$ZPLAY -St -D $msg`
	if [ $? = 2 ]; then exit 0; fi
}

Check_code () {
#
# check out if a private DTMF-Code is recieved
# this stuff works but isn't complete yet
#
        if [ -n "`grep -e\"^$1 \" $DTMF_DIR/Index`" ]
          then
          echo "`date +"%m/%d %H:%M:%S"` ##### voice DTMF $1 private" >> $RING_LOG
          MSGS=`grep -e"^$1 " $DTMF_DIR/Index | cut -b9- | tr -d " "`
          if [ -z "`echo $MSGS | tr -d \"\n\"`" ]
            then MSGS=$M_NONEW
          fi
          for i in $MSGS
            do
            if [ -f $DTMF_DIR/$i ]
              then
# check the alert event !
              if [ "$i" = "$M_ALERT" ]
                then
                ALERT &
              fi
# short beep
              $BEEP
# play the message
              $PLAY $DTMF_DIR/$i
            fi
          done                                   
# two long beeps
          $BEEP
          $BEEP
# now the recording should start ... not complete yet
# the naming_convention for the recorded File should be
# equal to the patched vgetty_version
# voc--dd.mm.yy--hh:mm:ss--DTMF:$1
# maybe a database will translate DTMF_code into callernames ;-)
          $REC `date +"$VOICE_DIR/incoming/voc--%d.%m.%y--%H:%M:%S--DTMF:$1"`
          exit 1
          
#
# that's all to control the private DTMF
#
# security hole is the fact of storing the DTMF codes recieved
# into the RINGLOG file AND the DTMF/Index
# (but that allows hacking detection :-)
#
        else
          case $1 in
	  "$CODE")
	 	# play back the new messages and quit
                echo "`date +"%m/%d %H:%M:%S"` ##### voice DTMF $1 secret" >> $RING_LOG
		$EXEC vg_nmp "telco" ;;
	  *)
                echo "`date +"%m/%d %H:%M:%S"` ##### voice DTMF $1 unknown" >> $RING_LOG
		$PLAY $M_INCORRECT	;;
	  esac
        fi
}


# Allow three tries to get the correct code

dtmf="$1"

while [ $TRIES -lt $MAXTRIES ]
do
	# if there were several digits passed to vg_dtmf,
	# treat them as the first input,
	# otherwise get the code now.
	if [ $TRIES -gt 0 -o -z "$dtmf" ]
	then
		Get_dtmf_command $M_GET_CODE
	fi
	case "$dtmf" in
	?) Get_dtmf_command $M_GET_CODE ;;
	*) ;;
	esac

	Check_code $dtmf

	TRIES=`expr $TRIES + 1`
done


exit 0
