Prepare required data folder for integration tests
[prosody.git] / util / jid.lua
blobec31f180c968b5ccd1120b5b03ee17d496f393fa
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
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;
17 local escapes = {
18 [" "] = "\\20"; ['"'] = "\\22";
19 ["&"] = "\\26"; ["'"] = "\\27";
20 ["/"] = "\\2f"; [":"] = "\\3a";
21 ["<"] = "\\3c"; [">"] = "\\3e";
22 ["@"] = "\\40"; ["\\"] = "\\5c";
24 local unescapes = {};
25 for k,v in pairs(escapes) do unescapes[v] = k; end
27 local _ENV = nil;
28 -- luacheck: std none
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;
38 end
40 local function bare(jid)
41 local node, host = split(jid);
42 if node and host then
43 return node.."@"..host;
44 end
45 return host;
46 end
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);
53 end
54 host = nameprep(host);
55 if not host then return; end
56 if node then
57 node = nodeprep(node);
58 if not node then return; end
59 end
60 if resource then
61 resource = resourceprep(resource);
62 if not resource then return; end
63 end
64 return node, host, resource;
65 end
66 end
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;
72 elseif node then
73 return node.."@"..host;
74 elseif resource then
75 return host.."/"..resource;
76 end
77 return host;
78 end
80 local function prep(jid)
81 local node, host, resource = prepped_split(jid);
82 return join(node, host, resource);
83 end
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
93 return true
94 end
95 return false
96 end
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
113 return {
114 split = split;
115 bare = bare;
116 prepped_split = prepped_split;
117 join = join;
118 prep = prep;
119 compare = compare;
120 node = node;
121 host = host;
122 resource = resource;
123 escape = escape;
124 unescape = unescape;