From 704fe630cc5a715111ea5c7d85e093d9e75431e3 Mon Sep 17 00:00:00 2001 From: - <-> Date: Tue, 25 Jan 2022 18:01:04 -0800 Subject: [PATCH] Using auth. header --- init.lua | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/init.lua b/init.lua index 69112bb..aa7e1db 100644 --- a/init.lua +++ b/init.lua @@ -14,8 +14,8 @@ end local token = nil local txid = 0 -- used for msync() -local since = nil -local last_batch = nil +local since = nil +local eventid = nil local function mchat(data) if data == nil then @@ -57,16 +57,17 @@ end local function msync(s) -- 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 param1 = "?access_token=" .. token + local param2 = "?full_state=false" local param3 = "&timeout=5000" - local u = MATRIX_SERVER.."/_matrix/client/r0/sync" .. param1 .. param2 -- .. param3 + local u = MATRIX_SERVER.."/_matrix/client/r0/sync" .. param2 .. param3 if s == nil then -- first time sync -- do nothing for now else -- second time sync -> append since parameter u = u .. "&since="..s end - http.fetch({url=u, method="GET"}, + local h = {"Authorization: Bearer " .. token} + http.fetch({url=u, method="GET", extra_headers=h}, function (res) if res == nil then -- received nothing from server minetest.log("error", "matrix_bridge - sync response is nil") @@ -77,8 +78,7 @@ local function msync(s) local response = minetest.parse_json(res.data) if response ~= nil then mchat(response) - since = response.next_batch - last_batch = response.prev_batch + since = response.next_batch end end end @@ -105,15 +105,23 @@ local function mlogin() end mlogin() -function send_message(msg) - txid = txid + 1 - http.fetch({ - url = MATRIX_SERVER.."/_matrix/client/r0/rooms/"..MATRIX_ROOM.."/send/m.room.message/"..txid.."?access_token="..token, - method = "PUT", - extra_headers = {"Content-Type: application/json"}, - data = minetest.write_json({msgtype="m.text", body=msg}) - }, function(res) - end) +local function send_message(msg) + txid = txid + 1 + local u = MATRIX_SERVER.."/_matrix/client/r0/rooms/"..MATRIX_ROOM.."/send/m.room.message/" .. txid -- ?access_token=..token + local h = {"Content-Type: application/json", "Authorization: Bearer " .. token} + local d = minetest.write_json({msgtype="m.text", body=msg}) + 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"]) + eventid = data["event_id"] + elseif res.code == 401 then + minetest.log("error", "matrix_bridge - not authorized to send messages") + elseif res.code == 404 then + minetest.log("error", "matrix_bridge - could not find endpoint for send") + end + end) end -- http.fetch({ @@ -135,10 +143,11 @@ end local GLOBAL_STEPS = 0 minetest.register_globalstep(function(dtine) -- print(GLOBAL_STEPS) --- if GLOBAL_STEPS == 4 then + local GLOBAL_THRESHHOLD = 50 + if GLOBAL_STEPS == GLOBAL_THRESHHOLD then msync(since) --- end --- GLOBAL_STEPS = (GLOBAL_STEPS + 1) % 5 + end + GLOBAL_STEPS = (GLOBAL_STEPS + 1) % (GLOBAL_THRESHHOLD+1) end) minetest.register_chatcommand("msync", {