poppy-server/rpc.lua

107 lines
2.5 KiB
Lua

local rpc = {
PROTOCOL_VERSION = "003"
}
function rpc.clean(record)
assert(type(record) == "string", "cleaned record is not a string! :" .. type(record))
return record:gsub("\31", "")
end
function rpc.print(record)
--local cleaned, _ = record:gsub("\31","|")
--print(cleaned)
end
function rpc.assert(...)
assert(...)
end
function rpc.validate(record)
rpc.print("Starting validation")
local errorMsg = false
local count
record, count = record:gsub("^poppyV" .. rpc.PROTOCOL_VERSION .."\31","")
if not count == 1 then
errorMsg = "unsupported version or message!"
record = nil
end
rpc.print("record is now ".. record)
local clientID, count = record:match("^%d-\31")
clientID = rpc.clean(clientID)
if not clientID then
errorMsg = "No clientID found!"
record = nil
else
record, count = record:gsub("^%d-\31","")
if count ~= 1 then
error("Failed to remove clientiD?")
end
end
rpc.print("record is now " .. record)
rpc.print("match clientID: " .. clientID)
rpc.print("ended validation")
return {
errorMsg = errorMsg,
clientID = tonumber(clientID),
version = rpc.PROTOCOL_VERSION,
record = record }
end
function rpc.unprivligedExecute(commands, record, ip, port)
assert(type(commands) == "table")
assert(type(record) == "string")
assert(ip)
assert(port)
rpc.print("exexute: " .. record)
local command = record:match("^.-\31")
if command then command = rpc.clean(command) end
record = record:gsub("^.-\31","")
if not command then
command = record
record = nil
end
rpc.assert(type(commands[command]) == "function", "Unknown command encountered!" .. command)
if record then
rpc.print("executing " .. command .. " with " .. record)
return commands[command](record, ip, port)
else
rpc.print("executing no arguments:" .. command)
return commands[command](ip, port)
end
end
function rpc.execute(commands, clientID, record)
assert(type(commands) == "table")
assert(type(record) == "string")
rpc.print("exexute: " .. record)
local command = record:match("^.-\31")
if command then command = rpc.clean(command) end
record = record:gsub("^.-\31","")
if not command then
command = record
record = nil
end
rpc.assert(type(commands[command]) == "function", "Unknown command encountered!" .. command)
if record then
rpc.print("executing " .. command .. " with " .. record)
return commands[command](clientID, record)
else
rpc.print("executing no arguments:" .. command)
return commands[command](clientID)
end
end
return rpc