Software /
code /
prosody
Annotate
util/jid.lua @ 4953:24c2150cc8b0
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 09 Jul 2012 02:36:09 +0100 |
parent | 4407:f78c6f5fa090 |
child | 5776:bd0ff8ae98a8 |
child | 5944:f3817912e8b2 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1171
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2245
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2245
diff
changeset
|
3 -- Copyright (C) 2008-2010 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 |
4407
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
16 local escapes = { |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
17 [" "] = "\\20"; ['"'] = "\\22"; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
18 ["&"] = "\\26"; ["'"] = "\\27"; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
19 ["/"] = "\\2f"; [":"] = "\\3a"; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
20 ["<"] = "\\3c"; [">"] = "\\3e"; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
21 ["@"] = "\\40"; ["\\"] = "\\5c"; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
22 }; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
23 local unescapes = {}; |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
24 for k,v in pairs(escapes) do unescapes[v] = k; end |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
25 |
0 | 26 module "jid" |
27 | |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
28 local function _split(jid) |
109
7efedc96352a
Minor edit, and added a TODO
Waqas Hussain <waqas20@gmail.com>
parents:
104
diff
changeset
|
29 if not jid then return; end |
3480
97831dfe7f72
util.jid: Fix parsing of JIDs with no nodepart and an @ in the resourcepart (thanks seth)
Matthew Wild <mwild1@gmail.com>
parents:
3375
diff
changeset
|
30 local node, nodepos = match(jid, "^([^@/]+)@()"); |
369 | 31 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
|
32 if node and not host then return nil, nil, nil; end |
369 | 33 local resource = match(jid, "^/(.+)$", hostpos); |
34 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
|
35 return node, host, resource; |
0 | 36 end |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
37 split = _split; |
104
cfbd3b849f9e
Fixed: util/jid.lua now returns module object
Waqas Hussain <waqas20@gmail.com>
parents:
29
diff
changeset
|
38 |
365
a59300fc22ec
Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
39 function bare(jid) |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 end |
384 | 44 return host; |
365
a59300fc22ec
Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
45 end |
a59300fc22ec
Add jid.bare() helper function
Matthew Wild <mwild1@gmail.com>
parents:
109
diff
changeset
|
46 |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
47 local function _prepped_split(jid) |
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
48 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
|
49 if host then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
50 host = nameprep(host); |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
51 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
|
52 if node then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
53 node = nodeprep(node); |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
54 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
|
55 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
56 if resource then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
57 resource = resourceprep(resource); |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
58 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
|
59 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
60 return node, 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 |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
63 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
|
64 |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
65 function prep(jid) |
1171
be11dc0610d5
util.jid: Eliminate global method use
Waqas Hussain <waqas20@gmail.com>
parents:
896
diff
changeset
|
66 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
|
67 if host then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
68 if node then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
69 host = node .. "@" .. host; |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
70 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
71 if resource then |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
72 host = host .. "/" .. resource; |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
73 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
74 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
75 return host; |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
76 end |
ab428c579cbc
util/jid: string prepping functions added: prepped_split and prep
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
77 |
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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 |
3375
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
91 function compare(jid, acl) |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
92 -- compare jid to single acl rule |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
93 -- TODO compare to table of rules? |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
94 local jid_node, jid_host, jid_resource = _split(jid); |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
95 local acl_node, acl_host, acl_resource = _split(acl); |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
96 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
97 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
98 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
99 return true |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
100 end |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
101 return false |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
102 end |
29e51e1c7c3d
util.jid: compare() added, with some tests.
Kim Alvefur <zash@zash.se>
parents:
2923
diff
changeset
|
103 |
4407
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
104 function escape(s) return s and (s:gsub(".", escapes)); end |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
105 function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end |
f78c6f5fa090
util.jid: Added escape() and unescape().
Waqas Hussain <waqas20@gmail.com>
parents:
3480
diff
changeset
|
106 |
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
|
107 return _M; |