Add sub-method handler. First attempt to unbox encrypted content
This commit is contained in:
parent
0171aa3c1d
commit
c4fd21a229
69
init.lua
69
init.lua
|
@ -1,11 +1,27 @@
|
||||||
minetest.request_insecure_environment()
|
minetest.request_insecure_environment()
|
||||||
ssb = dofile(minetest.get_modpath("scuttlebutt") .. "/ssb.lua")
|
ssb = dofile(minetest.get_modpath("scuttlebutt") .. "/ssb.lua")
|
||||||
|
chat = {}
|
||||||
|
|
||||||
function ssb_receive(key, json)
|
function ssb_poll(pname, value)
|
||||||
relation = "FROM JSON"
|
local content = {
|
||||||
message = key .. "said: " .. "FROM JSON"
|
id = value.me,
|
||||||
name = "FROM RELATION"
|
sequence = value.sequence,
|
||||||
minetest.chat_send_player(name, message)
|
limit = 1,
|
||||||
|
live = false,
|
||||||
|
old = true,
|
||||||
|
keys = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
local ret, err = ssb:createHistoryStream(content)
|
||||||
|
if err then
|
||||||
|
return (string.format("error happened: %s", err))
|
||||||
|
else
|
||||||
|
if ret == true then
|
||||||
|
-- early end of stream
|
||||||
|
ret = nil
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function ssb_send(player, key, message)
|
function ssb_send(player, key, message)
|
||||||
|
@ -23,14 +39,48 @@ function ssb_send(player, key, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
local ret, err = ssb:publish(content)
|
local ret, err = ssb:publish(content)
|
||||||
if (not err) then
|
if err then
|
||||||
return "ok: message sent."
|
|
||||||
-- return (string.format("ret: %s", json.encode(ret)))
|
|
||||||
else
|
|
||||||
return (string.format("error happened: %s", err))
|
return (string.format("error happened: %s", err))
|
||||||
|
else
|
||||||
|
minetest.log(json.encode(ret))
|
||||||
|
local v = {
|
||||||
|
id = ret.key,
|
||||||
|
sequence = ret.value.sequence + 1,
|
||||||
|
me = ret.value.author,
|
||||||
|
rcpt = key,
|
||||||
|
}
|
||||||
|
if chat[pname] == nil then
|
||||||
|
chat[pname] = {}
|
||||||
|
end
|
||||||
|
table.insert(chat[pname], v)
|
||||||
|
return "ok: message sent."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function reap()
|
||||||
|
for p, v in pairs(chat) do
|
||||||
|
minetest.log("polling responses for player: " .. p)
|
||||||
|
for i, c in ipairs(v) do -- crawl conversations
|
||||||
|
local ret, err = ssb_poll(p, c)
|
||||||
|
if err then
|
||||||
|
minetest.log(err)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if ret ~= nil then
|
||||||
|
if c.id == ret.value.previous and c.rcpt == ret.value.author then
|
||||||
|
table.remove(v, i) -- end conversation
|
||||||
|
local text = ssb:private():unbox(ret.value.content)
|
||||||
|
local author = "ssb: " .. string.sub(ret.value.author, 1, 7) .. "..."
|
||||||
|
minetest.chat_send_player(p, author .. " replied: " .. text)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
minetest.after(60, reap)
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.after(60, reap)
|
||||||
|
|
||||||
minetest.register_privilege("ssb", "Ability to chat through Scuttlebutt")
|
minetest.register_privilege("ssb", "Ability to chat through Scuttlebutt")
|
||||||
|
|
||||||
minetest.register_chatcommand("ssb", {
|
minetest.register_chatcommand("ssb", {
|
||||||
|
@ -49,7 +99,6 @@ minetest.register_chatcommand("ssb", {
|
||||||
local ip = settings.server_address or "127.0.0.1"
|
local ip = settings.server_address or "127.0.0.1"
|
||||||
local port = settings.port or 30000
|
local port = settings.port or 30000
|
||||||
message = "Please join me on minetest: " .. ip .. ":" .. port
|
message = "Please join me on minetest: " .. ip .. ":" .. port
|
||||||
-- return true, "error: could not parse message"
|
|
||||||
end
|
end
|
||||||
return true, ssb_send(player, key, message)
|
return true, ssb_send(player, key, message)
|
||||||
end
|
end
|
||||||
|
|
96
ssb.lua
96
ssb.lua
|
@ -21,6 +21,14 @@ local function read_manifest()
|
||||||
return js, nil
|
return js, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function method_to_json(meth)
|
||||||
|
local res = {}
|
||||||
|
for tok in string.gmatch(meth, "[^.]+") do
|
||||||
|
table.insert(res, tok)
|
||||||
|
end
|
||||||
|
return res
|
||||||
|
end
|
||||||
|
|
||||||
function do_read(conn, id, final)
|
function do_read(conn, id, final)
|
||||||
local buf, err = conn:receive(9)
|
local buf, err = conn:receive(9)
|
||||||
if string.len(buf) < 9 then
|
if string.len(buf) < 9 then
|
||||||
|
@ -42,7 +50,6 @@ function do_read(conn, id, final)
|
||||||
-- minetest.log("do_read: id : " .. id)
|
-- minetest.log("do_read: id : " .. id)
|
||||||
if id ~= 1 then
|
if id ~= 1 then
|
||||||
minetest.log("do_read: id should be 1 but is: " .. id)
|
minetest.log("do_read: id should be 1 but is: " .. id)
|
||||||
-- minetest.log("do_read: " .. string.byte(buf, 6) .. string.byte(buf, 7) .. string.byte(buf, 8) .. string.byte(buf, 9))
|
|
||||||
end
|
end
|
||||||
return conn:receive(len)
|
return conn:receive(len)
|
||||||
end
|
end
|
||||||
|
@ -79,10 +86,39 @@ function do_goodbye(conn)
|
||||||
return do_write(conn, "", "async", "buffer", 0, true)
|
return do_write(conn, "", "async", "buffer", 0, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function rpc_source(conn, meth, mtype, param)
|
||||||
|
local req = json.encode({
|
||||||
|
name = method_to_json(meth),
|
||||||
|
args = {param},
|
||||||
|
type = mtype,
|
||||||
|
})
|
||||||
|
|
||||||
|
local ret, err = do_write(conn, req, mtype, "json", 1, false)
|
||||||
|
if err then
|
||||||
|
minetest.log("do_write: " .. err)
|
||||||
|
return ret, err
|
||||||
|
end
|
||||||
|
|
||||||
|
ret, err = do_read(conn, 1, false)
|
||||||
|
if err then
|
||||||
|
minetest.log("do_read :" .. err)
|
||||||
|
return ret, err
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.log(ret)
|
||||||
|
do_goodbye(conn)
|
||||||
|
|
||||||
|
return ret, err
|
||||||
|
end
|
||||||
|
|
||||||
|
function rpc_sync(conn, meth, mtype, param)
|
||||||
|
return rpc_source(conn, meth, mtype, param)
|
||||||
|
end
|
||||||
|
|
||||||
function rpc_async(conn, meth, mtype, param)
|
function rpc_async(conn, meth, mtype, param)
|
||||||
-- wrap
|
-- wrap
|
||||||
local req = json.encode({
|
local req = json.encode({
|
||||||
name = meth,
|
name = method_to_json(meth),
|
||||||
args = {param},
|
args = {param},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -102,18 +138,14 @@ function rpc_async(conn, meth, mtype, param)
|
||||||
|
|
||||||
do_goodbye(conn)
|
do_goodbye(conn)
|
||||||
|
|
||||||
-- local ret = json.encode({
|
|
||||||
-- name = meth,
|
|
||||||
-- type = mtype,
|
|
||||||
-- arg = param,
|
|
||||||
-- })
|
|
||||||
-- local err = nil
|
|
||||||
return ret, err
|
return ret, err
|
||||||
end
|
end
|
||||||
|
|
||||||
function muxrpc(conn, meth, mtype, param)
|
function muxrpc(conn, meth, mtype, param)
|
||||||
local switch = {
|
local switch = {
|
||||||
async = rpc_async,
|
async = rpc_async,
|
||||||
|
source = rpc_source,
|
||||||
|
sync = rpc_sync,
|
||||||
-- TODO implement all types
|
-- TODO implement all types
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,15 +174,7 @@ local mt = {
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return function(s, ...)
|
local mux = function (k, mtype, ...)
|
||||||
local res, err = nil
|
|
||||||
|
|
||||||
if not manifest[k] then
|
|
||||||
return err, "method not found: " .. k
|
|
||||||
end
|
|
||||||
|
|
||||||
local mtype = manifest[k]
|
|
||||||
|
|
||||||
local params = {...}
|
local params = {...}
|
||||||
local js
|
local js
|
||||||
-- for key, val in ipairs(params) do
|
-- for key, val in ipairs(params) do
|
||||||
|
@ -174,17 +198,45 @@ local mt = {
|
||||||
-- minetest.log(ret)
|
-- minetest.log(ret)
|
||||||
if not err then
|
if not err then
|
||||||
if (type(ret) ~= "table") then
|
if (type(ret) ~= "table") then
|
||||||
res = json.decode(ret)
|
ret = json.decode(ret)
|
||||||
else
|
else
|
||||||
res = parse_msgs(ret)
|
ret = parse_msgs(ret)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
res = nil
|
|
||||||
err = ret
|
err = ret
|
||||||
|
ret = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
return res, err
|
return ret, err
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return function(s, ...)
|
||||||
|
if not manifest[k] then
|
||||||
|
return nil, "method not found: " .. k
|
||||||
|
end
|
||||||
|
|
||||||
|
local mtype = manifest[k]
|
||||||
|
|
||||||
|
if type(mtype) ~= "table" then
|
||||||
|
return mux(k, mtype, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
local submt = {
|
||||||
|
__index = function (self, subk)
|
||||||
|
return function (s, ...)
|
||||||
|
if mtype[subk] == nil then
|
||||||
|
return nil, "method not found: " .. k .. "." .. subk
|
||||||
|
end
|
||||||
|
|
||||||
|
return mux(k .. "." .. subk, mtype[subk], ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
local sub = {}
|
||||||
|
setmetatable(sub, submt)
|
||||||
|
return sub
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue