Changeset

846:5ddc43ce8993

mod_auth_external: Work even when the LuaProcessCall library isn't available.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 13 Oct 2012 02:33:06 +0500
parents 845:1c14edca74a4
children 847:1c9a3454eb43
files mod_auth_external/mod_auth_external.lua
diffstat 1 files changed, 48 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/mod_auth_external/mod_auth_external.lua	Sat Oct 06 19:25:53 2012 +0500
+++ b/mod_auth_external/mod_auth_external.lua	Sat Oct 13 02:33:06 2012 +0500
@@ -12,7 +12,7 @@
 
 local nodeprep = require "util.encodings".stringprep.nodeprep;
 --local process = require "process";
-local lpc = require "lpc";
+local lpc; pcall(function() lpc = require "lpc"; end);
 
 local config = require "core.configmanager";
 local log = module._log;
@@ -26,37 +26,56 @@
 local jid_bare = require "util.jid".bare;
 local new_sasl = require "util.sasl".new;
 
---local proc;
-local pid;
-local readfile;
-local writefile;
-
 local function send_query(text)
-	if pid and lpc.wait(pid,1) ~= nil then
-    	    log("debug","error, process died, force reopen");
-	    pid=nil;
-	end
-	if not pid then
-		log("debug", "Opening process " .. command);
-		-- proc = process.popen(command);
-		pid, writefile, readfile = lpc.run(command);
+	local tmpname = os.tmpname();
+	local tmpfile = io.open(tmpname, "wb");
+	tmpfile:write(text);
+	tmpfile:close();
+	local p = io.popen(command.." < "..tmpname, "r");
+	local result;
+	if script_type == "ejabberd" then
+		result = p:read(4);
+	elseif script_type == "generic" then
+		result = p:read();
 	end
-	-- if not proc then
-	if not pid then
-		log("debug", "Process failed to open");
-		return nil;
-	end
-	-- proc:write(text);
-	-- proc:flush();
+	os.remove(tmpname);
+	p:close();
+	return result;
+end
+
+if lpc then
+	--local proc;
+	local pid;
+	local readfile;
+	local writefile;
 
-	writefile:write(text);
-	writefile:flush();
-	if script_type == "ejabberd" then
-		-- return proc:read(4); -- FIXME do properly
-		return readfile:read(4); -- FIXME do properly
-	elseif script_type == "generic" then
-		-- return proc:read(1);
-		return readfile:read();
+	function send_query(text)
+		if pid and lpc.wait(pid,1) ~= nil then
+	    	    log("debug","error, process died, force reopen");
+		    pid=nil;
+		end
+		if not pid then
+			log("debug", "Opening process " .. command);
+			-- proc = process.popen(command);
+			pid, writefile, readfile = lpc.run(command);
+		end
+		-- if not proc then
+		if not pid then
+			log("debug", "Process failed to open");
+			return nil;
+		end
+		-- proc:write(text);
+		-- proc:flush();
+
+		writefile:write(text);
+		writefile:flush();
+		if script_type == "ejabberd" then
+			-- return proc:read(4); -- FIXME do properly
+			return readfile:read(4); -- FIXME do properly
+		elseif script_type == "generic" then
+			-- return proc:read(1);
+			return readfile:read();
+		end
 	end
 end