Check token before sync

This commit is contained in:
Pascal Engélibert 2022-01-31 14:29:19 +01:00
parent 4bd0e718a2
commit 5dfcf905f4
Signed by: tuxmain
GPG Key ID: 3504BC6D362F7DCA
1 changed files with 9 additions and 6 deletions

View File

@ -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"