From c3c6731d8b0a4ea5c20da5c696f77dcd26720c9e Mon Sep 17 00:00:00 2001 From: joenas <jon@jonnev.se> Date: Tue, 12 Sep 2017 11:28:56 +0200 Subject: [PATCH] Breaking changes: Removed port setting (fixes #9) Throw errors when weird/faulty matrix url (work on #10) Added neturl for url parsing --- .gitmodules | 3 +++ README.md | 7 ++++--- config.lua | 1 - debug.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ init.lua | 11 ++++++----- neturl | 1 + settingtypes.txt | 4 ---- validate_server.lua | 21 +++++++++++++++++++++ 8 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 debug.lua create mode 160000 neturl create mode 100644 validate_server.lua diff --git a/.gitmodules b/.gitmodules index fa2876d..c43a315 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lua-matrix"] path = lua-matrix url = https://github.com/diggers-mt/lua-matrix +[submodule "neturl"] + path = neturl + url = https://github.com/golgote/neturl.git diff --git a/README.md b/README.md index f2ce69d..4d519d0 100644 --- a/README.md +++ b/README.md @@ -62,12 +62,13 @@ secure.trusted_mods = matrix * `matrix.password`: Password for Matrix user -* `matrix.server`: Server to connect to, include http(s), `https://matrix.org` - -* `matrix.port`: Server port, default `8448` +* `matrix.server`: Server to connect to, include http(s) and port, `https://matrix.org` * `matrix.room_id`: Room to join, `room_id` in matrix. Always starts with `!` +### Removed, don't use +* `matrix.port`: Server port, default `8448` + ## License diff --git a/config.lua b/config.lua index 82d6bf4..6af12c3 100644 --- a/config.lua +++ b/config.lua @@ -28,6 +28,5 @@ end setting("string", "user", nil, true) -- User name, fe @digbot:matrix.org setting("string", "server", nil, true) -- Server address to connect to -setting("number", "port", 8448) -- Server port to connect to setting("string", "room_id", nil, true) -- Channel to join (not needed?) setting("string", "password", nil, true) -- Server password diff --git a/debug.lua b/debug.lua new file mode 100644 index 0000000..ae66e81 --- /dev/null +++ b/debug.lua @@ -0,0 +1,42 @@ +function eprintf(fmt, ...) + minetest.log("info", fmt:format(...)) +end + + +function table_print (tt, indent, done) + done = done or {} + indent = indent or 0 + if type(tt) == "table" then + local sb = {} + for key, value in pairs (tt) do + table.insert(sb, string.rep (" ", indent)) -- indent it + if type (value) == "table" and not done [value] then + done [value] = true + table.insert(sb, "{\n"); + table.insert(sb, table_print (value, indent + 2, done)) + table.insert(sb, string.rep (" ", indent)) -- indent it + table.insert(sb, "}\n"); + elseif "number" == type(key) then + table.insert(sb, string.format("\"%s\"\n", tostring(value))) + else + table.insert(sb, string.format( + "%s = \"%s\"\n", tostring (key), tostring(value))) + end + end + return table.concat(sb) + else + return tt .. "\n" + end +end + +function to_string( tbl ) + if "nil" == type( tbl ) then + return tostring(nil) + elseif "table" == type( tbl ) then + return table_print(tbl) + elseif "string" == type( tbl ) then + return tbl + else + return tostring(tbl) + end +end diff --git a/init.lua b/init.lua index 64c6fab..55a0177 100644 --- a/init.lua +++ b/init.lua @@ -13,6 +13,7 @@ end ie.package.path = modpath.."/lua-matrix/?.lua;" + ..modpath.."/neturl/lib/?.lua;" ..ie.package.path matrix = { @@ -24,16 +25,16 @@ matrix = { } dofile(modpath.."/config.lua") - -local function eprintf(fmt, ...) - minetest.log("info", fmt:format(...)) -end +--dofile(modpath.."/debug.lua") -- Temporarily set require so that LuaIRC can access it local old_require = require require = ie.require -local client = require("matrix").client(matrix.config.server..":"..matrix.config.port) +dofile(modpath.."/validate_server.lua") + +local hs_url = validate_server(matrix.config.server) +local client = require("matrix").client(hs_url) local start_ts = os.time() * 1000 diff --git a/neturl b/neturl new file mode 160000 index 0000000..e69ad0d --- /dev/null +++ b/neturl @@ -0,0 +1 @@ +Subproject commit e69ad0d005d7d70ff445d823ff69f1b41b6157ea diff --git a/settingtypes.txt b/settingtypes.txt index d29f4e7..2bfbaa5 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -7,10 +7,6 @@ matrix.user (Username) string # Server to connect to, include http(s) matrix.server (Matrix server) string https://matrix.org -# Server port. -# The standard matrix port is 8448 -matrix.port (Server port) int 8448 - # Channel the bot joins after connecting. matrix.room_id (Channel to join) string diff --git a/validate_server.lua b/validate_server.lua new file mode 100644 index 0000000..2f7a281 --- /dev/null +++ b/validate_server.lua @@ -0,0 +1,21 @@ +local url = require "net.url" + +function validate_server(hs_url) + u = url.parse(hs_url):normalize() + + if not (u.host and u.scheme) then + return error("'"..hs_url.."' doesn't look like a valid url. Please specify scheme (http/s) and hostname") + end + + if u.port and u.port == 8448 and u.scheme == 'http' then + return error("Port 8448 is for https, make sure your homeserver URL is correct") + end + + if u.port and u.port == 8008 and u.scheme == 'https' then + minetest.log("warn", "Port 8008 is not https, make sure your homeserver URL is correct") + end + + return hs_url +end + + -- GitLab