Compare commits

...

3 Commits

Author SHA1 Message Date
Bram van den Heuvel b1037a815a Add Matrix API 2024-02-20 10:57:29 +01:00
Bram van den Heuvel d6e0486188 Expose JSON 2024-02-20 10:56:43 +01:00
Bram van den Heuvel a847ea1b3b Add config.lua 2024-02-20 01:06:54 +01:00
4 changed files with 161 additions and 1 deletions

41
config.lua Normal file
View File

@ -0,0 +1,41 @@
config = {
-- The user one expects to log in as
user = "@alice:example.org",
-- The URL where the user communicates with the Matrix homeserver
url = "https://matrix.example.org",
-- Room ID that the Minetest server bridges to
room_id = "!room_id:example.org",
-- Access token to access the Matrix homeserver
access_token = "",
-- Average timeout duration
sync_timeout = 30
}
local function verify(key, expected_type, default_value)
if config == nil then
error("There is no config variable available!")
elseif config[key] == nil then
error("Config key " .. key .. " has not been set!")
elseif type(config[key]) ~= expected_type then
error(
"Expected config key " .. key .. " to be of type " ..
expected_type .. " but found type " .. type(config[key])
)
elseif config[key] == default_value then
error(
"Config key " .. key .. " is still the default (example) value. " ..
"This implies that you have not configured it yet."
)
end
end
verify("user", "string", "@alice:example.org")
verify("url", "string", "https://matrix.example.org")
verify("room_id", "string", "!room_id:example.org")
verify("access_token", "string", "")
verify("sync_timeout", "number", nil)

@ -1 +1 @@
Subproject commit dbf4b2dd2eb7c23be2773c89eb059dadd6436f94
Subproject commit 261adde7a76687db142641d50cf4ed29873ec476

43
lua/send.lua Normal file
View File

@ -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>" .. strong_text .. "</strong> " .. small_text,
callback
)
end
function matrix.say(http_api)
return function(name, message)
return matrix.send(name .. ":", message)
end
end

76
lua/sync.lua Normal file
View File

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