util/jid.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 21 Nov 2008 05:02:53 +0000
changeset 366 5691edc7dd63
parent 365 a59300fc22ec
child 367 cc26368294a3
permissions -rw-r--r--
Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     1
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     2
local match = string.match;
366
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
     3
local tostring = tostring;
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
     4
local print = print
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     5
module "jid"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     6
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
     7
function split(jid)
109
7efedc96352a Minor edit, and added a TODO
Waqas Hussain <waqas20@gmail.com>
parents: 104
diff changeset
     8
	if not jid then return; end
7efedc96352a Minor edit, and added a TODO
Waqas Hussain <waqas20@gmail.com>
parents: 104
diff changeset
     9
	-- TODO verify JID, and return; if invalid
366
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    10
	local node, nodelen = match(jid, "^([^@]+)@()");
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    11
	local host, hostlen = match(jid, "^([^@/]+)()", nodelen)
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    12
	if node and not host then return nil, nil, nil; end
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    13
	local resource = match(jid, "^/(.+)$", hostlen);
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    14
	if (not host) or ((not resource) and #jid >= hostlen) then return nil, nil, nil; end
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    15
	return node, host, resource;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    16
end
104
cfbd3b849f9e Fixed: util/jid.lua now returns module object
Waqas Hussain <waqas20@gmail.com>
parents: 29
diff changeset
    17
365
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    18
function bare(jid)
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    19
	local node, host = split(jid);
366
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    20
	if node and host then
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    21
		return node.."@"..host;
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    22
	elseif host then
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    23
		return host;
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    24
	end
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    25
	return nil;
365
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    26
end
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    27
366
5691edc7dd63 Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
Matthew Wild <mwild1@gmail.com>
parents: 365
diff changeset
    28
return _M;