/
jid.js
93 lines (86 loc) · 2.17 KB
/
jid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* JID Manipulation
* @class
* @extends XC.Base
*
* @see <a href="http://xmpp.org/rfcs/rfc3920.html#addressing">RFC 3920: XMPP Core; Addressing</a>
*/
XC.Mixin.JID = XC.Base.extend(/** @lends XC.Mixin.JID# */{
/**
* @returns {String} The bare JID of the entity.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
* });
* mal.getBareJID();
* // -> 'mal@serenity.com'
*/
getBareJID: function () {
var ret = "";
ret += (this.getJIDParts().node) ? this.getJIDParts().node + "@" : "";
ret += this.getJIDParts().domain;
return ret;
},
/**
* @returns {String} The JID node (commonly used as a username).
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
* });
* mal.getJIDNode();
* // -> 'mal'
*/
getJIDNode: function () {
return this.getJIDParts().node;
},
/**
* @returns {String} The JID's domain.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
* });
* mal.getJIDDomain();
* // -> 'serenity.com'
*/
getJIDDomain: function () {
return this.getJIDParts().domain;
},
/**
* @returns {String} The resource of the JID.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
* });
* mal.getJIDResource();
* // -> 'persephone'
*/
getJIDResource: function () {
return this.getJIDParts().resource;
},
/**
* @returns {String} The parts of the JID.
* @example
* var mal = this.xc.Entity.extend({
* jid: 'mal@serenity.com/persephone'
* });
* mal.getJIDParts();
* // -> {
* // node: 'mal',
* // domain: 'serenity.com',
* // resource: 'persephone'
* // }
*/
getJIDParts: function () {
if (this.jid && this.jid._cachedJIDParts) {
return this.jid._cachedJIDParts;
}
var parts = this.jid.match(/^([^@\/]*(?=@)|)[@]?([^\/]*)[\/]?(.*)?$/),
ret = {
node: parts[1] || null,
domain: parts[2] || null,
resource: parts[3] || null
};
this.jid._cachedJIDParts = ret;
return ret;
}
});