Mercurial > eldonilo > lightstring
annotate jid.js @ 108:5cb4733c5189
many api changes
author | Sonny Piers <sonny@fastmail.net> |
---|---|
date | Fri, 13 Jul 2012 15:26:18 +0200 |
parents | c06ec02217ee |
children |
rev | line source |
---|---|
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 'use strict'; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 /** |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 Permission to use, copy, modify, and/or distribute this software for any |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 purpose with or without fee is hereby granted, provided that the above |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 copyright notice and this permission notice appear in all copies. |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 */ |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
108 | 19 //https://tools.ietf.org/html/rfc6122 |
20 | |
21 (function() { | |
22 | |
23 | |
24 if (typeof define !== 'undefined') { | |
25 define(function() { | |
26 //return an object to define the "my/shirt" module. | |
27 return JID; | |
28 }); | |
29 } | |
30 else { | |
31 Lightstring.JID = JID; | |
32 } | |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 /** |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 * @constructor Creates a new JID object. |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 * @param {String} [aJID] The host, bare or full JID. |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 * @memberOf Lightstring |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 */ |
108 | 39 var JID = function(aJID) { |
40 this.local = null; | |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 this.domain = null; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 this.resource = null; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 if (aJID) |
106 | 45 this.full = aJID.toString(); |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 //TODO: use a stringprep library to validate the input. |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 }; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
108 | 50 JID.prototype = { |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 toString: function() { |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 return this.full; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 }, |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 |
25
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
55 equals: function(aJID) { |
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
56 if (!(aJID instanceof Lightstring.JID)) |
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
57 aJID = new Lightstring.JID(aJID); |
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
58 |
108 | 59 return (this.local === aJID.local && |
25
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
60 this.domain === aJID.domain && |
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
61 this.resource === aJID.resource) |
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
62 }, |
06e3a883d3a3
Add a JID comparison function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
63 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 get bare() { |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 if (!this.domain) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 return null; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 |
108 | 68 if (this.local) |
69 return this.local + '@' + this.domain; | |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 return this.domain; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 }, |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 set bare(aJID) { |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 if (!aJID) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 return; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 var s = aJID.indexOf('/'); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 if (s != -1) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 aJID = aJID.substring(0, s); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 s = aJID.indexOf('@'); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 if (s == -1) { |
108 | 84 this.local = null; |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 this.domain = aJID; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 } else { |
108 | 87 this.local = aJID.substring(0, s); |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 this.domain = aJID.substring(s+1); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 } |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 }, |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 get full() { |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 if (!this.domain) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 return null; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 var full = this.domain; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 |
108 | 98 if (this.local) |
99 full = this.local + '@' + full; | |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
100 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
101 if (this.resource) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 full = full + '/' + this.resource; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
104 return full; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
105 }, |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 set full(aJID) { |
106 | 108 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
109 if (!aJID) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
110 return; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
111 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
112 var s = aJID.indexOf('/'); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
113 if (s == -1) |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
114 this.resource = null; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
115 else { |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 this.resource = aJID.substring(s+1); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
117 aJID = aJID.substring(0, s); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
118 } |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
119 |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
120 s = aJID.indexOf('@'); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
121 if (s == -1) { |
108 | 122 this.local = null; |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 this.domain = aJID; |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
124 } else { |
108 | 125 this.local = aJID.substring(0, s); |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
126 this.domain = aJID.substring(s+1); |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
127 } |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 } |
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
129 }; |
108 | 130 })(); |
131 |