Comparison

plugins/mod_admin_shell.lua @ 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
parent 13062:da51c36ed1e7
child 13072:7fcf41b541e0
comparison
equal deleted inserted replaced
13070:be9ac41f1619 13071:b5a419ac0f84
1099 local b_local, b_remote = get_s2s_hosts(b); 1099 local b_local, b_remote = get_s2s_hosts(b);
1100 if (a_local or "") == (b_local or "") then return _sort_hosts(a_remote or "", b_remote or ""); end 1100 if (a_local or "") == (b_local or "") then return _sort_hosts(a_remote or "", b_remote or ""); end
1101 return _sort_hosts(a_local or "", b_local or ""); 1101 return _sort_hosts(a_local or "", b_local or "");
1102 end 1102 end
1103 1103
1104 local function match_wildcard(match_jid, jid)
1105 -- host == host or (host) == *.(host) or sub(.host) == *(.host)
1106 return jid == match_jid or jid == match_jid:sub(3) or jid:sub(-#match_jid + 1) == match_jid:sub(2);
1107 end
1108
1109 local function match_s2s_jid(session, match_jid)
1110 local host, remote = get_s2s_hosts(session);
1111 if not match_jid or match_jid == "*" then
1112 return true;
1113 elseif host == match_jid or remote == match_jid then
1114 return true;
1115 elseif match_jid:sub(1, 2) == "*." then
1116 return match_wildcard(match_jid, host) or match_wildcard(match_jid, remote);
1117 end
1118 return false;
1119 end
1120
1104 function def_env.s2s:show(match_jid, colspec) 1121 function def_env.s2s:show(match_jid, colspec)
1105 local print = self.session.print; 1122 local print = self.session.print;
1106 local columns = get_colspec(colspec, { "id"; "host"; "dir"; "remote"; "ipv"; "secure"; "s2s_sasl"; "dialback" }); 1123 local columns = get_colspec(colspec, { "id"; "host"; "dir"; "remote"; "ipv"; "secure"; "s2s_sasl"; "dialback" });
1107 local row = format_table(columns, self.session.width); 1124 local row = format_table(columns, self.session.width);
1108 1125
1109 local function match(session) 1126 local function match(session)
1110 local host, remote = get_s2s_hosts(session); 1127 return match_s2s_jid(session, match_jid);
1111 if not match_jid or match_jid == "*" then
1112 return true;
1113 elseif host == match_jid or remote == match_jid then
1114 return true;
1115 elseif match_jid:sub(1, 2) == "*." then
1116 -- (host) == *.(host) or sub(.host) == *(.host)
1117 if host == match_jid:sub(3) or host:sub(-#match_jid + 1) == match_jid:sub(2) then
1118 return true
1119 elseif remote == match_jid:sub(3) or remote:sub(-#match_jid + 1) == match_jid:sub(2) then
1120 return true
1121 end
1122 end
1123 return false;
1124 end 1128 end
1125 1129
1126 local group_by_host = true; 1130 local group_by_host = true;
1127 local currenthost = nil; 1131 local currenthost = nil;
1128 for _, col in ipairs(columns) do 1132 for _, col in ipairs(columns) do