From 52a457602fc2d7d106ddfa0a686ee8a07c979875 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Tue, 8 Apr 2008 15:11:02 +0000 Subject: Cleaning up code. git-svn-id: svn://svn.alpinelinux.org/acf/fetchmail/trunk@955 ab2d0c66-481e-0410-8bed-d214d4d58bed --- fetchmail-config-html.lsp | 69 +++-------------- fetchmail-controller.lua | 182 +++++++++------------------------------------ fetchmail-expert-html.lsp | 91 +++++------------------ fetchmail-model.lua | 184 ++++++---------------------------------------- fetchmail-status-html.lsp | 42 +++-------- 5 files changed, 97 insertions(+), 471 deletions(-) diff --git a/fetchmail-config-html.lsp b/fetchmail-config-html.lsp index fdc5422..8dc090d 100644 --- a/fetchmail-config-html.lsp +++ b/fetchmail-config-html.lsp @@ -1,4 +1,6 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -7,52 +9,14 @@ 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 io.write(" class='error'") end - io.write(">" .. val.label .. "\n") - - io.write("\t\t
" .. val.value .. "\n") - 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 -?> -") - for k,v in pairs(tags) do - if (myform[v]) and (myform[v].value) then - local val = myform[v] - io.write("\t 0) then io.write(" class='error'") end - io.write(">" .. val.label .. "\n") - - io.write("\t\t
" .. html.form[val.type](val) .. "\n") - 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

@@ -65,7 +29,7 @@ informationform(myform,tags)

Select how often the mailboxes will be checked. For ETRN transfers (below) this specifies how often transfers are initiated

@@ -141,14 +105,14 @@ io.write("")

The Postmaster address is the address of last resort when fetchmail cannot determine how to send a mail message. If left blank, undelieverable mail may be discarded.

ETRN

This is for branches that have DNS mail domains hosted by an ISP that allows for ETRN dequeueing. ETRN dequeueing causes the SMTP mail exchanger at the ISP to forward mail to you via SMTP. You will need to make sure that DNS is setup appropriately and that your ISP supports ETRN. If you have not arranged with your ISP to have ETRN support, leave these fields blank.

@@ -157,7 +121,7 @@ configform(myform,tags)

GENERAL

@@ -165,7 +129,7 @@ configform(myform,tags)

SAVE AND APPLY ABOVE SETTINGS

@@ -194,14 +158,3 @@ if (cmdform) and (cmdform[tags[1]]) then - -DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] -?> - - - diff --git a/fetchmail-controller.lua b/fetchmail-controller.lua index 654a519..93776c8 100644 --- a/fetchmail-controller.lua +++ b/fetchmail-controller.lua @@ -1,28 +1,14 @@ module(..., package.seeall) -local list_redir = function (self) +-- ################################################################################ +-- LOCAL FUNCTIONS + +local function list_redir (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 - status.status.value = "Enabled" - else - status.status.value = "Disabled" - end - return status -end - local function displaycmdsave(self) -- Add a cmd button to the view local cmdsave = cfe({ name="cmdsave", @@ -33,13 +19,14 @@ local function displaycmdsave(self) return cmdsave end -local function displaycmdmanagement(disablestart,disablestop,disablerestart) +local function displaycmdmanagement(pidofstatus) -- Add a management buttons local management = {} management.start = cfe({ name="cmdmanagement", label="Program control-panel", value="Start", type="submit", + descr="When starting or restarting BGP, the web-page will freeze for some reason!", }) management.stop = cfe({ name="cmdmanagement", label="Program control-panel", @@ -51,38 +38,20 @@ local function displaycmdmanagement(disablestart,disablestop,disablerestart) value="Restart", type="submit", }) - - -- Disable management buttons based on if the process is running or not - if (disablestart) then management.start.disabled = "yes" end - if (disablestop) then management.stop.disabled = "yes" end - if (disablerestart) then management.restart.disabled = "yes" end - - return management -end - -local function displaycmdmanagement(disablestart,disablestop,disablerestart) - -- 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", + descr="", --Content of this variable is displayed as
 ... 
in BLACK text + errtxt="", --Content of this variable is displayed as
 ... 
in RED text }) -- Disable management buttons based on if the process is running or not - if (disablestart) then management.start.disabled = "yes" end - if (disablestop) then management.stop.disabled = "yes" end - if (disablerestart) then management.restart.disabled = "yes" end + if (pidofstatus) then + management.start.disabled = "yes" + else + management.stop.disabled = "yes" + management.restart.disabled = "yes" + end return management end @@ -90,95 +59,20 @@ end -- ################################################################################ -- PUBLIC FUNCTIONS -function status(self) - return { status=getstatus(self) } -end ---[==[ -function config(self) - local cmdmanagement, cmdmanagementresult - local cmdsavereply = {} - local cmdsaveresult = {} - if ( self.clientdata.cmdmanagement) then - cmdmanagement = cfe({ - name="cmdmanagement", - value=string.lower(self.clientdata.cmdmanagement), - }) - cmdmanagementresult, cmdmanagement = self.model:startstop_service( cmdmanagement ) - end - if ( self.clientdata.cmdsave) then ---[[ - local variables="logfile loglevel smallerlogs maxsize numrotate localandnetworklog remotelogging" -- Advanced-config options --- local variables="remotelogging" -- Guided-config options - for var in string.gmatch(variables, "%S+") do - -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file - if (self.clientdata[var] == "") then self.clientdata[var] = nil end - cmdsaveresult[var], cmdsavereply[var] = self.model:setconfigs( var,self.clientdata[var] ) - end ---]] - end - - local status = getstatus(self) - local config = self.model.getconfig() - - -- Write out erros from previous cmdsave actions - for k,v in pairs(cmdsaveresult) do - if not (v) then - config[k]["errtxt"] = tostring(cmdsavereply[k]["errtxt"]) - end - end - - -- Display save button - config.cmdsave = displaycmdsave() - - -- Add a test button to the view - config.cmdtest = cfe({ name="cmdtest", - label="Test saved settings", - value="Test", - type="submit", - }) - - -- FIXME: This is temporary disabling the buttons (until they work) - config.cmdsave["disabled"] = "yes" - config.cmdtest["disabled"] = "yes" - ---[[ - -- Management buttons - local disablestart,disablestop,disablerestart - -- Disable management buttons based on if the process is running or not - if (string.lower(status.status.value) == "enabled" ) then - disablestart = "yes" - else - disablestop = "yes" - end - -- Disable management buttons if there exist errors in the config - for k,v in pairs(config) do - if (config[k]["errtxt"] ~= "") then - disablestart = "yes" - disablestop = "yes" - disablerestart = "yes" - break - end +mvc = {} +function mvc.on_load(self, parent) + if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then + self.worker[self.conf.action] = list_redir(self) end - -- Display management buttons - local management = displaycmdmanagement(disablestart,disablestop,disablerestart) ---]] +end - return { - option={ script=ENV["SCRIPT_NAME"], - prefix=self.conf.prefix, - controller = self.conf.controller, - action = "config", }, - status = status, - cmdmanagement = cmdmanagement, - management = management, - config = config, - debugclientdata = self.clientdata, - } +function status(self) + return { status=self.model:getstatus(self) } end ---]==] + function expert(self) -- Start/Stop/Restart process - local cmdmanagement + local cmdmanagement, actionresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", @@ -188,7 +82,7 @@ function expert(self) value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) }), }) - local actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) + actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) end -- Save changes @@ -197,7 +91,7 @@ function expert(self) modifications = self.model:update_filecontent(modifications) end - local status = getstatus(self) + local status = self.model:getstatus(self) local config = self.model:get_filedetails() @@ -205,16 +99,15 @@ function expert(self) config.cmdsave = displaycmdsave() - -- Management buttons - local disablestart,disablestop,disablerestart - -- Disable management buttons based on if the process is running or not - if (string.lower(status.status.value) == "enabled" ) then - disablestart = "yes" - else - disablestop = "yes" +--[[ -- 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 - -- Display management buttons --- management = displaycmdmanagement(disablestart,disablestop,disablerestart) +--]] return { option={ script=ENV["SCRIPT_NAME"], @@ -222,11 +115,8 @@ function expert(self) controller = self.conf.controller, action = "expert", }, management = management, - cmdmanagement = cmdmanagement, config = config, status = status, - startstop = startstop, - debugclientdata = self.clientdata, } end diff --git a/fetchmail-expert-html.lsp b/fetchmail-expert-html.lsp index 597e162..e46fdb2 100644 --- a/fetchmail-expert-html.lsp +++ b/fetchmail-expert-html.lsp @@ -1,4 +1,6 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -6,52 +8,15 @@ io.write(html.cfe_unpack(form)) io.write("
") --]] ?> -") - for k,v in pairs(tags) do - if (myform[v]) then - local val = myform[v] - io.write("\t 0) then io.write(" class='error'") end - io.write(">" .. val.label .. "\n") - - io.write("\t\t
" .. val.value .. "\n") - 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 -?> -") - for k,v in pairs(tags) do - if (myform[v]) then - local val = myform[v] - io.write("\t 0) then io.write(" class='error'") end - io.write(">" .. val.label .. "\n") - - io.write("\t\t
" .. html.form[val.type](val) .. "\n") - 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

@@ -62,7 +27,7 @@ informationform(myform,tags)

FILE DETAILS

FILE CONTENT

@@ -73,39 +38,17 @@ io.write(html.form[form.config.filecontent.type](form.config.filecontent))

SAVE AND APPLY ABOVE SETTINGS

- - -') - 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/fetchmail-model.lua b/fetchmail-model.lua index e2b2a92..8986605 100644 --- a/fetchmail-model.lua +++ b/fetchmail-model.lua @@ -1,29 +1,22 @@ module(..., package.seeall) +-- Load libraries require("fs") require("procps") require("getopts") require("format") require("daemoncontrol") require("validator") +require("processinfo") +-- Set variables +local packagename = "fetchmail" local processname = "fetchmail" local configfile = "/root/.fetchmailrc" local config = {} -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 +-- ################################################################################ +-- LOCAL FUNCTIONS local function getloglevels() local loglevels = {} @@ -32,6 +25,7 @@ local function getloglevels() end return loglevels end + local function getmethods() local methods = {"pop3","imap","pop3domain", } return methods @@ -188,18 +182,13 @@ pool protocol etrn smtpdomain end -local function autostarts() - local cmd_output_result, cmd_output_error - 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)") .. "')" +local function process_status_text(procname) + local t = procps.pidof(procname) + if (t) and (#t > 0) then + return "Enabled" else - cmd_output_error = "Not programmed to autostart" - end - f:close() - return cmd_output_result,cmd_output_error + return "Disabled" + end end -- ################################################################################ @@ -210,7 +199,6 @@ function startstop_service ( self, action ) local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, cmd) action.descr=cmdmessage action.errtxt=cmderror - -- Reporting back (true|false, the original acition) return cmdresult,action end @@ -218,7 +206,7 @@ function getstatus() local opts = getconfig() local status = {} - local value, errtxt = get_version() + local value, errtxt = processinfo.package_version(packagename) status.version = cfe({ name = "version", label="Program version", value=value, @@ -227,35 +215,21 @@ function getstatus() status.status = cfe({ name="status", label="Program status", - value=procps.pidof(processname), + value=process_status_text(processname), }) - status.configfile = cfe({ name="configfile", - label="Config file", - value=configfile, - }) - - 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, errtxt=autostart_errtxt, }) ---[[ - if (opts["remotelogging"]) and not ((opts["remotelogging"]["value"] ~= "") and not (opts["localandnetworklog"]["value"])) then - status.logfile = cfe({ name="logfile", - label="Locally logging to", - value=opts["logfile"]["value"], - }) - end - if (opts["SYSLOGD_OPTS"]) and (opts["SYSLOGD_OPTS"]["-R"]) and (opts["SYSLOGD_OPTS"]["-R"] ~= "") then - status.remote = cfe({ name="remotelogging", - label="Remote logging to", - value=opts["SYSLOGD_OPTS"]["-R"], - }) - end ---]] + status.configfile = cfe({ name="configfile", + label="Config file", + value=configfile, + }) + return status end @@ -328,8 +302,6 @@ function getconfig() value = configcontent, }) - - -- Next section selects which configurations we should show to the user config["freq"] = cfe({ name="freq", @@ -359,123 +331,9 @@ function getconfig() value = configcontent_etrn["ETRNDOMAIN"], }) ---[[ - - -- Next section is to print errormessages when configs are wrong - if (configcontent["SYSLOGD_OPTS"]["-l"]) and - ((tonumber(configcontent["SYSLOGD_OPTS"]["-l"]) == nil) or (tonumber(configcontent["SYSLOGD_OPTS"]["-l"]) > 8)) then - config["loglevel"]["errtxt"] = "Log value is out of range!\nCurrent value in config is '" .. - configcontent["SYSLOGD_OPTS"]["-l"] .. - "' - This is invalid!\nPlease select one of the above and save your changes." - table.insert(config["loglevel"]["option"], tonumber(configcontent["SYSLOGD_OPTS"]["-l"])) - end - - if (configcontent["SYSLOGD_OPTS"]["-L"] ~= nil) and ((configcontent["SYSLOGD_OPTS"]["-R"] == nil) or (configcontent["SYSLOGD_OPTS"]["-R"] == "")) then - config["localandnetworklog"]["errtxt"] = "Logging to local and network (-L) is not possible unless you define a host (-R) for remote logging or remove this option." - end - - -- Sum all errors into one cfe - local sumerrors = "" - for k,v in pairs(config) do - if (config[k]["errtxt"] ~= "") then - sumerrors = sumerrors .. config[k]["errtxt"] .. "\n" - end - end - if (sumerrors ~= "") then - config["sumerrors"] = cfe ({ - name="sumerrors", - label = "Configuration errors", - errtxt = sumerrors, - }) - end - ---]] - return config end --- IMPORTANT! This function is a exception! It's not fed with CFE's --- Parameter should be one of the ones defined in the variable 'variabletranslator'. --- value should be whatever the new value should be. -function setconfigs(self,parameter,value) - -- Set variables ---[[ - local variable = "SYSLOGD_OPTS" - local variabletranslator = ({ - logfile = "-O", - loglevel = "-l", - smallerlogs = "-S", - maxsize = "-s", - numrotate = "-b", - localandnetworklog = "-L", - remotelogging = "-R", - }) - cmdparameter = variabletranslator[parameter] - - -- Report a error if someone tryes to use a invalid parameter - if not (cmdparameter) then - local availablevariables = "" - for k,v in pairs(variabletranslator) do - availablevariables = k .. ", " .. availablevariables - end - parameter = parameter or "" - return false, cfe({ - name="syslog.model.setconfigs()", - errtxt="'" .. parameter .. "' is not a valid parameter!\nValid options are: " .. availablevariables, - }) - end - - --TODO: Validate so that user cant add values with '-' (could cause major breakage next time you do getopts) - - -- This config-file only accepts one type of parameters (report error if someone uses wrong parameter) - if not (string.find(cmdparameter, "-%a$")) then - return false, cfe({ - name="syslog.model.setconfigs()", - errtxt="Parameter must be formated '-a' (where a is one upper/lowercase letter [a-z])", - }) - end - - -- Validate userinput (if valid path/filename) - if (value) and (cmdparameter == "-O") then - local cmdresult, cmdmessage = validator.is_valid_filename(value, "/var/log" ) - if not (cmdresult) then - return false, cfe({ - name="syslog.model.setconfigs()", - errtxt=cmdmessage, - }) - end - end - - -- Validate userinput (Has the user entered a valid hostname and/or port) - if (value) and (cmdparameter == "-R") then - local hostport = format.string_to_table(value, ":") - local host = hostport[1] - local port = hostport[2] - if (port) and not (validator.is_port(port)) then - return false, cfe({ - name="syslog.model.setconfigs.getopts.setoptsinfile()", - errtxt="You entered '" .. tostring(port) .. "' as port - This is not valid!", - }) - end - end - - -- Set/Unset checkbox variables - if (value) and ((cmdparameter == "-S") or (cmdparameter == "-L")) then value = "" end - - local cmdresult, cmdmessage, cmderror = getopts.setoptsinfile(configfile,variable,cmdparameter,value) - if (cmderror) then - return false, cfe({ - name="syslog.model.setconfigs.getopts.setoptsinfile()", - errtxt=cmderror, - }) - end - return true, cfe({ - name="syslog.model.setconfigs()", - value=cmdmessage, - }) ---]] -end - -- modifications should be a CFE function update_filecontent (self, modifications) local path = configfile diff --git a/fetchmail-status-html.lsp b/fetchmail-status-html.lsp index 9b3372a..61494ae 100644 --- a/fetchmail-status-html.lsp +++ b/fetchmail-status-html.lsp @@ -1,44 +1,26 @@ - - + ") - for k,v in pairs(tags) do - if (myform[v]) then - local val = myform[v] - io.write("\t 0) then io.write(" class='error'") end - io.write(">" .. val.label .. "\n") - - io.write("\t\t
" .. val.value .. "\n") - 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 +--[[ DEBUG INFORMATION +io.write("

DEBUGGING

DEBUG INFO: CFE

") +io.write(html.cfe_unpack(form)) +io.write("
") +--]] ?>

SYSTEM INFO

+
+

PROGRAM SPECIFIC OPTIONS/INFORMATION

- -DEBUGGING

DEBUG INFO: CFE

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