-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
220 lines (179 loc) · 5.74 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use strict';
var StringPrep = require('./lib/stringprep');
// All of our StringPrep fallbacks work correctly
// in the ASCII range, so we can reliably mark
// ASCII-only JIDs as prepped.
var ASCII = /^[\x00-\x7F]*$/;
function bareJID(local, domain) {
if (local) {
return local + '@' + domain;
}
return domain;
}
function fullJID(local, domain, resource) {
if (resource) {
return bareJID(local, domain) + '/' + resource;
}
return bareJID(local, domain);
}
exports.prep = function (data) {
var local = data.local;
var domain = data.domain;
var resource = data.resource;
var unescapedLocal = local;
if (local) {
local = StringPrep.nodeprep(local);
unescapedLocal = exports.unescape(local);
}
if (resource) {
resource = StringPrep.resourceprep(resource);
}
if (domain[domain.length - 1] === '.') {
domain = domain.slice(0, domain.length - 1);
}
domain = StringPrep.nameprep(domain.split('.').map(StringPrep.toUnicode).join('.'));
return {
prepped: data.prepped || StringPrep.available,
local: local,
domain: domain,
resource: resource,
bare: bareJID(local, domain),
full: fullJID(local, domain, resource),
unescapedLocal: unescapedLocal,
unescapedBare: bareJID(unescapedLocal, domain),
unescapedFull: fullJID(unescapedLocal, domain, resource)
};
};
exports.parse = function (jid, trusted) {
var local = '';
var domain = '';
var resource = '';
trusted = trusted || ASCII.test(jid);
var resourceStart = jid.indexOf('/');
if (resourceStart > 0) {
resource = jid.slice(resourceStart + 1);
jid = jid.slice(0, resourceStart);
}
var localEnd = jid.indexOf('@');
if (localEnd > 0) {
local = jid.slice(0, localEnd);
jid = jid.slice(localEnd + 1);
}
domain = jid;
var preppedJID = exports.prep({
local: local,
domain: domain,
resource: resource,
});
preppedJID.prepped = preppedJID.prepped || trusted;
return preppedJID;
};
exports.equal = function (jid1, jid2, requirePrep) {
jid1 = new exports.JID(jid1);
jid2 = new exports.JID(jid2);
if (arguments.length === 2) {
requirePrep = true;
}
return jid1.local === jid2.local &&
jid1.domain === jid2.domain &&
jid1.resource === jid2.resource &&
(requirePrep ? jid1.prepped && jid2.prepped : true);
};
exports.equalBare = function (jid1, jid2, requirePrep) {
jid1 = new exports.JID(jid1);
jid2 = new exports.JID(jid2);
if (arguments.length === 2) {
requirePrep = true;
}
return jid1.local === jid2.local &&
jid1.domain === jid2.domain &&
(requirePrep ? jid1.prepped && jid2.prepped : true);
};
exports.isBare = function (jid) {
jid = new exports.JID(jid);
var hasResource = !!jid.resource;
return !hasResource;
};
exports.isFull = function (jid) {
jid = new exports.JID(jid);
var hasResource = !!jid.resource;
return hasResource;
};
exports.escape = function (val) {
return val.replace(/^\s+|\s+$/g, '')
.replace(/\\5c/g, '\\5c5c')
.replace(/\\20/g, '\\5c20')
.replace(/\\22/g, '\\5c22')
.replace(/\\26/g, '\\5c26')
.replace(/\\27/g, '\\5c27')
.replace(/\\2f/g, '\\5c2f')
.replace(/\\3a/g, '\\5c3a')
.replace(/\\3c/g, '\\5c3c')
.replace(/\\3e/g, '\\5c3e')
.replace(/\\40/g, '\\5c40')
.replace(/ /g, '\\20')
.replace(/\"/g, '\\22')
.replace(/\&/g, '\\26')
.replace(/\'/g, '\\27')
.replace(/\//g, '\\2f')
.replace(/:/g, '\\3a')
.replace(/</g, '\\3c')
.replace(/>/g, '\\3e')
.replace(/@/g, '\\40');
};
exports.unescape = function (val) {
return val.replace(/\\20/g, ' ')
.replace(/\\22/g, '"')
.replace(/\\26/g, '&')
.replace(/\\27/g, '\'')
.replace(/\\2f/g, '/')
.replace(/\\3a/g, ':')
.replace(/\\3c/g, '<')
.replace(/\\3e/g, '>')
.replace(/\\40/g, '@')
.replace(/\\5c/g, '\\');
};
exports.create = function (local, domain, resource) {
return new exports.JID(local, domain, resource);
};
exports.JID = function JID(localOrJID, domain, resource) {
var parsed = {};
if (localOrJID && !domain && !resource) {
if (typeof localOrJID === 'string') {
parsed = exports.parse(localOrJID);
} else if (localOrJID._isJID || localOrJID instanceof exports.JID) {
parsed = localOrJID;
} else {
throw new Error('Invalid argument type');
}
} else if (domain) {
var trusted = ASCII.test(localOrJID) && ASCII.test(domain);
if (resource) {
trusted = trusted && ASCII.test(resource);
}
parsed = exports.prep({
local: exports.escape(localOrJID),
domain: domain,
resource: resource,
prepped: trusted
});
} else {
parsed = {};
}
this._isJID = true;
this.local = parsed.local || '';
this.domain = parsed.domain || '';
this.resource = parsed.resource || '';
this.bare = parsed.bare || '';
this.full = parsed.full || '';
this.unescapedLocal = parsed.unescapedLocal || '';
this.unescapedBare = parsed.unescapedBare || '';
this.unescapedFull = parsed.unescapedFull || '';
this.prepped = parsed.prepped;
};
exports.JID.prototype.toString = function () {
return this.full;
};
exports.JID.prototype.toJSON = function () {
return this.full;
};