forked from ktiedt/NodeJS-IRC-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.js
86 lines (75 loc) · 2.16 KB
/
channel.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
/**
* Channel Class
*
* @author Karl Tiedt
* @website http://twitter.com/ktiedt
* @copyright
* @author Michael Owens
* @website http://www.michaelowens.nl
* @copyright Michael Owens 2011
*/
var sys = require('util');
// to prevent onReply from happening more than once
var _init = false;
exports.initialize = function(irc) {
var _this = irc;
irc.on('numeric', function(msg) {
var command = msg.command;
if (command !== '353') {
return;
}
var chan = msg.arguments[2],
// replace + and @ prefixes for channel privs, we only want the nick
nicks = msg.arguments[3].replace(/\+|@/g, '').split(' '),
chans = _this.channels,
user = null,
allusers = _this.users;
// TODO: support all channel prefixes - need to find proper documentation to list these
if (!chan || chan.charAt(0) !== '#') {
return;
}
chan = chans[chan];
for(var i=0; i<nicks.length; i++) {
user = allusers[nicks[i]];
if (!user) {
user = allusers[nicks[i]] = new _this.userObj(_this, nicks[i]);
}
user.join(chan.name);
}
});
};
Channel = exports.Channel = function(irc, room, join, password) {
this.irc = irc;
this.name = room;
this.inRoom = false;
this.password = password;
this.users = [];
if (join ) {
this.join();
}
};
Channel.prototype.join = function() {
var chans = this.irc.channels;
chans[this.name] = this;
this.irc.raw('JOIN', this.name, this.password);
this.inRoom = true;
};
Channel.prototype.part = function(msg) {
var user = null,
users = [].concat(this.users),
userCount = users.length,
allusers = this.irc.users,
chans = this.irc.channels;
this.irc.raw('PART', this.name, ':' + msg);
this.inRoom = false;
for(var i=0; i<userCount;i++) {
user = allusers[users[i]];
// if user is only in 1 channel and channel is this one
if (user.isOn(this.name)) {
user.part(this);
}
}
};
Channel.prototype.send = function(msg) {
this.irc.send(this.name, msg);
};