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-model.lua | 184 ++++++---------------------------------------------- 1 file changed, 21 insertions(+), 163 deletions(-) (limited to 'fetchmail-model.lua') 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 -- cgit v1.2.3