1--	Part of FusionPBX
2--	Copyright (C) 2016 Mark J Crane <markjcrane@fusionpbx.com>
3--	All rights reserved.
4--
5--	Redistribution and use in source and binary forms, with or without
6--	modification, are permitted provided that the following conditions are met:
7--
8--	1. Redistributions of source code must retain the above copyright notice,
9--	  this list of conditions and the following disclaimer.
10--
11--	2. Redistributions in binary form must reproduce the above copyright
12--	  notice, this list of conditions and the following disclaimer in the
13--	  documentation and/or other materials provided with the distribution.
14--
15--	THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16--	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17--	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18--	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19--	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20--	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21--	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22--	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23--	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24--	POSSIBILITY OF SUCH DAMAGE.
25
26--define a function to forward a message to an extension
27	function forward_add_intro(voicemail_id, uuid)
28
29		--flush dtmf digits from the input buffer
30			session:flushDigits();
31
32		--request whether to add the intro
33			--To add an introduction to this message press 1
34			add_intro_id = session:playAndGetDigits(1, 1, 3, 5000, "#*", "phrase:voicemail_forward_prepend:1:2", "phrase:invalid_entry", "\\d+");
35			freeswitch.consoleLog("notice", "[voicemail][forward add intro] "..add_intro_id.."\n");
36			if (add_intro_id == '1') then
37
38				--load libraries
39					local Database = require "resources.functions.database";
40					local Settings = require "resources.functions.lazy_settings";
41
42				--connect to the database
43					local db = dbh or Database.new('system');
44
45				--get the settings.
46					local settings = Settings.new(db, domain_name, domain_uuid);
47					local max_len_seconds = settings:get('voicemail', 'max_len_seconds', 'boolean') or 300;
48
49				--record your message at the tone press any key or stop talking to end the recording
50					if (session:ready()) then
51						session:sayPhrase("voicemail_record_greeting", "", "en")
52					end
53
54				--set the file full path
55					message_location = voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext;
56					message_intro_location = voicemail_dir.."/"..voicemail_id.."/intro_"..uuid.."."..vm_message_ext;
57
58				--record the message introduction
59					-- syntax is session:recordFile(file_name, max_len_secs, silence_threshold, silence_secs)
60					silence_seconds = 5;
61					if (storage_path == "http_cache") then
62						result = session:recordFile(message_intro_location, max_len_seconds, record_silence_threshold, silence_seconds);
63					else
64						mkdir(voicemail_dir.."/"..voicemail_id);
65						if (vm_message_ext == "mp3") then
66							shout_exists = trim(api:execute("module_exists", "mod_shout"));
67							if (shout_exists == "true") then
68								freeswitch.consoleLog("notice", "using mod_shout for mp3 encoding\n");
69								--record in mp3 directly
70									result = session:recordFile(message_intro_location, max_len_seconds, record_silence_threshold, silence_seconds);
71							else
72								--create initial wav recording
73									result = session:recordFile(message_intro_location, max_len_seconds, record_silence_threshold, silence_seconds);
74								--use lame to encode, if available
75									if (file_exists("/usr/bin/lame")) then
76										freeswitch.consoleLog("notice", "using lame for mp3 encoding\n");
77										--convert the wav to an mp3 (lame required)
78											resample = "/usr/bin/lame -b 32 --resample 8 -m s "..voicemail_dir.."/"..voicemail_id.."/intro_"..uuid..".wav "..message_intro_location;
79											session:execute("system", resample);
80										--delete the wav file, if mp3 exists
81											if (file_exists(message_intro_location)) then
82												os.remove(voicemail_dir.."/"..voicemail_id.."/intro_"..uuid..".wav");
83											else
84												vm_message_ext = "wav";
85											end
86									else
87										freeswitch.consoleLog("notice", "neither mod_shout or lame found, defaulting to wav\n");
88										vm_message_ext = "wav";
89									end
90							end
91						else
92							result = session:recordFile(message_intro_location, max_len_seconds, record_silence_threshold, silence_seconds);
93						end
94					end
95
96				--save the merged file into the database as base64
97					if (storage_type == "base64") then
98							local file = require "resources.functions.file"
99
100						--get the content of the file
101							local file_content = assert(file.read_base64(message_intro_location));
102
103						--save the merged file as base64
104							local sql = [[UPDATE SET v_voicemail_messages
105									SET message_intro_base64 = :file_content
106									WHERE domain_uuid = :domain_uuid
107									AND voicemail_message_uuid = :uuid]];
108							local params = {file_content = file_content, domain_uuid = domain_uuid, uuid = uuid};
109
110							if (debug["sql"]) then
111								freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "; params: " .. json.encode(params) .. "\n");
112							end
113
114							local dbh = Database.new('system', 'base64')
115							dbh:query(sql, params)
116							dbh:release()
117					end
118		end
119
120	end
121