1--
2--	FusionPBX
3--	Version: MPL 1.1
4--
5--	The contents of this file are subject to the Mozilla Public License Version
6--	1.1 (the "License"); you may not use this file except in compliance with
7--	the License. You may obtain a copy of the License at
8--	http://www.mozilla.org/MPL/
9--
10--	Software distributed under the License is distributed on an "AS IS" basis,
11--	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12--	for the specific language governing rights and limitations under the
13--	License.
14--
15--	The Original Code is FusionPBX
16--
17--	The Initial Developer of the Original Code is
18--	Mark J Crane <markjcrane@fusionpbx.com>
19--	Copyright (C) 2010-2018
20--	the Initial Developer. All Rights Reserved.
21--
22--	Contributor(s):
23--	Mark J Crane <markjcrane@fusionpbx.com>
24
25--predefined variables
26	predefined_destination = "";
27	fallback_destination = "";
28
29--define the trim function
30	require "resources.functions.trim";
31
32--define the explode function
33	require "resources.functions.explode";
34
35--prepare the api object
36	api = freeswitch.API();
37
38--answer the call
39	if (session:ready()) then
40		session:answer();
41	end
42
43--get and save the variables
44	if (session:ready()) then
45		sound_greeting = session:getVariable("sound_greeting");
46		sound_pin = session:getVariable("sound_pin");
47		sound_extension = session:getVariable("sound_extension");
48		pin_number = session:getVariable("pin_number");
49		sounds_dir = session:getVariable("sounds_dir");
50		caller_id_name = session:getVariable("caller_id_name");
51		caller_id_number = session:getVariable("caller_id_number");
52		predefined_destination = session:getVariable("predefined_destination");
53		fallback_destination = session:getVariable("fallback_destination");
54		digit_min_length = session:getVariable("digit_min_length");
55		digit_max_length = session:getVariable("digit_max_length");
56		digit_timeout = session:getVariable("digit_timeout");
57		context = session:getVariable("context");
58		privacy = session:getVariable("privacy");
59		max_tries = session:getVariable("max_tries");
60		pin_tries = session:getVariable("pin_tries");
61		extension_tries = session:getVariable("extension_tries");
62	end
63
64--set the sounds path for the language, dialect and voice
65	if (session:ready()) then
66		default_language = session:getVariable("default_language");
67		default_dialect = session:getVariable("default_dialect");
68		default_voice = session:getVariable("default_voice");
69		if (not default_language) then default_language = 'en'; end
70		if (not default_dialect) then default_dialect = 'us'; end
71		if (not default_voice) then default_voice = 'callie'; end
72	end
73
74--set defaults
75	if (not digit_min_length) then
76		digit_min_length = "2";
77	end
78
79	if (not digit_max_length) then
80		digit_max_length = "11";
81	end
82
83	if (not digit_timeout) then
84	    digit_timeout = "5000";
85	end
86
87	if (not sound_pin) then
88	    sound_pin = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-please_enter_pin_followed_by_pound.wav";
89	end
90
91	if (not sound_extension) then
92	    sound_extension = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-enter_destination_telephone_number.wav";
93	end
94
95	if (not max_tries) then
96	    max_tries = "3";
97	end
98
99	if (not pin_tries) then
100	    pin_tries = max_tries;
101	end
102
103	if (not extension_tries) then
104	    extension_tries = max_tries;
105	end
106
107--if the sound_greeting is provided then play it
108	if (session:ready() and sound_greeting) then
109		session:streamFile(sound_greeting);
110		session:sleep(200);
111	end
112
113--if the pin number is provided then require it
114	if (session:ready() and pin_number) then
115		min_digits = string.len(pin_number);
116		max_digits = string.len(pin_number)+1;
117		digits = session:playAndGetDigits(min_digits, max_digits, pin_tries, digit_timeout, "#", sound_pin, "", "\\d+");
118		if (digits == pin_number) then
119			--pin is correct
120		else
121			session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-pin_or_extension_is-invalid.wav");
122			session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/ivr/ivr-im_sorry.wav");
123			session:streamFile(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/voicemail/vm-goodbye.wav");
124			session:hangup("NORMAL_CLEARING");
125			return;
126		end
127	end
128
129--if a predefined_destination is provided then set the number to the predefined_destination
130	if (session:ready()) then
131		if (predefined_destination) then
132			destination_number = predefined_destination;
133		else
134			dtmf = ""; --clear dtmf digits to prepare for next dtmf request
135			destination_number = session:playAndGetDigits(digit_min_length, digit_max_length, extension_tries, digit_timeout, "#", sound_extension, "", "\\d+");
136			if (string.len(destination_number) == 0 and fallback_destination) then
137			    destination_number = fallback_destination;
138			end
139			--if (string.len(destination_number) == 10) then destination_number = "1"..destination_number; end
140		end
141	end
142
143--set privacy
144	if (session:ready()) then
145		if (privacy == "true") then
146			session:execute("privacy", "full");
147			session:execute("set", "sip_h_Privacy=id");
148			session:execute("set", "privacy=yes");
149		end
150	end
151
152--set the caller id name and number
153	if (session:ready()) then
154		cmd = "user_exists id ".. destination_number .." "..context;
155		user_exists = trim(api:executeString(cmd));
156		if (user_exists == "true") then
157			if (caller_id_name) then
158				--caller id name provided do nothing
159			else
160				caller_id_number = session:getVariable("effective_caller_id_name");
161			end
162			if (caller_id_number) then
163				--caller id number provided do nothing
164			else
165				caller_id_number = session:getVariable("effective_caller_id_number");
166			end
167		else
168			if (caller_id_name) then
169				--caller id name provided do nothing
170			else
171				caller_id_number = session:getVariable("outbound_caller_id_name");
172			end
173			if (caller_id_number) then
174				--caller id number provided do nothing
175			else
176				caller_id_number = session:getVariable("outbound_caller_id_number");
177			end
178		end
179	end
180
181--send the destination
182	if (session:ready()) then
183		if (user_exists == true) then
184			--local call
185			session:execute("transfer", destination_number .. " XML " .. context);
186		else
187			--exteernal call
188			session:execute("set", "effective_caller_id_name="..caller_id_name);
189			session:execute("set", "effective_caller_id_number="..caller_id_number);
190			session:execute("transfer", destination_number .. " XML " .. context);
191		end
192	end
193