diff options
author | Ted Trask <ttrask01@yahoo.com> | 2008-06-18 20:07:55 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2008-06-18 20:07:55 +0000 |
commit | 151f597ce365349add8c21fff63902be0084645f (patch) | |
tree | fdfa505adeaea2d4bb0e8c604a0439b211016111 /tinydns-controller.lua | |
parent | f0dcd211ed5fca855655d8b60b588b1d193e92fd (diff) | |
download | acf-tinydns-151f597ce365349add8c21fff63902be0084645f.tar.bz2 acf-tinydns-151f597ce365349add8c21fff63902be0084645f.tar.xz |
Rewrite of tinydns to simplify code and use new cfe model.
Added js to filter records.
git-svn-id: svn://svn.alpinelinux.org/acf/tinydns/trunk@1219 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'tinydns-controller.lua')
-rw-r--r-- | tinydns-controller.lua | 454 |
1 files changed, 97 insertions, 357 deletions
diff --git a/tinydns-controller.lua b/tinydns-controller.lua index dc06911..b0f4272 100644 --- a/tinydns-controller.lua +++ b/tinydns-controller.lua @@ -1,405 +1,145 @@ module(..., package.seeall) --- Set variables -local newrecordtxt = "[New]" - -- ################################################################################ -- LOCAL FUNCTIONS -local function getstatus(self) - local status = self.model.getstatus() - if (#status.status.value > 0) then - status.status.value = "Enabled" - else - status.status.value = "Disabled" - end - - return status -end - -local function filtertable(t,limit,counter) - if not (counter) then counter = 0 end - counter = counter + 1 - - if (t) then - for k,v in pairs(t) do - if (counter > (limit + 2)) then - t[k] = nil - end - if (type(t[k]) == "table") then - filtertable(v,limit,counter) - end - end - end -end - -local function unpackoptions(t,array,descrfield) - for k,v in pairs(t) do - if not (v.option) then - if (type(v) == "table") then - unpackoptions(v,array,descrfield) - end - else - if (v.option[descrfield]) and (v.option[descrfield].value) then - table.insert(array, v.label .. " (" .. v.option[descrfield].value .. ")") - else - table.insert(array, v.label) - end - end - end -end - -local function unpackoptionstooriginal(t,array,filter) - for k,v in pairs(t) do - if not (v.option) then - if (type(v) == "table") then - unpackoptionstooriginal(v,array,filter) - end - else - if (v.orgrecordtable) and (v.orgrecordtable[2] == filter) then - table.insert(array, v.orgrecordtable) - end - end - end -end - -local function displaycmdmanagement(pidofstatus) - -- Add a management buttons - local management = {} - management.start = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Start", - type="submit", - }) - management.stop = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Stop", - type="submit", - }) - management.restart = cfe({ name="cmdmanagement", - label="Program control-panel", - value="Restart", - type="submit", - }) - -- next CFE can be used to present the result of the previous action - management.actionresult = cfe({ name="actionresult", - label="Previous action result", - }) - - -- Disable management buttons based on if the process is running or not - if (pidofstatus) then - management.start.disabled = "yes" - else - management.stop.disabled = "yes" - management.restart.disabled = "yes" - end - - return management -end - -- ################################################################################ -- PUBLIC FUNCTIONS default_action = "status" -function status(self) - local config = {} - config.settings = {} - local locations=self.model.getlocations(self) - local availablesigns = self.model.check_signs("prefix") - - config.locations = cfe ({ - name="locations", - label="Defining locations", - value=locations, - }) - - local prefix = "." - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.nsourdomain = cfe ({ - name="nsourdomain", - label=availablesigns[prefix], - value=domainoptions, - }) - end +function basicstatus(self) + return self.model.getstatus(self) +end - local prefix = "&" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.nsdomain = cfe ({ - name="nsdomain", - label=availablesigns[prefix], - value=domainoptions, - }) - end +function status(self) + return self.model.getconfigobjects(self) +end - local prefix = "=" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.host = cfe ({ - name="host", - label=availablesigns[prefix], - value=domainoptions, - }) +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 prefix = "+" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.alias = cfe ({ - name="alias", - label=availablesigns[prefix], - value=domainoptions, - }) + local status = self.model.getstatus() + status = status.value.status + if self.sessiondata.tinydnsstartstopresult then + result = self.sessiondata.tinydnsstartstopresult + self.sessiondata.tinydnsstartstopresult = nil end - local prefix = "@" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.mx = cfe ({ - name="mx", - label=availablesigns[prefix], - value=domainoptions, - }) - end + return cfe({ type="group", value={status=status, result=result} }) +end - local prefix = "^" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.ptr = cfe ({ - name="ptr", - label=availablesigns[prefix], - value=domainoptions, - }) - end +function config(self) + local config = self.model.getconfig() - local prefix = "C" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.cname = cfe ({ - name="cname", - label=availablesigns[prefix], - value=domainoptions, - }) - end + 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 - local prefix = "Z" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.soa = cfe ({ - name="soa", - label=availablesigns[prefix], - value=domainoptions, - }) + config = self.model.setconfig(config) + if not config.errtxt then + config.descr = "Configuration Set" + end end - local prefix = ":" - local domainoptions = self.model.getconfigobjects(self,prefix) - if (#domainoptions > 0) then - config.settings.generic = cfe ({ - name="generic", - label=availablesigns[prefix], - value=domainoptions, - }) - end ---[[ - -- This could be used for DEBUGGING - config.settings.all = cfe ({ - name="generic", - label="All available records", - value=self.model.getconfigobjects(self), - }) ---]] - local counter + config.type = "form" + config.option = "Save" + config.label = "Edit configuration" - return { - status=getstatus(self), - config=config, - } + return config end -function expert(self) - -- Start/Stop/Restart process - local cmdmanagement, actionresult - if ( self.clientdata.cmdmanagement) then - cmdmanagement = cfe({ - name="cmdmanagement", - label="Previous action result", - action=cfe({ - name="cmdmanagement", - value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) - }), - }) - actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) - end - - local configopts = self.model:getconfig() - -- Show buttons/fileds for creation of new confifiles - local create = {} - create.name = cfe ({ - name="name", - label="Name of the new configfile", - value=string.gsub(configopts.baseurl.value, "/$", "") .. "/", - }) - create.cmdnew = cfe ({ - name="cmdnew", - label="Create new file", - type="submit", - value="Create", - }) +function newfile(self) + local create = self.model.getnewconfigfile() + -- In case we are trying to create a new configfile - if (self.clientdata.cmdnew) then - if (self.clientdata.name) and - (#self.clientdata.name > 0) and not - (string.gsub(self.clientdata.name, "/$", "") == string.gsub(create.name.value, "/$", "")) then - local createcreated, createerrtxt = self.model:createconfigfile(self.clientdata.name) - if (createerrtxt) and (#createerrtxt > 0) then - create.name.errtxt = createerrtxt - create.name.value = self.clientdata.name + 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 - else - create.name.errtxt = "Incorrect filename" end - if not (create.name.errtxt) then - create.cmdnew.descr = "* File was created" - redirect(self, "edit?name=" .. (self.clientdata.name or "")) + + create = self.model.createconfigfile(create) + if not create.errtxt then + create.descr = "Created new config file" + redirect(self, "listfiles") end end - local status = getstatus(self) + create.type = "form" + create.option = "Create" + create.label = "Create new config file" - local configfiles = self.model:getfilelist() + return create +end + +function listfiles(self) + local configfiles = self.model.getfilelist() local config = {} - for k,v in pairs(configfiles) do - local filedetails = self.model:get_filedetails(v) + for k,v in pairs(configfiles.value) do + local filedetails = self.model.get_filedetails(v) table.insert(config,filedetails) end - -- Management buttons (Hide/show buttons - local pidofstatus - if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end - management = displaycmdmanagement(pidofstatus) - if (actionresult) then - management.actionresult.descr=cmdmanagement.descr - management.actionresult.errtxt=cmdmanagement.errtxt - end - if (status) and (status.version) and (#status.version.value == 0) then - management.start.disabled = "yes" - management.stop.disabled = "yes" - management.restart.disabled = "yes" - end - - return { - option={ script=self.conf.script, - prefix=self.conf.prefix, - controller = self.conf.controller, - action = "expert", - link = self.conf.script .. self.conf.prefix .. self.conf.controller, }, - management = management, - config = config, - status = status, - create = create, - startstop = startstop, - mhdebug = self.clientdata, - } - + return cfe({ type="list", value=config, label="Config files" }) end function edit(self) - - -- Save changes - if ( self.clientdata.cmdsave) then - local filetochange = cfe ({ name=self.clientdata.name, value=self.clientdata.filecontent, }) - modifications = self.model:updatefilecontent(filetochange) - end - - if not (self.model:valid_filename(self.clientdata.name)) then - redirect(self) - end - - local status = getstatus(self) - local file = self.model:get_filedetails(self.clientdata.name) - - - -- Add a cmd button to the view - file.cmdsave = cfe({ name="cmdsave", - label="Save/Apply above settings", - value="Save", - type="submit", - }) - file.cmddelete = cfe({ name="cmddelete", - label="Delete this configfile", - value="Delete", - type="submit", - }) - - if (self.clientdata.cmddelete) then - redirect(self, "confirmaction?action=delete&name=" .. (self.clientdata.name or "")) + 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 - if (modifications) then - file.cmdsave.descr="* Changes has been saved!" - end - - return { - option={ script=self.conf.script, - prefix=self.conf.prefix, - controller = self.conf.controller, - action = "edit", - extra = "", - link = self.conf.script .. self.conf.prefix .. self.conf.controller, }, - modifications = modifications, - file = file, - status = status, - startstop = startstop, - debugclientdata = self.clientdata, - } + config.type = "form" + config.option = "Save" + config.label = "Edit config file" + return config end -function confirmaction(self) - if (self.clientdata.cancel) then - redirect(self, "expert") - end - -- See to that only allowed files are modified - local fileexists, fileerrros = self.model:get_filedetails(self.clientdata.name) - if not (fileexists) then - redirect(self) +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 - local output = {} - output.cancel = cfe({ - name="cancel", - value="Cancel", - type="submit", - }) - output.cmddelete = cfe({ - name="cmddelete", - label="Are you sure you want to delete?", - value="Delete", - type="submit", - }) - output.name = cfe({ - name="name", - value=self.clientdata.name, - type="hidden", - }) + config.type = "form" + config.option = "Save" + config.label = "Edit config file" - output.mhdebug = cfe({ - name="mhdebug", - value=fileexists, - descr=self.clientdata, - errtxt=fileerrros, - }) - if (self.clientdata.cmddelete) then - local removesuccess, removeerrors = self.model:remove_file(self.clientdata.name) - if (removesuccess) then - redirect(self, "expert") - elseif (#removeerrors > 0) then - output.cmddelete.errtxt = removeerrors - end - end + return config +end - return output +function delete(self) + local cmdresult = self.model.remove_file(self.clientdata.filename) + --self.sessiondata.cmdresult = cmdresult + redirect_to_referrer(self) + return cmdresult end |