From fffd98587d8467dfe8254689e43383031ca42b66 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Fri, 29 Feb 2008 15:52:45 +0000 Subject: Changed so that everything uses acf instead. Now every keypress reports back status for previous action. Information is presented next to the button recently pressed. git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@782 ab2d0c66-481e-0410-8bed-d214d4d58bed --- openntpd-model.lua | 320 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 272 insertions(+), 48 deletions(-) (limited to 'openntpd-model.lua') diff --git a/openntpd-model.lua b/openntpd-model.lua index 7a93151..e5d8163 100644 --- a/openntpd-model.lua +++ b/openntpd-model.lua @@ -40,13 +40,15 @@ local function config_content( f ) return config end -local function get_version () - local f,error = io.popen("/sbin/apk_version -v -s openntpd") - local programversion = f:read("*a") - f:close() - return programversion +local function get_version() + local cmd = "/sbin/apk_version -v -s openntpd | cut -d ' ' -f 1" + local cmd_output = io.popen( cmd ) + local cmd_output_result = cmd_output:read("*a") or "" + cmd_output:close() + return cmd_output_result end + local function last_time_change() local cmdoutput = {} local cmd, error = io.popen("cat /var/log/messages | grep ntpd | grep adjusting | tail -1" ,r) @@ -82,43 +84,106 @@ local function addremove_opts( addremove, file, variable, option ) end return cmdtxt end -local function addremove_config( 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) - local cmdoutput = nil +local function addremove_config( addremove, variable, value ) + local file = configfile + local cmdoutput + local filecontentarray = fs.read_file_as_array(file) if (addremove == "delete" ) then - cmdtxt = "/bin/sed -i '/" .. variable .. "/d' " .. file - local cmd, error = io.popen ( cmdtxt ) - cmdoutput = cmd:read("*a") - cmd:close() + if not (value) then + return false, cfe({ + name="openntpd.model.addremove_config(delete)", + errtxt="You need to choose a 'Timeserver host' to delete!", + }) + end + + local modifyresult + for k,v in pairs(filecontentarray) do + if (string.find(v,"^%s*" .. variable .. "%s*" .. value .. "%s*$" )) then + modifyresult = k + break + end + end + + if (modifyresult) then + table.remove(filecontentarray,modifyresult) + fs.write_file(file, table.concat(filecontentarray, "\n")) + + return true, cfe({ + name="openntpd.model.addremove_config(delete)", + descr="* Record was successfully deleted!", + }) + end + + -- If nothing happened... then report error! + return false, cfe({ + name="openntpd.model.addremove_config(delete)", + errtxt="Couldn't find the record to delete!", + }) + elseif (addremove == "add" ) then - cmdtxt = "/bin/echo '" .. variable .. "' >> " .. file - local cmd, error = io.popen ( cmdtxt ) - cmdoutput = cmd:read("*a") - cmd:close() - end - if (cmdoutput) then - cmdresult = "Config is modified! (" .. cmdtxt .. ")" + if not (value) then + return false, cfe({ + name="openntpd.model.addremove_config(add)", + errtxt="You need to enter a value to add!", + }) + end + if not (variable) then + return false, cfe({ + name="openntpd.model.addremove_config(add)", + errtxt="You need to enter a variable for the value to add!", + }) + end + + local alreadyexists + for k,v in pairs(filecontentarray) do +-- if (string.find(v,"^.-%s+" .. value .. "%s*$" )) then + if (string.find(v,"^%s*" .. variable .. "%s*" .. value .. "%s*$" )) then + alreadyexists = k + break + end + end + + if (alreadyexists) then + return false, cfe({ + name="openntpd.model.addremove_config(add)", + errtxt="The entered record already exists in the config!", + }) + end + + table.insert(filecontentarray, variable .. " " .. value ) + fs.write_file(file, table.concat(filecontentarray, "\n")) + + return true, cfe({ + name="openntpd.model.addremove_config(add)", + descr="* Record was successfully added!", + }) + else + return false, cfe({ + name="openntpd.model.addremove_config()", + errtxt="Wrong usage of this function!", + }) end - return cmdresult + return false, cfe({ + name="openntpd.model.addremove_config()", + errtxt="Something went wrong! You entered '" .. tostring(addremove) .. "' as function.", + }) end -- ################################################################################ -- PUBLIC FUNCTIONS -function startstop_service ( self, state ) - local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol("ntpd", state) - cmdresult = {cmdresult=cmdmessage,} - return cmdresult +-- action should be a CFE +function startstop_service ( self, action ) + local cmd = action.value + local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol("ntpd", cmd) + action.descr=cmdmessage + action.errtxt=cmderror + -- Reporting back (true|false, the original acition) + return cmdresult,action end -function modify_config (self, addremove, file, variable) - if (file == nil) then file = configfile end - -- See to that only *my* configs are modyfied. - if (file == configfile) or (file == confdfile) then - return addremove_config(addremove, file, variable) - end +function modify_config(self, addremove, variable, value) + return addremove_config(addremove, variable, value) end function modify_opts (self, addremove, file, variable, opts) @@ -135,42 +200,201 @@ end function getstatus () local path = configfile local status = {} + local config = getconfig() if not (fs.is_file(path)) then local file_result,err = fs.write_file(path, "") end - status["version"] = string.match(get_version(), "^(%S*)" ) - status["date"] = os.date() - status["enabled"] = procps.pidof("ntpd") - status["setstimeonstartup"] = getopts.getoptsfromfile(confdfile,"NTPD_OPTS", "-s") - status["listenstate"] = server - status["timechanged"] = last_time_change() + + status.version = cfe({ + name = "version", + label="Program version", + value=get_version(), + }) + + status.status = cfe({ + name="status", + label="Program status", + value=procps.pidof("ntpd"), + }) + + status.date = config.date + + status.setstimeonstartup = config.setstimeonstartup + + status.timechanged = config.timechanged + return status end function getconfig () local path = configfile local config = {} - config = getopts.getoptsfromfile(confdfile) - config["variables"] = config_content(path) + local configopts = getopts.getoptsfromfile(confdfile) + + configopts["variables"] = config_content(path) + + config.setstimeonstartup = cfe({ + name = "setstimeonstartup", + label="Sets time directly at startup", + value = "No", + type="checkbox", + }) + if (getopts.getoptsfromfile(confdfile,"NTPD_OPTS", "-s")) then + config.setstimeonstartup.value = "Yes" + config.setstimeonstartup.checked = "Yes" + end + + config.cmdsavesetstimeonstartup = cfe({ + name = "cmdsavesetstimeonstartup", + label="Save the above settings", + value = "Save", + type="submit", + descr="See above checkbox", + }) + + config.timechanged = cfe({ + name = "timechanged", + label="Previous time adjustment", + value = last_time_change(), + }) + + config.date = cfe({ + name = "date", + label="Current time", + value=os.date(), + }) + + local options = {} + for k,v in pairs(configopts.variables) do + if (type(v) == "table") and ( (string.lower(k) == "servers") or (string.lower(k) == "server")) then + local srvtype = "" + if (string.lower(k) == "servers") then srvtype = " (pool)" end + for k1, v1 in pairs(v) do + if (v1.value) then + table.insert(options,v1.value..srvtype) + end + end + end + end + config.hosts_list = cfe({ + name = "hosts_list", + label="Timeserver hosts", + type="select", + option=options, + size=#options + 1, + descr="In most cases you could use pool.ntp.org or [countryname].pool.ntp.org (if listed in http://www.pool.ntp.org/)." + }) + if (#options < 2) then + config.hosts_list.size = 2 + end + + config.hosts_add = cfe({ + name = "hosts_add", + label="Host to add", + }) + + local options = { + cfe({value="1", label="Val1",}), + cfe({value="2", label="Val1",}), + } + config.hosts_type = cfe({ + name="hosts_type", + label="Type of server", + value="2", + type="radio", + option={ + cfe({value="servers", label="Server pool",}), + cfe({value="server", label="Single server",}), + }, + }) + + local options = {} + for k,v in pairs(configopts.variables) do + if (type(v) == "table") and (string.lower(k) == "listen") then + for k1, v1 in pairs(v) do + if (v1.value) then + table.insert(options,v1.value) + end + end + end + end + config.listen_list = cfe({ + name = "listen_list", + label="Listen on address", + type="select", + option=options, + size=#options + 1, + descr="Empty list = Listening (acting as server) is disabled; '*' = Listen on all local addresses.", + }) + if (#options < 2) then + config.listen_list.size = 2 + end + + config.listen_add = cfe({ + name = "listen_add", + label="New listen address", + }) + + + -- DEBUG INFORMATION + config.debug = cfe({ + name = "debug", + label="Debug information", + value=configopts, + }) + return config end function get_logfile () - local me = {} + local file = {} local cmdtxt = "cat /var/log/messages | grep ntpd" local cmd, error = io.popen(cmdtxt ,r) local cmdoutput = cmd:read("*a") cmd:close() - me.value = cmdoutput - me.cmd = cmdtxt - return me + + file["filename"] = cfe({ + name="filename", + label="File name", + value=cmdtxt, + }) + + file["filecontent"] = cfe({ + type="longtext", + name="filecontent", + label="File content", + value=cmdoutput, + }) + + return file end function get_filedetails() - local filedetails = {} local path = configfile - filedetails.details = fs.stat(path) - filedetails.content = fs.read_file(path) - return filedetails + local filedetails = fs.stat(path) + local file = {} + + file["filename"] = cfe({ + name="filename", + label="File name", + value=path, + }) + file["filesize"] = cfe({ + name="filesize", + label="File size", + value=filedetails.size or 0, + }) + file["mtime"] = cfe({ + name="mtime", + label="File date", + value=filedetails.mtime or "---", + }) + file["filecontent"] = cfe({ + type="longtext", + name="filecontent", + label="File content", + value=fs.read_file(path), + }) + return file end function update_filecontent (self, modifications) -- cgit v1.2.3