summaryrefslogtreecommitdiffstats
path: root/tinydns-controller.lua
blob: c97bca285db3df5d959e826f24883d72752939df (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
module(..., package.seeall)
require("validator")

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

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

default_action = "status"

function status(self)
	return self.model.getstatus(self)
end

function view(self)
	return self.model.getconfigobjects(self.clientdata.filename)
end

function startstop(self)
	local result
	if self.clientdata.action then
		result = self.model:startstop_service(self.clientdata.action)
		self.sessiondata.tinydnsstartstopresult = result
		self.redirect_to_referrer(self)
	end

	local status = self.model.getstatus()
	status = status.value.status
	if self.sessiondata.tinydnsstartstopresult then
		result = self.sessiondata.tinydnsstartstopresult
		self.sessiondata.tinydnsstartstopresult = nil
	end

	return cfe({ type="group", value={status=status, result=result} })
end

function config(self)
	local config = self.model.getconfig()

	if self.clientdata.Save then
		for name,value in pairs(self.clientdata) do
			if config.value[name] then
				config.value[name].errtxt = nil
				config.value[name].value = value
			end
		end

		config = self.model.setconfig(config)
		if not config.errtxt then
			config.descr = "Configuration Set"
		end
	end

	config.type = "form"
	config.option = "Save"
	config.label = "Edit configuration"

	return config
end

function newfile(self)
	local create = self.model.getnewconfigfile()
	
	-- In case we are trying to create a new configfile
	if (self.clientdata.Create) then
		for name,value in pairs(self.clientdata) do
			if create.value[name] then
				create.value[name].errtxt = nil
				create.value[name].value = value
			end
		end

		create = self.model.createconfigfile(create)
		if not create.errtxt then
			create.descr = "Created new config file"
			redirect(self, "listfiles")
		end
	end

	create.type = "form"
	create.option = "Create"
	create.label = "Create new config file"

	return create
end

function listfiles(self)
	local configfiles = self.model.getfilelist()
	local config = {}
	for k,v in pairs(configfiles.value) do
		local filedetails = self.model.get_filedetails(v)
		table.insert(config,filedetails)
	end

	return cfe({ type="list", value=config, label="Config files" })
end

function edit(self)
	local config = self.model.get_filedetails(self.clientdata.filename)
	if self.clientdata.Save then
		local result = self.model.updatefilecontent(self.clientdata.filename, self.clientdata.filecontent)
		if not result.value then
			config.value.filecontent.value = self.clientdata.filecontent
			config.value.filecontent.errtxt = result.errtxt
			config.errtxt = "Failed to save config!"
		else
			config = self.model.get_filedetails(self.clientdata.filename)
			config.descr = "Saved file"
		end
	elseif validator.is_integer(self.clientdata.linenumber) then
		config.value.filecontent.linenumber = self.clientdata.linenumber
	end

	config.type = "form"
	config.option = "Save"
	config.label = "Edit config file"

	return config
end

function expert(self)
	local config = self.model.get_filedetails(self.clientdata.filename)
	if self.clientdata.Save then
		local result = self.model.updatefilecontent(self.clientdata.filename, self.clientdata.filecontent)
		if not result.value then
			config.value.filecontent.value = self.clientdata.filecontent
			config.value.filecontent.errtxt = result.errtxt
			config.errtxt = "Failed to save config!"
		else
			config = self.model.get_filedetails(self.clientdata.filename)
			config.descr = "Saved file"
		end
	end

	config.type = "form"
	config.option = "Save"
	config.label = "Edit config file"

	return config
end

function delete(self)
	local cmdresult = self.model.remove_file(self.clientdata.filename)
	--self.sessiondata.cmdresult = cmdresult
	redirect_to_referrer(self)
	return cmdresult
end