yl_matterbridge/init.lua

128 lines
4.3 KiB
Lua
Raw Permalink Normal View History

-- Version 0.0.2
2022-02-24 18:09:00 -08:00
-- Author AliasAlreadyTaken
-- License MIT
-- Changelog
-- 0.0.1 First puny attempts
-- 0.0.2 feature complete including settings
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 = {}
yl_matterbridge.information.version = "0.0.2"
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
local address = core.settings:get("yl_matterbridge.address") or "127.0.0.1"
local port = core.settings:get("yl_matterbridge.port") or "4242"
local gateway = core.settings:get("yl_matterbridge.gateway") or "default"
local token = core.settings:get("yl_matterbridge.token") or ""
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
local post_headers = {"Content-Type: application/json"}
if token and token ~= "" then
table.insert(post_headers, "Authorization: Bearer " .. token)
end
local url_send = "http://" .. address .. ":" .. port .. "/api/message"
local url_receive = "http://" .. address .. ":" .. port .. "/api/messages"
2022-02-24 18:57:26 -08:00
function send(user_name, message_text)
2022-02-24 18:09:00 -08:00
local timeout = 10
local data = {
text = message_text,
username = user_name,
2022-02-25 09:39:26 -08:00
gateway = 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)
if result.succeeded then
local data = core.parse_json(result.data)
core.log("action", "[MOD] yl_matterbridge : Posted " .. dump(data))
2022-02-24 18:09:00 -08:00
else
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
end
end
)
return true
end
2022-02-25 12:30:38 -08:00
local function receive()
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)
if result.succeeded then
local data = core.parse_json(result.data)
for _, v in ipairs(data) do
if v.username and v.text and v.account then
core.log("action", "[MOD] yl_matterbridge : Received " .. dump(data))
yl_matterbridge.receive_from_bridge(v.username, v.text, v.account)
2022-02-24 18:57:26 -08:00
end
end
else
2022-02-25 18:14:32 -08:00
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result = " .. dump(result))
2022-02-24 18:57:26 -08:00
return false
end
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
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-02-27 19:39:56 -08:00
minetest.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]")