Skip to content
Snippets Groups Projects
config_asterisk.sh 77.6 KiB
Newer Older
#!/bin/sh
#
# The purpose of this file is to (re)create asterisk
# configuration files from $VOICE_UCI_CONFIG UCI config file.
#

. /lib/functions.sh
. /lib/functions/network.sh
. /lib/voice/utils.sh

TEMPLATE_DIR=/etc/asterisk
WORK_DIR=
# Whitespace separated list of feature access codes
channel_fac=
# Flag indicating if voice UCI config has been modified by the script
uci_modified=0
# Escape special characters in a string that is being passed to sed
escape_sed_substitution() {
	echo "$@" | sed -e 's/[\/&]/\\&/g'
}

# Codecs for service providers and users
read_codecs() {
	local uci_section="$1"
	local without_ptime="$2"
	local codec_allow=""
	local codec_list
	local codec
	local ptime


	config_get codec_list "$uci_section" "codecs"

        if [ -z "$codec_list" ] ; then
		codec_allow="allow = alaw\nallow = ulaw\\n"
        fi

	for codec in $codec_list; do
		[ -z "$codec" ] && continue

		# The configured codec must exist in a codec_profile section
		config_get ptime "$codec" "ptime"
		[ -z "$ptime" ] && continue

		if [ "$without_ptime" == "yes" ]; then
			codec_allow=$codec_allow"allow = $codec\\n"
		else
			codec_allow=$codec_allow"allow = $codec:$ptime\\n"
		fi
	done
	echo $codec_allow
}

get_bindaddr() {
	local intf="$1"
	local bindaddr=""

	[ -n "$intf" ] && network_get_ipaddr bindaddr "$intf"
	[ -z "$bindaddr" ] && network_get_ipaddr6 bindaddr "$intf"
	echo "${bindaddr:-0.0.0.0}"
}

configure_sip_options() {
	local useragent
	local externhost
	local bindintf
	local bindport
	local tos_sip
	local rtpstart
	local rtpend
	local rtcpinterval
	local dtmfmode
	local blindxfer
	local localnet
	local stun_server
	local dnsmgr
	local dnsmgr_refresh_interval
	local tls_version
	local tls_cafile
	local tls_private_key
	local tls_certfile
	local tls_cipher

	config_get useragent sip_options useragent
	config_get externhost sip_options externhost
	config_get bindintf sip_options bindintf
	config_get tos_sip sip_options tos_sip
	config_get rtpstart sip_options rtp_start 10000
	config_get rtpend sip_options rtp_end 20000
	config_get rtcpinterval sip_options rtcpinterval
	config_get dtmfmode sip_options dtmf_mode
	config_get blindxfer sip_options blindxfer
	config_get localnet sip_options localnet
	config_get tls_cipher sip_options tls_cipher
	config_get stun_server sip_options stun_server
	config_get dnsmgr sip_options dns_mgr
	config_get dnsmgr_refresh_interval sip_options dns_mgr_refresh_interval
	config_get srv_lookup sip_options srv_lookup
	config_get tls_version sip_options tls_version "sslv23"
	config_get tls_cafile sip_options tls_cafile
	config_get tls_private_key sip_options tls_private_key
	config_get tls_certfile sip_options tls_certfile
	config_get tls_cipher sip_options tls_cipher
	config_get accept_proxy_req_only sip_options accept_proxy_req_only 0

	[ -z "$useragent" ] && useragent="$USERAGENT"

	sed -i "s/|USERAGENT|/$(escape_sed_substitution $useragent)/g"		$WORK_DIR/pjsip.conf
        if [ -z "$externhost" ] ; then
                sed -i "s/external_media_address = |EXTERNHOST|/;external_media_address = /g"           $WORK_DIR/pjsip.conf
                sed -i "s/external_signaling_address = |EXTERNHOST|/;external_signaling_address = /g"   $WORK_DIR/pjsip.conf
        else
                sed -i "s/|EXTERNHOST|/$externhost/g"                   $WORK_DIR/pjsip.conf
        fi

	if [ -z "$bindintf" ] ; then
		sed -i "s/bind =.*/bind = 0.0.0.0/g"			$WORK_DIR/pjsip.conf
		sed -i "s/bind =.*/bind = $(get_bindaddr $bindintf)/g"	$WORK_DIR/pjsip.conf
	sed -i "s/|BINDPORT_UDP|/$bindport/g"           $WORK_DIR/pjsip.conf
	sed -i "s/|BINDPORT_TCP|/$bindport/g"           $WORK_DIR/pjsip.conf
	sed -i "s/|BINDPORT_TLS|/$bindport/g"           $WORK_DIR/pjsip.conf
    if [ -z "$tos_sip" ] ; then
            sed -i "s/tos = |TOS_SIP|/;tos = /g"                                    $WORK_DIR/pjsip.conf
    else
            sed -i "s/|TOS_SIP|/$(escape_sed_substitution $tos_sip)/g"              $WORK_DIR/pjsip.conf
    fi
	#Add localnets
	if [ -n "$localnet" ] ; then
		for i in $localnet ; do
			echo "Adding localnet $i"
			echo "local_net = $i" >> $WORK_DIR/pjsip.conf
		done
	sed -i "s/|TLSCLIENTMETHOD|/$tls_version/" $WORK_DIR/pjsip.conf
	sed -i "s/|TLSCIPHER|/$tls_cipher/"  $WORK_DIR/pjsip.conf

	if [ -z "$tls_cafile" ]; then
		sed -i "/|TLSCAFILE|/d" $WORK_DIR/pjsip.conf
	else
		sed -i "s:|TLSCAFILE|:$tls_cafile:g" $WORK_DIR/pjsip.conf
	fi

	sed -i "s:|TLSPRIVATEKEY|:$tls_private_key:g" $WORK_DIR/pjsip.conf
	sed -i "s:|TLSCERTFILE|:$tls_certfile:g" $WORK_DIR/pjsip.conf

	# DNS manager
	if [ -z "$dnsmgr" ] ; then
		dnsmgr=no
	fi
	sed -i "s/|ENABLE|/$dnsmgr/g"							$WORK_DIR/dnsmgr.conf

	if [ -z "$dnsmgr_refresh_interval" ] ; then
		dnsmgr_refresh_interval=300
	fi
	sed -i "s/|REFRESHINTERVAL|/$dnsmgr_refresh_interval/g"				$WORK_DIR/dnsmgr.conf

	
	# Set RTP port range
	sed -i "s/|RTPSTART|/$rtpstart/"		$WORK_DIR/rtp.conf
	sed -i "s/|RTPEND|/$rtpend/"			$WORK_DIR/rtp.conf

	# Set or remove rtcpinterval
	if [ -z "$rtcpinterval" ] ; then
		sed -i "/|RTCPINTERVAL|/d"		$WORK_DIR/rtp.conf
	else
		sed -i "s/|RTCPINTERVAL|/$rtcpinterval/g"		$WORK_DIR/rtp.conf
	fi

	# Set blind transfer key
	sed -i "s/|BLINDXFER|/$(escape_sed_substitution $blindxfer)/"		$WORK_DIR/features.conf

	# STUN server
	sed -i "s/|SERVER|/$stun_server/"	$WORK_DIR/res_stun_monitor.conf

	sed -i "s/|ACCEPT_PROXY_REQ_ONLY|/$accept_proxy_req_only/" $WORK_DIR/pjsip.conf
}

mailbox_get_boxnumber() {
	local boxnumber
	local enabled

	if [ "$#" -ne 1 ] ; then
		return 
	fi

	config_get boxnumber $1 boxnumber
	if [ -n "$boxnumber" ] ; then
		echo "$boxnumber"
		return 0
	fi
	
	return 1
}

#Configure Message waiting indication, musiconhold and call_waiting features
configure_mwi_moh_cw(){
        local calling_features
        local mwi_enable
        local provider
        local messages_waiting
        local mwi_dialtone_state
        local user=$3
        local server_uri=$4
        local moh_passthrough

        config_get calling_features $1 calling_features
        config_get provider $1  provider

        if [ -n "$calling_features" ]; then
            config_get mwi_enable $calling_features mwi_enable "0"
            config_get messages_waiting $calling_features messages_waiting "no"
            config_get mwi_dialtone_state $calling_features mwi_dialtone_state "on"
            config_get moh_passthrough  $calling_features moh_passthrough
            config_get call_waiting_enable $calling_features call_waiting_enable "0"
        fi
        if [ -n "$provider" -a "$provider" == "$2" ]; then
            if [ -n "$mwi_enable" -a  "$mwi_enable" == "1" ]; then
	        sed -i "s/|MAILBOX|/sip:$(escape_sed_substitution $user)@$server_uri/"	$WORK_DIR/pjsip_endpoint
                sed -i "s/|MESSAGES|/$messages_waiting/"                                $WORK_DIR/pjsip_endpoint
                sed -i "s/|MWI_DIALTONE|/$mwi_dialtone_state/"                          $WORK_DIR/pjsip_endpoint
            else
                sed -i "s/incoming_mwi_mailbox.*//"                                     $WORK_DIR/pjsip_endpoint
                sed -i "s/messages_waiting.*//"                                         $WORK_DIR/pjsip_endpoint
                sed -i "s/mwi_dialtone_state.*//"                                       $WORK_DIR/pjsip_endpoint
            fi

            if [ "$call_waiting_enable" == "1" ]; then
		sed -i "s/|CALLWAITING|/"yes"/"			                        $WORK_DIR/pjsip_endpoint
            else
                sed -i "s/|CALLWAITING|/"no"/"                                         $WORK_DIR/pjsip_endpoint
            fi


            # Enable re-invite if moh is set
            if [ -n "$moh_passthrough" -a "$moh_passthrough" == "0" ] ; then
                sed -i "s/|MOH_PASSTHROUGH|/0/"                                         $WORK_DIR/pjsip_endpoint
            else
                sed -i "s/|MOH_PASSTHROUGH|/1/"                                         $WORK_DIR/pjsip_endpoint
            fi
        fi
}
configure_sip_provider() {
	local enabled
	local outbound_proxy
	local outbound_proxy_port
	local user
	local authuser
	local secret
	local mediasec
	local realm
	local dtmf_mode
	local tos_audio
	local tos_video
	local defaultexpiry
	local registertimeout
	local registerattempts
	local server_uri
	local port
	config_get profile_enabled general profile_enable "1"
	config_get enabled $1 enable "0"
	config_get outbound_proxy $1 outbound_proxy ""
	config_get outbound_proxy_port $1 outbound_proxy_port ""
	config_get domain $1 domain ""
	config_get host $1 host "$domain"
	config_get user $1 user
	config_get authuser $1 authuser
	config_get secret $1 secret
	config_get support_fax $1 support_fax "0"
	config_get transport $1 transport "udp"
	config_get encryption $1 encryption "0"
	config_get mediasec $1 mediasec "0"
	config_get realm $1 realm ""
	config_get dtmf_mode $1 dtmf_mode "rfc4733"
	config_get tos_audio $1 tos_audio
	config_get tos_video $1 tos_video
	config_get defaultexpiry $1 defaultexpiry
	config_get registertimeout $1 registertimeout
	config_get registerattempts $1 registerattempts
	# This is a hack to fix security issue #14962
	user=${user//"'"/}
	authuser=${authuser//"'"/}

	if [ "$profile_enabled" == "0" -o "$enabled" == "0" ] ; then
		echo "SIP provider $1 or the profile is not enabled"
		return
	fi

	if [ -z "$user" ] ; then
		echo "SIP provider $1 has no user configured, ignored"
		return
	fi

		server_uri=$outbound_proxy;
		if [ -z "$outbound_proxy_port" ] ; then
			if [ "$transport" == "udp" -o "$transport" == "tcp" ] ; then
				port=5060;
			elif [ "$transport" == "tls" ] ; then
				port=5061;
			fi
		else
			port=$outbound_proxy_port;
		fi
	else
		server_uri=$host;
		if [ "$transport" == "udp" -o "$transport" == "tcp" ] ; then
			port=5060;
		elif [ "$transport" == "tls" ] ; then
			port=5061;
		fi
	fi

	sed -i "s/|PORT|/$port/g"                $WORK_DIR/pjsip_aor

	# Generating pjsip_aors.conf
	cp "$TEMPLATE_DIR/pjsip_aor" "$WORK_DIR/pjsip_aor"
	sed -i "s/|NAME|/$1/g"                          			$WORK_DIR/pjsip_aor
	sed -i "s/|SERVER_URI|/$server_uri/g"                       		$WORK_DIR/pjsip_aor
	sed -i "s/|PORT|/$port/g"                				$WORK_DIR/pjsip_aor
	sed -i "s/|USER|/$user/"                                                $WORK_DIR/pjsip_aor
	if [ -z "$outbound_proxy" ] ; then
		sed -i "/outbound_proxy = sip:|OUTBOUND_PROXY|/c\;outbound_proxy ="	$WORK_DIR/pjsip_aor
	else
		sed -i "s/|OUTBOUND_PROXY|/$outbound_proxy/"				$WORK_DIR/pjsip_aor
	fi

	cat $WORK_DIR/pjsip_aor >> $WORK_DIR/pjsip_aors.conf
	# Generating pjsip_auths.conf
	cp "$TEMPLATE_DIR/pjsip_auth" "$WORK_DIR/pjsip_auth"
	sed -i "s/|NAME|/$1/g"                                                  $WORK_DIR/pjsip_auth
        sed -i "s/|AUTHUSER|/$(escape_sed_substitution $authuser)/g"            $WORK_DIR/pjsip_auth
        sed -i "s/|SECRET|/$(escape_sed_substitution $secret)/"                 $WORK_DIR/pjsip_auth
	if [ -z "$realm" ] ; then
                realm=""
        sed -i "s/|REALM|/$(escape_sed_substitution $realm)/g"          	$WORK_DIR/pjsip_auth
	cat $WORK_DIR/pjsip_auth >> $WORK_DIR/pjsip_auths.conf
	# Generating pjsip_endpoints.conf
	cp "$TEMPLATE_DIR/pjsip_endpoint" "$WORK_DIR/pjsip_endpoint"
	sed -i "s/|NAME|/$1/g"                                                  $WORK_DIR/pjsip_endpoint
	sed -i "s/|DTMFMODE|/$dtmf_mode/"                			$WORK_DIR/pjsip_endpoint
	if [ -z "$outbound_proxy" ] ; then
		sed -i "/outbound_proxy = sip:|OUTBOUND_PROXY|/c\;outbound_proxy ="	$WORK_DIR/pjsip_endpoint
	else
		sed -i "s/|OUTBOUND_PROXY|/$outbound_proxy/"				$WORK_DIR/pjsip_endpoint
	fi

	if [ -z "$tos_audio" ] ; then
                sed -i "s/tos_audio = |TOS_AUDIO|/;tos_audio = /g"                      $WORK_DIR/pjsip_endpoint
                sed -i "s/|TOS_AUDIO|/$(escape_sed_substitution $tos_audio)/g"          $WORK_DIR/pjsip_endpoint
        fi

        if [ -z "$tos_video" ] ; then
                sed -i "s/tos_video = |TOS_VIDEO|/;tos_video = /g"                      $WORK_DIR/pjsip_endpoint
        else
                sed -i "s/|TOS_VIDEO|/$(escape_sed_substitution $tos_video)/g"          $WORK_DIR/pjsip_endpoint
        fi

		sed -i "s/|DOMAIN|/$outbound_proxy/"                                 	$WORK_DIR/pjsip_endpoint
	else
		sed -i "s/|DOMAIN|/$host/"                                            $WORK_DIR/pjsip_endpoint
	sed -i "s/|USER|/$(escape_sed_substitution $user)/"                     $WORK_DIR/pjsip_endpoint

        # Force ulaw/alaw if fax, otherwise read selected codecs
        if [ "$support_fax" == "1" ] ; then
                sed -i "s/|ALLOW|/allow = ulaw\nallow = alaw/"  			$WORK_DIR/pjsip_endpoint
        else
                sed -i "s/|ALLOW|/$(read_codecs $1)/"           			$WORK_DIR/pjsip_endpoint
        fi

	sed -i "s/|TRANSPORT|/$transport/"              				$WORK_DIR/pjsip_endpoint

                sed -i "s/|ENCRYPTION|/sdes/g"               				$WORK_DIR/pjsip_endpoint
        else
                sed -i "s/|ENCRYPTION|/no/g"               				$WORK_DIR/pjsip_endpoint
        fi
        if [ "$mediasec" == "0" ] ; then
                sed -i "s/|MEDIASEC|/0/g"               				$WORK_DIR/pjsip_endpoint
        else
                sed -i "s/|MEDIASEC|/1/g"               				$WORK_DIR/pjsip_endpoint
        fi
        if [ -z "$early_media" ] ; then
		sed -i "s/|EARLYMEDIA|/sendrecv/g"                                              $WORK_DIR/pjsip_endpoint
        else
		if [ "$early_media" = "sendrecv" -o "$early_media" = "recvonly" -o "$early_media" = "sendonly" -o "$early_media" = "none" ]; then
			sed -i "s/|EARLYMEDIA|/$early_media/g"		                                $WORK_DIR/pjsip_endpoint
		else
			sed -i "s/|EARLYMEDIA|/sendrecv/g"                                              $WORK_DIR/pjsip_endpoint
		fi
        fi
        config_foreach configure_mwi_moh_cw line $1 $user $server_uri
Yalu Zhang's avatar
Yalu Zhang committed
	# Generating pjsip_endpoints.conf
	cat $WORK_DIR/pjsip_endpoint >> $WORK_DIR/pjsip_endpoints.conf

	# Generating pjsip_identifies.conf
        cp "$TEMPLATE_DIR/pjsip_identify" "$WORK_DIR/pjsip_identify"
	sed -i "s/|USER|/$(escape_sed_substitution $user | sed 's/+44//')/"     $WORK_DIR/pjsip_identify
	sed -i "s/|NAME|/$1/g"                                                  $WORK_DIR/pjsip_identify
	cat $WORK_DIR/pjsip_identify >> $WORK_DIR/pjsip_identifies.conf

        # Generating pjsip_registrations.conf
        cp "$TEMPLATE_DIR/pjsip_registration" "$WORK_DIR/pjsip_registration"
        sed -i "s/|NAME|/$1/g"                                          	$WORK_DIR/pjsip_registration
	sed -i "s/|USER|/$(escape_sed_substitution $user)/"                     $WORK_DIR/pjsip_registration

	sed -i "s/|SERVER_URI|/$server_uri/g"	              			$WORK_DIR/pjsip_registration
	sed -i "s/|PORT|/$port/g"						$WORK_DIR/pjsip_registration
	if [ -z "$outbound_proxy" ] ; then
		sed -i "/outbound_proxy = sip:|OUTBOUND_PROXY|/c\;outbound_proxy ="	$WORK_DIR/pjsip_registration
	else
		sed -i "s/|OUTBOUND_PROXY|/$outbound_proxy/"				$WORK_DIR/pjsip_registration
	fi

        if [ -z "$defaultexpiry" ] ; then
                sed -i "s/|DEFAULTEXPIRY|/3600/g"                               $WORK_DIR/pjsip_registration
        else
                sed -i "s/|DEFAULTEXPIRY|/$defaultexpiry/g"                     $WORK_DIR/pjsip_registration
        fi

        if [ -z "$registertimeout" ] ; then
                sed -i "s/retry_interval = |REGISTERTIMEOUT|/;retry_interval = /g"      $WORK_DIR/pjsip_registration
        else
                sed -i "s/|REGISTERTIMEOUT|/$registertimeout/g"                         $WORK_DIR/pjsip_registration
        fi

        if [ -z "$registerattempts" ] ; then
                sed -i "s/max_retries = |REGISTERATTEMPTS|/;max_retries = /g"   $WORK_DIR/pjsip_registration
        else
                sed -i "s/|REGISTERATTEMPTS|/$registerattempts/g"               $WORK_DIR/pjsip_registration
        fi

        sed -i "s/|TRANSPORT|/$transport/"              $WORK_DIR/pjsip_registration

        if [ "$mediasec" == "0" ] ; then
                sed -i "s/|MEDIASEC|/0/g"               $WORK_DIR/pjsip_registration
        else
                sed -i "s/|MEDIASEC|/1/g"               $WORK_DIR/pjsip_registration
        fi

	cat $WORK_DIR/pjsip_registration >> $WORK_DIR/pjsip_registrations.conf
}

# Encrypt passwords if voicesec is used, remove cleartext password from config
encrypt_password() {
	local secret
	config_get secret $1 secret

	if [ -n "$secret" ] ; then
		if [ -e /usr/bin/voicesec ] ; then
			echo "Encrypting password for $1"
			/usr/bin/voicesec -e $secret > /usr/lib/asterisk/voicesec_$1
			fsync /usr/lib/asterisk/voicesec_$1
		fi
		#uci_remove $VOICE_UCI_CONFIG $1 secret
		#uci_modified=1
	fi
}

# Configure mailbox
configure_mailbox() {
	local pin
	local boxnumber
	local enabled

	config_get pin $1 pin
	config_get boxnumber $1 boxnumber
	config_get enabled $1 enable

	echo "Setting up mailbox $boxnumber"
	echo "|ENABLED||BOXNUMBER| => |PIN|,,,," > $WORK_DIR/mailbox

	# Construct a mailbox
	if [ -z "$enabled" -o "$enabled" = "0" ] ; then
		# Comment out the mailbox entry.
		# This allows us to keep the PIN code once the mailbox is re-enabled
		sed -i "s/|ENABLED|/;/g"		$WORK_DIR/mailbox
	else
		sed -i "s/|ENABLED|//g"			$WORK_DIR/mailbox
	fi
	sed -i "s/|BOXNUMBER|/$boxnumber/g"		$WORK_DIR/mailbox
	sed -i "s/|PIN|/$pin/g"				$WORK_DIR/mailbox

	cat $WORK_DIR/mailbox >> $WORK_DIR/voicemail.conf
	rm -f $WORK_DIR/mailbox
}

configure_ivr() {
	local name
	local enabled
	local extension
	local sound_file
	local opening_hours_profile

	config_get name $1 name
	config_get enabled $1 enable
	config_get extension $1 extension
	config_get sound_file $1 sound_file
	config_get opening_hours_profile $1 opening_hours_profile

	if [ -z "$enabled" -o "$enabled" = "0" ] ; then
		return
	fi

	opening_hours_profile_macro="NoOp()"
	if [ -n "$opening_hours_profile" -a "x$opening_hours_profile" != "x-" ] ; then
		opening_hours_profile_macro="GoSub(sub-check-$opening_hours_profile,s,1)"
	echo "[sub-$1]" >> 				$WORK_DIR/macros
	echo "exten => $extension,1,GoSub(sub-$1,s,1)" >>	$WORK_DIR/extensions_local
	echo "exten => $extension,n,Hangup()" >>	$WORK_DIR/extensions_local

	tmp=$(mktemp)
	# Find all tone selections belonging to the IVR being configured
	local section cfgtype
	num_selections=0
	[ -z "$CONFIG_SECTIONS" ] && return 0
	for section in ${CONFIG_SECTIONS}; do
		config_get cfgtype "$section" TYPE
		[ -n "tone_selection" -a "x$cfgtype" != "xtone_selection" ] && continue
		local ts_owner ts_enabled ts_user ts_number ts_extension

		config_get ts_owner $section owner
		config_get ts_enabled $section enable
		config_get ts_user $section user
		config_get ts_number $section number

		if [ "$ts_owner" = "$1" ] ; then
			if [ -z "$ts_enabled" -o "$ts_enabled" = "0" ] ; then
				continue
			fi

			config_get ts_extension $ts_user extension
			if [ -z "$ts_extension" ] ; then
				echo "User $ts_user has no extension"
				continue
			fi
			echo "exten => s,n,GotoIf($[\"\${digit}\"=\"$ts_number\"]?local_extensions,$ts_extension,1)" >> $tmp
			num_selections=$((num_selection+1))
		fi
	done

	echo "exten => s,1,Set(OPEN=1)" >>					$WORK_DIR/macros
	echo "exten => s,n,$opening_hours_profile_macro" >>			$WORK_DIR/macros
	echo "exten => s,n,GotoIf($[\"\${OPEN}\"=\"0\"]?closed)" >>		$WORK_DIR/macros
	echo "exten => s,n,Answer()" >>						$WORK_DIR/macros
	if [ "$num_selections" -eq 0 ] ; then
		if ! [ -z $sound_file -o "x$sound_file" == "x-" ] ; then	
			echo "exten => s,n(intro),Playback($sound_file)" >>	$WORK_DIR/macros
		fi
		echo "exten => s,n,Goto(hangup,h,2)" >>				$WORK_DIR/macros
	else
		if ! [ -z $sound_file -o "x$sound_file" == "x-" ] ; then	
			echo "exten => s,n(intro),Background($sound_file)" >>	$WORK_DIR/macros
		fi
		echo "exten => s,n,Read(digit,,1,,,10)" >>			$WORK_DIR/macros
		cat $tmp >>							$WORK_DIR/macros
		echo "exten => s,n,Goto(intro)" >>				$WORK_DIR/macros
	fi
	echo "exten => s,n,Return()" >>					$WORK_DIR/macros
	echo "exten => s,n(closed),NoOp()" >>					$WORK_DIR/macros

	# check for mailbox
	config_get mailbox $1 mailbox
	if [ -n "$mailbox" -a "$mailbox" != "-" ] ; then
		boxnumber=$(mailbox_get_boxnumber $mailbox)
		if [ "$?" -eq 0 ] ; then
			echo "Enabling mailbox $boxnumber for IVR $1"
			echo "exten => s,n,VoiceMail($boxnumber)" >>		$WORK_DIR/macros
		fi
	fi

	echo "" >>								$WORK_DIR/macros
	rm -f $tmp
}

configure_queue() {
	local name enabled strategy extension members

	config_get name $1 name
	config_get enabled $1 enable
	config_get strategy $1 strategy
	config_get extension $1 extension
	config_get members $1 members

	if [ -z "$enabled" -o "$enabled" = "0" ] ; then
		continue
	fi

	echo "exten => $extension,1,GoSub(sub-$1,s,1)" >>		$WORK_DIR/extensions_local
	echo "exten => $extension,n,Hangup()" >>		$WORK_DIR/extensions_local
	
	cp "$TEMPLATE_DIR/queue" "$WORK_DIR/queue"
	sed -i "s/|SECTION|/$1/g"				$WORK_DIR/queue
	sed -i "s/|STRATEGY|/$strategy/g"			$WORK_DIR/queue

	for member in $(echo $members | tr " ")
	do
		local out=""
		re='^[0-9]+$'
		num=${member#$LINENAME}
		if [ -z "${num##[0-9]*}" ] ; then
			out="$CHANNELNAME/$num"
		else
			local sip_user
			config_get sip_user $member user
			if ! [ -z $sip_user ] ; then
				out="PJSIP/$sip_user"
			fi
		fi
		if ! [ -z $out ] ; then
			echo "member => $out" >>		$WORK_DIR/queue
		fi
	done

	cat $WORK_DIR/queue >>				$WORK_DIR/queues.conf

	echo "[sub-$1]" >>					$WORK_DIR/macros
	echo "exten => s,1,Answer()" >>				$WORK_DIR/macros

	# Workaround to fix no ringback issue for incoming SIP calls
	echo "exten => s,n,Playback(silence/1)" >>		$WORK_DIR/macros

	echo "exten => s,n,Queue($1,R)" >>			$WORK_DIR/macros
	echo "exten => s,n,Return()" >>			$WORK_DIR/macros
	echo "" >>						$WORK_DIR/macros
}

configure_cdr() {
	local csv_max_row

	config_get csv_max_row cdr_options csv_max_row

	if [ -z "$csv_max_row" ] ; then
		csv_max_row="100"
	fi

	sed -i "s/|MAXROW|/$csv_max_row/"			$WORK_DIR/cdr.conf

	mkdir -p /var/log/asterisk/cdr-csv
	touch /var/log/asterisk/cdr-csv/Master.csv
}

configure_call_filter() {
	configure_call_filter_helper $1 "incoming"
	echo "" >>						$WORK_DIR/macros
	configure_call_filter_helper $1 "outgoing"
	echo "" >>						$WORK_DIR/macros
}

configure_call_filter_helper() {
	local block_outgoing
	local block_incoming

	config_get block_outgoing $1 block_outgoing
	config_get block_incoming $1 block_incoming

	[ "$#" -ge 2 ] || return 0

	direction=$2
	if ! [ "x$direction" != "xincoming" -o "x$direction" != "xoutgoing" ] ; then
		return
	fi

	echo "[sub-$1-$direction]" >>				$WORK_DIR/macros
	echo "exten => s,1,NoOp()" >>				$WORK_DIR/macros

	# Do nothing if blocking of outgoing calls is disabled
	if [ "x$direction" == "xoutgoing" -a "x$block_outgoing" != "x1" ] ; then
		echo "exten => s,n,Return()" >>		$WORK_DIR/macros
		return
	fi

	# Do nothing if blocking of incoming calls is disabled
	if [ "x$direction" == "xincoming" -a "x$block_incoming" != "x1" ] ; then
		echo "exten => s,n,Return()" >>		$WORK_DIR/macros
		return
	fi

	local section cfgtype owner
	for section in ${CONFIG_SECTIONS}; do
		config_get cfgtype "$section" TYPE
		[ "x$cfgtype" != "xcall_filter_rule_$direction" ] && continue
		config_get owner $section owner
		if [ "x$owner" == "x$1" ] ; then
			local rule_extension rule_enabled
			config_get rule_extension $section extension
			config_get rule_enabled $section enable
			[ -z "$rule_enabled" -o "$rule_enabled" = "0" ] && continue

			# Regular expression for matching calling or called number
			regexp=$(echo $rule_extension | sed 's/#.*/\[0-9\]*/')
		
			if [ "x$direction" == "xoutgoing" ] ; then
				cmd="GotoIf($[\"\${DIAL_EXTEN}\":\"$regexp\">0]?block)"
				echo "exten => s,n,$cmd" >>	$WORK_DIR/macros
			fi
			
			if [ "x$direction" == "xincoming" ] ; then
				cmd="GotoIf($[\"\${CALLERID(num)}\":\"$regexp\">0]?block)"
				echo "exten => s,n,$cmd" >>	$WORK_DIR/macros
			fi
		fi
	done
	
	echo "exten => s,n,Return()" >>			$WORK_DIR/macros
	if [ "x$direction" == "xincoming" ] ; then
		cmd="GoSub(sub-callhandler-incoming-blocked,s,1(\${CALLERID(num)}))"
		echo "exten => s,n(block),$cmd" >>		$WORK_DIR/macros
	else
		cmd="GoSub(sub-callhandler-outgoing-blocked,s,1(\${DIAL_EXTEN}))"
		echo "exten => s,n(block),$cmd" >>		$WORK_DIR/macros
	fi
}

get_call_filter() {
	provider=$1
	direction=$2
	macro="NoOp()"
	
	if ! [ "x$direction" != "xincoming" -o "x$direction" != "xoutgoing" ] ; then
		echo $macro
	fi

	local call_filter enabled
	config_get call_filter $provider call_filter "call_filter0"
	if ! [ -z "$call_filter" -o "$call_filter" == "-" ] ; then
		macro="GoSub(sub-$call_filter-$direction,s,1)"
	fi
	echo $macro
}

configure_opening_hours_profile() {
	echo "[sub-check-$1]" >>		$WORK_DIR/macros
	echo "exten => s,1,NoOp()" >>		$WORK_DIR/macros
	local invert label
	config_get invert $1 invert
	if [ "$invert" == "1" ] ; then
		label="closed"
	else
		label="open"
	fi
	# Find all timespans belonging to the opening hours profile
	local section cfgtype
	[ -z "$CONFIG_SECTIONS" ] && return 0
	for section in ${CONFIG_SECTIONS}; do
		config_get cfgtype "$section" TYPE
		[ -n "timespan" -a "x$cfgtype" != "xtimespan" ] && continue
		local owner
	
		config_get owner $section owner
		if [ "$owner" == "$1" ] ; then
			local tr dow dom months
			config_get tr $section time_range
			config_get dow $section days_of_week
			config_get dom $section days_of_month
			config_get months $section months
	
			echo "exten => s,n,GotoIfTime($tr,$dow,$dom,$months,?$label)" >> $WORK_DIR/macros
		fi
	done
	if [ "$invert" == "0" ] ; then
		echo "exten => s,n,Goto(closed)" >>		$WORK_DIR/macros
	fi
	echo "exten => s,n(open),Set(OPEN=1)" >>		$WORK_DIR/macros
	echo "exten => s,n,Return()" >>			$WORK_DIR/macros
	echo "exten => s,n(closed),Set(OPEN=0)" >>		$WORK_DIR/macros
	echo "" >>						$WORK_DIR/macros
}

configure_extensions() {
	local custom_incoming_enabled
	local custom_hangup_enabled
	local extension_all_ports
	local test_audio_extension
	local test_echo_extension
	local record_message_extension

	config_get custom_incoming_enabled custom_dialplan custom_incoming_enable
	config_get custom_hangup_enabled custom_dialplan custom_hangup_enable
	config_get howler_timeout custom_dialplan howler_timeout
	config_get extension_all_ports custom_dialplan all_ports_extension
	config_get test_audio_extension custom_dialplan test_audio_extension
	config_get test_echo_extension custom_dialplan test_echo_extension
	config_get record_message_extension custom_dialplan record_message_extension

	if [ -z "$custom_incoming_enabled" -o "$custom_incoming_enabled" = "0" ] ; then
		sed -i "s/|CUSTOM_INCOMING|/NoOp()/"					$WORK_DIR/extensions.conf
	else
		sed -i "s/|CUSTOM_INCOMING|/GoSub(sub-custom-incoming,s,1)/"			$WORK_DIR/extensions.conf
	fi

	if [ -z "$custom_hangup_enabled" -o "$custom_hangup_enabled" = "0" ] ; then
		sed -i "s/|CUSTOM_HANGUP|/Playback(beep)/"				$WORK_DIR/extensions.conf
	else
		sed -i "s/|CUSTOM_HANGUP|/GoSub(sub-custom-hangup,s,1)/"			$WORK_DIR/extensions.conf
	if [ -z "$howler_timeout" ] ; then
		sed -i "s/|HOWLER_TIMEOUT|/180/"				$WORK_DIR/extensions.conf
	else
		sed -i "s/|HOWLER_TIMEOUT|/$howler_timeout/"			$WORK_DIR/extensions.conf
	fi
	if [ -n "$extension_all_ports" ] ; then
		local all_lines=$(getAllLines)
		if [ -z $all_lines ]; then
			echo "exten => $extension_all_ports,1,NoOp()"		>> $WORK_DIR/extensions_local
		else
			echo "exten => $extension_all_ports,1,Dial($all_lines,,t)"	>> $WORK_DIR/extensions_local
		fi
		echo "exten => $extension_all_ports,n,Hangup()"			>> $WORK_DIR/extensions_local
	fi

	if [ -n "$test_audio_extension" ] ; then
		echo "exten => $test_audio_extension,1,Playback(tt-monkeys)"	>> $WORK_DIR/extensions_local
		echo "exten => $test_audio_extension,n,Hangup()"		>> $WORK_DIR/extensions_local
	fi

	if [ -n "$test_echo_extension" ] ; then
		echo "exten => $test_echo_extension,1,Echo()"			>> $WORK_DIR/extensions_local
		echo "exten => $test_echo_extension,n,Hangup()"			>> $WORK_DIR/extensions_local
	fi
	
	if [ -n "$record_message_extension" ] ; then
		echo "exten => $record_message_extension,1,Playback(beep)"		>> $WORK_DIR/extensions_local
		echo "exten => $record_message_extension,n,Record(\"/usr/lib/asterisk/recordings/user-recording\${STRFTIME(\${EPOCH},,%C%y%m%d-%T)}.gsm\")"	>> $WORK_DIR/extensions_local
		echo "exten => $record_message_extension,n,Playback(auth-thankyou)"	>> $WORK_DIR/extensions_local
		echo "exten => $record_message_extension,n,Hangup()"			>> $WORK_DIR/extensions_local
	fi

	echo "" >> $WORK_DIR/extensions.conf
}

configure_voicemail_incoming()
{
        config_get calling_features $1 calling_features
        config_get provider $1 provider
        if [ -n "$calling_features" -a "$3" == "$provider" ]; then
             config_get voice_mail_enable $calling_features voice_mail_enable
             if [ -n "$voice_mail_enable" -a "$voice_mail_enable" == "1" ]; then
                 if [ -n "$2" ] ; then
                    echo "Enabling mailbox $boxnumber for IVR $2"
                    echo "exten => $user,n,VoiceMail($2)" >>            $tmp
                 fi
             fi
        fi
}
configure_extensions_provider() {
	local enabled
	local displayname
	local user
	local custom_outgoing
	local direct_dial
	local congestiontone
	local internal_service
    local minimumnumberofdigits
    local prefixenable
    local prefixrange
    local prefixmaxdigits
    local maxnumdigits
    local counter=1
    local prefixnumber
    local prefixfound=0
    local max_count_prefix
Hemlata's avatar
Hemlata committed

        #setting total number of prefix entries possible
        max_count_prefix=50
	# load general settings
Hemlata's avatar
Hemlata committed
        config_get maxnumdigits numberingplan maxnumdigits

	# check if SIP Provider is disabled
	config_get enabled $1 enable
	if [ -z "$enabled" -o "$enabled" = "0" ] ; then
		return
	fi

	# check that SIP Provider has a configured user
	config_get user $1 user
	if [ -z "$user" ] ; then
		return
	fi

	# add speed dial options
	speed_dial=""
	[ -z "$CONFIG_SECTIONS" ] && return 0
	for section in ${CONFIG_SECTIONS}; do
		config_get cfgtype "$section" TYPE
		[ "x$cfgtype" != "xspeed_dial" ] && continue
		local tone
		local number
		config_get tone $section tone
		config_get number $section number
		speed_dial="${speed_dial}exten => ${tone},1,Goto($number, 1)\n"
		speed_dial="${speed_dial}exten => ${tone}#,1,Goto($number, 1)\n"
	done

	config_get displayname $1 displayname
	[ -z "$displayname" ] && displayname=$user
	config_get custom_outgoing custom_dialplan custom_outgoing_enable
	if [ -z "$custom_outgoing" -o "$custom_outgoing" = "0" ] ; then
		custom_outgoing="NoOp()"
	else
		custom_outgoing="GoSub(sub-custom-outgoing,s,1)"
	fi

	# check call filters for outgoing calls
	call_filter_macro=$(get_call_filter $1 outgoing)
		
	# set congestion tone (default to congestion)
	config_get congestiontone sip_options congestiontone
	if [ -z "$congestiontone" ] ; then
		congestiontone=congestion
	fi

	# create tempfile for outgoing, transfer and callforwarding contexts
	cp $TEMPLATE_DIR/extensions_provider $WORK_DIR/extensions_provider
Hemlata's avatar
Hemlata committed
        while [ "$counter" != $max_count_prefix ]
        do
          prefixnumber="prefixinfo"
          prefixnumber="${prefixnumber}$counter"
          config_get facilityaction $prefixnumber facilityaction
          # If facilityaction is missing means its prefixinfo for outgoingcalls
          if [ -z "$facilityaction" ]; then
            config_get prefixrange $prefixnumber  prefixrange
            config_get prefixenable $prefixnumber prefixenable
            config_get prefixmaxdigits $prefixnumber prefixmaxdigits
            # Setting up the prefix info if available
            if [ -n "$prefixenable" -a "$prefixenable" = "1" ] ; then
                if [ -n "$prefixrange" ]; then
                    #iterate through the prefix list seperated by "|" and add to extension file
                    prefixlist=$(echo $prefixrange | tr -d "()" | tr "|" "\n")
                    for prefixname in $prefixlist
                    do
                        if [ -n "$prefixmaxdigits" ] ; then
                            sed -i "25i exten => _TEST,1,        Set(DIAL_EXTEN=$\{EXTEN:0:$prefixmaxdigits\})" $WORK_DIR/extensions_provider
                        else
                            sed -i '25i exten => _TEST,1,        Set(DIAL_EXTEN=$\{EXTEN\})' $WORK_DIR/extensions_provider
                        fi
                        sed -i "s/TEST/$prefixname/g"  $WORK_DIR/extensions_provider
                        prefixfound=1
                    done
                fi
            fi
        fi
Hemlata's avatar
Hemlata committed
        counter=$((counter+1))
        done
        #if no prefix info found set the default dialplan
        if [ $prefixfound = 0 ] ; then
           sed -i "24a exten => _[*#0-9].,1,   Set(DIAL_EXTEN=$\{EXTEN:0:|MAXDIGITS|\})" $WORK_DIR/extensions_provider
        fi

        # setting of the numbering plan value
        if [ -n "$maxnumdigits" ] ; then
            sed -i "s/|MAXDIGITS|/$maxnumdigits/g"  $WORK_DIR/extensions_provider
        else
            sed -i "s/|MAXDIGITS|/15/g"  $WORK_DIR/extensions_provider
Hemlata's avatar
Hemlata committed
        fi
        #configure outgoing map
        config_foreach configure_outgoingmap outgoing_map $1 $user

        #set default to empty if not present
        sed -i "s/|PROVIDER|//g"                        $WORK_DIR/extensions_provider