Commit 52a45760 authored by Mika Havela's avatar Mika Havela

Cleaning up code.

git-svn-id: svn://svn.alpinelinux.org/acf/fetchmail/trunk@955 ab2d0c66-481e-0410-8bed-d214d4d58bed
parent 386b8d30
<? local form = ... ?>
<? local form = ...
require("viewfunctions")
?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
......@@ -7,52 +9,14 @@ io.write("</span>")
--]]
?>
<?
function informationform(myform,tags)
io.write("<DL>")
for k,v in pairs(tags) do
if (myform[v]) and (myform[v].value) then
local val = myform[v]
io.write("\t<DT")
if (#val.errtxt > 0) then io.write(" class='error'") end
io.write(">" .. val.label .. "</DT>\n")
io.write("\t\t<DD>" .. val.value .. "\n")
if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end
if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end
io.write("\t\t</DD>\n")
end
end
io.write("</DL>")
end
?>
<?
function configform(myform,tags)
io.write("<DL>")
for k,v in pairs(tags) do
if (myform[v]) and (myform[v].value) then
local val = myform[v]
io.write("\t<DT")
if (#val.errtxt > 0) then io.write(" class='error'") end
io.write(">" .. val.label .. "</DT>\n")
io.write("\t\t<DD>" .. html.form[val.type](val) .. "\n")
if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end
if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end
io.write("\t\t</DD>\n")
end
end
io.write("</DL>")
end
?>
<H1>SYSTEM INFO</H1>
<DL>
<?
local myform = form.status
local tags = { "status", "version", "autostart", }
informationform(myform,tags)
displayinfo(myform,tags,"viewonly")
?>
</DL>
<H1>CONFIGURATION</H1>
<? local myform = form.config ?>
......@@ -65,7 +29,7 @@ informationform(myform,tags)
<P>Select how often the mailboxes will be checked. For ETRN transfers (below) this specifies how often transfers are initiated</P>
<?
local tags = { "freq", }
configform(myform,tags)
displayinfo(myform,tags)
?>
<? --]] ?>
......@@ -141,14 +105,14 @@ io.write("</TABLE></CENTER>")
<P>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.</P>
<?
local tags = { "postmaster", }
configform(myform,tags)
displayinfo(myform,tags)
?>
<H3>ETRN</H3>
<P>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.</P>
<?
local tags = { "etrnremote","etrnquedomain", }
configform(myform,tags)
displayinfo(myform,tags)
?>
......@@ -157,7 +121,7 @@ configform(myform,tags)
<H3>GENERAL</H3>
<?
local tags = { "logfile", "loglevel", "smallerlogs", }
configform(myform,tags)
displayinfo(myform,tags)
?>
<? --]] ?>
......@@ -165,7 +129,7 @@ configform(myform,tags)
<H2>SAVE AND APPLY ABOVE SETTINGS</H2>
<?
local tags = { "cmdsave","cmdtest", }
configform(myform,tags)
displayinfo(myform,tags)
?>
</form>
......@@ -194,14 +158,3 @@ if (cmdform) and (cmdform[tags[1]]) then
</form>
<? end ?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
?>
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 <PRE> ... </PRE> in BLACK text
errtxt="", --Content of this variable is displayed as <PRE> ... </PRE> 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
......
<? local form = ... ?>
<? local form = ...
require("viewfunctions")
?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
......@@ -6,52 +8,15 @@ io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
?>
<?
function informationform(myform,tags)
io.write("<DL>")
for k,v in pairs(tags) do
if (myform[v]) then
local val = myform[v]
io.write("\t<DT")
if (#val.errtxt > 0) then io.write(" class='error'") end
io.write(">" .. val.label .. "</DT>\n")
io.write("\t\t<DD>" .. val.value .. "\n")
if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end
if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end
io.write("\t\t</DD>\n")
end
end
io.write("</DL>")
end
?>
<?
function configform(myform,tags)
io.write("<DL>")
for k,v in pairs(tags) do
if (myform[v]) then
local val = myform[v]
io.write("\t<DT")
if (#val.errtxt > 0) then io.write(" class='error'") end
io.write(">" .. val.label .. "</DT>\n")
io.write("\t\t<DD>" .. html.form[val.type](val) .. "\n")
if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end
if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end
io.write("\t\t</DD>\n")
end
end
io.write("</DL>")
end
?>
<H1>SYSTEM INFO</H1>
<DL>
<?
local myform = form.status
local tags = { "status", "version", "autostart", }
informationform(myform,tags)
displayinfo(myform,tags,"viewonly")
?>
</DL>
<H1>CONFIGURATION</H1>
<? local myform = form.config ?>
......@@ -62,7 +27,7 @@ informationform(myform,tags)
<H3>FILE DETAILS</H3>
<?
local tags = { "filename", "filesize", "mtime", "sumerrors" }
informationform(myform,tags)
displayinfo(myform,tags,"viewonly")
?>
<H3>FILE CONTENT</H3>
......@@ -73,39 +38,17 @@ io.write(html.form[form.config.filecontent.type](form.config.filecontent))
<H2>SAVE AND APPLY ABOVE SETTINGS</H2>
<?
local tags = { "cmdsave", }
configform(myform,tags)
displayinfo(myform,tags)
?>
</form>
<? -- MANAGEMENT BUTTONS
local cmdform = form.management
local cmdresult = form.cmdmanagement
local tags = { "start", "stop", "restart" }
if (cmdform) and (cmdform[tags[1]]) then
io.write('<form name="management" action="" method="POST">')
io.write('<H1>MANAGEMENT</H1>')
io.write('<dl>')
io.write('<dt>' .. cmdform[tags[1]]["label"] .. '</dt>')
io.write('<dd>')
for k,v in pairs(tags) do
if (cmdform[v]) then
io.write(html.form[cmdform[v].type](cmdform[v]))
end
end
io.write('</dd>')
if (cmdresult) and (cmdresult.action) and (#cmdresult.action.descr > 0) then
io.write('<dt>' .. cmdresult.label .. '</dt>')
io.write('<dd><pre>' .. cmdresult.action.descr .. '</pre></dd>')
end
io.write('</dl></form>')
end ?>
<?
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
-- Management buttons
local myform = form.management
local tags = { "start", "stop", "restart" }
if (myform) then
io.write("<H1>MANAGEMENT</H1>\n<DL>")
displaymanagement(myform,tags)
io.write("</DL>")
end
?>
</form>
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 <host> protocol etrn smtpdomain <mydomain>
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,
})