blob: 41a02fb7dd71041b109bb3d1c506da12f81855c9 (
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
|
--print("DEBUG: got message:", msg)
--ipcmsg = require("json")
ipcmsg = require("cmsgpack")
ipcmsg.encode = ipcmsg.pack
ipcmsg.decode = ipcmsg.unpack
ml = require("ml")
modules_path = "../modules/"
mods = {}
local function ret_error(errmsg)
io.stderr:write("ERROR: "..tostring(errmsg).."\n")
return ipcmsg.encode{ status = false, errmsg = errmsg}
end
local function ret_success(...)
return ipcmsg.encode{ status = true, errmsg = "success",
result = {...} }
end
reqhandler = {}
function reqhandler.load(req)
-- make sure we dont have any path elements in modname so we cannot pass
-- modnames like '../myevilmod'
local file = modules_path..string.gsub(req.modname, ".*/", "")..".lua"
mods[req.modidx] = dofile(file)
if mods[req.modidx] == nil then
return ret_error(file)
end
return true
end
function reqhandler.call(req)
if req.modidx == nil or req.func == nil then
return ret_error("failed to call function "..tostring(req.func))
end
local m = mods[req.modidx]
-- 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("DEBUG: Calling mods["..req.modidx.."]."..req.func)
-- execute the func and pack the return values into a table
return m[req.func](unpack(req.args))
end
function handler(msg)
local req = ipcmsg.decode(msg)
-- print("DEBUG: req="..ml.tstring(req))
if type(req.type) ~= "string" then
return ret_error("request type missing")
end
if type(reqhandler[req.type]) == "function" then
return ret_success(reqhandler[req.type](req))
end
return ret_error("no handler for request type "..req.type)
end
|