comparison jid.js @ 27:b6e4bc19ff5a

undoing
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 28 Jan 2012 01:27:00 +0100
parents 06e3a883d3a3
children c06ec02217ee
comparison
equal deleted inserted replaced
19:fc577e5b2f4a 27:b6e4bc19ff5a
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 equals: function(aJID) {
42 if (!(aJID instanceof Lightstring.JID))
43 aJID = new Lightstring.JID(aJID);
44
45 return (this.node === aJID.node &&
46 this.domain === aJID.domain &&
47 this.resource === aJID.resource)
48 },
49
50 get bare() {
51 if (!this.domain)
52 return null;
53
54 if (this.node)
55 return this.node + '@' + this.domain;
56
57 return this.domain;
58 },
59
60 set bare(aJID) {
61 if (!aJID)
62 return;
63
64 var s = aJID.indexOf('/');
65 if (s != -1)
66 aJID = aJID.substring(0, s);
67
68 s = aJID.indexOf('@');
69 if (s == -1) {
70 this.node = null;
71 this.domain = aJID;
72 } else {
73 this.node = aJID.substring(0, s);
74 this.domain = aJID.substring(s+1);
75 }
76 },
77
78 get full() {
79 if (!this.domain)
80 return null;
81
82 var full = this.domain;
83
84 if (this.node)
85 full = this.node + '@' + full;
86
87 if (this.resource)
88 full = full + '/' + this.resource;
89
90 return full;
91 },
92
93 set full(aJID) {
94 if (!aJID)
95 return;
96
97 var s = aJID.indexOf('/');
98 if (s == -1)
99 this.resource = null;
100 else {
101 this.resource = aJID.substring(s+1);
102 aJID = aJID.substring(0, s);
103 }
104
105 s = aJID.indexOf('@');
106 if (s == -1) {
107 this.node = null;
108 this.domain = aJID;
109 } else {
110 this.node = aJID.substring(0, s);
111 this.domain = aJID.substring(s+1);
112 }
113 }
114 };