Software /
code /
prosody
Comparison
util/jid.lua @ 6777:5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 21 Feb 2015 10:36:37 +0100 |
parent | 6340:7e820979fd9b |
child | 6889:7f7920f2aebf |
comparison
equal
deleted
inserted
replaced
6774:3965662ae091 | 6777:5de6b93d0190 |
---|---|
21 ["@"] = "\\40"; ["\\"] = "\\5c"; | 21 ["@"] = "\\40"; ["\\"] = "\\5c"; |
22 }; | 22 }; |
23 local unescapes = {}; | 23 local unescapes = {}; |
24 for k,v in pairs(escapes) do unescapes[v] = k; end | 24 for k,v in pairs(escapes) do unescapes[v] = k; end |
25 | 25 |
26 module "jid" | 26 local _ENV = nil; |
27 | 27 |
28 local function _split(jid) | 28 local function split(jid) |
29 if not jid then return; end | 29 if not jid then return; end |
30 local node, nodepos = match(jid, "^([^@/]+)@()"); | 30 local node, nodepos = match(jid, "^([^@/]+)@()"); |
31 local host, hostpos = match(jid, "^([^@/]+)()", nodepos) | 31 local host, hostpos = match(jid, "^([^@/]+)()", nodepos) |
32 if node and not host then return nil, nil, nil; end | 32 if node and not host then return nil, nil, nil; end |
33 local resource = match(jid, "^/(.+)$", hostpos); | 33 local resource = match(jid, "^/(.+)$", hostpos); |
34 if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end | 34 if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end |
35 return node, host, resource; | 35 return node, host, resource; |
36 end | 36 end |
37 split = _split; | |
38 | 37 |
39 function bare(jid) | 38 local function bare(jid) |
40 return jid and match(jid, "^[^/]+"); | 39 return jid and match(jid, "^[^/]+"); |
41 end | 40 end |
42 | 41 |
43 local function _prepped_split(jid) | 42 local function prepped_split(jid) |
44 local node, host, resource = _split(jid); | 43 local node, host, resource = split(jid); |
45 if host then | 44 if host then |
46 if sub(host, -1, -1) == "." then -- Strip empty root label | 45 if sub(host, -1, -1) == "." then -- Strip empty root label |
47 host = sub(host, 1, -2); | 46 host = sub(host, 1, -2); |
48 end | 47 end |
49 host = nameprep(host); | 48 host = nameprep(host); |
57 if not resource then return; end | 56 if not resource then return; end |
58 end | 57 end |
59 return node, host, resource; | 58 return node, host, resource; |
60 end | 59 end |
61 end | 60 end |
62 prepped_split = _prepped_split; | |
63 | 61 |
64 local function _join(node, host, resource) | 62 local function join(node, host, resource) |
65 if not host then return end | 63 if not host then return end |
66 if node and resource then | 64 if node and resource then |
67 return node.."@"..host.."/"..resource; | 65 return node.."@"..host.."/"..resource; |
68 elseif node then | 66 elseif node then |
69 return node.."@"..host; | 67 return node.."@"..host; |
70 elseif resource then | 68 elseif resource then |
71 return host.."/"..resource; | 69 return host.."/"..resource; |
72 end | 70 end |
73 return host; | 71 return host; |
74 end | 72 end |
75 join = _join; | |
76 | 73 |
77 function prep(jid) | 74 local function prep(jid) |
78 local node, host, resource = _prepped_split(jid); | 75 local node, host, resource = prepped_split(jid); |
79 return _join(node, host, resource); | 76 return join(node, host, resource); |
80 end | 77 end |
81 | 78 |
82 function compare(jid, acl) | 79 local function compare(jid, acl) |
83 -- compare jid to single acl rule | 80 -- compare jid to single acl rule |
84 -- TODO compare to table of rules? | 81 -- TODO compare to table of rules? |
85 local jid_node, jid_host, jid_resource = _split(jid); | 82 local jid_node, jid_host, jid_resource = split(jid); |
86 local acl_node, acl_host, acl_resource = _split(acl); | 83 local acl_node, acl_host, acl_resource = split(acl); |
87 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and | 84 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and |
88 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and | 85 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and |
89 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then | 86 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then |
90 return true | 87 return true |
91 end | 88 end |
92 return false | 89 return false |
93 end | 90 end |
94 | 91 |
95 function escape(s) return s and (s:gsub(".", escapes)); end | 92 local function escape(s) return s and (s:gsub(".", escapes)); end |
96 function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end | 93 local function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end |
97 | 94 |
98 return _M; | 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 }; |