From b84a1e82b71ba6a6ac1bf2b599c311c4c024cd08 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Mon, 1 Jun 2009 10:33:19 +0000 Subject: Modified parsing of config to handle multiline entries and find all database files for postmap. --- postfix-model.lua | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'postfix-model.lua') diff --git a/postfix-model.lua b/postfix-model.lua index 33d4520..b54bce4 100644 --- a/postfix-model.lua +++ b/postfix-model.lua @@ -18,6 +18,21 @@ local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin -- ################################################################################ -- LOCAL FUNCTIONS +local getconfig = function() + -- the postfix config is like an ini file, but it allows multi-line entries + -- so, we convert and then parse like ini file + local content = fs.read_file(baseurl.."main.cf") or "" + -- lines that begin with whitespace and first non-whitespace character is not '#' + -- are continuations, so delete the carriage return + -- have to delete blank and comment lines first, because could be before continuation line + -- I've seen no documentation on inline comments, so user beware + content = string.gsub(content, "\n+", "\n") + content = string.gsub(content, "\n%s*#[^\n]*", "") + content = string.gsub(content, "\n[^%S\n]+([^#])", " %1") + + return format.parse_ini_file(content, "") or {}, content +end + -- ################################################################################ -- PUBLIC FUNCTIONS @@ -30,7 +45,7 @@ function getstatus() -- 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 {} + local config = getconfig() if config.queue_directory then local pidfiles = fs.find_files_as_array(".*\.pid", config.queue_directory.."/pid/") if pidfiles and pidfiles[1] then @@ -127,16 +142,18 @@ function rebuild_databases() table.insert(result.value, " See logfile for possible errors") local cmd,f,cmdresult -- parse main.cf looking for hash files - local config = format.parse_ini_file(fs.read_file("/etc/postfix/main.cf") or "", "") or {} - for name,val in pairs(config) do - -- run postmap on files - local filename = string.match(val, "hash:(.*)") - if filename and not string.find(filename, aliasesfile) then - cmd = "postmap "..filename - table.insert(result.value, "Running: "..cmd) - f = io.popen(format.escapespecialcharacters(path..cmd)) - table.insert(result.value, f:read("*a")) - f:close() + local config, content = getconfig() + for i,db in ipairs({"btree", "cdb", "dbm", "hash", "sdbm"}) do + for filename in string.gmatch(content, "[%s=]("..db..":%S+)") do + filename = string.gsub(filename, ",$", "") + -- run postmap on file + if filename and not string.find(filename, aliasesfile) then + cmd = "postmap "..filename + table.insert(result.value, "Running: "..cmd) + f = io.popen(format.escapespecialcharacters(path..cmd)) + table.insert(result.value, f:read("*a")) + f:close() + end end end -- finally, run newaliases -- cgit v1.2.3