- -- TODO verify JID, and return; if invalid
- local node = match(jid, "^([^@]+)@");
- local server = (node and match(jid, ".-@([^@/]+)")) or match(jid, "^([^@/]+)");
- local resource = match(jid, "/(.+)$");
- return node, server, resource;
+ local node, nodepos = match(jid, "^([^@/]+)@()");
+ local host, hostpos = match(jid, "^([^@/]+)()", nodepos)
+ if node and not host 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
+ return node, host, resource;
+end
+
+local function bare(jid)
+ local node, host = split(jid);
+ if node and host then
+ return node.."@"..host;
+ end
+ return host;
+end
+
+local function prepped_split(jid)
+ local node, host, resource = split(jid);
+ if 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
+ end
+ if resource then
+ resource = resourceprep(resource);
+ if not resource then return; end
+ end
+ return node, host, resource;
+ end