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 match, sub = string.match, string.sub;
12 local nodeprep = require "util.encodings".stringprep.nodeprep;
13 local nameprep = require "util.encodings".stringprep.nameprep;
14 local resourceprep = require "util.encodings".stringprep.resourceprep;
17 [" "] = "\\20"; ['"'] = "\\22";
18 ["&"] = "\\26"; ["'"] = "\\27";
19 ["/"] = "\\2f"; [":"] = "\\3a";
20 ["<"] = "\\3c"; [">"] = "\\3e";
21 ["@"] = "\\40"; ["\\"] = "\\5c";
24 for k,v in pairs(escapes) do unescapes[v] = k; end
28 local function split(jid)
29 if not jid then return; end
30 local node, nodepos = match(jid, "^([^@/]+)@()");
31 local host, hostpos = match(jid, "^([^@/]+)()", nodepos)
32 if node and not host then return nil, nil, nil; end
33 local resource = match(jid, "^/(.+)$", hostpos);
34 if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end
35 return node, host, resource;
38 local function bare(jid)
39 local node, host = split(jid);
41 return node.."@"..host;
46 local function prepped_split(jid)
47 local node, host, resource = split(jid);
49 if sub(host, -1, -1) == "." then -- Strip empty root label
50 host = sub(host, 1, -2);
52 host = nameprep(host);
53 if not host then return; end
55 node = nodeprep(node);
56 if not node then return; end
59 resource = resourceprep(resource);
60 if not resource then return; end
62 return node, host, resource;
66 local function join(node, host, resource)
67 if not host then return end
68 if node and resource then
69 return node.."@"..host.."/"..resource;
71 return node.."@"..host;
73 return host.."/"..resource;
78 local function prep(jid)
79 local node, host, resource = prepped_split(jid);
80 return join(node, host, resource);
83 local function compare(jid, acl)
84 -- compare jid to single acl rule
85 -- TODO compare to table of rules?
86 local jid_node, jid_host, jid_resource = split(jid);
87 local acl_node, acl_host, acl_resource = split(acl);
88 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and
89 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and
90 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then
96 local function escape(s) return s and (s:gsub(".", escapes)); end
97 local function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end
102 prepped_split = prepped_split;