summaryrefslogtreecommitdiffstats
path: root/freeswitch-model.lua
blob: d4bf837c257fdfc2e6aabd7551bfd6571806ccf9 (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
local mymodule = {}

-- Load libraries
modelfunctions = require("modelfunctions")
posix = require("posix")
fs = require("acf.fs")
format = require("acf.format")
validator = require("acf.validator")
xml = require("LuaXml")

-- Set variables
local processname = "freeswitch"
local packagename = "freeswitch"
local baseurl = "/etc/freeswitch"
local configfile = "/var/log/freeswitch/freeswitch.xml.fsxml"

-- ################################################################################
-- LOCAL FUNCTIONS

local is_valid_filename = function(filename)
	local dirname = posix.dirname(filename)
	return validator.is_valid_filename(filename) and string.match(dirname, baseurl) and not string.match(dirname, "%.%.")
end

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

mymodule.get_status = function()
	return modelfunctions.getstatus(processname, packagename, "Freeswitch Status")
end

function mymodule.get_startstop(self, clientdata)
        return modelfunctions.get_startstop(processname)
end
	
function mymodule.startstop_service(self, startstop, action)
        return modelfunctions.startstop_service(startstop, action)
end

function mymodule.getreloadxml()
	return cfe({ type="group", value={}, label="Reload Freeswitch XML" })
end

function mymodule.reload_xml(self, relo)
	relo.descr, relo.errtxt = modelfunctions.run_executable({"fs_cli", "-x", "reloadxml"}, true)
	return relo
end

mymodule.get_file = function(self, clientdata)
	local filename = clientdata.filename
	return modelfunctions.getfiledetails(filename, is_valid_filename)
end

mymodule.update_file = function(self, filedetails)
	local ret = modelfunctions.setfiledetails(self, filedetails, is_valid_filename)
	if not ret.errtxt then
		posix.chmod(filedetails.value.filename.value, "rw-------")
		posix.chown(filedetails.value.filename.value, posix.getpasswd("freeswitch", "uid") or 0, posix.getpasswd("freeswitch", "gid") or 0)
	end
	return ret
end

mymodule.list_files = function()
	local retval = {}
	for file in fs.find(null, baseurl) do
		local details = fs.stat(file)
		if details.type == "regular" then
			details.filename = file
			table.insert(retval, details)
		end
	end
	table.sort(retval, function(a,b) return a.filename < b.filename end)
	return cfe({ type="structure", value=retval, label="List of Freeswitch files" })
end

function mymodule.getnewfile()
	local filename = cfe({ label="File Name", descr="Must be in "..baseurl })
	return cfe({ type="group", value={filename=filename}, label="Freeswitch File" })
end

function mymodule.createfile(self, filedetails)
	local success = true
	local path = string.match(filedetails.value.filename.value, "^%s*(.*%S)%s*$") or ""
	if not string.find(path, "/") then
		path = baseurl.."/"..path
	end

	if not is_valid_filename(path) then
		success = false
		filedetails.value.filename.errtxt = "Invalid filename"
	else
		if not fs.is_dir(baseurl) then fs.create_directory(baseurl) end
		if posix.stat(path) then
			success = false
			filedetails.value.filename.errtxt = "Filename already exists"
		end
	end

	if success then
		fs.create_file(path)
	else
		filedetails.errtxt = "Failed to Create File"
	end

	return filedetails
end

function mymodule.getdeletefile(self, clientdata)
	local retval = {}
	retval.filename = cfe({ label="File Name", value=clientdata.filename or "" })
	return cfe({ type="group", value=retval, label="Delete Freeswitch File" })
end

function mymodule.deletefile(self, delfile)
	delfile.errtxt = "Failed to delete Freeswitch File - invalid filename"
	for i,file in ipairs(mymodule.list_files().value) do
		if delfile.value.filename.value == file.filename then
			delfile.errtxt = nil
			os.remove(delfile.value.filename.value)
			break
		end
	end
	
	return delfile
end

function mymodule.get_logfile(self, clientdata)
	local retval = cfe({ type="structure", value={}, label="Log File Configuration" })
	local config
	local res, err = pcall(function()
		config = xml.load(configfile)
	end)
	if not res and err then
		retval.errtxt = err
		config = xml.new()
	end

	if (config:find("load", "module", "mod_logfile")) then
		local logfileparam
		local logfileconfig = config:find("configuration", "name", "logfile.conf")
		-- There may be multiple profiles, but for now just use the first one found
		if logfileconfig then
			logfileparam = logfileconfig:find("param", "name", "logfile")
		end
		if logfileparam then
			retval.value[#retval.value+1] = {filename=logfileparam.value}
		else
			retval.value[#retval.value+1] = {filename="/var/log/freeswitch/freeswitch.log"}
		end
	end
	if (config:find("load", "module", "mod_syslog")) then
		local syslog = config:find("configuration", "name", "syslog.conf")
		local facility = syslog:find("param", "name", "facility") or {value="user"}
		local ident = syslog:find("param", "name", "ident") or {value="freeswitch"}
		retval.value[#retval.value+1] = {facility=facility.value, grep=ident.value}
	end

	return retval
end

return mymodule