summaryrefslogtreecommitdiffhomepage
path: root/contrib/package/asterisk-xip/files/uci/voicemenu
blob: 7b046770febb0b10c4ddd3e12753b0ef8da4af16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh

# IVR - Interactive Voice Response Menus.

ast_add_module voicemenu
init_voicemenu() {
	ast_enable_type voicemenu
	ast_enable_type voiceoption
}
check_append_voicemenu() {
	local context="${1}"
	eval "local isadded=\"\${dialplan_add_voicemenu_${context}-0}\""
	if [ "$isadded" != "1" ] ; then
		eval "dialplan_add_voicemenu_${context}=1"
		append voicemenu_contexts "$context"
		return 0
	else
		return 1
	fi
}
voicemenu_list="greeting timeoutlen timeoutsmax invalidmax includelocal localmenu"

handle_voicemenu() {
	opt_voicemenu_context="$1"
	check_append_voicemenu "${opt_voicemenu_context}"

	option_cb() {
		case "$1" in
			greeting|timeoutlen|timeoutsmax|invalidmax|includelocal|localmenu)
				eval "opt_voicemenu_${opt_voicemenu_context}_$1=\"\$2\"" ;;
			_*) ;;
			*) logerror "Invalid Voicemenu option: $1" ;;
		esac
	}
}

voiceoption_list="digit menu dial goto"

check_add_voiceoption() {
	[ -z "${last_vo_voicemenu}" ] && return 0

	if [ -z "${last_vo_dial}" ] ; then
		append opt_voicemenu_${last_vo_voicemenu}_action "exten => ${last_vo_digit},1,Goto(${last_vo_goto},s,1)" "${N}"
	else
		append opt_voicemenu_${last_vo_voicemenu}_action "exten => ${last_vo_digit},1,Dial(${last_vo_dial})" "${N}"
	fi

	append opt_voicemenu_${last_vo_voicemenu}_menu "${last_vo_menu}" "&"

	for i in ${voiceoption_list} ; do
		eval "unset last_vo_$i"
	done

}

handle_voiceoption() {
	opt_voicemenu_context="$1"
	check_add voiceoption

	option_cb() {
		case "$1" in
			voicemenu|digit|menu|dial|goto)
				eval "last_vo_$1=\"\$2\"" ;;
			_*) ;;
			*) logerror "Invalid Voiceoption option in ${opt_voicemenu_context}: $1" ;;
		esac
	}
}

createdialplan_voicemenu(){
	logdebug 0 "Creating Voicemenu dialplans"
	[ -z "${voicemenu_contexts}" ]  && return 0

	for curvm in ${voicemenu_contexts} ; do
		check_add_context "${curvm}"
		for i in ${voicemenu_list} menu action ; do
			eval "vmc_${i}=\"\${opt_voicemenu_${curvm}_${i}}\""
		done

		[ -z "${vmc_timeoutlen}" ] && vmc_timeoutlen=5
		[ -z "${vmc_timeoutsmax}" ] && vmc_timeoutsmax=3
		[ -z "${vmc_invalidmax}" ] && vmc_invalidmax=0

		# Construct a voicemenu - with as small as possible code.
		local nn=1
		if [ ! -z "${vmc_greeting}" ] ; then
			append_dialplan_context $curvm "exten => s,${nn},Background(${vmc_greeting})"
			nn=n
		fi

		local againtimeout="(againtimeout)"
		if [ ${vmc_invalidmax} -gt 0 ] ; then
			append_dialplan_context $curvm "exten => s,${nn},Set(NUMINVALID=${vmc_invalidmax})"
			nn=n
		fi

		if [ ${vmc_timeoutsmax} -gt 0 ] ; then
			local labagaininvalid="(againinvalid)"
			if [ ${vmc_invalidmax} -le 0 ] ; then
				labagaininvalid=""
			fi
			append_dialplan_context $curvm "exten => s,${nn}${labagaininvalid},Set(NUMTIMEOUTS=${vmc_timeoutsmax})"
			nn=n
		else
			if [ ${vmc_invalidmax} -gt 0 ] ; then
				againtimeout="(againinvalid)"
			else
				againtimeout=
			fi
		fi
		if [ ! -z "${vmc_localmenu}" ] ; then
			append_dialplan_context $curvm "exten => s,${nn}${againtimeout},Background(${vmc_localmenu})"
			nn=n
			againtimeout=
		fi
		if [ ! -z "${vmc_menu}" ] ; then
			append_dialplan_context $curvm "exten => s,${nn}${againtimeout},Background(${vmc_menu})"
			againtimeout=
			nn=n
		fi
		append_dialplan_context $curvm "exten => s,${nn}${againtimeout},WaitExten(${vmc_timeoutlen})
exten => s,n,Goto(t,1)
exten => s,n(goodbye),Playback(vm-goodbye)
exten => s,n,Hangup()"
		if [ ${vmc_timeoutsmax} -gt 0 ] ; then
			append_dialplan_context $curvm "exten => t,1,Set(NUMTIMEOUTS=\$[\${NUMTIMEOUTS} - 1])
exten => t,n,Gotoif(\$[\${NUMTIMEOUTS} >= 0]?s,againtimeout:s,goodbye)"
		else
			append_dialplan_context $curvm "exten => t,1,Goto(s,goodbye)"
		fi
		append_dialplan_context ${curvm} "${vmc_action}"
		[ -z "${vmc_includelocal}" ] || append_dialplan_context ${curvm} "include => ${vmc_includelocal}"
		if [ ${vmc_invalidmax} -gt 0 ] ; then
			append_dialplan_context $curvm "exten => i,1,Set(NUMINVALID=\$[\${NUMINVALID} - 1])
exten => i,n,Background(invalid)
exten => i,n,Gotoif(\$[\${NUMINVALID} >= 0]?s,againinvalid:s,goodbye)"
		else
			append_dialplan_context $curvm "exten => i,1,Background(invalid)${N}exten => i,n,Goto(s,goodbye)"
		fi
	done

}


# vim: ts=2 sw=2 noet foldmethod=indent