Diff

util/jid.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 11056:0b0a42542456
child 12190:3616128cd2e3
line wrap: on
line diff
--- a/util/jid.lua	Thu Nov 05 22:27:17 2020 +0100
+++ b/util/jid.lua	Thu Nov 05 22:31:25 2020 +0100
@@ -22,7 +22,11 @@
 	["@"] = "\\40"; ["\\"] = "\\5c";
 };
 local unescapes = {};
-for k,v in pairs(escapes) do unescapes[v] = k; end
+local backslash_escapes = {};
+for k,v in pairs(escapes) do
+	unescapes[v] = k;
+	backslash_escapes[v] = v:gsub("\\", escapes)
+end
 
 local _ENV = nil;
 -- luacheck: std none
@@ -45,20 +49,20 @@
 	return host;
 end
 
-local function prepped_split(jid)
+local function prepped_split(jid, strict)
 	local node, host, resource = split(jid);
 	if host and host ~= "." then
 		if sub(host, -1, -1) == "." then -- Strip empty root label
 			host = sub(host, 1, -2);
 		end
-		host = nameprep(host);
+		host = nameprep(host, strict);
 		if not host then return; end
 		if node then
-			node = nodeprep(node);
+			node = nodeprep(node, strict);
 			if not node then return; end
 		end
 		if resource then
-			resource = resourceprep(resource);
+			resource = resourceprep(resource, strict);
 			if not resource then return; end
 		end
 		return node, host, resource;
@@ -77,8 +81,8 @@
 	return host;
 end
 
-local function prep(jid)
-	local node, host, resource = prepped_split(jid);
+local function prep(jid, strict)
+	local node, host, resource = prepped_split(jid, strict);
 	return join(node, host, resource);
 end
 
@@ -107,7 +111,7 @@
 	return (select(3, split(jid)));
 end
 
-local function escape(s) return s and (s:gsub(".", escapes)); end
+local function escape(s) return s and (s:gsub("\\%x%x", backslash_escapes):gsub("[\"&'/:<>@ ]", escapes)); end
 local function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end
 
 return {