Compare commits

...

17 Commits

Author SHA1 Message Date
AliasAlreadyTaken
28e21a9d46 AliasAlreadyTaken/yl_matterbridge#10 Adds recveive timeout setting 2024-12-26 09:32:58 +01:00
AliasAlreadyTaken
120ec6ed5d AliasAlreadyTaken/yl_matterbridge#9 Adds fetch frequency setting 2024-12-26 09:21:33 +01:00
6ae8ccda85 add optional function param with alternative gateway
util as api to mods that want use an alternative gateway to send msgs
2024-08-08 19:07:34 +02:00
AliasAlreadyTaken
f44729462a Adds another project, more tested MT versions and corrects token doc in readme 2024-01-30 10:10:15 +01:00
AliasAlreadyTaken
b3964c12c1 Added year and name to license 2024-01-20 00:30:58 +01:00
a514764db7 Formatting 2023-05-08 09:51:28 +02:00
8b5bc33b59 Simplify comparisons 2023-05-08 09:48:27 +02:00
095614a6b6 #5 Add () to compare in correct order 2023-05-08 09:45:19 +02:00
f166d44a3e Distinguish data_in and data_out 2023-05-08 09:41:10 +02:00
1dff0cf26f Fix findings of luacheck 2023-05-08 09:39:24 +02:00
a2079ec16a Added luacheck 2023-05-08 09:34:30 +02:00
679023774d Disable bridge from ingame 2022-04-26 07:59:47 +02:00
604f8dc0d3 v1.0.3 Release to cdb 2022-04-07 00:13:01 +02:00
fa3d6b1589 Add tested and unknown versions to readme 2022-04-07 00:08:26 +02:00
5468229d9e Bugfix settingtypes: No "" around values 2022-04-06 23:04:14 +02:00
AliasAlreadyTaken
9e0f1f29bc Bugfix settingtypes: bool instead of boolean 2022-03-31 14:45:33 +02:00
AliasAlreadyTaken
cd0c9c92d1 bugtracker#1646 corrects evaluation of debug setting 2022-03-31 13:13:24 +02:00
5 changed files with 191 additions and 89 deletions

13
.luacheckrc Normal file
View File

@ -0,0 +1,13 @@
unused_args = true
allow_defined_top = false
globals = {
"minetest",
"core",
"yl_matterbridge"
}
read_globals = {
-- Builtin
"dump", "DIR_DELIM"
}

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2024 AliasAlreadyTaken
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -16,7 +16,6 @@ This mod adds support for [matterbridge](https://github.com/42wim/matterbridge)
Get it from https://gitea.your-land.de/AliasAlreadyTaken/yl_matterbridge
## Installation
copy the "yl_matterbridge" folder to your mod folder and enable it in your world.mt.
@ -31,6 +30,8 @@ Download and install [matterbridge](https://github.com/42wim/matterbridge)
## Configuration
### Matterbridge
Create a config for [matterbridge](https://github.com/42wim/matterbridge) and the chat protocols you want to use.
* https://github.com/42wim/matterbridge/wiki/How-to-create-your-config
@ -43,7 +44,7 @@ An example config for minetest looks like this
BindAddress="127.0.0.1:4242"
Buffer=1000
RemoteNickFormat="{NICK}"
#Token="DISABLED, NOT YET SUPPORTED"
#Token="mytotallysecrettoken"
```
Gateway section:
@ -56,6 +57,54 @@ account="api.mt"
channel="api"
```
### Minetest
See also [settingtypes.txt](settingtypes.txt)
On top of adding `yl_matterbridge` to http_mods, there are settings to add to minetest.conf:
```
yl_matterbridge.address = 127.0.0.1
```
The address or IP to the matterbridge server
```
yl_matterbridge.port = 4242
```
The port the matterbridge server listens on
```
yl_matterbridge.gateway = default
```
The name of the gateway of the matterbridge server
```
yl_matterbridge.token
```
A secret token string to identify yourself towards the matterbridge server
```
yl_matterbridge.debug = false
```
Set this to true to enable debugging
```
yl_matterbridge.fetch_interval = 0.0
```
Specifies the interval (in seconds) at which a new query is sent to the bridge.
```
yl_matterbridge.receive_timeout = 0.89
```
Specifies the timeout (in seconds) a query to the bridge waits before it breaks.
## Modmakers
API: Overwrite these function in your chat mod, if you want to handle messages yourself
@ -100,11 +149,40 @@ function yl_matterbridge.chat_message(user_name, message_text)
end
```
## Supported versions
If you run yl_matterbridge in one of the supported versions, but something is wrong, please [file a bug](https://gitea.your-land.de/AliasAlreadyTaken/yl_matterbridge/issues/new). PRs also welcome.
If you run yl_matterbridge in one of the unknown versions, like MacOS or Android or Linux, please tell us whether it works or not. We will add your findings to the readme.
There is no reason to believe it doesn't work anywhere, but you never know.
### Windows
MT versions 5.0.0, 5.0.1, 5.1.0, 5.1.1, 5.2.0, 5.3.0, 5.4.0 5.4.1, 5.5.0, 5.6.0, 5.6.1, 5.7.0, 5.8.0
### Linux
MT versions 5.5.0, 5.6.0, 5.6.1, 5.7.0, 5.8.0
### MacOS
unknown
### Android
unknown
### Misc
unknown
## Allied projects
If you know a project that uses this bridge tell us and we will add it to the list.
* smart_chat : https://github.com/acmgit/smart_chat/
* player_events: https://github.com/insanity54/matterbridge_player_events
## Uninstall

161
init.lua
View File

@ -1,24 +1,24 @@
-- Version 1.0.0
-- Version 1.0.3
-- Author AliasAlreadyTaken
-- License MIT
-- Changelog
-- 0.0.1 First puny attempts
-- 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
-- 1.0.2 Bugfix settingtypes: bool instead of boolean
-- 1.0.3 Release to cdb
local mod_start_time = core.get_us_time()
core.log("action", "[MOD] yl_matterbridge loading")
yl_matterbridge = {}
--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
-- 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
yl_matterbridge.information = {}
yl_matterbridge.information.version = "1.0.0"
yl_matterbridge.information.version = "1.0.3"
yl_matterbridge.information.author = "AliasAlreadyTaken"
yl_matterbridge.information.license = "MIT"
yl_matterbridge.information.name = "yl_matterbridge"
@ -26,12 +26,16 @@ yl_matterbridge.information.source = "https://gitea.your-land.de/AliasAlreadyTak
yl_matterbridge.information.additional =
"Sends and receives ingame chat to and from matterbridge https://github.com/42wim/matterbridge"
yl_matterbridge.enabled = true
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("yl_matterbridge.debug") or false
settings.debug = core.settings:get_bool("yl_matterbridge.debug", false) or false
settings.fetch_interval = tonumber(core.settings:get("yl_matterbridge.fetch_interval") or "0.0") or 0
settings.receive_timeout = tonumber(core.settings:get("yl_matterbridge.receive_timeout") or "0.89") or 0.89
local http = core.request_http_api()
@ -48,91 +52,90 @@ end
local url_send = "http://" .. settings.address .. ":" .. settings.port .. "/api/message"
local url_receive = "http://" .. settings.address .. ":" .. settings.port .. "/api/messages"
function send(user_name, message_text)
local function send(user_name, message_text, gateway)
if yl_matterbridge.enabled == false then
return false
end
if settings.debug then
core.log(
"action",
"[MOD] yl_matterbridge : send user_name = " .. dump(user_name) .. ", message_text = " .. dump(message_text)
)
core.log("action", "[MOD] yl_matterbridge : send user_name = " .. dump(user_name) .. ", message_text = " ..
dump(message_text))
end
local timeout = 10
local data = {
local data_in = {
text = message_text,
username = user_name,
gateway = settings.gateway
gateway = gateway or settings.gateway
}
http.fetch(
{
url = url_send,
extra_headers = post_headers,
timeout = timeout,
post_data = core.write_json(data)
},
function(result)
if not result.completed or not result.succeeded or not result.code == 200 then
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
return false
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
http.fetch({
url = url_send,
extra_headers = post_headers,
timeout = timeout,
post_data = core.write_json(data_in)
}, function(result)
if (not result.completed) or (not result.succeeded) or (result.code ~= 200) then
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
return false
else
if settings.debug then
local data_out = core.parse_json(result.data)
core.log("action",
"[MOD] yl_matterbridge : send result = " .. dump(result) .. ", data = " .. dump(data_out))
end
return true
end
)
end)
end
local function receive()
local timer = 0
local function receive(dtime)
if yl_matterbridge.enabled == false then
return false
end
if settings.debug then
core.log("action", "[MOD] yl_matterbridge : receive")
end
local timeout = 0
http.fetch(
{
url = url_receive,
extra_headers = post_headers,
timeout = timeout
},
function(result)
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
timer = timer + dtime
if timer <= settings.fetch_interval then
return false
end
timer = 0
http.fetch({
url = url_receive,
extra_headers = post_headers,
timeout = settings.receive_timeout
}, function(result)
if (not result.completed) or (not result.succeeded) or (result.code ~= 200) then
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result (req) = " .. dump(result))
return false
else
local data_out = core.parse_json(result.data)
if not data_out then
if not next(data_out) then
if settings.debug then
core.log("error",
"[MOD] yl_matterbridge: receive/http.fetch failed. Result (data) = " .. dump(result))
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)
)
return false
end
for _, dataset in ipairs(data_out) 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_out))
end
end
end
)
end)
end
-- API
@ -152,17 +155,15 @@ function yl_matterbridge.receive_all_from_bridge(dataset)
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)
)
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)
send(user_name, message_text)
function yl_matterbridge.send_to_bridge(user_name, message_text, gateway)
send(user_name, message_text, gateway)
end
-- Set this function to yl_matterbridge.chat_message = function() end

View File

@ -6,24 +6,34 @@
# The address or IP to the matterbridge server
# If you run the bridge on the same machine where you run your MT, you can use the default
# Defaults to 127.0.0.1
yl_matterbridge.address (Bridge IP address or URL) string "127.0.0.1"
yl_matterbridge.address (Bridge IP address or URL) string 127.0.0.1
# The port the matterbridge server listens to
# The port the matterbridge server listens on
# You need to set this to the port of the API connection, which you configured in the toml
# Defaults to 4242
yl_matterbridge.port (Bridge port) string "4242"
yl_matterbridge.port (Bridge port) string 4242
# The name of the gateway of the matterbridge server
# You need to set this to the gatewayname, which you configured in the toml
# Defaults to "default"
yl_matterbridge.gateway (Bridge gateway) string "default"
yl_matterbridge.gateway (Bridge gateway) string default
# The token the matterbridge server
# If you don't configure this, every wellformed message to the right port will be relayed
# Optional
yl_matterbridge.token (Bridge token) string ""
yl_matterbridge.token (Bridge token) string
# Whether or not debugging messages are enabled
# If set to true, the mod will write every send and receive to the log, among other debug messages.
# Optional, default false
yl_matterbridge.debug (Debug mode) boolean false
yl_matterbridge.debug (Debug mode) bool false
# Fetch/Receive interval
# Specifies the interval (in seconds) at which a new query is sent to the bridge. A value of 0.0 means queries are sent on every server step.
# Optional, defaults to 0.0
yl_matterbridge.fetch_interval (Fetch/Receive interval) float 0.0 0.0 30.0
# Receive timeout
# Specifies the timeout (in seconds) a query to the bridge waits before it breaks. Set low for good connections and/or low fetch_interval.
# Optional, defaults to 0.89
yl_matterbridge.receive_timeout (Receive timeout) float 0.89 0.1 10.0