msg = ... --print("DEBUG: got message:", msg) ipcmsg = require("cmsgpack") ipcmsg.encode = ipcmsg.pack ipcmsg.decode = ipcmsg.unpack function ret_error(errmsg) io.stderr:write("ERROR: "..tostring(errmsg).."\n") return ipcmsg.encode{ status = false, errmsg = errmsg} end function ret_success(result) return ipcmsg.encode{ status = true, errmsg = "success", result = result} end req = ipcmsg.decode(msg) --print("DEBUG: msg decoded") -- path must be absolute for production so users cannot load scripts from -- non secured dirs modules_path = "./modules/" if type(req.mod) ~= "string" then return ret_error("mod is missing in message or is bad format") end if type(req.func) ~= "string" then return ret_error("func is missing in message or is wrong format") end -- make sure we dont have any path elements in modname so we cannot pass -- modnames like '../myevilmod' mfile = modules_path..string.gsub(req.mod, ".*/", "")..".lua" -- load the module m = dofile(mfile) --print("DEBUG: mfile:", mfile) --print("DEBUG: '"..req.func.."' type:", type(m[req.func])) -- check that the func we want exists if type(m[req.func]) ~= "function" then ret_error(func..": not a function in '".. mfile .."'") end -- TODO: check permissions here print("Calling '"..req.mod.."."..req.func.."'") --print("DEBUG: args:", req.args) -- execute the func and pack the return values into a table result = { m[req.func](unpack(req.args)) } --result = { m[func](unpack(req.args or {})) } --print("DEBUG: result:", result) return ret_success(result)