diff options
| author | Mika Havela <mika.havela@gmail.com> | 2007-12-07 10:36:41 +0000 | 
|---|---|---|
| committer | Mika Havela <mika.havela@gmail.com> | 2007-12-07 10:36:41 +0000 | 
| commit | 6f8bd836d7be61dd04b3521a2ffefe7e927de18d (patch) | |
| tree | 1e37c12a367e8541978ee8c992dc282fe260d3d8 | |
| parent | 95f699fa380368841157fdd7043ff7730bcae50e (diff) | |
| download | acf-openntpd-6f8bd836d7be61dd04b3521a2ffefe7e927de18d.tar.bz2 acf-openntpd-6f8bd836d7be61dd04b3521a2ffefe7e927de18d.tar.xz | |
Cleaning up code. Adding functionallity to add/remove listen address. Error message displayed when something is missing for the desired action. The controller keeps track of things needed before asking the model.
git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@418 ab2d0c66-481e-0410-8bed-d214d4d58bed
| -rw-r--r-- | openntpd-controller.lua | 92 | ||||
| -rw-r--r-- | openntpd-model.lua | 17 | ||||
| -rw-r--r-- | openntpd-settings-html.lsp | 18 | 
3 files changed, 86 insertions, 41 deletions
| diff --git a/openntpd-controller.lua b/openntpd-controller.lua index 4a74fca..6a22744 100644 --- a/openntpd-controller.lua +++ b/openntpd-controller.lua @@ -21,39 +21,72 @@ end  settings = function (self)  	local cmd = self.clientdata.cmd -	errors = {} +	local errors = {} +	local modify_opts = nil  	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller -	if (self.clientdata.settings_cmd ~= nil) then +	local hosts_cmd = tostring(self.clientdata.hosts_cmd) +	local hosts_cmd = string.lower(hosts_cmd) +	local listen_cmd = tostring(self.clientdata.listen_cmd) +	local listen_cmd = string.lower(listen_cmd) +	local settings_cmd = tostring(self.clientdata.settings_cmd) +	local settings_cmd = string.lower(settings_cmd) + +	-- SECTION WHERE YOU SAVE NEW SETTINGS +	if ( hosts_cmd == "delete") then +		if not (self.clientdata.hosts_list) then +			errors["hosts_list"]="You need to choose something in the list to delete" +		end +		if (self.clientdata.hosts_list) then +			modify_opts = self.model:modify_config(hosts_cmd, nil, self.clientdata.hosts_list) +		end +	elseif (hosts_cmd == "add") then +		if (self.clientdata.hosts_add == "") then +			errors["hosts_add"]="You need to enter a server/IP" +		end +		if (self.clientdata.hosts_type == nil) then +			errors["hosts_type"]="You need to choose type of server" +		end +		if (self.clientdata.hosts_add ~= "") and (self.clientdata.hosts_type ~= nil) then +			modify_opts = self.model:modify_config(hosts_cmd, nil, self.clientdata.hosts_type .. " " .. self.clientdata.hosts_add) +		end +	elseif (listen_cmd == "add") then +		if (self.clientdata.listen_add == "") then +			errors["listen_add"]="You need to enter what you want to listen at" +		end +		if (self.clientdata.listen_add ~= "") then +			modify_opts = self.model:modify_config(listen_cmd, nil, "listen on " .. self.clientdata.listen_add) +		end +	elseif (listen_cmd == "delete") then +		if not (self.clientdata.listen_list) then +			errors["listen_list"]="You need to choose something in the list to delete" +		end +		if (self.clientdata.listen_list) then +			modify_opts = self.model:modify_config(listen_cmd, nil, self.clientdata.listen_list) +		end +	elseif (settings_cmd == "save") then  		if (self.clientdata.settings_startup) then  			modify_opts = self.model:modify_opts("add", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s")  		else  			modify_opts = self.model:modify_opts("remove", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s")  		end  	end -	if (self.clientdata.hosts_cmd) then -		if (string.lower(self.clientdata.hosts_cmd) == "delete") then -			modify_opts = self.model:modify_config(self.clientdata.hosts_cmd, nil, self.clientdata.hosts_list) -		elseif (string.lower(self.clientdata.hosts_cmd) == "add") then -			if (self.clientdata.hosts_add == "") then -				errors = {hosts_add = "You need to enter a server/IP" } -			end -			if (self.clientdata.hosts_type == nil) then -				errors = {hosts_type = "You need to choose type of server" } -			end -			if (self.clientdata.hosts_add ~= "") and (self.clientdata.hosts_type ~= nil) then -				modify_opts = self.model:modify_config(self.clientdata.hosts_cmd, nil,  -					self.clientdata.hosts_type .. " " .. self.clientdata.hosts_add) -			end -		end -	end ---	DEBUG INFO ---	modify_opts = self.clientdata.hosts_cmd + +	DEBUGMODEL = modify_opts		-- <<< DEBUG INFO >>> +	DEBUGCLIENTDATA = self.clientdata	-- <<< DEBUG INFO >>>  	if ( cmd ~= nil ) then  		local startstop = self.model:startstop_service( cmd )  		posix.sleep(1)	-- Wait for the process to start|stop -		return ( {statusinfo = self.model:get(), startstop = "", modify_opts=modify_opts, url = url } ) +		return ( {statusinfo = self.model:get(),  +			startstop = "",  +			modify_opts=modify_opts,  +			url = url } )  	else -		return ( {statusinfo = self.model:get(), startstop = "", errors = errors, modify_opts=modify_opts, url = url, DEBUG=test } ) +		return ( {statusinfo = self.model:get(), startstop = "",  +			errors = errors,  +			modify_opts=modify_opts,  +			url = url,  +			DEBUGMODEL=DEBUGMODEL, +			DEBUGCLIENTDATA=DEBUGCLIENTDATA } )  	end  end @@ -74,14 +107,23 @@ advanced = function (self)  	if ( filecontent ~= "") then  		local me = ( {filecontent = self.model:update_filecontent(filecontent), url = url } ) -		return ( {startstop = "", statusinfo = self.model:get(), filecontent = self.model:get_filecontent(), url = url } ) +		return ( {startstop = "",  +			statusinfo = self.model:get(),  +			filecontent = self.model:get_filecontent(),  +			url = url } )  	else  		if ( cmd ~= nil ) then  			local startstop = self.model:startstop_service( cmd )  			posix.sleep(1)	-- Wait for the process to start|stop -			return ( {startstop = startstop, statusinfo = self.model:get(), filecontent = self.model:get_filecontent(), url = url } ) +			return ( {startstop = startstop,  +				statusinfo = self.model:get(),  +				filecontent = self.model:get_filecontent(),  +				url = url } )  		else -			return ( {startstop = "", statusinfo = self.model:get(), filecontent = self.model:get_filecontent(), url = url } ) +			return ( {startstop = "",  +				statusinfo = self.model:get(),  +				filecontent = self.model:get_filecontent(),  +				url = url } )  		end  	end  end diff --git a/openntpd-model.lua b/openntpd-model.lua index 454db5d..f74f941 100644 --- a/openntpd-model.lua +++ b/openntpd-model.lua @@ -141,18 +141,22 @@ local addremove_config = function ( addremove, file, variable )  	-- Notes on known/unknown bugs: Remove 'server www.test.org' wont work if config has multiple space/tab e.g. 'server    www.test.org'  	-- FIXME: Make num-space unsensetive.  	-- FIXME: Can hold multiple rows with same values (when deleting... every equal row is deleted) -	if (string.lower(addremove) == "delete" ) then +	local cmdoutput = nil +	if (addremove == "delete" ) then  		cmdtxt = "/bin/sed -i '/" .. variable .. "/d' " .. file  		local cmd, error = io.popen ( cmdtxt ) -		local cmdoutput = cmd:read("*a") +		cmdoutput = cmd:read("*a")  		cmd:close() -	elseif (string.lower(addremove) == "add" ) then +	elseif (addremove == "add" ) then  		cmdtxt = "/bin/echo '" .. variable .. "' >> " .. file  		local cmd, error = io.popen ( cmdtxt ) -		local cmdoutput = cmd:read("*a") +		cmdoutput = cmd:read("*a")  		cmd:close()  	end -	return cmdtxt +	if (cmdoutput) then +		cmdresult = "Config is modified! (" .. cmdtxt .. ")" +	end +	return cmdresult  end  -- ################################################################################  -- PUBLIC FUNCTIONS @@ -171,13 +175,12 @@ function startstop_service ( self, state )  end -function modify_config (self, addremove, file, variable, opts) +function modify_config (self, addremove, file, variable)  	if (file == nil) then file = ntpdconfig end  	-- See to that only *my* configs are modyfied.  	if (file == ntpdconfig) or (file == ntpdconfd) then  		return addremove_config(addremove, file, variable)  	end -  end  function modify_opts (self, addremove, file, variable, opts) diff --git a/openntpd-settings-html.lsp b/openntpd-settings-html.lsp index 16a0eaa..9bc7c78 100644 --- a/openntpd-settings-html.lsp +++ b/openntpd-settings-html.lsp @@ -37,7 +37,7 @@ for i = 1, table.maxn(view.statusinfo.server) do ?>  <? end end ?>  </select><BR>  In most cases you could use <i><b>pool.ntp.org</b></i> or<br><i><b>[countryname].pool.ntp.org</i></b> (if listed in <i><b>http://www.pool.ntp.org/</b></i>). -</dd> +<? if (view.errors.hosts_list) then io.write("<p class=error>"..view.errors.hosts_list.."</p>") end ?></dd>  <dt>Delete selected host</dt>  <dd><input name="hosts_cmd" type="submit" value="Delete"> (see above)</dd> @@ -51,25 +51,25 @@ In most cases you could use <i><b>pool.ntp.org</b></i> or<br><i><b>[countryname]  </form>  <h3>'PRESENT TIME' OPTIONS (ACT AS TIME SERVER)</h3> + +<form name="cmd" action="" method="POST">  <dt>Listen on address...</dt>  <dd> -<select name="" size="3" style="width:200px;"> +<select name="listen_list" size="3" style="width:200px;">  <? if (view.statusinfo.listen) then  for i = 1, table.maxn(view.statusinfo.listen) do ?> -	<option name="<? io.write(view.statusinfo.listen[i].value) ?>"><? io.write(view.statusinfo.listen[i].value) ?></option> +	<option value="listen on <? io.write(view.statusinfo.listen[i].value) ?>"><? io.write(view.statusinfo.listen[i].value) ?></option>  <? end end ?>  </select><BR>Empty list = Listening (acting as server) is disabled<br>"*" = Listen on all local addresses +<? if (view.errors.listen_list) then io.write("<p class=error>"..view.errors.listen_list.."</p>") end ?>  </dd> -<? ---[[ FIXME!!! ?>  <dt>Delete selected address</dt> -<dd><input type="submit" value="Delete"> (see above)</dd> - +<dd><input name="listen_cmd" type="submit" value="Delete"> (see above)</dd>  <dt>Add new listen address</dt> -<dd><input type="text" name="" value=""> <input type="submit" value="Add"></dd> -<? --]] ?> - +<dd><input name="listen_add" type="text" value=""> <input name="listen_cmd" type="submit" value="Add"><? if (view.errors.listen_add) then io.write("<p class=error>"..view.errors.listen_add.."</p>") end ?></dd> +</form>  <? | 
