summaryrefslogtreecommitdiffstats
path: root/tinydns-model.lua
blob: a3f8a103d151838891c0fdf0564c42a4fccc1b5f (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
module(..., package.seeall)

require("procps")
require("getopts")
require("fs")

local configdir
local datafile
local configfiles = {}
local configitems = {}

local processname = "tinydns"
local configfile = "/etc/conf.d/" .. processname
local initdoptions = getopts.getoptsfromfile_onperline("/etc/init.d/" .. processname)
if (initdoptions) then
	configdir = initdoptions.DATADIR
	datafile = initdoptions.ROOT .. "/data" or "/var/cache/data"
else
	configdir = "/etc/" .. processname
	datafile = "/var/cache/data"
end

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

local function get_version()
	local cmd_output_result, cmd_output_error
	local cmd = "/sbin/apk_version -vs " .. processname .." 2>/dev/null"
	local f = io.popen( cmd )
	local cmdresult = f:read("*l")
	if (cmdresult) and (#cmdresult > 0) then
		cmd_output_result = string.match(cmdresult,"^%S*") or "Unknown"
	else
		cmd_output_error = "Program not installed"
	end	
	f:close()
	return cmd_output_result,cmd_output_error
end

--[[
-- This function could be used to check that valid parameters are used in different places
local function check_signs(sign)
	local output = {}
	local output = {prefix={"Z", "&", "=", "+", "@", "C",}}
	output = output[sign]
	return output
end
--]]

-- Return a table with the config-content of a file
-- Commented/Blank lines are ignored
local function get_value_from_file(file)
	local output = {}
	local filecontent = fs.read_file_as_array(file)
	for i=1,table.maxn(filecontent) do
		local l = filecontent[i]
		if not (string.find ( l, "^[;#].*" )) and not (string.find (l, "^%s*$")) then
			table.insert(output, string.match(l,"(.-)%s*$"))
		end
	end
	if (#output > 0) then
		return true, output
	else
		return false, output
	end

end

-- Function to recursively inserts all filenames in a dir into an array
local function recursedir(path, filearray)
	local k,v
	for k,v in pairs(posix.dir(path) or {}) do
		-- Ignore files that begins with a '.'
		if not string.match(v, "^%.") then
			local f = path .. "/" .. v
			-- If subfolder exists, list files in this subfolder
			if (posix.stat(f).type == "directory") then
				recursedir(f, filearray)
			else
				table.insert(filearray, f)
			end
		end
	end
end

-- Feed the configfiles table with list of all availage configfiles
local function searchforconfigfiles()
	local cnffile = {}
	recursedir(configdir, cnffile)
	for k,v in pairs(cnffile) do
		local configcontent = get_value_from_file(v)
		if (configcontent) then
			table.insert(configfiles, v)
		end
	end
end
searchforconfigfiles()
-- ################################################################################
-- PUBLIC FUNCTIONS

-- Present some general status
function getstatus()
	local status = {}
	local version,versionerrtxt = get_version()
	status.version = cfe({ name = "version",
		label="Program version",
		value=version,
		errtxt=versionerrtxt,
		 })
	status.status = cfe({ name="status",
		label="Program status",
		value=procps.pidof(processname),
		})
	return status
end

-- Return config-information
function getconfig()
	local config = {}
	local version,versionerrtxt = get_version()
	local listenaddr = getopts.getoptsfromfile_onperline(configfile,"IP") or {}
	config.listen = cfe({ 
		name = "listen",
		label="IP address to listen on",
		value=listenaddr.IP or "",
		 })

	return config
end


-- ################################################################################
-- DEBUG INFORMATION (Everything below will be deleted in the future)

function getdebug()
	local debug = {}

--[[
	local signs = get_available_signs("prefix") or {}
	debug.debugprefixes = cfe({ 
		name = "debugprefixes",
		label="Available prefixes",
		option=signs,
		type="select",
		size=table.maxn(signs)+1,
		 })

	local signs = get_available_signs("suffix") or {}
	debug.debugsuffixes = cfe({ 
		name = "debugsuffixes",
		label="Available suffixes",
		option=signs,
		type="select",
		size=table.maxn(signs)+1,
		 })
--]]
	debug.configdir = cfe({ 
		name = "configdir",
		label="configdir",
		value=configdir,
		 })
	debug.datafile = cfe({ 
		name = "datafile",
		label="datafile",
		value=datafile,
		 })

	for k,v in pairs(configfiles) do
		local cnfcontent
		fake, cnfcontent = get_value_from_file(v)
		for kk,vv in pairs(cnfcontent) do
			table.insert(configitems,vv)
		end
	end
	debug.configitems = cfe({ 
		name = "configitems",
		label="configitems",
		option=configitems,
		type="select",
		 })

	debug.configfiles = cfe({ 
		name = "configfiles",
		label="configfiles",
		option=configfiles,
		type="select",
		 })

	return debug
end