From 63e8c6b82c980876ddc522174970e5b1b9014b06 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Tue, 8 Apr 2008 12:35:04 +0000 Subject: Cleaning up the code. git-svn-id: svn://svn.alpinelinux.org/acf/tinydns/trunk@954 ab2d0c66-481e-0410-8bed-d214d4d58bed --- tinydns-model.lua | 323 ++++++++++++++++++------------------------------------ 1 file changed, 105 insertions(+), 218 deletions(-) (limited to 'tinydns-model.lua') diff --git a/tinydns-model.lua b/tinydns-model.lua index 24e0437..ad83798 100644 --- a/tinydns-model.lua +++ b/tinydns-model.lua @@ -1,15 +1,18 @@ module(..., package.seeall) +-- Load libraries require("procps") require("getopts") require("fs") require("format") +require("processinfo") +-- Set variables local configdir local datafile local configfiles = {} local configitems = {} - +local packagename = "tinydns" local processname = "tinydns" local configfile = "/etc/conf.d/" .. processname local baseurl = "/etc/tinydns" @@ -21,26 +24,50 @@ else configdir = "/etc/" .. processname datafile = "/var/cache/data" end - +descr = { + prefix={ + ['.']="Name server for your domain (NS + A + SOA)", + ['&']="Deletegate subdomain (NS + A)", + ['=']="Host (A + PTR)", + ['+']="Alias (A, no PTR)", + ['@']="Mail exchanger (MX)", + ["'"]="Text record (TXT)", + ['^']="Reverse record (PTR)", + ['C']="Canonical Name (CNAME)", + ['Z']="SOA record (SOA)", + [':']="Generic record", + ['%']="Client location", + }, + reverse={ + ['nsourdomain']=".", + ['nsdomain']="&", + ['host']="=", + ['alias']="+", + ['mx']="@", + ['ptr']="^", + ['cname']="C", + ['soa']="Z", + [':']=":", + ['locations']="%", + }, + fieldlabels={ + ['.']={"Prefix", "Domain", "IP address", "Name server", "Time to live", "Timestamp", "Location", }, + ['&']={"Prefix", "Domain", "IP address", "Name server", "Time to live", "Timestamp", "Location", }, + ['=']={"Prefix", "Host", "IP address", "Time to live", "Timestamp", "Location", }, + ['+']={"Prefix", "Alias", "IP address", "Time to live", "Timestamp", "Location", }, + ['@']={"Prefix", "Domain", "IP address", "Mail exchanger", "Distance", "Time to live", "Timestamp", "Location", }, + ['^']={"Prefix", "PTR", "Domain name", "Time to live", "Timestamp", "Location", }, + ['C']={"Prefix", "Domain name", "Canonical name", "Time to live", "Timestamp", "Location", }, + ['Z']={"Prefix", "Unknown", "Primary name server", "Contact address", "Serial number", "Refresh time", "Retry time", "Expire time", "Minimum time", "Time to live", "Timestamp", "Location",}, + [':']={"Prefix", }, + ['%']={"Prefix", }, + }, +} --configdir = "hidden for the moment - This row is here only for debug purpose" -- ################################################################################ -- LOCAL FUNCTIONS -local function get_version() - local cmd_output_result, cmd_output_error - local cmd = "/sbin/apk_version -vs " .. processname .." 2>/dev/null" - local f = io.popen( cmd ) - local cmdresult = f:read("*l") - if (cmdresult) and (#cmdresult > 0) then - cmd_output_result = string.match(cmdresult,"^%S*") or "Unknown" - else - cmd_output_error = "Program not installed" - end - f:close() - return cmd_output_result,cmd_output_error -end - -- Return a table with the config-content of a file -- Commented/Blank lines are ignored local function get_value_from_file(file) @@ -77,26 +104,12 @@ local function recursedir(path, filearray) end end -local function autostarts() - local cmd_output_result - local cmd = "/sbin/rc_status | egrep '^S' | egrep '" .. processname .."' 2>/dev/null" - local f = io.popen( cmd ) - local cmdresult = f:read("*a") - if (cmdresult) and (#cmdresult > 0) then - cmd_output_result = "Process will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')" - else - cmd_output_error = "Not programmed to autostart" - end - f:close() - return cmd_output_result, cmd_output_error -end - -- Functin to split items into a table local function split_config_items(orgitem) local delimiter = ":" local output = {} output = format.string_to_table(string.sub(orgitem,1,1) .. ":" .. string.sub(orgitem,2),delimiter) - output.type = check_signs("prefix") + output.type = descr['prefix'] output.type = output.type[string.sub(orgitem,1,1)] or "unknown" return output end @@ -111,14 +124,9 @@ local function searchforconfigfiles() table.insert(configfiles, v) end end - - -- Debug option (adds the sampleconfig content) --- table.insert(configfiles, "/usr/share/acf/app/tinydns/sampleconfig.conf") - end searchforconfigfiles() - local function recurseoutput(table,cnt) if not (cnt) then cnt=0 end cnt = cnt + 1 @@ -203,6 +211,54 @@ local function validfilename(path) return false, "Not a valid filename!" end +-- Example taken from PIL +-- Sort by Keys +local function pairsByKeys(t,f) + local a = {} + + for n in pairs(t) do + -- This is to fix some bug when next table is indexnumber instead of name + if (tonumber(n) == nil) then + a[#a + 1] = n + end + end + table.sort(a,f) + local i = 0 -- iterator variable + return function () --iterator function + i = i + 1 + return a[i], t[a[i]] + end +end + +local function rebuild_table(t,domains_rebuilt) + if not (type(t) == "string") then + for k,v in pairs(t) do + if (tonumber(k)) then + table.insert(domains_rebuilt, v) + else + table.insert(domains_rebuilt, {label=k}) + rebuild_table(v,domains_rebuilt[#domains_rebuilt]) + end + end + table.sort(domains_rebuilt, function(a,b) return (a.label < b.label) end) + end +end + +-- This function removes all records that doesn't have the filter-value +local function filter_table(t1,domains_filtered,filter) + if not (type(t1) == "string") then + for k1,v1 in pairs(t1) do + for k2,v2 in pairs(v1) do + if (v2.label) then + if ( string.find(filter,v2.label) ) then + table.insert(domains_filtered, v2) + end + end + end + end + end +end + -- ################################################################################ -- PUBLIC FUNCTIONS @@ -212,47 +268,9 @@ end -- This function could be used to check that valid parameters are used in different places function check_signs(sign) - local output = {} - local output = {prefix={ - ['.']="Name server for your domain (NS + A + SOA)", - ['&']="Deletegate subdomain (NS + A)", - ['=']="Host (A + PTR)", - ['+']="Alias (A, no PTR)", - ['@']="Mail exchanger (MX)", - ["'"]="Text record (TXT)", - ['^']="Reverse record (PTR)", - ['C']="Canonical Name (CNAME)", - ['Z']="SOA record (SOA)", - [':']="Generic record", - ['%']="Client location", - }, - reverse={ - ['nsourdomain']=".", - ['nsdomain']="&", - ['host']="=", - ['alias']="+", - ['mx']="@", - ['ptr']="^", - ['cname']="C", - ['soa']="Z", - [':']=":", - ['locations']="%", - }, - fieldlabels={ - ['.']={"Prefix", "Domain", "IP address", "Name server", "Time to live", "Timestamp", "Location", }, - ['&']={"Prefix", "Domain", "IP address", "Name server", "Time to live", "Timestamp", "Location", }, - ['=']={"Prefix", "Host", "IP address", "Time to live", "Timestamp", "Location", }, - ['+']={"Prefix", "Alias", "IP address", "Time to live", "Timestamp", "Location", }, - ['@']={"Prefix", "Domain", "IP address", "Mail exchanger", "Distance", "Time to live", "Timestamp", "Location", }, - ['^']={"Prefix", "PTR", "Domain name", "Time to live", "Timestamp", "Location", }, - ['C']={"Prefix", "Domain name", "Canonical name", "Time to live", "Timestamp", "Location", }, - ['Z']={"Prefix", "Unknown", "Primary name server", "Contact address", "Serial number", "Refresh time", "Retry time", "Expire time", "Minimum time", "Time to live", "Timestamp", "Location",}, - [':']={"Prefix", }, - ['%']={"Prefix", }, - }, - } - if (sign) then - output = output[sign] + local output + if (sign) and (descr[sign]) then + output = descr[sign] end return output end @@ -260,12 +278,14 @@ end -- Present some general status function getstatus() local status = {} - local version,versionerrtxt = get_version() + + local value, errtxt = processinfo.package_version(packagename) status.version = cfe({ name = "version", label="Program version", - value=version, - errtxt=versionerrtxt, + value=value, + errtxt=errtxt, }) + status.status = cfe({ name="status", label="Program status", value=procps.pidof(processname), @@ -281,7 +301,7 @@ function getstatus() value=configfiles, }) - local autostart_sequense, autostart_errtxt = autostarts() + local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname) status.autostart = cfe({ name="autostart", label="Autostart sequence", value=autostart_sequense, @@ -297,8 +317,6 @@ function getlocations(self,filter_type) local config = {} local configobjects = {} local locations = {} - local debug - local version,versionerrtxt = get_version() local listenaddr = getopts.getoptsfromfile_onperline(configfile,"IP") or {} -- Loop through all available configfiles @@ -344,54 +362,6 @@ function getconfig(self,filter_type) return config end --- Example taken from PIL --- Sort by Keys -local function pairsByKeys(t,f) - local a = {} - - for n in pairs(t) do - -- This is to fix some bug when next table is indexnumber instead of name - if (tonumber(n) == nil) then - a[#a + 1] = n - end - end - table.sort(a,f) - local i = 0 -- iterator variable - return function () --iterator function - i = i + 1 - return a[i], t[a[i]] - end -end - -local function rebuild_table(t,domains_rebuilt) - if not (type(t) == "string") then - for k,v in pairs(t) do - if (tonumber(k)) then - table.insert(domains_rebuilt, v) - else - table.insert(domains_rebuilt, {label=k}) - rebuild_table(v,domains_rebuilt[#domains_rebuilt]) - end - end - table.sort(domains_rebuilt, function(a,b) return (a.label < b.label) end) - end -end - --- This function removes all records that doesn't have the filter-value -local function filter_table(t1,domains_filtered,filter) - if not (type(t1) == "string") then - for k1,v1 in pairs(t1) do - for k2,v2 in pairs(v1) do - if (v2.label) then - if ( string.find(filter,v2.label) ) then - table.insert(domains_filtered, v2) - end - end - end - end - end -end - -- If you enter 'filter_type' (this should be one of the options found in local function check_signs() ) then -- the output will be filtered to only contain this type of data. function getconfigobjects(self,filter_type, filter_levels) @@ -409,8 +379,8 @@ function getconfigobjects(self,filter_type, filter_levels) -- Create domain information tables local domain - -- * START * COMMONT SETTINGS *************************************************************************************** - local descr=check_signs("prefix") + -- * START * COMMONT SETTINGS **************************************** + local descr=descr['prefix'] -- Use only configs that has a valid prefix -- We filter away location-definitions -- If function is called with some filter options... then show only the filtered values @@ -616,7 +586,7 @@ function getconfigobjects(self,filter_type, filter_levels) }) end - -- * END * COMMONT SETTINGS *************************************************************************************** + -- * END * COMMONT SETTINGS ******************************************** -- Inject the previous data into the right table local value = filecontent_table[2] local currenttable @@ -651,84 +621,6 @@ function getconfigobjects(self,filter_type, filter_levels) return domains_filtered end --- Function to recursively inserts all filenames in a dir into an array -local function recursedir(path, filearray) - local k,v - for k,v in pairs(posix.dir(path) or {}) do - -- Ignore files that begins with a '.' - if not string.match(v, "^%.") then - local f = path .. "/" .. v - -- If subfolder exists, list files in this subfolder - if (posix.stat(f).type == "directory") then - recursedir(f, filearray) - else - table.insert(filearray, f) - end - end - end -end - --- ################################################################################ --- DEBUG INFORMATION (Everything below will be deleted in the future) - -function getdebug() - local debug = {} - ---[[ - local signs = get_available_signs("prefix") or {} - debug.debugprefixes = cfe({ - name = "debugprefixes", - label="Available prefixes", - option=signs, - type="select", - size=table.maxn(signs)+1, - }) - - local signs = get_available_signs("suffix") or {} - debug.debugsuffixes = cfe({ - name = "debugsuffixes", - label="Available suffixes", - option=signs, - type="select", - size=table.maxn(signs)+1, - }) ---]] - debug.configdir = cfe({ - name = "configdir", - label="configdir", - value=configdir, - }) - debug.datafile = cfe({ - name = "datafile", - label="datafile", - value=datafile, - }) - - for k,v in pairs(configfiles) do - local cnfcontent - fake, cnfcontent = get_value_from_file(v) - for kk,vv in pairs(cnfcontent) do - table.insert(configitems,vv) - end - end ----[[ - debug.configitems = cfe({ - name = "configitems", - label="configitems", - option=configitems, - type="select", - }) ---]] - debug.configfiles = cfe({ - name = "configfiles", - label="configfiles", - option=configfiles, - type="select", - }) - - return debug -end - function getfilelist () local listed_files = {} for k,v in pairs{baseurl} do @@ -740,12 +632,6 @@ function getfilelist () end function get_filedetails(self,path) --- local path --- if (num == "2") then --- path = configfile2 --- else --- path = configfile --- end if not (validfilename(path)) then return false, "Not a valid filename!" end @@ -802,6 +688,7 @@ function get_filedetails(self,path) return file end + function updatefilecontent (self, filetochange) local path = filetochange.name local modifications = filetochange.value -- cgit v1.2.3