module(..., package.seeall) -- Set variables local newrecordtxt = "[New]" -- ################################################################################ -- LOCAL FUNCTIONS local list_redir = function (self) self.conf.action = "status" self.conf.type = "redir" error (self.conf) end 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 -- ################################################################################ -- PUBLIC FUNCTIONS mvc = {} mvc.on_load = function(self, parent) if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then self.worker[self.conf.action] = list_redir(self) end end 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 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 local prefix = "=" local domainoptions = self.model.getconfigobjects(self,prefix) if (#domainoptions > 0) then config.settings.host = cfe ({ name="host", label=availablesigns[prefix], value=domainoptions, }) 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, }) 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 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 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 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, }) 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 return { status=getstatus(self), config=config, } end function expert(self) local cmdmanagement, cmdmanagementresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), }) cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) end local status = getstatus(self) local config = self.model:getfilelist() return { option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "expert", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdmanagement = cmdmanagement, management = management, config = config, status = status, startstop = startstop, debugclientdata = self.clientdata, } end function edit(self) local cmdmanagement, cmdmanagementresult, modifications if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), }) cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) end -- Save changes if ( self.clientdata.cmdsave) then local filetochange = cfe ({ name=self.clientdata.name, value=self.clientdata.filecontent, }) modifications = self.model:updatefilecontent(filetochange) -- if not (modifications) then -- self.conf.action = "status" -- self.conf.type = "redir" -- error (self.conf) -- end end if not (self.model:valid_filename(self.clientdata.name)) then self.conf.action = "status" self.conf.type = "redir" error (self.conf) 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", }) if (modifications) then file.cmdsave.descr="* Changes has been saved!" end return { option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "edit", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdmanagement = cmdmanagement, modifications = modifications, management = management, file = file, status = status, startstop = startstop, debugclientdata = self.clientdata, } end --[=[ function edit_records(self,types,record,errormessage) local recorddetails = {} local edit = {} local config=self.model:getconfig() -- Fetch the original record into a table local availablesigns = self.model.check_signs() local prefix = availablesigns["reverse"][types] local domainopts = {} local domainoptions = self.model.getconfigobjects(self,prefix) local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptionstooriginal(domainoptions, domainopts,string.match(record, "^%S*")) domainopts=unpack(domainopts) -- Display the inputfields (with the original record) local numfields = math.max(#domainopts,#availablesigns["fieldlabels"][prefix]) for fieldnum=2,tonumber(numfields) do edit[fieldnum] = cfe({ name=fieldnum, value=domainopts[fieldnum], label=availablesigns["fieldlabels"][prefix][fieldnum] or "", }) end table.insert(edit, cfe({ name=#edit, label="DEBUG: Original record", value=domainopts.orgrecord, errtxt="This field will go away soon! It's only for debugging!" })) -- Adding select-boxes local locationselectbox local locationfileds = { ['.']=7, ['&']=7, ['=']=6, ['+']=6, ['@']=8, ['^']=6, ['C']=6, ['Z']=12, [':']=7, } locationselectbox = locationfileds[prefix] if (locationselectbox) then local locations=self.model.getlocations(self) edit[locationselectbox]["option"] = {} edit[locationselectbox]["type"] = "select" table.insert(edit[locationselectbox]["option"], "") for v in pairs(locations or {}) do table.insert(edit[locationselectbox]["option"], v) end -- IF the value is not one of the existing options, then warn and add this option. for k,v in pairs(edit[locationselectbox]["option"]) do edit[locationselectbox]["errtxt"] = "'" .. edit[locationselectbox]["value"] .. "' is defined as a location!" if (tostring(v) == tostring(edit[locationselectbox]["value"])) then edit[locationselectbox]["errtxt"] = "" break end end -- Now add this option to the list (just to show what it was) if (#edit[locationselectbox]["errtxt"] > 0) then table.insert(edit[locationselectbox]["option"], edit[locationselectbox]["value"]) end end return { edit=edit, option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "config", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, cmdsave=cmdsave, cmddelete=cmddelete, clientdata=clientdata, debugtypes=types, debugrecord=string.match(record, "^%S*"), } end function config(self,messages) local config = self.model.getconfig(self) local debug = self.model.getdebug(self) local availablesigns = self.model.check_signs("prefix") -- LISTEN _________________________________________________________ -- Add button config.listen_cmd = cfe ({ name="listen_cmd", label="Save above changes", value="Save", type="submit", disabled="yes", }) -- LOCATIONS _________________________________________________________ local locations=self.model.getlocations(self) config.locations = cfe ({ name="locations", label="Locations", option={}, type="select", }) -- Add options for n in pairs(locations) do table.insert(config.locations.option, n) end -- Add a [New] record to the options -- table.insert(config.locations.option, newrecordtxt) -- Set size of the inputbox config.locations.size = #config.locations.option if (#config.locations.option == 1) then config.locations.size = #config.locations.option + 1 end -- Add button config.locations_cmd = cfe ({ name="locations_cmd", label="Edit above location", value="Edit", descr="Mark a item in '" .. config.locations.label .. "'-list before pressing [Edit]", type="submit", disabled="yes", }) -- Nameservers _________________________________________________________ local prefix = "." local domainopts = {} local domainoptions = self.model.getconfigobjects(self,prefix) local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.nsourdomain = cfe ({ name="nsourdomain", type="select", label=availablesigns[prefix], option=domainopts, -- descr="Above records look like '.fqdn:ip:x:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.nsourdomain.option, newrecordtxt) -- Set size of the inputbox config.nsourdomain.size = #config.nsourdomain.option if (#config.nsourdomain.option == 1) then config.nsourdomain.size = #config.nsourdomain.option + 1 end -- Add button config.nsourdomain_cmd = cfe ({ name="nsourdomain_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.nsourdomain.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- Nameservers _________________________________________________________ local prefix = "&" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.nsdomain = cfe ({ name="nsdomain", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like '&fqdn:ip:x:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.nsdomain.option, newrecordtxt) -- Set size of the inputbox config.nsdomain.size = #config.nsdomain.option if (#config.nsdomain.option == 1) then config.nsdomain.size = #config.nsdomain.option + 1 end -- Add button config.nsdomain_cmd = cfe ({ name="nsdomain_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.nsdomain.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- Host _________________________________________________________ local prefix = "=" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.host = cfe ({ name="host", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like '=fqdn:ip:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.host.option, newrecordtxt) -- Set size of the inputbox config.host.size = #config.host.option if (#config.host.option == 1) then config.host.size = #config.host.option + 1 end -- Add button config.host_cmd = cfe ({ name="host_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.host.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- Alias _________________________________________________________ local prefix = "+" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.alias = cfe ({ name="alias", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like '+fqdn:ip:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.alias.option, newrecordtxt) -- Set size of the inputbox config.alias.size = #config.alias.option if (#config.alias.option == 1) then config.alias.size = #config.alias.option + 1 end -- Add button config.alias_cmd = cfe ({ name="alias_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.alias.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- MX _________________________________________________________ local prefix = "@" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.mx = cfe ({ name="mx", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like '@fqdn:ip:x:dist:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.mx.option, newrecordtxt) -- Set size of the inputbox config.mx.size = #config.mx.option if (#config.mx.option == 1) then config.mx.size = #config.mx.option + 1 end -- Add button config.mx_cmd = cfe ({ name="mx_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.mx.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- PTR _________________________________________________________ local prefix = "^" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.ptr = cfe ({ name="ptr", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like '^fqdn:p:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.ptr.option, newrecordtxt) -- Set size of the inputbox config.ptr.size = #config.ptr.option if (#config.ptr.option == 1) then config.ptr.size = #config.ptr.option + 1 end -- Add button config.ptr_cmd = cfe ({ name="ptr_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.ptr.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- Cname _________________________________________________________ local prefix = "C" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.cname = cfe ({ name="cname", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like 'Cfqdn:p:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.cname.option, newrecordtxt) -- Set size of the inputbox config.cname.size = #config.cname.option if (#config.cname.option == 1) then config.cname.size = #config.cname.option + 1 end -- Add button config.cname_cmd = cfe ({ name="cname_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.cname.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- SOA _________________________________________________________ local prefix = "Z" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.soa = cfe ({ name="soa", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like 'Zfqdn:mname:rname:ser:ref:ret:exp:min:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.soa.option, newrecordtxt) -- Set size of the inputbox config.soa.size = #config.soa.option if (#config.soa.option == 1) then config.soa.size = #config.soa.option + 1 end -- Add button config.soa_cmd = cfe ({ name="soa_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.soa.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- GENERIC _________________________________________________________ local prefix = ":" local domainoptions = self.model.getconfigobjects(self,prefix) local domainopts = {} -- Filter out options local descrfield = 2 -- This is the filed that is shown as description (in the select-box) unpackoptions(domainoptions, domainopts,descrfield) config.generic = cfe ({ name="generic", label=availablesigns[prefix], type="select", option=domainopts, -- descr="Above records look like ':fqdn:n:rdata:ttl:timestamp:lo' when they are in the config-file.", }) -- Add a [New] record to the options -- table.insert(config.generic.option, newrecordtxt) -- Set size of the inputbox config.generic.size = #config.generic.option if (#config.generic.option == 1) then config.generic.size = #config.generic.option + 1 end -- Add button config.generic_cmd = cfe ({ name="generic_cmd", label="Edit above item", value="Edit", descr="Mark a item in '" .. config.generic.label .. "'-list before pressing [Edit]", type="submit", -- disabled="yes", }) -- DEBUGGING local level1 = {} local level2 = {} local level3 = {} local domainoptions = self.model.getconfigobjects(self) for k1,v1 in pairs(domainoptions) do table.insert(level1, v1.label) for k2,v2 in pairs(v1) do table.insert(level2, v2.label) if (type(v2) == "table") then for k3,v3 in pairs(v2) do table.insert(level3, v3.label) end end end end --[[ config.debuglevels = cfe ({ name="debuglevels", label="Level1", type="select", option=domainoptions, }) --]] -- Redirect if button is pressed *********************************** local tags = {"locations", "nsourdomain", "nsdomain", "host", "alias", "mx", "ptr", "cname", "soa", "generic", } for k,tag in pairs(tags) do if (self.clientdata[tag .. "_cmd"]) and (self.clientdata[tag]) then self.conf.action = "edit_records" self.conf.type = "redir" return edit_records(self,tag, self.clientdata[tag]) elseif (self.clientdata[tag .. "_cmd"]) and not (self.clientdata[tag]) then config[tag .. "_cmd"]["errtxt"] = "You need to specify a record to edit!" end end return ({ status=getstatus(self), config=config, option={ script=ENV["SCRIPT_NAME"], prefix=self.conf.prefix, controller = self.conf.controller, action = "config", link = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller, }, }) end --]=]