diff options
Diffstat (limited to 'util/jid.lua')
-rw-r--r-- | util/jid.lua | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/util/jid.lua b/util/jid.lua index ec31f180..694a6b1f 100644 --- a/util/jid.lua +++ b/util/jid.lua @@ -22,63 +22,67 @@ local escapes = { ["@"] = "\\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 local function split(jid) - if not jid then return; end + if jid == nil then return; end local node, nodepos = match(jid, "^([^@/]+)@()"); local host, hostpos = match(jid, "^([^@/]+)()", nodepos); - if node and not host then return nil, nil, nil; end + if node ~= nil and host == nil then return nil, nil, nil; end local resource = match(jid, "^/(.+)$", hostpos); - if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end + if (host == nil) or ((resource == nil) and #jid >= hostpos) then return nil, nil, nil; end return node, host, resource; end local function bare(jid) local node, host = split(jid); - if node and host then + if node ~= nil and host ~= nil then return node.."@"..host; end 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 host ~= nil and host ~= "." then if sub(host, -1, -1) == "." then -- Strip empty root label host = sub(host, 1, -2); end - host = nameprep(host); - if not host then return; end - if node then - node = nodeprep(node); - if not node then return; end + host = nameprep(host, strict); + if host == nil then return; end + if node ~= nil then + node = nodeprep(node, strict); + if node == nil then return; end end - if resource then - resource = resourceprep(resource); - if not resource then return; end + if resource ~= nil then + resource = resourceprep(resource, strict); + if resource == nil then return; end end return node, host, resource; end end local function join(node, host, resource) - if not host then return end - if node and resource then + if host == nil then return end + if node ~= nil and resource ~= nil then return node.."@"..host.."/"..resource; - elseif node then + elseif node ~= nil then return node.."@"..host; - elseif resource then + elseif resource ~= nil then return host.."/"..resource; end 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 @@ local function resource(jid) 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 { |