Merge 0.10->trunk
[prosody.git] / util / jid.lua
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 --
8
9
10
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;
15
16 local escapes = {
17         [" "] = "\\20"; ['"'] = "\\22";
18         ["&"] = "\\26"; ["'"] = "\\27";
19         ["/"] = "\\2f"; [":"] = "\\3a";
20         ["<"] = "\\3c"; [">"] = "\\3e";
21         ["@"] = "\\40"; ["\\"] = "\\5c";
22 };
23 local unescapes = {};
24 for k,v in pairs(escapes) do unescapes[v] = k; end
25
26 local _ENV = nil;
27
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;
36 end
37
38 local function bare(jid)
39         return jid and match(jid, "^[^/]+");
40 end
41
42 local function prepped_split(jid)
43         local node, host, resource = split(jid);
44         if host then
45                 if sub(host, -1, -1) == "." then -- Strip empty root label
46                         host = sub(host, 1, -2);
47                 end
48                 host = nameprep(host);
49                 if not host then return; end
50                 if node then
51                         node = nodeprep(node);
52                         if not node then return; end
53                 end
54                 if resource then
55                         resource = resourceprep(resource);
56                         if not resource then return; end
57                 end
58                 return node, host, resource;
59         end
60 end
61
62 local function join(node, host, resource)
63         if not host then return end
64         if node and resource then
65                 return node.."@"..host.."/"..resource;
66         elseif node then
67                 return node.."@"..host;
68         elseif resource then
69                 return host.."/"..resource;
70         end
71         return host;
72 end
73
74 local function prep(jid)
75         local node, host, resource = prepped_split(jid);
76         return join(node, host, resource);
77 end
78
79 local function compare(jid, acl)
80         -- compare jid to single acl rule
81         -- TODO compare to table of rules?
82         local jid_node, jid_host, jid_resource = split(jid);
83         local acl_node, acl_host, acl_resource = split(acl);
84         if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and
85                 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and
86                 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then
87                 return true
88         end
89         return false
90 end
91
92 local function escape(s) return s and (s:gsub(".", escapes)); end
93 local function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end
94
95 return {
96         split = split;
97         bare = bare;
98         prepped_split = prepped_split;
99         join = join;
100         prep = prep;
101         compare = compare;
102         escape = escape;
103         unescape = unescape;
104 };