yl_matterbridge/init.lua

193 lines
6.9 KiB
Lua
Raw Permalink Normal View History

2022-04-06 15:13:01 -07:00
-- Version 1.0.3
2022-02-24 18:09:00 -08:00
-- Author AliasAlreadyTaken
-- License MIT
-- Changelog
-- 0.0.1 First puny attempts
2022-03-04 03:09:10 -08:00
-- 0.0.2 Feature complete including settings
-- 1.0.0 Ready for release
-- 1.0.1 Bugfix https://gitea.your-land.de/your-land/bugtracker/issues/1646
2022-04-06 15:13:01 -07:00
-- 1.0.2 Bugfix settingtypes: bool instead of boolean
-- 1.0.3 Release to cdb
2022-02-24 18:09:00 -08:00
local mod_start_time = core.get_us_time()
core.log("action", "[MOD] yl_matterbridge loading")
yl_matterbridge = {}
2022-02-25 15:41:05 -08:00
--yl_matterbridge.error = {}
--yl_matterbridge.modstorage = core.get_mod_storage()
--yl_matterbridge.modpath = core.get_modpath("yl_matterbridge") .. DIR_DELIM
--yl_matterbridge.worldpath = core.get_worldpath() .. DIR_DELIM
2022-02-24 18:09:00 -08:00
yl_matterbridge.information = {}
2022-04-06 15:13:01 -07:00
yl_matterbridge.information.version = "1.0.3"
2022-02-24 18:09:00 -08:00
yl_matterbridge.information.author = "AliasAlreadyTaken"
yl_matterbridge.information.license = "MIT"
yl_matterbridge.information.name = "yl_matterbridge"
2022-02-25 19:26:21 -08:00
yl_matterbridge.information.source = "https://gitea.your-land.de/AliasAlreadyTaken/yl_matterbridge"
yl_matterbridge.information.additional =
"Sends and receives ingame chat to and from matterbridge https://github.com/42wim/matterbridge"
2022-02-24 18:09:00 -08:00
2022-04-25 22:59:47 -07:00
yl_matterbridge.enabled = true
2022-03-01 23:18:22 -08:00
local settings = {}
settings.address = core.settings:get("yl_matterbridge.address") or "127.0.0.1"
settings.port = core.settings:get("yl_matterbridge.port") or "4242"
settings.gateway = core.settings:get("yl_matterbridge.gateway") or "default"
settings.token = core.settings:get("yl_matterbridge.token") or ""
settings.debug = core.settings:get_bool("yl_matterbridge.debug", false) or false
2022-02-24 18:09:00 -08:00
local http = core.request_http_api()
2022-02-24 18:09:00 -08:00
2022-02-25 18:14:32 -08:00
assert(http, "[MOD] yl_matterbridge: Please add yl_matterbridge to secure.http_mods")
2022-02-25 15:00:38 -08:00
2022-03-01 23:18:22 -08:00
if settings.debug then
core.log("action", "[MOD] yl_matterbridge : settings = " .. dump(settings))
end
local post_headers = {"Content-Type: application/json"}
2022-03-01 23:18:22 -08:00
if settings.token and settings.token ~= "" then
table.insert(post_headers, "Authorization: Bearer " .. settings.token)
end
2022-03-01 23:18:22 -08:00
local url_send = "http://" .. settings.address .. ":" .. settings.port .. "/api/message"
local url_receive = "http://" .. settings.address .. ":" .. settings.port .. "/api/messages"
2022-02-24 18:57:26 -08:00
function send(user_name, message_text)
2022-04-25 22:59:47 -07:00
if yl_matterbridge.enabled == false then return false end
2022-03-01 23:18:22 -08:00
if settings.debug then
core.log(
"action",
"[MOD] yl_matterbridge : send user_name = " .. dump(user_name) .. ", message_text = " .. dump(message_text)
)
end
2022-02-24 18:09:00 -08:00
local timeout = 10
local data = {
text = message_text,
username = user_name,
2022-03-01 23:18:22 -08:00
gateway = settings.gateway
2022-02-24 18:09:00 -08:00
}
2022-02-24 18:57:26 -08:00
2022-02-24 18:09:00 -08:00
http.fetch(
{
url = url_send,
2022-02-24 18:09:00 -08:00
extra_headers = post_headers,
timeout = timeout,
post_data = core.write_json(data)
2022-02-24 18:09:00 -08:00
},
function(result)
2022-03-01 23:18:22 -08:00
if not result.completed or not result.succeeded or not result.code == 200 then
2022-02-25 18:14:32 -08:00
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
2022-02-24 18:09:00 -08:00
return false
2022-03-01 23:18:22 -08:00
else
if settings.debug then
local data = core.parse_json(result.data)
core.log(
"action",
"[MOD] yl_matterbridge : send result = " .. dump(result) .. ", data = " .. dump(data)
)
end
return true
2022-02-24 18:09:00 -08:00
end
end
)
end
2022-02-25 12:30:38 -08:00
local function receive()
2022-04-25 22:59:47 -07:00
if yl_matterbridge.enabled == false then return false end
2022-03-01 23:18:22 -08:00
if settings.debug then
core.log("action", "[MOD] yl_matterbridge : receive")
end
2022-02-24 18:57:26 -08:00
local timeout = 0
http.fetch(
{
url = url_receive,
2022-02-26 13:38:27 -08:00
extra_headers = post_headers,
2022-02-24 18:57:26 -08:00
timeout = timeout
},
function(result)
2022-03-01 23:18:22 -08:00
if not result.completed or not result.succeeded or not result.code == 200 then
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result (req) = " .. dump(result))
return false
else
local data = core.parse_json(result.data)
if not data then
if not next(data) then
if settings.debug then
core.log(
"error",
"[MOD] yl_matterbridge: receive/http.fetch failed. Result (data) = " .. dump(result)
)
end
end
return false
end
for _, dataset in ipairs(data) do
if dataset then
if settings.debug then
core.log("action", "[MOD] yl_matterbridge : dataset = " .. dump(dataset))
end
yl_matterbridge.receive_all_from_bridge(dataset)
else
core.log(
"error",
"[MOD] yl_matterbridge: receive/http.fetch failed. No dataset in returned data = " ..
dump(data)
)
end
end
end
2022-02-24 18:57:26 -08:00
end
)
end
2022-02-25 12:07:31 -08:00
-- API
-- Overwrite these function in your chat mod, if you want to govern sending and receveiving yourself
-- Don't forget to add an optional dependency to yl_matterbridge
function yl_matterbridge.receive_from_bridge(user_name, message_text, account_name)
core.chat_send_all("<" .. account_name .. "|" .. user_name .. "> " .. message_text)
2022-02-25 12:07:31 -08:00
end
2022-03-01 23:18:22 -08:00
-- Overwrite this function instead if you want to use other fields coming from the bridge.
function yl_matterbridge.receive_all_from_bridge(dataset)
if dataset.username and dataset.text and dataset.account then
local user_name = dataset.username
local message_text = dataset.text
local account_name = dataset.account
yl_matterbridge.receive_from_bridge(user_name, message_text, account_name)
else
core.log(
"error",
"[MOD] yl_matterbridge: receive/http.fetch failed. No dataset in returned data = " .. dump(dataset)
)
end
end
-- Call this function to feed into the bridge from your own mod, if you unset yl_matterbridge.chat_message
function yl_matterbridge.send_to_bridge(user_name, message_text)
2022-02-25 12:07:31 -08:00
send(user_name, message_text)
end
-- Set this function to yl_matterbridge.chat_message = function() end
-- if you call yl_matterbridge.send_to_bridge yourself
function yl_matterbridge.chat_message(user_name, message_text)
yl_matterbridge.send_to_bridge(user_name, message_text)
end
2022-02-25 12:07:31 -08:00
-- connect to Minetest
2022-02-27 19:39:56 -08:00
function yl_matterbridge.register_chat()
core.register_on_chat_message(yl_matterbridge.chat_message)
end
2022-02-25 12:07:31 -08:00
2022-03-01 23:18:22 -08:00
core.register_on_mods_loaded(yl_matterbridge.register_chat)
2022-02-25 12:07:31 -08:00
core.register_globalstep(receive)
2022-02-24 18:57:26 -08:00
2022-02-24 18:09:00 -08:00
local mod_end_time = (core.get_us_time() - mod_start_time) / 1000000
core.log("action", "[MOD] yl_matterbridge loaded in [" .. mod_end_time .. "s]")