module(..., package.seeall) -- Load libraries require("modelfunctions") require("fs") -- Set variables local packagename = "fetchmail" local processname = "fetchmail" local configfile = "/root/.fetchmailrc" local config = {} -- ################################################################################ -- LOCAL FUNCTIONS local function getloglevels() local loglevels = {} for i=1,8 do table.insert(loglevels,i) end return loglevels end local function getmethods() local methods = {"pop3","imap","pop3domain", } return methods end local function getmailboxes(t) local objects = cfe({}) objects.label = "Mailbox " .. tostring(t["RBOX"]) objects.method = cfe({ name="method", label = "Method", type = "select", value = t["METHOD"], option = getmethods(), }) objects.disabled = cfe({ name="disabled", type="checkbox", label = "Disabled", checked = t["DISABLED"], }) objects.remotehost = cfe({ name="remotehost", label = "RemoteHost", value = t["RHOST"], }) objects.remotemailbox = cfe({ name="remotemailbox", label = "Mailbox", value = t["RBOX"], }) objects.remotepassword = cfe({ name="remotepassword", label = "Password", type = "passwd", value = t["PASSWD"], }) objects.localhost = cfe({ name="localhost", label = "LocalHost", value = t["LHOST"], }) objects.localmailbox = cfe({ name="localmailbox", label = "LocalMailbox", value = t["LBOX"], }) objects.localdomain = cfe({ name="localdomain", label = "LocalDomain", value = t["DOMAIN"], }) return objects end local function read_config() --[[ imap/pop3 lines are: 1 2 3 4 5 6 7 8 9 10 poll protocol no dns username password 11 12 13 14 15 16 17 is smtphost no rewrite fetchall pop3domain lines are: 1 2 3 4 5 6 7 8 9 10 11 12 poll localdomainst protocol pop3 no dns username password 13 14 15 16 17 18 19 20 21 22 to * here smtphost smtpaddress no rewrite fetchall etrn is: 1 2 3 4 5 6 pool protocol etrn smtpdomain --]] local mailboxes = {} local configcontent_postmaster = {} local configcontent_etrn = {} local path = configfile local valid = nil configcontenttable = fs.read_file_as_array(path) or {} for k,v in pairs(configcontenttable) do if (string.match(v, "^%s*#Begin Fetchmail")) then valid=1 end if (valid) then -- local DISABLED = string.match(v, "^%s*(#)") -- Set parameters for POP3 or IMAP local val = {RHOST=2, CHECK=9 ,METHOD=4, RBOX=8, PASSWD=10, LBOX=12, LHOST=14} local configcontent = {} configcontent[k] = {} configcontent[k]["DISABLED"] = DISABLED for kk,vv in pairs(val) do configcontent[k][kk] = tostring(string.match(v,"^%s*" .. string.rep("%S*%s*",vv-1) .. "(%S*)%s*")) end -- Remove quotes from passwords if (configcontent[k]["PASSWD"]) then configcontent[k]["PASSWD"] = string.match(configcontent[k]["PASSWD"], "^\"(.-)\"") end -- Check if row is valid config if (configcontent[k]["CHECK"]) and (string.lower(configcontent[k]["CHECK"]) == "password") then table.insert(mailboxes,getmailboxes(configcontent[k])) end -- Set parameters for POP3domain local val = {RHOST=2, METHOD=6, RBOX=10, CHECK=11, PASSWD=12, LHOST=17, DOMAIN=19,} local configcontent = {} configcontent[k] = {} configcontent[k]["DISABLED"] = DISABLED for kk,vv in pairs(val) do configcontent[k][kk] = tostring(string.match(v,"^%s*" .. string.rep("%S*%s*",vv-1) .. "(%S*)%s*")) end -- Display this config as Method=pop3domain (as the current options in the view) configcontent[k]["METHOD"] = "pop3domain" -- Remove quotes from passwords if (configcontent[k]["PASSWD"]) then configcontent[k]["PASSWD"] = string.match(configcontent[k]["PASSWD"], "^\"(.-)\"") end -- Check if row is valid config if (configcontent[k]["CHECK"]) and (string.lower(configcontent[k]["CHECK"]) == "password") then table.insert(mailboxes,getmailboxes(configcontent[k])) end -- Set parameters for etrn local val = {ETRNSMTPHOST=2, CHECK=4, ETRNDOMAIN=6} local configcontent = {} configcontent[k] = {} configcontent[k]["DISABLED"] = DISABLED for kk,vv in pairs(val) do configcontent[k][kk] = tostring(string.match(v,"^%s*" .. string.rep("%S*%s*",vv-1) .. "(%S*)%s*")) end -- Check if row is valid config if (configcontent[k]["CHECK"]) and (string.lower(configcontent[k]["CHECK"]) == "etrn") then configcontent_etrn=configcontent[k] end -- Set parameters for postmaster local val = {CHECK=2, POSTMASTER=3} local configcontent = {} configcontent[k] = {} configcontent[k]["DISABLED"] = DISABLED for kk,vv in pairs(val) do configcontent[k][kk] = tostring(string.match(v,"^%s*" .. string.rep("%S*%s*",vv-1) .. "(%S*)%s*")) end -- Check if row is valid config if (configcontent[k]["CHECK"]) and (string.lower(configcontent[k]["CHECK"]) == "postmaster") then configcontent_postmaster=configcontent[k] end end if (string.match(v, "^%s*#End Fetchmail")) then valid=nil end end -- Create one empty record so that user can add settings table.insert(mailboxes,getmailboxes({})) return mailboxes,configcontent_postmaster,configcontent_etrn end -- ################################################################################ -- PUBLIC FUNCTIONS function startstop_service(action) return modelfunctions.startstop_service(processname, action) end function getstatus() return modelfunctions.getstatus(processname, packagename, "Fetchmail Status") end function get_filedetails() -- FIXME - validation return modelfunctions.getfiledetails(configfile) end function update_filecontent(filedetails) filedetails.value.filename.value = configfile -- FIXME - validation return modelfunctions.setfiledetails(filedetails) end function getconfig() local config = {} local mailboxes,configcontent_postmaster,configcontent_etrn = read_config() if not (fs.is_file(configfile)) then config["configfile"] = "Config file '".. configfile .. "' is missing!" end config["debug"] = cfe({ name="debug", label = "Debug info", type = "longtext", value = configcontent, }) -- Next section selects which configurations we should show to the user config["freq"] = cfe({ name="freq", label = "Check mail once every", type = "select", value = "123", option = {"15min", "hour","day",}, }) config["mailboxes"] = cfe({ name="mailboxes", label = "Mailboxes", value = mailboxes, }) config["postmaster"] = cfe({ name="postmaster", label = "Postmaster", value = configcontent_postmaster["POSTMASTER"], }) config["etrnremote"] = cfe({ name="etrnremote", label = "Remote server", value = configcontent_etrn["ETRNSMTPHOST"], }) config["etrnquedomain"] = cfe({ name="etrnquedomain", label = "Queued domain", value = configcontent_etrn["ETRNDOMAIN"], }) return config end