summaryrefslogtreecommitdiffstats
path: root/vmail-model.lua
blob: 9032dca8d0ba8f0aec8e978ff80b8c81a1aaf7e3 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
module (..., package.seeall)

-- Load libraries
require("modelfunctions")
require("posix")
require("fs")
require("format")
require("validator")
require("luasql.sqlite3")
require("session")

-- Set variables
local configfile = "/etc/freeswitchvmail.conf"
local configcontent = fs.read_file(configfile) or ""
local config = format.parse_ini_file(configcontent, "") or {}
config.database = config.database or "/var/lib/freeswitch/db/voicemail_default.db"
config.domain = config.domain or "voicemail"
config.event_socket_ip = config.event_socket_ip or "127.0.0.1"
config.event_socket_port = config.event_socket_port or "8021"
config.event_socket_password = config.event_socket_password or "ClueCon"
config.callback_url = config.callback_url or "sofia/gateway/asterlink.com/$1"
local env
local con

local voicemail_users_creation_script = {
	"CREATE TABLE voicemail_users (uid INTEGER PRIMARY KEY, username VARCHAR(255) UNIQUE)",
	"CREATE INDEX users_username_idx ON voicemail_users (username)",
}

local voicemail_values_creation_script = {
	"CREATE TABLE voicemail_values (uid INTEGER, nid INTEGER, value VARCHAR(255))",
	"CREATE INDEX values_uid_nid_idx on voicemail_values (uid, nid)",
}

local voicemail_params_creation_script = {
	"CREATE TABLE voicemail_params (nid INTEGER PRIMARY KEY, name VARCHAR(255) UNIQUE, type VARCHAR(255), label VARCHAR(255), descr VARCHAR(255), value VARCHAR(255))",
	"CREATE INDEX params_name_idx ON voicemail_params (name)",
	"INSERT INTO voicemail_params VALUES(null, 'username', 'text', 'Extension', '', '')",
	"INSERT INTO voicemail_params VALUES(null, 'firstname', 'text', 'User First Name', '', '')",
	"INSERT INTO voicemail_params VALUES(null, 'lastname', 'text', 'User Last Name', '', '')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-password', 'text', 'Voicemail Password', '', '')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-password-confirm', 'text', 'Enter again to confirm', '', '')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-mailto', 'text', 'Email Address', 'Email a notification, including audio file if enabled', '')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-email-all-messages', 'boolean', 'Email Enable', '', 'false')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-attach-file', 'boolean', 'Attach voicemail to email', 'Option to attach audio file to email', 'false')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-keep-local-after-email', 'boolean', 'Keep voicemail after emailed', 'When disabled the message will be deleted from the voicemailbox after the notification email is sent. This allows receiving voicemail via email alone, rather than having the voicemail available from the Web interface or by telephone. CAUTION: Attach voicemail to email must be enabled, OTHERWISE YOUR MESSAGES WILL BE LOST FOREVER.', 'true')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-notify-mailto', 'text', 'Pager Email Address', 'Email a short notification', '')",
	"INSERT INTO voicemail_params VALUES(null, 'vm-notify-email-all-messages', 'boolean', 'Pager Email Enable', '', 'false')",
	"INSERT INTO voicemail_params VALUES(null, 'callmenumber', 'text', 'Call Me Number', '', '')",
}

local voicemail_folders_creation_script = {
	"CREATE TABLE voicemail_folders (in_folder VARCHAR(255) PRIMARY KEY, label VARCHAR(255))",
	"INSERT INTO voicemail_folders VALUES('inbox', 'Inbox')",
}

local voicemail_prefs_creation_script = {"CREATE TABLE voicemail_prefs (username VARCHAR(255), domain VARCHAR(255), name_path VARCHAR(255), greeting_path VARCHAR(255), password VARCHAR(255))"}

-- ################################################################################
-- LOCAL FUNCTIONS
local function escape_quotes(str)
	return string.gsub(str or "", "'", "'\\''")
end

local function voicemail_inject(user, domain, sound_file, cid_num, cid_name)
	local cmd = "echo -e 'auth "..escape_quotes(config.event_socket_password).."\n\n"
	cmd = cmd.."api voicemail_inject "..escape_quotes(user).."@"..escape_quotes(domain).." "..escape_quotes(sound_file).." "..escape_quotes(cid_num).." "..string.gsub(escape_quotes(cid_name), " ", "%%20")
	cmd = cmd.."\n\nexit\n\n' | nc "..format.escapespecialcharacters(config.event_socket_ip).." "..format.escapespecialcharacters(config.event_socket_port).." 2>&1"
	local f = io.popen( cmd )
	local result = f:read("*a") or ""
	f:close()
	return result
end

local function voicemail_callback(extension, sound_file, cid_num, cid_name)
	local cmd = "echo -e 'auth "..escape_quotes(config.event_socket_password).."\n\n"
	cmd = cmd.."bgapi originate {ignore_early_media=true,origination_caller_id_name='\\''"..string.gsub(cid_name or "Voicemail", "'", "").."'\\'',origination_caller_id_number='\\''"
	cmd = cmd..string.gsub(cid_num or "", "'", "").."'\\''}"..escape_quotes(string.gsub(config.callback_url, "$1", extension)).." &playback("..escape_quotes(sound_file)..")"
	cmd = cmd.."\n\nexit\n\n' | nc "..format.escapespecialcharacters(config.event_socket_ip).." "..format.escapespecialcharacters(config.event_socket_port).." 2>&1"
	local f = io.popen( cmd )
	local result = f:read("*a") or ""
	f:close()
	return result
end

local function assert (v, m)
	if not v then
		m = m or "Assertion failed!"
		error(m, 0)
	end
	return v, m
end

-- Escape special characters in sql statements
local escape = function(sql)
	sql = sql or ""
	sql = string.gsub(sql, "'", "''")
	return string.gsub(sql, "\\", "\\\\")
end

local databaseconnect = function()
	if not con then
		-- create environment object
		env = assert (luasql.sqlite3())
		-- connect to data source
		con = assert (env:connect(config.database))
		return true
	end
	return false
end

local databasedisconnect = function()
	if env then
		env:close()
		env = nil
	end
	if con then
		con:close()
		con = nil
	end
end

local runscript = function(script)
	for i,scr in ipairs(script) do
		logevent(scr)
		assert( con:execute(scr) )
	end
end

local checktable = function(table)
	local success = false
	local errtxt
        local res, err = pcall(function()
		local sql = "SELECT * FROM "..table.." LIMIT 1"
		local cur = assert (con:execute(sql))
		cur:close()
		success = true
	end)
	if not res and err then
		errtxt = err
	end
	return success, errtxt
end

local getselectresponse = function(sql)
	local retval = {}
	local cur = assert (con:execute(sql))
	local row = cur:fetch ({}, "a")
	while row do
		local tmp = {}
		for name,val in pairs(row) do
			tmp[name] = val
		end
		retval[#retval + 1] = tmp
		row = cur:fetch (row, "a")
	end
	cur:close()
	return retval
end

local generatewhereclause = function(username, message, foldername, uid)
        local sql = ""
	local where = {}
	if username and username ~= "" then
		where[#where+1] = "username = '"..escape(username).."'"
	end
	if message and type(message) == "string" and message ~= "" then
		where[#where+1] = "uuid = '"..escape(message).."'"
	elseif message and type(message) == "table" and #message > 0 then
		local where2 = {}
		for i,m in ipairs(message) do
			where2[#where2+1] = "uuid = '"..escape(m).."'"
		end
		where[#where+1] = "(" .. table.concat(where2, " OR ") .. ")"
	end
	if foldername and foldername ~= "" then
		where[#where+1] = "in_folder = '"..escape(foldername).."'"
	end
	if uid and uid ~= "" then
		where[#where+1] = "uid = '"..escape(uid).."'"
	end
	if #where > 0 then
		sql = " WHERE " .. table.concat(where, " AND ")
	end
	return sql
end

-- These funtions access the new voicemail tables added for ACF

local listfolders = function(foldername)
	if not checktable("voicemail_folders") then runscript(voicemail_folders_creation_script) end
	local sql = "SELECT * FROM voicemail_folders" .. generatewhereclause(nil, nil, foldername).." ORDER BY label"
	return getselectresponse(sql)
end

local validfolder = function(foldername)
	return foldername and (foldername ~= "") and (#listfolders(foldername) > 0)
end

local listusers = function(username)
	if not checktable("voicemail_users") then runscript(voicemail_users_creation_script) end
	local sql = "SELECT * FROM voicemail_users" .. generatewhereclause(username).." ORDER BY username"
	return getselectresponse(sql)
end

local validuser = function(username)
	return username and (username ~= "") and (#listusers(username) > 0)
end

local getuserparams = function(username)
	local retval = {}
	if not checktable("voicemail_params") then runscript(voicemail_params_creation_script) end
	local sql = "SELECT * FROM voicemail_params"
	local params = getselectresponse(sql)
	local reverse_nids = {}
	for i,parm in ipairs(params) do
		if parm.name then
			reverse_nids[parm.nid] = parm.name
			retval[parm.name] = {}
			for n,v in pairs(parm) do
				retval[parm.name][n] = v
			end
			if retval[parm.name].type == "boolean" then
				retval[parm.name].value = (retval[parm.name].value == "true")
			end
		end
	end
	if retval.username and username then retval.username.value = username end
	if validuser(username) then
		-- Get password from voicemail_prefs (don't fail for missing table)
		if checktable("voicemail_prefs") then
			local sql = "SELECT password FROM voicemail_prefs"..generatewhereclause(username)
			local password = getselectresponse(sql)
			if retval["vm-password"] and password[1] then
				retval["vm-password"].value = password[1].password
			end
		end

		-- Get the uid that corresponds to this username
		sql = "SELECT uid FROM voicemail_users"..generatewhereclause(username)
		local uid = getselectresponse(sql)
		if #uid == 1 then
			-- Get other parameters from voicemail_values
			if not checktable("voicemail_values") then runscript(voicemail_values_creation_script) end
			sql = "SELECT * FROM voicemail_values"..generatewhereclause(nil, nil, nil, uid[1].uid)
			local params = getselectresponse(sql)
			for i,param in ipairs(params) do
				if param.nid and reverse_nids[param.nid] and retval[reverse_nids[param.nid]] and param.value then
					if retval[reverse_nids[param.nid]].type == "boolean" then
						param.value = (param.value == "true")
					end
					retval[reverse_nids[param.nid]].value = param.value
				end
			end
		end
	end
	return retval
end

local setuserparams = function(userparams)
	if not userparams.username or not userparams.username.value or not validuser(userparams.username.value) then
		return false, "Invalid User"
	end
	local success = true
	if not checktable("voicemail_params") then runscript(voicemail_params_creation_script) end
	local sql = "SELECT * FROM voicemail_params"
	local params = getselectresponse(sql)
	-- Get the uid that corresponds to this username
	sql = "SELECT uid FROM voicemail_users"..generatewhereclause(userparams.username.value)
	local uid = getselectresponse(sql)
	if #uid == 1 then
		-- There are a few params not to put in the voicemail_values table
		if not checktable("voicemail_values") then runscript(voicemail_values_creation_script) end
		local ignoreparam = { username=true, ["vm-password"]=true, ["vm-password-confirm"]=true }
		con:execute("START TRANSACTION")
		for i,parm in ipairs(params) do
			if parm.name and not ignoreparam[parm.name] then
				sql = "DELETE FROM voicemail_values"..generatewhereclause(nil, nil, nil, uid[1].uid).." and nid='"..parm.nid.."'"
				assert( con:execute(sql) )
				if userparams[parm.name] and (userparams[parm.name].value ~= nil) and tostring(userparams[parm.name].value) ~= parm.value then
					sql = "INSERT INTO voicemail_values VALUES('"..uid[1].uid.."', '"..parm.nid.."', '"..tostring(userparams[parm.name].value).."')"
					assert( con:execute(sql) )
				end
			end
		end
	end
	-- Set password to voicemail_prefs
	if userparams["vm-password"] and userparams["vm-password"].value and userparams["vm-password"].value ~= "" then
		if not checktable("voicemail_prefs") then runscript(voicemail_prefs_creation_script) end
		sql = "SELECT password FROM voicemail_prefs"..generatewhereclause(userparams.username.value)
		local password = getselectresponse(sql)
		if #password > 0 then
			-- update
			sql = "UPDATE voicemail_prefs SET password='"..userparams["vm-password"].value.."'"..generatewhereclause(userparams.username.value)
		else
			-- insert
			sql = "INSERT INTO voicemail_prefs (username, domain, password) VALUES ('"..userparams.username.value.."', '"..config.domain.."', '"..userparams["vm-password"].value.."')"
		end
		assert( con:execute(sql) )
	end
	con:execute("COMMIT")
	return success
end

local function validateconfig(newconfig)
	local success = true
	if newconfig.value.domain.value == "" then
		newconfig.value.domain.errtxt = "Cannot be blank"
		success = false
	end
	if newconfig.value.database.value == "" then
		newconfig.value.database.errtxt = "Cannot be blank"
		success = false
	end
	if newconfig.value.event_socket_ip.value == "" then
		newconfig.value.event_socket_ip.errtxt = "Cannot be blank"
		success = false
	end
	if newconfig.value.event_socket_port.value == "" then
		newconfig.value.event_socket_port.errtxt = "Cannot be blank"
		success = false
	end
	if newconfig.value.event_socket_password.value == "" then
		newconfig.value.event_socket_password.errtxt = "Cannot be blank"
		success = false
	end
	if newconfig.value.callback_url.value == "" then
		newconfig.value.callback_url.errtxt = "Cannot be blank"
		success = false
	end
	return success, newconfig
end

-- ################################################################################
-- PUBLIC FUNCTIONS

get_config = function()
	local result = {}
	result.domain = cfe({ value=config.domain, label="Domain" })
	result.database = cfe({ value=config.database, label="Database" })
	result.event_socket_ip = cfe({ value=config.event_socket_ip, label="FS Event Socket IP" })
	result.event_socket_port = cfe({ value=config.event_socket_port, label="FS Event Socket Port" })
	result.event_socket_password = cfe({ value=config.event_socket_password, label="FS Event Socket Password" })
	result.callback_url = cfe({ value=config.callback_url, label="FS SIP URL for Callback", desc="Use $1 for extension. No other parameters allowed." })
	return cfe({ type="group", value=result, label="Voicemail Config" })
end

update_config = function(newconfig)
	local success = validateconfig(newconfig)
	if success then
		for name,val in pairs(newconfig.value) do
			configcontent = format.update_ini_file(configcontent, "", name, tostring(val.value))
		end

		fs.write_file(configfile, configcontent)
		config = format.parse_ini_file(configcontent, "") or {}
	else
		newconfig.errtxt = "Failed to update config"
	end

	return newconfig
end

list_messages = function(username)
	local retval = {}
	local errtxt
        local res, err = pcall(function()
		local connected = databaseconnect()
		local sql = "SELECT * FROM voicemail_msgs"
		sql = sql .. generatewhereclause(username)
		sql = sql .. " ORDER BY username ASC, created_epoch ASC"
		retval = getselectresponse(sql)
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end
	return cfe({ type="structure", value=retval, label="List of Messages", errtxt=errtxt })
end

get_message = function(message, username)
	local retval = cfe({ type="raw", label="error", option="audio/x-wav" })
        local res, err = pcall(function()
		local connected = databaseconnect()
		local sql = "SELECT file_path FROM voicemail_msgs"
		sql = sql .. generatewhereclause(username, message)
		local tmp = getselectresponse(sql)
		if connected then databasedisconnect() end
		if #tmp == 0 then
			retval.errtxt = "Invalid message"
		else
			retval.label = posix.basename(tmp[1].file_path)
			retval.value = fs.read_file(tmp[1].file_path)
		end
	end)
	if not res and err then
		retval.errtxt = err
	end
	return retval
end

delete_message = function(message, username)
	local retval = cfe({ label="Delete message result", errtxt="Failed to delete message - message not found" })
	local messages = format.string_to_table(message, "%s*,%s*")
        local res, err = pcall(function()
		local connected = databaseconnect()
		local sql = "SELECT * FROM voicemail_msgs"
		sql = sql .. generatewhereclause(username, messages)
		local tmp = getselectresponse(sql)
		if #tmp == #messages then
			sql = "DELETE FROM voicemail_msgs" .. generatewhereclause(username, messages)
			assert (con:execute(sql))
			for i,t in ipairs(tmp) do
				os.remove(t.file_path)
			end
			if #messages == 1 then
				retval.value = "Deleted message"
			else
				retval.value = "Deleted "..#messages.." messages"
			end
			retval.errtxt = nil
		end
		if connected then databasedisconnect() end
	end)
	if not res and err then
		retval.errtxt = err
	end

	return retval
end

forward_message = function(message, newuser, username)
	local retval = cfe({ label="Forward message result" })
	local messages = format.string_to_table(message, "%s*,%s*")
        local res, err = pcall(function()
		local connected = databaseconnect()
		-- Check if message exists
		local sql = "SELECT * FROM voicemail_msgs" .. generatewhereclause(username, messages)
		local mess = getselectresponse(sql)
		if #mess == #messages then
			-- Check if newuser exists
			if validuser(newuser) then
				for i,m in ipairs(mess) do
					-- Forward message using mod_voicemail API
					-- doesn't seem like there's any way to tell whether or not it worked
					voicemail_inject(newuser, config.domain, m.file_path, m.cid_number, m.cid_name)
				end
				if #mess == 1 then
					retval.value = "Forwarded message"
				else
					retval.value = "Forwarded "..#mess.." messages"
				end
			else
				retval.errtxt = "Failed to forward message - invalid user"
			end
		else
			retval.errtxt = "Failed to forward message - message not found"
		end
		if connected then databasedisconnect() end
	end)
	if not res and err then
		retval.errtxt = err
	end

	return retval
end

email_message = function(message, address, username)
	local retval = cfe({ label="E-mail message result" })
	local messages = format.string_to_table(message, "%s*,%s*")
	if address == "" or string.find(address, "%s") or not string.find(address, "@") then
		retval.errtxt = "Failed to e-mail message - invalid address"
	else
        	local res, err = pcall(function()
			local connected = databaseconnect()
			-- Check if message exists
			local sql = "SELECT * FROM voicemail_msgs" .. generatewhereclause(username, messages)
			local mess = getselectresponse(sql)
			if #mess == #messages then
				-- Create a temporary user and settings
				local newuser = "tempuser"..session.random_hash(128)
				while validuser(newuser) do
					newuser = "tempuser"..session.random_hash(128)
				end
				local settings = get_usersettings(newuser)
				if settings.value["vm-mailto"] and settings.value["vm-email-all-messages"] and settings.value["vm-attach-file"] and settings.value["vm-keep-local-after-email"] then
					settings.value["vm-mailto"].value = address
					settings.value["vm-email-all-messages"].value = true
					settings.value["vm-attach-file"].value = true
					settings.value["vm-keep-local-after-email"].value = false
					if settings.value["vm-password"] then settings.value["vm-password"].value = "1234" end
					if settings.value["vm-password-confirm"] then settings.value["vm-password-confirm"].value = "1234" end
					settings = create_usersettings(settings)
					if not settings.errtxt then
						for i,m in ipairs(mess) do
							-- E-mail message using mod_voicemail API
							-- doesn't seem like there's any way to tell whether or not it worked
							voicemail_inject(newuser, config.domain, m.file_path, m.cid_number, m.cid_name)
						end
						if #mess == 1 then
							retval.value = "E-mailed message"
						else
							retval.value = "E-mailed "..#mess.." messages"
						end
						-- Now, delete the temporary user
						delete_user(newuser)
					else
						retval.errtxt = "Failed to e-mail message - "..settings.errtxt
					end
				else
					retval.errtxt = "Failed to e-mail message - unsupported"
				end
			else
				retval.errtxt = "Failed to e-mail message - message not found"
			end
			if connected then databasedisconnect() end
		end)
		if not res and err then
			retval.errtxt = err
		end
	end

	return retval
end

move_message = function(message, newfolder, username)
	local retval = cfe({ label="Move message result" })
	local messages = format.string_to_table(message, "%s*,%s*")
        local res, err = pcall(function()
		local connected = databaseconnect()
		-- Check if message exists
		local sql = "SELECT * FROM voicemail_msgs" .. generatewhereclause(username, messages)
		local mess = getselectresponse(sql)
		if #mess == #messages then
			-- Check if newfolder exists
			if validfolder(newfolder) then
				for i,m in ipairs(mess) do
					local sql = "UPDATE voicemail_msgs SET in_folder='"..newfolder.."'" .. generatewhereclause(username, messages)
					assert (con:execute(sql))
				end
				if #mess == 1 then
					retval.value = "Moved message"
				else
					retval.value = "Moved "..#mess.." messages"
				end
			else
				retval.errtxt = "Failed to move message - invalid folder"
			end
		else
			retval.errtxt = "Failed to move message - message not found"
		end
		if connected then databasedisconnect() end
	end)
	if not res and err then
		retval.errtxt = err
	end

	return retval
end

callback_message = function(message, extension, username)
	local retval = cfe({ label="Callback message result" })
	if string.find(message, ",") then
		retval.errtxt = "Failed to callback message - can only callback one message at a time"
	elseif extension == "" or string.find(extension, "[%s@]") then
		retval.errtxt = "Failed to callback message - invalid extension"
	else
        	local res, err = pcall(function()
			local connected = databaseconnect()
			-- Check if message exists
			local sql = "SELECT * FROM voicemail_msgs" .. generatewhereclause(username, message)
			local mess = getselectresponse(sql)
			if #mess == 1 then
				-- Initiate the call to the extension
				voicemail_callback(extension, mess[1].file_path, username)
				retval.value = "Initiated callback"
			else
				retval.errtxt = "Failed to callback message - message not found"
			end
			if connected then databasedisconnect() end
		end)
		if not res and err then
			retval.errtxt = err
		end
	end

	return retval
end

list_folders = function()
	local errtxt
	local folders = {}
        local res, err = pcall(function()
		local connected = databaseconnect()
		folders = listfolders()
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end

	return cfe({ type="structure", value=folders, label="Voicemail Folders", errtxt=errtxt })
end

list_passwords = function(username)
	local errtxt
	local users = {}
        local res, err = pcall(function()
		local connected = databaseconnect()
		if not checktable("voicemail_prefs") then runscript(voicemail_prefs_creation_script) end
		local sql = "select username, password from voicemail_prefs"..generatewhereclause(username)
		users = getselectresponse(sql)
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end

	return cfe({ type="structure", value=users, label="Voicemail User Passwords", errtxt=errtxt })
end

list_users = function()
	local errtxt
	local users = {}
        local res, err = pcall(function()
		local connected = databaseconnect()
		if not checktable("voicemail_users") then runscript(voicemail_users_creation_script) end
		if not checktable("voicemail_values") then runscript(voicemail_values_creation_script) end
		if not checktable("voicemail_params") then runscript(voicemail_params_creation_script) end
		-- This crazy query gets the username from voicemail_users, the firstname and lastname from two instances of voicemail_values (using voicemail_params to determine the corresponding nid values) drops usernames starting with "tempuser" and ordering by username 
		local sql = "select u.username, v1.value lastname, v2.value firstname from voicemail_users u left outer join voicemail_values v1 on u.uid = v1.uid AND v1.nid=(select nid from voicemail_params where name='lastname') left outer join voicemail_values v2 on u.uid = v2.uid and v2.nid=(select nid from voicemail_params where name='firstname') where u.username NOT LIKE 'tempuser%' order by u.username"
		users = getselectresponse(sql)
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end

	return cfe({ type="structure", value=users, label="Voicemail Users", errtxt=errtxt })
end

delete_user = function(username)
	local result = ""
	local errtxt
	errtxt = nil
       	local res, err = pcall(function()
		local connected = databaseconnect()
		local users = listusers(username)
		if #users == 0 then
			errtxt = "User does not exist"
		else
			-- Delete all of the user's voicemails
			local messages = list_messages(username)
			if #messages.value then
				for i,m in ipairs(messages.value) do
					delete_message(m.uuid)
				end
			end
			-- Remove the user parameters
			sql = "DELETE FROM voicemail_values " .. generatewhereclause(nil, nil, nil, users[1].uid)
			assert (con:execute(sql))
			-- Remove the user
			sql = "DELETE FROM voicemail_users " .. generatewhereclause(nil, nil, nil, users[1].uid)
			assert (con:execute(sql))
			result = "Voicemail User Deleted"
		end
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end

	return cfe({ value=result, errtxt=errtxt, label="Delete User Result" })
end

get_usersettings = function(username)
	local retval = {}
	local errtxt
        local res, err = pcall(function()
		local connected = databaseconnect()
		retval = getuserparams(username)
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end
	if retval["vm-password"] and retval["vm-password-confirm"] then retval["vm-password-confirm"].value = retval["vm-password"].value end

	return cfe({ type="group", value=retval, label="Voicemail User Settings", errtxt=errtxt })
end

create_usersettings = function(usersettings)
	return update_usersettings(usersettings, true)
end

update_usersettings = function(usersettings, create)
	local success = true
	local errtxt
	-- Validate the settings
	if not validator.is_integer(usersettings.value["vm-password"].value) then
		success = false
		usersettings.value["vm-password"].errtxt = "Password must be all numbers"
	end
	if usersettings.value["vm-password"].value ~= usersettings.value["vm-password-confirm"].value then
		success = false
		usersettings.value["vm-password-confirm"].errtxt = "Password does not match"
	end
	if success then
        	local res, err = pcall(function()
			local connected = databaseconnect()
			local u = listusers(usersettings.value.username.value)
			if create and #u > 0 then
				success = false
				errtxt = "User already exists"
			elseif not create and #u == 0 then
				success = false
				errtxt = "User does not exist"
			else
				if create then
					sql = "INSERT INTO voicemail_users VALUES(null, '"..escape(usersettings.value.username.value).."')"
					assert (con:execute(sql))
				end
				success,errtxt = setuserparams(usersettings.value)
			end
			if connected then databasedisconnect() end
		end)
		if not res and err then
			success = false
			errtxt = err
		end
	end
	if not success then
		if create then
			usersettings.errtxt = errtxt or "Failed to create user"
		else
			usersettings.errtxt = errtxt or "Failed to save settings"
		end
	end
	return usersettings
end

process_directory_xml_request = function(input)
	local output = {}
	local errtxt
        local res, err = pcall(function()
		local connected = databaseconnect()
		if validuser(input.user) then
			output = getuserparams(input.user)
			-- Add the domain
			output.domain = cfe({ value=input.domain })
		else
			errtxt = "User not found"
		end
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end
	return cfe({ type="group", value=output, label="Directory Data", errtxt=errtxt })
end

process_dialplan_xml_request = function(input)
	local output = {}
	local errtxt
        local res, err = pcall(function()
		local connected = databaseconnect()
		if validuser(input["Caller-Destination-Number"]) then
			output.domain = cfe({ value=config.domain })
			output.username = cfe({ value=input["Caller-Destination-Number"] })
		else
			errtxt = "User not found"
		end
		if connected then databasedisconnect() end
	end)
	if not res and err then
		errtxt = err
	end
	return cfe({ type="group", value=output, label="Dialplan Data", errtxt=errtxt })
end