2022-01-23 02:31:16 +01:00
|
|
|
|
|
|
|
print("Matrix bridge loaded.")
|
|
|
|
|
2022-01-21 15:16:11 +01:00
|
|
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
|
|
|
|
2022-01-21 16:02:39 +01:00
|
|
|
dofile(modpath.."/config.lua")
|
2022-01-21 15:16:11 +01:00
|
|
|
dofile(modpath.."/debug.lua")
|
|
|
|
|
|
|
|
local http = minetest.request_http_api()
|
|
|
|
if http == nil then
|
|
|
|
error("Please add matrix_bridge to secure.http_mods")
|
|
|
|
end
|
|
|
|
|
|
|
|
local token = nil
|
2022-01-23 02:31:16 +01:00
|
|
|
local txid = 0
|
|
|
|
-- used for msync()
|
|
|
|
local since = nil
|
|
|
|
|
|
|
|
local function msync()
|
|
|
|
-- optimization note: request more recent instead of unfiltered req
|
|
|
|
-- local param1 = '&filter={\"room\":{\"timeline\":{\"limit\":1}}}'
|
|
|
|
local param1 = "?access_token=" .. token
|
|
|
|
local param2 = "&full_state=false"
|
|
|
|
local u = MATRIX_SERVER.."/_matrix/client/r0/sync" .. param1 .. param2
|
|
|
|
if since == nil then -- first time sync
|
|
|
|
-- do nothing for now
|
|
|
|
else -- second time sync -> append since parameter
|
|
|
|
u = u + "&since="..since
|
|
|
|
end
|
|
|
|
http.fetch({url=u},
|
|
|
|
function (res)
|
|
|
|
if res == nil then
|
|
|
|
print("Matrix - sync response is nil")
|
|
|
|
else
|
|
|
|
local response = minetest.parse_json(res.data)
|
|
|
|
since = response.next_batch
|
|
|
|
minetest.log("action", "sync state:" .. since)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function mlogin()
|
|
|
|
local u = MATRIX_SERVER.."/_matrix/client/r0/login"
|
|
|
|
local d = '{"type":"m.login.password","password":"'..MATRIX_PASSWORD..'","identifier":{"type":"m.id.user","user":"'..MATRIX_USERNAME..'"}}'
|
|
|
|
local h = {"Content-Type: application/json"}
|
|
|
|
http.fetch({url=u, method="POST", extra_headers=h, data=d},
|
|
|
|
function(res)
|
|
|
|
if res.code == 200 then
|
|
|
|
minetest.log("action", res.data)
|
|
|
|
local data = minetest.parse_json(res.data)
|
|
|
|
token = data.access_token
|
|
|
|
minetest.log("action", "Matrix authenticated")
|
|
|
|
msync()
|
|
|
|
else
|
|
|
|
minetest.log("error", to_string(res))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
mlogin()
|
2022-01-21 15:16:11 +01:00
|
|
|
|
|
|
|
function send_message(msg)
|
|
|
|
txid = txid + 1
|
|
|
|
http.fetch({
|
2022-01-21 16:02:39 +01:00
|
|
|
url = MATRIX_SERVER.."/_matrix/client/r0/rooms/"..MATRIX_ROOM.."/send/m.room.message/"..txid.."?access_token="..token,
|
2022-01-21 15:16:11 +01:00
|
|
|
method = "PUT",
|
|
|
|
extra_headers = {"Content-Type: application/json"},
|
|
|
|
data = minetest.write_json({msgtype="m.text", body=msg})
|
|
|
|
}, function(res)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-01-23 02:31:16 +01:00
|
|
|
-- http.fetch({
|
|
|
|
-- url = MATRIX_SERVER.."/_matrix/client/r0/login",
|
|
|
|
-- method = "POST",
|
|
|
|
-- extra_headers = {"Content-Type: application/json"},
|
|
|
|
-- data = '{"type":"m.login.password","password":"'..MATRIX_PASSWORD..'","identifier":{"type":"m.id.user","user":"'..MATRIX_USERNAME..'"}}'
|
|
|
|
-- }, function(res)
|
|
|
|
-- if res.code == 200 then
|
|
|
|
-- minetest.log("action", res.data)
|
|
|
|
-- local data = minetest.parse_json(res.data)
|
|
|
|
-- token = data.access_token
|
|
|
|
-- minetest.log("action", "Matrix authenticated")
|
|
|
|
-- else
|
|
|
|
-- minetest.log("error", to_string(res))
|
|
|
|
-- end
|
|
|
|
-- end)
|
|
|
|
|
|
|
|
|
2022-01-21 15:16:11 +01:00
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if token then
|
|
|
|
send_message("*** "..name.." joined the game")
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player, timed_out)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if token then
|
|
|
|
send_message("*** "..name.." left the game"..
|
|
|
|
(timed_out and " (Timed out)" or ""))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_chat_message(function(name, message)
|
|
|
|
if token == nil
|
|
|
|
or message:sub(1, 1) == "/"
|
|
|
|
or message:sub(1, 5) == "[off]"
|
|
|
|
or (not minetest.check_player_privs(name, {shout=true})) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local nl = message:find("\n", 1, true)
|
|
|
|
if nl then
|
|
|
|
message = message:sub(1, nl - 1)
|
|
|
|
end
|
|
|
|
send_message("<"..name.."> "..message)
|
|
|
|
end)
|
2022-01-23 02:31:16 +01:00
|
|
|
|
|
|
|
|