-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannelObject.js
481 lines (430 loc) · 11.4 KB
/
channelObject.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* @constructor
* @param name {String} Name of channel to be created
* @param {Number} [creationTime] the time the channel was created. Used to reconcile netsplits and such
*/
var channel = function (name, creationTime) {
this.name = name;
this.nameSafe = name.toLowerCase();
this.modes = '';
this.creationTime = parseInt(creationTime);
this.modeParams = [];
this.topic = '';
this.bans = [];
this.ban_exceptions = [];
this.users = {};
};
/**
* Returns the channel name, which is used in protocol messages as a target
* @returns {string}
*/
channel.prototype.getTargetString = function () {
return this.name;
};
/**
* Returns count of users in the channel
* @returns {Number}
*/
channel.prototype.membershipCount = function () {
return Object.keys(this.users).length
};
/**
* Using regex, determines if maskTest matches maskSource
*
* In maskSource, ? is "any integer" and * is a typical wildcard.
* @param maskTest {string}
* @param maskSource {String}
* @return {Boolean}
* @todo Implement support for extbans
*/
channel.prototype.maskMatches = function (maskTest, maskSource) {
if (maskSource.length == 0) {
// I am unsure why, but sometimes a zero-length string can get inserted into ban lists.
// Putting a zero-length string in the regex factory generates a weird pattern.
return false;
}
var pattern = new RegExp(maskSource.replace(/\?/, "[0-9]+").replace(/\*/, "(.*)"), 'ig');
return !!maskTest.match(pattern);
};
/**
* Adds a ban mask to the internal tracking
* @param mask
* @see banRemove
* @see banExists
*/
channel.prototype.banAdd = function (mask) {
if (mask.length > 0)
this.bans[this.bans.length] = mask.toLowerCase();
};
/**
* Tests if the channel has a specific mask banned
* @param mask
* @returns {boolean}
* @see banAdd
* @see banRemove
*/
channel.prototype.banExists = function (mask) {
for (var x = 0; x < this.bans.length; x++) {
if (mask.toLowerCase() == this.bans[x]) {
return true;
}
}
return false;
};
/**
* Removes a ban mask from the internal tracking
* @param mask
* @see banAdd
* @see banExists
*/
channel.prototype.banRemove = function (mask) {
this.bans = this.bans.filter(function (banSearch) {
return mask.toLowerCase() !== banSearch;
});
};
/**
* Gets an array of bans that match the mask specified
* @param mask
* @returns {Array}
* @see maskMatches
*/
channel.prototype.getMatchingBans = function (mask) {
var context = this;
return this.bans.filter(function (banSearch) {
return context.maskMatches(mask, banSearch);
});
};
/**
* Tests if a mask is banned
* @param mask
* @returns {boolean}
* @see getMatchingBans
*/
channel.prototype.isMaskBanned = function (mask) {
return (this.getMatchingBans(mask).length > 0);
};
/**
* Checks to see if a user is banned based off their mask
* @param user {user}
* @throws TypeError
* @returns {boolean}
*/
channel.prototype.isUserBanned = function (user) {
if (!core.isType_user(user)) {
throw new TypeError("Valid user instance expected");
}
return (this.isMaskBanned(user.getMask()));
};
/**
* Gets an array of exceptions that match the mask specified
* @param mask
* @returns {Array}
* @see maskMatches
*/
channel.prototype.getMatchingExceptions = function (mask) {
var context = this;
return this.ban_exceptions.filter(function (exceptionSearch) {
return context.maskMatches(mask, exceptionSearch);
});
};
/**
* Tests if a mask marked as an exception
* @param mask
* @returns {boolean}
* @see getMatchingExceptions
*/
channel.prototype.isMaskExcepted = function (mask) {
return (this.getMatchingExceptions(mask).length > 0);
};
/**
* Checks to see if a user is an exception based off their mask
* @param user {user}
* @throws TypeError
* @returns {boolean}
*/
channel.prototype.isUserExcepted = function (user) {
if (!core.isType_user(user)) {
throw new TypeError("Valid user instance expected.");
}
return (this.isMaskExcepted(user.getMask()));
};
/**
* Determines if a user can join the channel based on the ban and exception list
*
* If the user is not banned or if they are banned but match an exception, this returns true.
* @param user {user}
* @returns {boolean}
*/
channel.prototype.isJoinableBy = function (user) {
return (!this.isUserBanned(user) || (this.isUserBanned(user) && this.isUserExcepted(user)));
};
/**
* Adds an exception mask to the internal tracking
* @param mask
* @see banExceptionExists
* @see banExceptionRemove
*/
channel.prototype.banExceptionAdd = function (mask) {
this.ban_exceptions[this.ban_exceptions.length] = mask.toLowerCase();
};
/**
* Tests if the channel has a specific mask in its exception list
* @param mask
* @returns {boolean}
* @see banExceptionAdd
* @see banExceptionRemove
*/
channel.prototype.banExceptionExists = function (mask) {
for (var x = 0; x < this.ban_exceptions.length; x++) {
if (mask.toLowerCase() == this.ban_exceptions[x]) {
return true;
}
}
return false;
};
/**
* Removes a exception mask from the internal tracking
* @param mask
* @see banExceptionAdd
* @see banExceptionExists
*/
channel.prototype.banExceptionRemove = function (mask) {
this.ban_exceptions = this.ban_exceptions.filter(function (banSearch) {
return mask.toLowerCase() !== banSearch;
});
};
/**
* Tests if a user has a certain mode on the channel
* @param user {user}
* @param modeQuery {String}
* @returns {boolean}
* @throws TypeError
* @see userIsOp
* @see userIsHalfOp
* @see userIsVoice
*/
channel.prototype.userHasMode = function (user, modeQuery) {
if (!core.isType_user(user)) {
throw new Error("Valid user instance expected.");
}
if (this.hasUser(user) === false) {
return false;
}
var foundUser = this.users[user.numeric];
var splitModes = foundUser.mode.split('');
return (splitModes.indexOf(modeQuery) !== -1);
};
/**
* Tests if a user is op on the channel
* @param user {user}
* @returns {boolean}
* @see userHasMode
*/
channel.prototype.userIsOp = function (user) {
return this.userHasMode(user, 'o');
};
/**
* Checks to see if a user is half-op on the channel
* @param user {user}
* @returns {boolean}
* @see userHasMode
*/
channel.prototype.userIsHalfOp = function (user) {
return this.userHasMode(user, 'h');
};
/**
* Checks to see if the user is a voice on the channel
* @param user {user}
* @returns {boolean}
* @see userHasMode
*/
channel.prototype.userIsVoice = function (user) {
return this.userHasMode(user, 'v');
};
/**
* Internally registers a user as a member of the channel
* @param user {user} Targeted user
* @param modes {String} The channel-modes that was applied to the user
*/
channel.prototype.userJoin = function (user, modes) {
if (!core.isType_user(user)) {
throw new TypeError("Valid user instance expected.");
}
modes = (modes === undefined ? '' : modes);
this.users[user.numeric] = {'user': user, 'mode': modes};
user.channels[this.nameSafe] = {channel: this, mode: modes};
};
/**
* Internally deregisters a user as a member of the channel
* @throws TypeError
* @param user {user}
*/
channel.prototype.userPart = function (user) {
if (!core.isType_user(user)) {
throw new TypeError("Valid user instance expected.");
}
delete this.users[user.numeric];
delete user.channels[this.nameSafe];
if (this.membershipCount() === 0 && !this.isPersisted()) {
core.destroyChannel(this);
}
};
/**
* Tests if a user with a specified numeric is a member of the channel
* @param numeric {String}
* @returns {boolean}
*/
channel.prototype.hasUserNumeric = function (numeric) {
return (this.users[numeric] !== undefined);
};
/**
* Tests if a user object is a member of the channel
* @param user {user}
* @returns {boolean}
*/
channel.prototype.hasUser = function (user) {
if (!core.isType_user(user)) {
throw new TypeError("Valid user instance expected.");
}
for (var key in this.users) {
if (key === user.numeric) {
return true;
}
}
return false;
};
/**
* Tests if the channel has a certain mode set
* @param modeQuery {String}
* @returns {boolean}
*/
channel.prototype.hasMode = function (modeQuery) {
return (this.modes.split('').indexOf(modeQuery) !== -1);
};
/**
* Retrieves the parameter value of a specified mode
*
* Returns false if the channel does not have this param-mode set
* @param modeQuery
* @returns {String|Boolean}
*/
channel.prototype.getParameterModeValue = function (modeQuery) {
if (!this.hasMode(modeQuery) || typeof this.modeParams[modeQuery] !== "string") {
return false;
}
return this.modeParams[modeQuery];
};
/*
* Tests if the channel has mode +m
* @returns {boolean}
*/
channel.prototype.isModerated = function () {
return this.hasMode('m');
};
/**
* Tests if the channel has mode +r
* @returns {boolean}
*/
channel.prototype.isRegistered = function () {
return this.hasMode('r');
};
/**
* Tests if the channel has mode +i
* @returns {boolean}
*/
channel.prototype.isInviteOnly = function () {
return this.hasMode('i');
};
/**
* Tests if the channel has mode +s
* @returns {boolean}
*/
channel.prototype.isSecret = function () {
return this.hasMode('s');
};
/**
* Tests if the channel has mode +z
*
* If true, that means channel can be empty without being destroyed.
* @returns {boolean}
*/
channel.prototype.isPersisted = function () {
return this.hasMode('z');
}
/**
* Tests if the channel has mode +p
* @returns {boolean}
*/
channel.prototype.isPrivate = function () {
return this.hasMode('p');
};
/**
* Tests if the channel has mode +n
* @returns {boolean}
*/
channel.prototype.isNoExternalMsg = function () {
return this.hasMode('n');
};
/**
* Tests if the channel has mode +t
* @returns {boolean}
*/
channel.prototype.isTopicSetByOps = function () {
return this.hasMode('t');
};
/**
* Tests if the channel has mode +k
* @returns {boolean}
*/
channel.prototype.isKeyed = function () {
return this.hasMode('k');
};
/**
* Tests if the channel has mode +l
* @returns {boolean}
*/
channel.prototype.isLimited = function () {
return this.hasMode('l');
};
/**
* Tests if the channel has mode +L
* @returns {boolean}
*/
channel.prototype.isRedirected = function () {
return this.hasMode('L');
};
/**
* Retrieves the channel's key. If it does not have one set, false is returned
* @returns {String|Boolean}
* @see isKeyed
*/
channel.prototype.getChannelKey = function () {
if (!this.isKeyed()) {
return false;
}
return this.getParameterModeValue('k');
};
/**
* Retrieves the channel's limit. If it does not have one set, false is returned
* @returns {String|Boolean}
* @see isLimited
*/
channel.prototype.getChannelLimit = function () {
if (!this.isLimited()) {
return false;
}
var value = this.getParameterModeValue('l');
return (value != false ? parseInt(value) : false);
};
/**
* Retrieves the channel's overflow target. If it does not have one set, false is returned
* @returns {String|Boolean}
* @see isRedirected
*/
channel.prototype.getChannelOverflowTarget = function () {
return (this.isRedirected() ? this.getParameterModeValue('L') : false);
};
/**
* An instance of a channel
*/
module.exports = channel;