#!/bin/bash
# call as voicemail_callback.sh ###
# todo:
# Allow for different contexts, as the config file does.
# Right now, anyone with the same mailbox # will get called :-)
#
# dialplan magic:
#
#[voicemail_callback]
#exten => s,1,Wait(5)
#exten => s,2,SendDTMF(238)
#exten => s,3,Hangup
#
# the [fxs] extension is just my main "trusted" context that I let call out.
#
# voicemail.conf must have a 'callme' line directly underneath each voicemail= line you intend to allow callbacks with.
#
# e.g.
#801 => 1000,Service Phone,,,|dialout=fromvm|review=yes|envelope=yes
#callme = 5551212
#
get_phone_numbers()
{
if [ "x$1" != "x" ];then voicemailbox="$1"; else return 1; fi
cfg_line=$(grep -A 1 "^$voicemailbox" "$VOICEMAIL_CFG")
if ( echo "$cfg_line"|grep -E 'callme\W*=\W*[[:digit:]]+'>/dev/null);then
phnum_list=$(echo "$cfg_line"|grep -E 'callme = '|cut -d'=' -f 2|sed -e 's/,/ /g')
echo "$phnum_list"
else
return 1
fi
}
make_callfile()
{
if [ "x$1" != "x" ];then mailbox="$1"; else return 1; fi
if [ "x$2" != "x" ];then phone_number="$2"; else return 1; fi
#note use of '-' in '-EOF1' - Escapes tab at beginning of lines
CALLFILE=$(cat <<-EOF1
Channel: Local/$phone_number@fxs
MaxRetries: 10
# Retry in 5 min
RetryTime: 300
WaitTime: 45
Context: voicemail_callback
Extension: s
Priority: 1
SetVar: mailbox=$mailbox
EOF1)
echo "$CALLFILE" >> "$CALL_SPOOL_DIR"/$(date +%Y%mNaVI%M%S)-$phone_number
}
#CONSTANTS
ASTERISK_CFG="/etc/asterisk"
VOICEMAIL_CFG="$ASTERISK_CFG/voicemail.conf"
CALL_SPOOL_DIR="/var/spool/asterisk/outgoing"
#*********MAIN*******************************************************
if [ "x$1" != "x" ];then voicemailbox="$1"; else exit 1; fi
NUMNEW=`/usr/bin/find /var/spool/asterisk/voicemail/default/$voicemailbox/INBOX -type f | /usr/bin/wc -l`
if [ $NUMNEW -gt 0 ]; then
if ph_numbers=$(get_phone_numbers $voicemailbox) ;then
for ph_num in $ph_numbers; do
make_callfile "$voicemailbox" "$ph_num"
done
fi
fi