#!/usr/bin/bash

# Last edited: May 11, 2021  

# Skeleton Server Hangup Extension
# Needs code to detect when to hangup a number.

# Script is not called if caller number or name is in ncidd.whitelist.

# All hangup modes are supported.
# Make sure you 'set hupmode = 1|2|3' in ncidd.conf.
  
# This hangup script is REPLACED whenever NCID is updated.
# Make a copy of this script by replacing "skel" with your name.
# Name your new script in the form: hangup-<name>
# Edit the new script, add your custom code and change the hangup reason..
# Be sure your new script has the execute permission set.
# Make sure you 'set hupmode' to be non-zero in ncidd.conf.
#
# To cause ncidd to hangup on this call, you must send "hangup"
# (without quotes) to STDOUT.  
#
# If you do not want to hangup this call, send "OK" to STDOUT.
# The ncidd.log, enabling verbose will show:
# MODE value and description, a recording file if MODE=3 and
# either "OK" or "hangup" each time your script is executed.
#
# The general way to use a recording file is as follows:
#    - make sure you 'set hupmode = 3' in ncidd.conf,
#    - specify a voice file by sending this to STDOUT:
#      Recording: [full path]<name>
#      (it must be sent to STDOUT before the "hangup" line)

# If using hupmode=3, change RECORDING to whatever you want to be played
# when this script indicates hangup.
RECORDING="CannotBeCompleted.rmd"

# Choose to hangup on either either NMBR or NAME.  Default is NMBR.
CHECK='$NMBR'    # hangup on a number
#CHECK='$NAME'   # hangup on a name

# number used for testing
TARGET="0000000000"

check_target() {
    # Code goes here to check number or name using a web site or database
    #   Returns true:  match (hangup wanted)
    #   Returns false: no match (no hangup)
    # if check cannot be completed
    #   echo reason
    #   echo OK
    #   exit 3

    # replace this test - for use with hangup-skel only
    [ "$CHECK" = "$TARGET" ]
}

########################
# Function Definitions #
########################

usage() {
   cat |more <<EOF

Usage: $script [options] <string>

       Skeleton code/example to demonstrate a server hangup extension.
       
       Input: The server passes one <string> to this script:

              *DATE*<mmddyyyy>\\
              *TIME*<hhmm>\\
              *LINE*<lineid>\\
              *NMBR*<number>\\
              *MODE*<1|2|3>\\
              *FNMBR*<formatted number>\\
              *NTYPE*<type of device>\\
              *CTRY*<country code>\\
              *LOCA*<location>\\
              *CARI*<carrier>\\
              *NAME*<name>*

              <number> and <name> have already been changed to aliases 
              if applicable. To test this example script, alias a number
              to be "0000000000".

              There is NO guarantee that the order of the field pairs will
              remain the same in future NCID versions (e.g., *LINE*<lineid>* 
              might be moved to the end of the string). Your code must 
              take this into account. This example script handles this
              properly.
              
              When testing, just use the input fields needed. This 
              example script only requires the NMBR field; it can be
              modified to use the NAME field. The <string> must be
              enclosed in double quotes.

              For example:
                  $script "*NAME*SCAMMER*"
                                                      
       Output: hupmode value received and description
               if hangup required:
                    if MODE=3, Recording: <file name or full path>
                    HangupReason: <your custom hangup reason>
                    hangup
               if hangup not required:
                    OK
       
Options are only used for manual testing and are NEVER SENT by the server:

       [-h] [-v] 

       -h = show this help
       
       -v = turns verbose on and sends additional data to STDOUT for 
            troubleshooting
            
EOF
exit 1
}

###############################
# End of function definitions #
###############################

script=`basename $0`

# Options on command line
while getopts :hv opt ; do
    case $opt in
        h) usage;;
        v) verbose=1;;
        :) echo "Option -$OPTARG requires an argument."; usage;;
        *) echo "Invalid option: -$OPTARG"; usage;;
    esac
done
shift $((OPTIND-1)) # skip over command line args (if any)

# All passed fields from the server are parsed below, use only fields needed.
tmp=${1#*NAME?}; NAME=${tmp%%\**}
tmp=${1#*NMBR?}; NMBR=${tmp%%\**}
tmp=${1#*LINE?}; LINE=${tmp%%\**}
tmp=${1#*DATE?}; DATE=${tmp%%\**}
tmp=${1#*TIME?}; TIME=${tmp%%\**}
tmp=${1#*MODE?}; MODE=${tmp%%\**}

: ${NMBR:=_nmbr_} # default value if null
: ${NAME:=_name_} # default value if null
: ${MODE:=3}      # default value if null

if [ -n "$verbose" ]
then
    echo "NAME:      $NAME"
    echo "NMBR:      $NMBR"
    echo "LINE:      $LINE"
    echo "DATE:      $DATE"
    echo "TIME:      $TIME"
    echo "MODE:      $MODE"
    echo "CHECK:     $CHECK"
    echo "TARGET:    $TARGET"
fi

echo -n "Using HUPMODE $MODE - "
case $MODE in
    1) echo "Normal hangup" ;;
    2) echo "FAX hangup" ;;
    3) echo "VOICE hangup" ;;
    *) echo "unknown MODE" ;;
esac

eval "CHECK=$CHECK"

if check_target
then
    [ "$MODE" = 3 ] && echo "Recording: $RECORDING"
    echo "HangupReason: your custom hangup reason"
    echo "hangup"
else
    echo "OK"
fi
