fetchmail-model.lua 7.02 KB
Newer Older
1 2
module(..., package.seeall)

Mika Havela's avatar
Mika Havela committed
3
-- Load libraries
4
require("modelfunctions")
5 6
require("fs")

Mika Havela's avatar
Mika Havela committed
7 8
-- Set variables
local packagename = "fetchmail"
9 10 11 12
local processname = "fetchmail"
local configfile = "/root/.fetchmailrc"
local config = {}

Mika Havela's avatar
Mika Havela committed
13 14
-- ################################################################################
-- LOCAL FUNCTIONS
15 16 17 18 19 20 21 22

local function getloglevels()
	local loglevels = {}
	for i=1,8 do
		table.insert(loglevels,i)
	end
	return loglevels
end
Mika Havela's avatar
Mika Havela committed
23

24 25 26 27 28
local function getmethods()
	local methods = {"pop3","imap","pop3domain", }
	return methods
end

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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
77 78
end

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
local function read_config()
--[[
imap/pop3 lines are:
1    2      3        4       5  6   7        8       9        10
poll <host> protocol <proto> no dns username <uname> password <passwd>
11 12             13       14         15 16      17
is <localmailbox> smtphost <desthost> no rewrite fetchall

pop3domain lines are:
1    2      3             4        5        6    7  8   9        10      11       12
poll <host> localdomainst <domain> protocol pop3 no dns username <uname> password <passwd>
13 14 15   16       17         18          19       20 21      22
to *  here smtphost <desthost> smtpaddress <domain> no rewrite fetchall

etrn is:
1    2      3        4    5          6
pool <host> protocol etrn smtpdomain <mydomain>
--]]
97
	local mailboxes = {}
98 99 100 101 102 103
	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
104
		if (string.match(v, "^%s*#Begin Fetchmail")) then valid=1 end
105
		if (valid) then
106
			-- 
107 108 109 110 111 112
			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] = {}
113
			configcontent[k]["DISABLED"] = DISABLED
114 115 116 117 118 119 120 121 122
			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 
123
				table.insert(mailboxes,getmailboxes(configcontent[k])) 
124 125 126 127 128 129
			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] = {}
130
			configcontent[k]["DISABLED"] = DISABLED
131 132 133
			for kk,vv in pairs(val) do
				configcontent[k][kk] = tostring(string.match(v,"^%s*" .. string.rep("%S*%s*",vv-1) .. "(%S*)%s*"))
			end
134 135
			-- Display this config as Method=pop3domain (as the current options in the view)
			configcontent[k]["METHOD"] = "pop3domain"
136 137 138 139 140 141
			-- 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 
142
				table.insert(mailboxes,getmailboxes(configcontent[k])) 
143 144 145 146 147 148
			end

			-- Set parameters for etrn
			local val = {ETRNSMTPHOST=2, CHECK=4, ETRNDOMAIN=6}
			local configcontent = {}
			configcontent[k] = {}
149
			configcontent[k]["DISABLED"] = DISABLED
150 151 152 153 154 155 156 157 158 159 160 161
			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] = {}
162
			configcontent[k]["DISABLED"] = DISABLED
163 164 165 166 167 168 169 170 171
			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
172
		if (string.match(v, "^%s*#End Fetchmail")) then valid=nil end
173
	end
174 175
	-- Create one empty record so that user can add settings
	table.insert(mailboxes,getmailboxes({})) 
176
	return mailboxes,configcontent_postmaster,configcontent_etrn
177 178

end
Mika Havela's avatar
Mika Havela committed
179

180 181 182
-- ################################################################################
-- PUBLIC FUNCTIONS

183 184
function startstop_service(action)
	return modelfunctions.startstop_service(processname, action)
185 186 187
end

function getstatus()
188
	return modelfunctions.getstatus(processname, packagename, "Fetchmail Status")
189 190 191
end

function get_filedetails()
192 193 194
	-- FIXME - validation
	return modelfunctions.getfiledetails(configfile)
end
195

196 197 198 199
function update_filecontent(filedetails)
	filedetails.value.filename.value = configfile
	-- FIXME - validation
	return modelfunctions.setfiledetails(filedetails)
200
end
201

202 203
function getconfig()
	local config = {}
204
	local mailboxes,configcontent_postmaster,configcontent_etrn = read_config()
205
	if not (fs.is_file(configfile)) then
206 207 208
		config["configfile"] = "Config file '".. configfile .. "' is missing!"
	end
	
209 210 211 212 213 214 215
	config["debug"] = cfe({ 
		name="debug",
		label = "Debug info",
		type = "longtext",
		value = configcontent,
		})

216 217 218 219 220 221 222 223 224 225 226
	-- 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",
227
		value = mailboxes,
228 229 230 231
		})
	config["postmaster"] = cfe({ 
		name="postmaster",
		label = "Postmaster",
232
		value = configcontent_postmaster["POSTMASTER"],
233 234 235 236
		})
	config["etrnremote"] = cfe({ 
		name="etrnremote",
		label = "Remote server",
237
		value = configcontent_etrn["ETRNSMTPHOST"],
238 239 240 241
		})
	config["etrnquedomain"] = cfe({ 
		name="etrnquedomain",
		label = "Queued domain",
242
		value = configcontent_etrn["ETRNDOMAIN"],
243 244 245 246 247
		})

	return config
end