summaryrefslogtreecommitdiffstats
path: root/lib/modelfunctions.lua
blob: 9dd1d6b0449da2fe47b27cce010632515aa616c8 (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
module(..., package.seeall)

-- Load libraries
require("fs")
require("processinfo")

function getenabled(processname)
	local result = cfe({ label = "Program status" })
	local t = processinfo.pidof(processname)
	if (t) and (#t > 0) then
		result.value = "Enabled"
	else
		result.value = "Disabled"
	end
	return result
end

function startstop_service(processname, action)
	-- action is validated in daemoncontrol
	local cmdresult,cmdmessage,cmderror,cmdaction = processinfo.daemoncontrol(processname, action)
	return cfe({ value=cmdmessage or "", errtxt=cmderror, label="Start/Stop result" })
end

function getstatus(processname, packagename, label)
	local status = {}
	
	local value, errtxt = processinfo.package_version(packagename)
	status.version = cfe({
		label="Program version",
		value=value,
		errtxt=errtxt,
		})

	status.status = getenabled(processname)

	local autostart_sequence, autostart_errtxt = processinfo.process_botsequence(processname)
	status.autostart = cfe({
		label="Autostart sequence",
		value=autostart_sequence,
		errtxt=autostart_errtxt,
		})
	
	return cfe({ type="group", value=status, label=label })
end

function getfiledetails(file, validatefunction)
	local filename = cfe({ value=file, label="File name" })
	local filecontent = cfe({ type="longtext", label="File content" })
	local filesize = cfe({ value="0", label="File size" })
	local mtime = cfe({ value="---", label="File date" })
	if fs.is_file(file) then
		local filedetails = fs.stat(file)
		filecontent.value = fs.read_file(file)
		filesize.value = filedetails.size
		mtime.value = filedetails.mtime
	else
		filename.errtxt = "File not found"
	end
	local filedetails = cfe({ type="group", value={filename=filename, filecontent=filecontent, filesize=filesize, mtime=mtime}, label="Config file details" })
	local success = true
	if validatefunction then
		success, filedetails = validatefunction(filedetails)
	end
	return filedetails
end

function setfiledetails(filedetails, validatefunction)
	filedetails.value.filecontent.value = string.gsub(format.dostounix(filedetails.value.filecontent.value), "\n+$", "")
	local success = true
	if validatefunction then
		success, filedetails = validatefunction(filedetails)
	end
	if success then
		fs.write_file(filedetails.value.filename.value, filedetails.value.filecontent.value)
		filedetails = getfiledetails(filedetails.value.filename.value)
	else
		filedetails.errtxt = "Failed to set file"
	end

	return filedetails
end

function validateselect(select)
	for i,option in ipairs(select.option) do
	 	if option == select.value then
			return true
		end
	end
	select.errtxt = "Invalid selection"
	return false
end

function validatemulti(multi)
	local reverseoption = {}
	for i,option in ipairs(multi.option) do
		reverseoption[option] = i
	end
	for i,value in ipairs(multi.value) do
		if not reverseoption[value] then
			multi.errtxt = "Invalid selection"
			return false
		end
	end
	return true
end