Software /
code /
prosody
Annotate
util/jid.lua @ 2245:df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 27 Nov 2009 17:33:55 +0000 |
parent | 1523:841d61be198f |
child | 2923:b7049746bd29 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1171
diff
changeset
|
1 -- Prosody IM |
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
384
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
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 | 10 |
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 | 16 module "jid" |
17 | |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
18 local 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 | 20 local node, nodepos = match(jid, "^([^@]+)@()"); |
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 | 23 local resource = match(jid, "^/(.+)$", hostpos); |
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 | 26 end |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
27 split = _split; |
104
cfbd3b849f9e
Fixed: util/jid.lua now returns module object
Waqas Hussain <waqas20@gmail.com>
parents:
29
diff
changeset
|
28 |
365
a59300fc22ec
Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
29 function bare(jid) |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 end |
384 | 34 return host; |
365
a59300fc22ec
Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
35 end |
a59300fc22ec
Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
36 |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
37 local function _prepped_split(jid) |
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
38 local node, host, resource = _split(jid); |
717
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
39 if host then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
40 host = nameprep(host); |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
41 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
|
42 if node then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
43 node = nodeprep(node); |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
44 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
|
45 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
46 if resource then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
47 resource = resourceprep(resource); |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
48 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
|
49 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
50 return node, host, resource; |
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 end |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
53 prepped_split = _prepped_split; |
717
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
54 |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
55 function prep(jid) |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
56 local node, host, resource = _prepped_split(jid); |
717
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
57 if host then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
58 if node then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
59 host = node .. "@" .. host; |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
60 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
61 if resource then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
62 host = host .. "/" .. resource; |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
63 end |
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 return host; |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
66 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
67 |
2245
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
68 function join(node, host, resource) |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
69 if node and host and resource then |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
70 return node.."@"..host.."/"..resource; |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
71 elseif node and host then |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
72 return node.."@"..host; |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
73 elseif host and resource then |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
74 return host.."/"..resource; |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
75 elseif host then |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
76 return host; |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
77 end |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
78 return nil; -- Invalid JID |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
79 end |
df9e18f5c808
util.jid: Add join(node, host, resource) function to join the components and return nil if invalid
Matthew Wild <mwild1@gmail.com>
parents:
1523
diff
changeset
|
80 |
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
|
81 return _M; |