Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
[prosody.git] / util / jid.lua
1
2 local match = string.match;
3 local tostring = tostring;
4 local print = print
5 module "jid"
6
7 function split(jid)
8         if not jid then return; end
9         -- TODO verify JID, and return; if invalid
10         local node, nodelen = match(jid, "^([^@]+)@()");
11         local host, hostlen = match(jid, "^([^@/]+)()", nodelen)
12         if node and not host then return nil, nil, nil; end
13         local resource = match(jid, "^/(.+)$", hostlen);
14         if (not host) or ((not resource) and #jid >= hostlen) then return nil, nil, nil; end
15         return node, host, resource;
16 end
17
18 function bare(jid)
19         local node, host = split(jid);
20         if node and host then
21                 return node.."@"..host;
22         elseif host then
23                 return host;
24         end
25         return nil;
26 end
27
28 return _M;