summaryrefslogtreecommitdiffstats
path: root/postfix-model.lua
blob: 8676ecd902ab8c115770fef5184b23350fd02b21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
require("fs")
require("format")

-- Set variables
local processname = "postfix"
local packagename = "postfix"
local baseurl = "/etc/postfix/"
local filelist = {baseurl.."main.cf", baseurl.."master.cf", baseurl.."saslpass"}

local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "

-- ################################################################################
-- LOCAL FUNCTIONS

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service(action)
	return modelfunctions.startstop_service(processname, action, {"start", "stop", "restart", "reload"})
end

function getstatus()
	local status = modelfunctions.getstatus(processname, packagename, "Postfix Status")

	-- Enabled status is unique for postfix
	-- Look for pid file stored in queue_directory .. /pid/
	local config = format.parse_ini_file(fs.read_file(baseurl.."main.cf") or "", "") or {}
	if config.queue_directory then
		local pidfiles = fs.find_files_as_array(".*\.pid", config.queue_directory.."/pid/")
		if pidfiles and pidfiles[1] then
			local file = pidfiles[1]
			-- check to see if there's a matching proc directory and that it was created slightly after the pid file
			-- this allows us to avoid the problem with proc numbers wrapping
			local tmp = string.match(fs.read_file(file) or "", "%d+")
			if tmp then
				local dir = "/proc/" .. tmp
				filetime = posix.stat(file, "ctime")
				dirtime = posix.stat(dir, "ctime")
				if dirtime and (tonumber(dirtime) - tonumber(filetime) < 100) then
					status.value.status.value = "Running"
				end
			end
		end
	end

	return status
end

function getstatusdetails()
	return cfe({ type="longtext", value="", label="Postfix Status Details" })
end

function getfilelist()
	local listed_files = {}

	for i,name in ipairs(filelist) do
		local filedetails = fs.stat(name) or {}
		table.insert ( listed_files , {filename=name, mtime=filedetails.mtime or "---", filesize=filedetails.size or "0"} )
	end

	table.sort(listed_files, function (a,b) return (a.filename < b.filename) end )

	return cfe({ type="list", value=listed_files, label="Postfix File List" })
end

function getfiledetails(filename)
	return modelfunctions.getfiledetails(filename, filelist)
end

function updatefiledetails(filedetails)
	return modelfunctions.setfiledetails(filedetails, filelist)
end