minetest.request_insecure_environment() ssb = dofile(minetest.get_modpath("scuttlebutt") .. "/ssb.lua") chat = {} function ssb_poll(pname, value) local content = { id = value.me, sequence = value.sequence, 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 function ssb_send(player, key, message) if key == "" or message == "" then return "error: empty key or message!" end local pname = player:get_player_name() message = string.format("Minetest bot here. %s says: %s", pname, message) local content = { type = "post", text = message, recps = {key}, } local ret, err = ssb:publish(content) if err then 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 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).text 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_chatcommand("ssb", { params = "<@key> [message]", description = "Scuttlebutt Chat", privs = { ssb = true, }, func = function(name, param) local player = minetest.get_player_by_name(name) local key, message = string.match(param, "^(@[^ ]+) *(.*)$") if not key or key == "" then return true, "error: could not parse key" elseif not message or message == "" then local settings = minetest.settings:to_table() local ip = settings.server_address or "127.0.0.1" local port = settings.port or 30000 message = "Please join me on minetest: " .. ip .. ":" .. port end return true, ssb_send(player, key, message) end })