util/jid.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 30 Jan 2009 17:22:56 +0000
changeset 758 b1885732e979
parent 717 ab428c579cbc
child 759 5cccfb5da6cb
permissions -rw-r--r--
GPL->MIT!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
     1
-- Prosody IM v0.2
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 384
diff changeset
     2
-- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 384
diff changeset
     3
-- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 384
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 717
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 717
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 384
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 384
diff changeset
     8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 384
diff changeset
     9
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    10
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    11
local match = string.match;
717
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    12
local nodeprep = require "util.encodings".stringprep.nodeprep;
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    13
local nameprep = require "util.encodings".stringprep.nameprep;
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    14
local resourceprep = require "util.encodings".stringprep.resourceprep;
367
cc26368294a3 Remove some declarations I added while debugging
Matthew Wild <mwild1@gmail.com>
parents: 366
diff changeset
    15
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    16
module "jid"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    17
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    18
function split(jid)
109
7efedc96352a Minor edit, and added a TODO
Waqas Hussain <waqas20@gmail.com>
parents: 104
diff changeset
    19
	if not jid then return; end
369
42de92add67b Better names for variables
Matthew Wild <mwild1@gmail.com>
parents: 368
diff changeset
    20
	local node, nodepos = match(jid, "^([^@]+)@()");
42de92add67b Better names for variables
Matthew Wild <mwild1@gmail.com>
parents: 368
diff changeset
    21
	local host, hostpos = match(jid, "^([^@/]+)()", nodepos)
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
    22
	if node and not host then return nil, nil, nil; end
369
42de92add67b Better names for variables
Matthew Wild <mwild1@gmail.com>
parents: 368
diff changeset
    23
	local resource = match(jid, "^/(.+)$", hostpos);
42de92add67b Better names for variables
Matthew Wild <mwild1@gmail.com>
parents: 368
diff changeset
    24
	if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end
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
    25
	return node, host, resource;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
    26
end
104
cfbd3b849f9e Fixed: util/jid.lua now returns module object
Waqas Hussain <waqas20@gmail.com>
parents: 29
diff changeset
    27
365
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    28
function bare(jid)
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    29
	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
    30
	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
    31
		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
    32
	end
384
4542bcdb7f55 Removed useless check
Waqas Hussain <waqas20@gmail.com>
parents: 369
diff changeset
    33
	return host;
365
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    34
end
a59300fc22ec Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents: 109
diff changeset
    35
717
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    36
function prepped_split(jid)
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    37
	local node, host, resource = split(jid);
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    38
	if host then
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    39
		host = nameprep(host);
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    40
		if not host then return; end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    41
		if node then
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    42
			node = nodeprep(node);
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    43
			if not node then return; end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    44
		end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    45
		if resource then
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    46
			resource = resourceprep(resource);
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    47
			if not resource then return; end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    48
		end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    49
		return node, host, resource;
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    50
	end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    51
end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    52
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    53
function prep(jid)
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    54
	local node, host, resource = prepped_split(jid);
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    55
	if host then
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    56
		if node then
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    57
			host = node .. "@" .. host;
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    58
		end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    59
		if resource then
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    60
			host = host .. "/" .. resource;
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    61
		end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    62
	end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    63
	return host;
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    64
end
ab428c579cbc util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
    65
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
    66
return _M;