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 --- Makefile | 2 +- tinydns-config-html.lsp | 51 +------- tinydns-controller.lua | 18 +-- tinydns-edit-html.lsp | 80 ++---------- tinydns-expert-html.lsp | 50 ++------ tinydns-model.lua | 323 ++++++++++++++++-------------------------------- tinydns-status-html.lsp | 48 +------ 7 files changed, 142 insertions(+), 430 deletions(-) diff --git a/Makefile b/Makefile index 28df3d1..1f4923e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ APP_NAME=tinydns PACKAGE=acf-$(APP_NAME) -VERSION=0.1.0 +VERSION=0.1.1 APP_DIST=\ tinydns* \ diff --git a/tinydns-config-html.lsp b/tinydns-config-html.lsp index b708d93..839570e 100755 --- a/tinydns-config-html.lsp +++ b/tinydns-config-html.lsp @@ -1,4 +1,6 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -6,31 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("
") --]] ?> -") - for k,v in pairs(tags) do - if (myform[v]) and (myform[v]["value"]) then - local val = myform[v] - io.write("\t 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "\n") - if (viewonly) then - io.write("\t\t
" .. val.value .. "\n") - else - io.write("\t\t
" .. html.form[val.type](val) .. "\n") - end - if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

\n") end - if (#val.errtxt > 0) then io.write("\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

\n") end - io.write("\t\t
\n") - end - end - io.write("") -end -?>

SYSTEM INFO

- - -

DEGUGGING

- - - - -DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] -?> - diff --git a/tinydns-controller.lua b/tinydns-controller.lua index 502af03..47bb8d7 100644 --- a/tinydns-controller.lua +++ b/tinydns-controller.lua @@ -1,20 +1,17 @@ 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 -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 - local function getstatus(self) local status = self.model.getstatus() if (#status.status.value > 0) then @@ -76,6 +73,13 @@ 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 = {} diff --git a/tinydns-edit-html.lsp b/tinydns-edit-html.lsp index e5bb6ba..08dee18 100644 --- a/tinydns-edit-html.lsp +++ b/tinydns-edit-html.lsp @@ -1,4 +1,6 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -6,39 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("
") --]] ?> - 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "") - io.write("\n\t\t
") - if (viewtype == "viewonly") then - io.write(val.value) - elseif (val.type == "radio") and (type(val.option) == "table") and (#val.option > 0) then - io.write("") - for k1,v1 in pairs(val.option) do - io.write(tostring(v1.label) .. ":") - io.write("") - end - io.write("") - else - io.write(html.form[val.type](val)) - end - if (val.descr) and (#val.descr > 0) then io.write("\n\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

") end - if (#val.errtxt > 0) then io.write("\n\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

") end - io.write("\n\t\t
\n") - end - end -end -?>

SYSTEM INFO

@@ -49,7 +18,6 @@ displayinfo(myform,tags,"viewonly") ?>
-

CONFIGURATION

Expert config

@@ -77,38 +45,14 @@ displayinfo(myform,tags) ?> -
- -') - io.write('

MANAGEMENT

') - io.write('
') - io.write('
' .. cmdform[tags[1]]["label"] .. '
') - io.write('
') - for k,v in pairs(tags) do - if (cmdform[v]) then - io.write(html.form[cmdform[v].type](cmdform[v])) - end - end - io.write('
') - - if (cmdresult) and (cmdresult.action) and (#cmdresult.action.descr > 0) then - io.write('
' .. cmdresult.label .. '
') - io.write('
' .. cmdresult.action.descr .. '
') - end - io.write('
') -end ?> - DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] +-- Management buttons +local myform = form.management +local tags = { "start", "stop", "restart" } +if (myform) then + io.write("

MANAGEMENT

\n
") + displaymanagement(myform,tags) + io.write("
") +end ?> - + diff --git a/tinydns-expert-html.lsp b/tinydns-expert-html.lsp index 7744934..310d8ce 100644 --- a/tinydns-expert-html.lsp +++ b/tinydns-expert-html.lsp @@ -1,4 +1,6 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -7,31 +9,6 @@ io.write("
") --]] ?> -") - for k,v in pairs(tags) do - if (myform[v]) and (myform[v]["value"]) then - local val = myform[v] - io.write("\t 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "\n") - if (viewonly) then - io.write("\t\t
" .. val.value .. "\n") - else - io.write("\t\t
" .. html.form[val.type](val) .. "\n") - end - if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

\n") end - if (#val.errtxt > 0) then io.write("\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

\n") end - io.write("\t\t
\n") - end - end - io.write("") -end -?>

SYSTEM INFO

CONFIGURATION

-

Expert config

List of files

- - - - - ---]]?> - - +
File
-DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] -?> 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 diff --git a/tinydns-status-html.lsp b/tinydns-status-html.lsp index 06b5339..17a1b60 100644 --- a/tinydns-status-html.lsp +++ b/tinydns-status-html.lsp @@ -1,4 +1,6 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -6,31 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("
") --]] ?> -") - for k,v in pairs(tags) do - if (myform[v]) and (myform[v]["value"]) then - local val = myform[v] - io.write("\t 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "\n") - if (viewonly) then - io.write("\t\t
" .. val.value .. "\n") - else - io.write("\t\t
" .. html.form[val.type](val) .. "\n") - end - if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

\n") end - if (#val.errtxt > 0) then io.write("\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

\n") end - io.write("\t\t
\n") - end - end - io.write("") -end -?>

SYSTEM INFO

PROGRAM SPECIFIC OPTIONS/INFORMATION

Locations

") ---for k,v in pairs(tags) do if (myform) and (myform.value) then local val = myform io.write("\n\t" .. val.label .. "") - io.write("\n\t\t
") for k,v in pairs(myform.value) do io.write(" " .. tostring(k) .. "") @@ -77,7 +45,6 @@ end io.write("") ?> - ") ?> - -DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] -?> - -- cgit v1.2.3