2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local select
= select
;
12 local match
, sub
= string.match
, string.sub
;
13 local nodeprep
= require
"util.encodings".stringprep
.nodeprep
;
14 local nameprep
= require
"util.encodings".stringprep
.nameprep
;
15 local resourceprep
= require
"util.encodings".stringprep
.resourceprep
;
18 [" "] = "\\20"; ['"'] = "\\22";
19 ["&"] = "\\26"; ["'"] = "\\27";
20 ["/"] = "\\2f"; [":"] = "\\3a";
21 ["<"] = "\\3c"; [">"] = "\\3e";
22 ["@"] = "\\40"; ["\\"] = "\\5c";
25 for k
,v
in pairs(escapes
) do unescapes
[v
] = k
; end
30 local function split(jid
)
31 if not jid
then return; end
32 local node
, nodepos
= match(jid
, "^([^@/]+)@()");
33 local host
, hostpos
= match(jid
, "^([^@/]+)()", nodepos
);
34 if node
and not host
then return nil, nil, nil; end
35 local resource
= match(jid
, "^/(.+)$", hostpos
);
36 if (not host
) or ((not resource
) and #jid
>= hostpos
) then return nil, nil, nil; end
37 return node
, host
, resource
;
40 local function bare(jid
)
41 local node
, host
= split(jid
);
43 return node
.."@"..host
;
48 local function prepped_split(jid
)
49 local node
, host
, resource
= split(jid
);
50 if host
and host
~= "." then
51 if sub(host
, -1, -1) == "." then -- Strip empty root label
52 host
= sub(host
, 1, -2);
54 host
= nameprep(host
);
55 if not host
then return; end
57 node
= nodeprep(node
);
58 if not node
then return; end
61 resource
= resourceprep(resource
);
62 if not resource
then return; end
64 return node
, host
, resource
;
68 local function join(node
, host
, resource
)
69 if not host
then return end
70 if node
and resource
then
71 return node
.."@"..host
.."/"..resource
;
73 return node
.."@"..host
;
75 return host
.."/"..resource
;
80 local function prep(jid
)
81 local node
, host
, resource
= prepped_split(jid
);
82 return join(node
, host
, resource
);
85 local function compare(jid
, acl
)
86 -- compare jid to single acl rule
87 -- TODO compare to table of rules?
88 local jid_node
, jid_host
, jid_resource
= split(jid
);
89 local acl_node
, acl_host
, acl_resource
= split(acl
);
90 if ((acl_node
~= nil and acl_node
== jid_node
) or acl_node
== nil) and
91 ((acl_host
~= nil and acl_host
== jid_host
) or acl_host
== nil) and
92 ((acl_resource
~= nil and acl_resource
== jid_resource
) or acl_resource
== nil) then
98 local function node(jid
)
99 return (select(1, split(jid
)));
102 local function host(jid
)
103 return (select(2, split(jid
)));
106 local function resource(jid
)
107 return (select(3, split(jid
)));
110 local function escape(s
) return s
and (s
:gsub(".", escapes
)); end
111 local function unescape(s
) return s
and (s
:gsub("\\%x%x", unescapes
)); end
116 prepped_split
= prepped_split
;