comparison jid.js @ 22:6a6bb8ded046

Add a JID object, and use it in Lightstring.Connection.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Jan 2012 22:11:51 +0100 (2012-01-26)
parents
children 06e3a883d3a3
comparison
equal deleted inserted replaced
21:b7d52bf259e0 22:6a6bb8ded046
1 'use strict';
2
3 /**
4 Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20 /**
21 * @constructor Creates a new JID object.
22 * @param {String} [aJID] The host, bare or full JID.
23 * @memberOf Lightstring
24 */
25 Lightstring.JID = function(aJID) {
26 this.node = null;
27 this.domain = null;
28 this.resource = null;
29
30 if (aJID)
31 this.full = aJID;
32
33 //TODO: use a stringprep library to validate the input.
34 };
35
36 Lightstring.JID.prototype = {
37 toString: function() {
38 return this.full;
39 },
40
41 get bare() {
42 if (!this.domain)
43 return null;
44
45 if (this.node)
46 return this.node + '@' + this.domain;
47
48 return this.domain;
49 },
50
51 set bare(aJID) {
52 if (!aJID)
53 return;
54
55 var s = aJID.indexOf('/');
56 if (s != -1)
57 aJID = aJID.substring(0, s);
58
59 s = aJID.indexOf('@');
60 if (s == -1) {
61 this.node = null;
62 this.domain = aJID;
63 } else {
64 this.node = aJID.substring(0, s);
65 this.domain = aJID.substring(s+1);
66 }
67 },
68
69 get full() {
70 if (!this.domain)
71 return null;
72
73 var full = this.domain;
74
75 if (this.node)
76 full = this.node + '@' + full;
77
78 if (this.resource)
79 full = full + '/' + this.resource;
80
81 return full;
82 },
83
84 set full(aJID) {
85 if (!aJID)
86 return;
87
88 var s = aJID.indexOf('/');
89 if (s == -1)
90 this.resource = null;
91 else {
92 this.resource = aJID.substring(s+1);
93 aJID = aJID.substring(0, s);
94 }
95
96 s = aJID.indexOf('@');
97 if (s == -1) {
98 this.node = null;
99 this.domain = aJID;
100 } else {
101 this.node = aJID.substring(0, s);
102 this.domain = aJID.substring(s+1);
103 }
104 }
105 };