From b1037a815a02a24e775c41c898a2a39c72e55e74 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Tue, 20 Feb 2024 10:57:29 +0100 Subject: [PATCH] Add Matrix API --- lua/send.lua | 43 +++++++++++++++++++++++++++++ lua/sync.lua | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 lua/send.lua create mode 100644 lua/sync.lua diff --git a/lua/send.lua b/lua/send.lua new file mode 100644 index 0000000..e655573 --- /dev/null +++ b/lua/send.lua @@ -0,0 +1,43 @@ +function matrix.send_message(http_api, body, formatted_body, callback, txnId) + if txnId == nil then + txnId = os.time() + end + + http_api.fetch({ + url = ( + config.url .. "/_matrix/client/v3/rooms/" .. config.room_id .. + "/send/m.room.message/" .. txnId + ), + data = json.encode({ + msgtype = "m.text", + body = body, + format = "org.matrix.custom.html", + formatted_body = formatted_body + }), + extra_headers = { "Authorization: Bearer " .. config.access_token }, + method = "PUT" + }, function(result) + if result.code ~= 200 then + minetest.debug("Failed to send message to Matrix homeserver with code " .. result.code) + minetest.debug(result.data) + else + if callback ~= nil then + callback(json.decode(result.data)) + end + end + end) +end + +function matrix.send(http_api, strong_text, small_text, callback) + return matrix.send_message( + http_api, "**" .. strong_text .. "** " .. small_text, + "" .. strong_text .. " " .. small_text, + callback + ) +end + +function matrix.say(http_api) + return function(name, message) + return matrix.send(name .. ":", message) + end +end \ No newline at end of file diff --git a/lua/sync.lua b/lua/sync.lua new file mode 100644 index 0000000..c199e8c --- /dev/null +++ b/lua/sync.lua @@ -0,0 +1,76 @@ +sync_so_far = nil + +function matrix.sync(http_api, callback, global_callback) + local s = "" + if sync_so_far ~= nil then + s = "since=" .. sync_so_far .. "&" + end + + local url = ( config.url .. "/_matrix/client/v3/sync?" .. s .. + "full_state=false&set_presence=online&" .. + "timeout=" .. (config.sync_timeout * 1000) .. "&" + ) + + http_api.fetch({ + url = url, + method = "GET", + timeout = config.sync_timeout + 1, + extra_headers = { "Authorization: Bearer " .. config.access_token }, + multipart = false + }, function(result) + if result.code ~= 200 then + minetest.debug("Failed to sync with Matrix server; received code " .. result.code) + minetest.debug(result.data) + else + local answer = json.decode(result.data) + + if answer.next_batch ~= nil then + sync_so_far = answer.next_batch + end + + if answer.rooms == nil then + elseif answer.rooms.join == nil then + elseif callback == nil then + else + for room_id, room in pairs(answer.rooms.join) do + if room_id == config.room_id then + -- Iterate over old state event updates + if room.state == nil then + elseif room.state.events == nil then + else + for _, event in pairs(room.state.events) do + callback(event) + end + end + + -- Iterate over most recent timeline + if room.timeline == nil then + elseif room.timeline.events == nil then + else + for _, event in pairs(room.timeline.events) do + callback(event) + end + end + end + end + end + end + + global_callback(result) + end) +end + +local function sleep(duration) + local time = os.time() + + while os.time() < time + duration do + + end +end + +function matrix.sync_forever(http_api, callback) + matrix.sync(http_api, callback, function(result) + sleep(1) + matrix.sync_forever(http_api, callback) + end) +end \ No newline at end of file