aboutsummaryrefslogtreecommitdiffstats
path: root/util/jid.lua
blob: b35ddc82c54bd305995533ed9b8d820ec80c39db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

local match = string.match;
local tostring = tostring;
local print = print
module "jid"

function split(jid)
	if not jid then return; end
	-- TODO verify JID, and return; if invalid
	local node, nodelen = match(jid, "^([^@]+)@()");
	local host, hostlen = match(jid, "^([^@/]+)()", nodelen)
	if node and not host then return nil, nil, nil; end
	local resource = match(jid, "^/(.+)$", hostlen);
	if (not host) or ((not resource) and #jid >= hostlen) then return nil, nil, nil; end
	return node, host, resource;
end

function bare(jid)
	local node, host = split(jid);
	if node and host then
		return node.."@"..host;
	elseif host then
		return host;
	end
	return nil;
end

return _M;