Changeset

13071:b5a419ac0f84

mod_admin_shell: Factor apart wildcard matching into function for reuse Applying this for s2s:close[all]() would also be nice.
author Kim Alvefur <zash@zash.se>
date Mon, 10 Apr 2023 14:12:48 +0200
parents 13070:be9ac41f1619
children 13072:7fcf41b541e0
files plugins/mod_admin_shell.lua
diffstat 1 files changed, 18 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_admin_shell.lua	Mon Apr 10 13:50:09 2023 +0200
+++ b/plugins/mod_admin_shell.lua	Mon Apr 10 14:12:48 2023 +0200
@@ -1101,26 +1101,30 @@
 	return _sort_hosts(a_local or "", b_local or "");
 end
 
+local function match_wildcard(match_jid, jid)
+	-- host == host or (host) == *.(host) or sub(.host) == *(.host)
+	return jid == match_jid or jid == match_jid:sub(3) or jid:sub(-#match_jid + 1) == match_jid:sub(2);
+end
+
+local function match_s2s_jid(session, match_jid)
+	local host, remote = get_s2s_hosts(session);
+	if not match_jid or match_jid == "*" then
+		return true;
+	elseif host == match_jid or remote == match_jid then
+		return true;
+	elseif match_jid:sub(1, 2) == "*." then
+		return match_wildcard(match_jid, host) or match_wildcard(match_jid, remote);
+	end
+	return false;
+end
+
 function def_env.s2s:show(match_jid, colspec)
 	local print = self.session.print;
 	local columns = get_colspec(colspec, { "id"; "host"; "dir"; "remote"; "ipv"; "secure"; "s2s_sasl"; "dialback" });
 	local row = format_table(columns, self.session.width);
 
 	local function match(session)
-		local host, remote = get_s2s_hosts(session);
-		if not match_jid or match_jid == "*" then
-			return true;
-		elseif host == match_jid or remote == match_jid then
-			return true;
-		elseif match_jid:sub(1, 2) == "*." then
-			-- (host) == *.(host) or sub(.host) == *(.host)
-			if host == match_jid:sub(3) or host:sub(-#match_jid + 1) == match_jid:sub(2) then
-				return true
-			elseif remote == match_jid:sub(3) or remote:sub(-#match_jid + 1) == match_jid:sub(2) then
-				return true
-			end
-		end
-		return false;
+		return match_s2s_jid(session, match_jid);
 	end
 
 	local group_by_host = true;