Update copyright notices for 2009
[prosody.git] / util / jid.lua
1 -- Prosody IM v0.3
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 = string.match;
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 module "jid"
17
18 function split(jid)
19         if not jid then return; end
20         local node, nodepos = match(jid, "^([^@]+)@()");
21         local host, hostpos = match(jid, "^([^@/]+)()", nodepos)
22         if node and not host then return nil, nil, nil; end
23         local resource = match(jid, "^/(.+)$", hostpos);
24         if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end
25         return node, host, resource;
26 end
27
28 function bare(jid)
29         local node, host = split(jid);
30         if node and host then
31                 return node.."@"..host;
32         end
33         return host;
34 end
35
36 function prepped_split(jid)
37         local node, host, resource = split(jid);
38         if host then
39                 host = nameprep(host);
40                 if not host then return; end
41                 if node then
42                         node = nodeprep(node);
43                         if not node then return; end
44                 end
45                 if resource then
46                         resource = resourceprep(resource);
47                         if not resource then return; end
48                 end
49                 return node, host, resource;
50         end
51 end
52
53 function prep(jid)
54         local node, host, resource = prepped_split(jid);
55         if host then
56                 if node then
57                         host = node .. "@" .. host;
58                 end
59                 if resource then
60                         host = host .. "/" .. resource;
61                 end
62         end
63         return host;
64 end
65
66 return _M;