Mercurial > isshouni
comparison jid.js @ 0:156c2fd7c626
First commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 22 Feb 2012 19:57:56 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:156c2fd7c626 |
---|---|
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 */ | |
24 var JID = function(aJID) { | |
25 this.node = null; | |
26 this.domain = null; | |
27 this.resource = null; | |
28 | |
29 if (aJID) | |
30 this.full = aJID; | |
31 | |
32 //TODO: use a stringprep library to validate the input. | |
33 }; | |
34 | |
35 JID.prototype = { | |
36 toString: function() { | |
37 return this.full; | |
38 }, | |
39 | |
40 equals: function(aJID) { | |
41 if (!(aJID instanceof JID)) | |
42 aJID = new JID(aJID); | |
43 | |
44 return (this.node === aJID.node && | |
45 this.domain === aJID.domain && | |
46 this.resource === aJID.resource) | |
47 }, | |
48 | |
49 get bare() { | |
50 if (!this.domain) | |
51 return null; | |
52 | |
53 if (this.node) | |
54 return this.node + '@' + this.domain; | |
55 | |
56 return this.domain; | |
57 }, | |
58 | |
59 set bare(aJID) { | |
60 if (!aJID) | |
61 return; | |
62 | |
63 var s = aJID.indexOf('/'); | |
64 if (s != -1) | |
65 aJID = aJID.substring(0, s); | |
66 | |
67 s = aJID.indexOf('@'); | |
68 if (s == -1) { | |
69 this.node = null; | |
70 this.domain = aJID; | |
71 } else { | |
72 this.node = aJID.substring(0, s); | |
73 this.domain = aJID.substring(s+1); | |
74 } | |
75 }, | |
76 | |
77 get full() { | |
78 if (!this.domain) | |
79 return null; | |
80 | |
81 var full = this.domain; | |
82 | |
83 if (this.node) | |
84 full = this.node + '@' + full; | |
85 | |
86 if (this.resource) | |
87 full = full + '/' + this.resource; | |
88 | |
89 return full; | |
90 }, | |
91 | |
92 set full(aJID) { | |
93 if (!aJID) | |
94 return; | |
95 | |
96 var s = aJID.indexOf('/'); | |
97 if (s == -1) | |
98 this.resource = null; | |
99 else { | |
100 this.resource = aJID.substring(s+1); | |
101 aJID = aJID.substring(0, s); | |
102 } | |
103 | |
104 s = aJID.indexOf('@'); | |
105 if (s == -1) { | |
106 this.node = null; | |
107 this.domain = aJID; | |
108 } else { | |
109 this.node = aJID.substring(0, s); | |
110 this.domain = aJID.substring(s+1); | |
111 } | |
112 } | |
113 }; | |
114 | |
115 module.exports = JID; |