Check token before sync
This commit is contained in:
parent
4bd0e718a2
commit
5dfcf905f4
15
init.lua
15
init.lua
@ -49,7 +49,7 @@ function MatrixChat:minechat(data)
|
|||||||
end
|
end
|
||||||
minetest.log("action", "matrix_bridge - sync'd and found new messages")
|
minetest.log("action", "matrix_bridge - sync'd and found new messages")
|
||||||
for i, event in ipairs(events) do
|
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
|
and event.sender ~= self.userid then
|
||||||
local message = event.sender .. ": " .. event.content.body
|
local message = event.sender .. ": " .. event.content.body
|
||||||
minetest.log("action", message)
|
minetest.log("action", message)
|
||||||
@ -78,8 +78,11 @@ function MatrixChat:get_sync_table(timeout)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function MatrixChat:sync(timeout)
|
function MatrixChat:sync(timeout)
|
||||||
|
if self.token == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
http.fetch(MatrixChat:get_sync_table(timeout),
|
http.fetch(MatrixChat:get_sync_table(timeout),
|
||||||
function(res)
|
function(res)
|
||||||
if res == nil then -- received nothing from server
|
if res == nil then -- received nothing from server
|
||||||
minetest.log("error", "matrix_bridge - sync response is nil")
|
minetest.log("error", "matrix_bridge - sync response is nil")
|
||||||
elseif res.code == 0 then
|
elseif res.code == 0 then
|
||||||
@ -102,7 +105,7 @@ function MatrixChat:login()
|
|||||||
local u = self.server .."/_matrix/client/r0/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 d = '{"type":"m.login.password","password":"'.. self.password ..'","identifier":{"type":"m.id.user","user":"'.. self.username ..'"}}'
|
||||||
local h = {"Content-Type: application/json"}
|
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)
|
function(res)
|
||||||
if res.code == 200 then
|
if res.code == 200 then
|
||||||
minetest.log("action", res.data)
|
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 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 h = {"Content-Type: application/json", "Authorization: Bearer " .. self.token}
|
||||||
local d = minetest.write_json({msgtype="m.text", body=msg})
|
local d = minetest.write_json({msgtype="m.text", body=msg})
|
||||||
http.fetch({url=u, method="PUT", extra_headers=h, data=d},
|
http.fetch({url=u, method="PUT", extra_headers=h, data=d},
|
||||||
function(res)
|
function(res)
|
||||||
if res.code == 200 then
|
if res.code == 200 then
|
||||||
local data = minetest.parse_json(res.data)
|
local data = minetest.parse_json(res.data)
|
||||||
minetest.log("action", "got " .. data["event_id"])
|
minetest.log("action", "got " .. data["event_id"])
|
||||||
@ -200,7 +203,7 @@ minetest.register_chatcommand("matrix", {
|
|||||||
privs = {
|
privs = {
|
||||||
matrix = true
|
matrix = true
|
||||||
},
|
},
|
||||||
func = function(name, param)
|
func = function(name, param)
|
||||||
if param == "sync" then -- test sync as called from login
|
if param == "sync" then -- test sync as called from login
|
||||||
MatrixChat:sync()
|
MatrixChat:sync()
|
||||||
return true, "[matrix_bridge] command: sync"
|
return true, "[matrix_bridge] command: sync"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user