Comparison

util/jid.lua @ 12190:3616128cd2e3

util.jid: Explicitly check for nil rather than falsy A boolean false should blow up.
author Kim Alvefur <zash@zash.se>
date Sat, 15 Jan 2022 16:25:25 +0100
parent 11056:0b0a42542456
child 12605:053417068957
comparison
equal deleted inserted replaced
12189:82c8e855c850 12190:3616128cd2e3
30 30
31 local _ENV = nil; 31 local _ENV = nil;
32 -- luacheck: std none 32 -- luacheck: std none
33 33
34 local function split(jid) 34 local function split(jid)
35 if not jid then return; end 35 if jid == nil then return; end
36 local node, nodepos = match(jid, "^([^@/]+)@()"); 36 local node, nodepos = match(jid, "^([^@/]+)@()");
37 local host, hostpos = match(jid, "^([^@/]+)()", nodepos); 37 local host, hostpos = match(jid, "^([^@/]+)()", nodepos);
38 if node and not host then return nil, nil, nil; end 38 if node ~= nil and host == nil then return nil, nil, nil; end
39 local resource = match(jid, "^/(.+)$", hostpos); 39 local resource = match(jid, "^/(.+)$", hostpos);
40 if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end 40 if (host == nil) or ((resource == nil) and #jid >= hostpos) then return nil, nil, nil; end
41 return node, host, resource; 41 return node, host, resource;
42 end 42 end
43 43
44 local function bare(jid) 44 local function bare(jid)
45 local node, host = split(jid); 45 local node, host = split(jid);
46 if node and host then 46 if node ~= nil and host ~= nil then
47 return node.."@"..host; 47 return node.."@"..host;
48 end 48 end
49 return host; 49 return host;
50 end 50 end
51 51
52 local function prepped_split(jid, strict) 52 local function prepped_split(jid, strict)
53 local node, host, resource = split(jid); 53 local node, host, resource = split(jid);
54 if host and host ~= "." then 54 if host ~= nil and host ~= "." then
55 if sub(host, -1, -1) == "." then -- Strip empty root label 55 if sub(host, -1, -1) == "." then -- Strip empty root label
56 host = sub(host, 1, -2); 56 host = sub(host, 1, -2);
57 end 57 end
58 host = nameprep(host, strict); 58 host = nameprep(host, strict);
59 if not host then return; end 59 if host == nil then return; end
60 if node then 60 if node ~= nil then
61 node = nodeprep(node, strict); 61 node = nodeprep(node, strict);
62 if not node then return; end 62 if node == nil then return; end
63 end 63 end
64 if resource then 64 if resource ~= nil then
65 resource = resourceprep(resource, strict); 65 resource = resourceprep(resource, strict);
66 if not resource then return; end 66 if resource == nil then return; end
67 end 67 end
68 return node, host, resource; 68 return node, host, resource;
69 end 69 end
70 end 70 end
71 71
72 local function join(node, host, resource) 72 local function join(node, host, resource)
73 if not host then return end 73 if host == nil then return end
74 if node and resource then 74 if node ~= nil and resource ~= nil then
75 return node.."@"..host.."/"..resource; 75 return node.."@"..host.."/"..resource;
76 elseif node then 76 elseif node ~= nil then
77 return node.."@"..host; 77 return node.."@"..host;
78 elseif resource then 78 elseif resource ~= nil then
79 return host.."/"..resource; 79 return host.."/"..resource;
80 end 80 end
81 return host; 81 return host;
82 end 82 end
83 83