#!/bin/bash # This script send the SIP NOTIFY message to UA. The NOTIFY message # can either enable or disable the UA message indicator. The script # looks for any file in $VM_HOME and creates the NOTIFY message # # The actual NOTIFY message is sent to the SIP proxy by the sipsak # utilty. This script can be called from Asterisk when a voice mail # is left to notify immediately, and it can also be called from a cron # job to send out message notifications to all users with voicemail # # Adapted from the script found on [Serusers] mailing list: # http://lists.iptel.org/pipermail/serusers/2005-May/019684.html # # Adaptations by Josh Mahonin (jmahonin@cbnco.com), with credit to the # original creator. # If Asterisk called us, save its parameters VM_CONTEXT=$1 EXTENSION=$2 VM_COUNT=$3 # Base directory for voicemail VM_ROOT=/vmail/voicemail # Default context if called from Cron if [ -z "$VM_CONTEXT" ] ; then VM_CONTEXT=phones fi # Voice mail file type VM_TYPE=wav # SIP settings SIP_SERVER= SIP_PORT=5060 SIP_FROM=asterisk # Asterisk database settings DB_NAME=asterisk DB_SERVER= DB_OPTIONS="--skip-column-names --connect_timeout=3" DB_USER=asterisk DB_PASSWORD= DB_SIPUSERTABLE=sipusers DB_VMTABLE=vmusers # Allow glob expansions to return NULL #shopt -s nullglob # Sanity check that VM_HOME exists, warn if it does not if [ ! -d $VM_HOME ] ; then logger -t notify-script "Error: cannot chdir to '$VM_HOME', exiting" exit 1 fi # Did Asterisk call this script? if [ -n "$VM_CONTEXT" -a -n "$EXTENSION" -a -n "$VM_COUNT" ] ; then USERS=$EXTENSION else USERS=`find $VM_ROOT/$VM_CONTEXT -name '*.txt' |grep INBOX | awk -F/ '{ print $5 }' | sort -u` fi for user in $USERS do # Grab the user's IP from the database QUERY="SELECT ipaddr FROM $DB_SIPUSERTABLE WHERE username='$user' LIMIT 1" DOMAIN=`mysql ${DB_OPTIONS} -h ${DB_SERVER} -u${DB_USER} -p${DB_PASSWORD} -e "$QUERY" ${DB_NAME}` # If the MySQL call fails, then $DOMAIN will hold the error message instead if [ $? -ne 0 ] ; then logger -t notify-script "Error: problem connecting to MySQL: $DOMAIN" exit 2 fi # Grab the user's mailbox from the database QUERY="SELECT mailbox FROM $DB_VMTABLE WHERE customer_id='$user' LIMIT 1" MAILBOX=`mysql ${DB_OPTIONS} -h ${DB_SERVER} -u${DB_USER} -p${DB_PASSWORD} -e "$QUERY" ${DB_NAME}` # If the MySQL call fails, then $MAILBOX will hold the error message instead if [ $? -ne 0 ] ; then logger -t notify-script "Error: problem connecting to MySQL: $MAILBOX" exit 2 fi if [ -n "$DOMAIN" -a -n "$MAILBOX" ]; then # Get the full path for the mailbox MAILBOX=$VM_ROOT/$VM_CONTEXT/$MAILBOX # Sanity check that INBOX exists, warn if it does not if [ ! -d ${MAILBOX} ]; then logger -t notify-script "Error: ${MAILBOX} does not exist" continue fi TOTAL_MESSAGES=0 NEW_MESSAGES=0 OLD_MESSAGES=0 # Delete all messages with a zero length. If the subscriber still # has voice mail files in their INBOX after this step then notify # them even if the total is zero, which will extinguish their MWI for message in $MAILBOX/INBOX/*.$VM_TYPE do [ -f "$message" ] || continue logger -t notify-script "In message loop var message: $message" if [ ! -s $message ]; then DELETE=`basename $message .$VM_TYPE` rm -f $MAILBOX/INBOX/$DELETE.* fi done if [ -d ${MAILBOX}/INBOX ]; then tmp=( $( ls ${MAILBOX}/INBOX/*.txt 2>/dev/null ) ) NEW_MESSAGES=${#tmp[*]} else NEW_MESSAGES=0 fi if [ -d ${MAILBOX}/Old ]; then tmp=( $( ls ${MAILBOX}/Old/*.txt 2>/dev/null ) ) OLD_MESSAGES=${#tmp[*]} else OLD_MESSAGES=0 fi TOTAL_MESSAGES=$(($NEW_MESSAGES + $OLD_MESSAGES)) if [ "$NEW_MESSAGES" == "0" ]; then HAS_NEW="no" else HAS_NEW="yes" fi CONTENT_LENGTH=$(( 34 + ${#HAS_NEW} + ${#NEW_MESSAGES} + ${#OLD_MESSAGES} )) SEQUENCE=$( printf "%06d" $RANDOM ) ( cat <<-EOM NOTIFY sip:${user}@${DOMAIN} SIP/2.0 From: To: Contact: Call-ID: ${SEQUENCE}@${SIP_SERVER} CSeq: ${SEQUENCE} NOTIFY Event: message-summary Content-Type: application/simple-message-summary Content-Length: ${CONTENT_LENGTH} Messages-Waiting: ${HAS_NEW} Voicemail: ${NEW_MESSAGES}/${OLD_MESSAGES} EOM ) > out.msg cat out.msg | /usr/local/bin/sipsak -s sip:${user}@${DOMAIN} -f - if [ $? -ne 0 ] ; then logger -t notify-script "Error: sipsak was not successful sending to $user" fi else logger -t notify-script "Error: DOMAIN returned null for $user" fi done exit 0