From 5dfcf905f4094380f8d723eb5c01bfabdaabca0f Mon Sep 17 00:00:00 2001 From: tuxmain Date: Mon, 31 Jan 2022 14:29:19 +0100 Subject: [PATCH] Check token before sync --- init.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index 0087d9a..66fa0ab 100644 --- a/init.lua +++ b/init.lua @@ -49,7 +49,7 @@ function MatrixChat:minechat(data) end minetest.log("action", "matrix_bridge - sync'd and found new messages") for i, event in ipairs(events) do - if event.type == "m.room.message" + if event.type == "m.room.message" and event.sender ~= self.userid then local message = event.sender .. ": " .. event.content.body minetest.log("action", message) @@ -78,8 +78,11 @@ function MatrixChat:get_sync_table(timeout) end function MatrixChat:sync(timeout) + if self.token == nil then + return + end http.fetch(MatrixChat:get_sync_table(timeout), - function(res) + function(res) if res == nil then -- received nothing from server minetest.log("error", "matrix_bridge - sync response is nil") elseif res.code == 0 then @@ -102,7 +105,7 @@ function MatrixChat:login() local u = self.server .."/_matrix/client/r0/login" local d = '{"type":"m.login.password","password":"'.. self.password ..'","identifier":{"type":"m.id.user","user":"'.. self.username ..'"}}' local h = {"Content-Type: application/json"} - http.fetch({url=u, method="POST", extra_headers=h, data=d}, + http.fetch({url=u, method="POST", extra_headers=h, post_data=d}, function(res) if res.code == 200 then minetest.log("action", res.data) @@ -132,8 +135,8 @@ function MatrixChat:send(msg) local u = self.server .."/_matrix/client/r0/rooms/".. self.room .."/send/m.room.message/" .. txid -- ?access_token=..token local h = {"Content-Type: application/json", "Authorization: Bearer " .. self.token} local d = minetest.write_json({msgtype="m.text", body=msg}) - http.fetch({url=u, method="PUT", extra_headers=h, data=d}, - function(res) + http.fetch({url=u, method="PUT", extra_headers=h, data=d}, + function(res) if res.code == 200 then local data = minetest.parse_json(res.data) minetest.log("action", "got " .. data["event_id"]) @@ -200,7 +203,7 @@ minetest.register_chatcommand("matrix", { privs = { matrix = true }, - func = function(name, param) + func = function(name, param) if param == "sync" then -- test sync as called from login MatrixChat:sync() return true, "[matrix_bridge] command: sync"