forked from ktiedt/NodeJS-IRC-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
113 lines (96 loc) · 2.72 KB
/
user.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
/**
* User Class
*
* @author Karl Tiedt
* @website http://twitter.com/ktiedt
* @copyright Karl Tiedt 2011
*/
var sys = require('util');
User = exports.User = function(irc, /* fully qualified irc hostmask - nick!ident@host */ mask) {
this.irc = irc;
this.channels = [];
this.passive = true;
this.nick = '';
this.ident = '';
this.host = '';
this.update(mask)
};
User.prototype.update = function(mask) {
var match = mask.match(/([^!]+)!([^@]+)@(.+)/);
if (!match && this.passive === true) {
// this happens when only a nick is available IE: on join's 353 numeric
this.nick = mask;
this.ident = '';
this.host = '';
} else {
this.passive = false;
this.nick = match[1];
this.ident = match[2];
this.host = match[3];
}
this.nick = this.nick.replace(/\+|@/, '');
};
User.prototype.changeNick = function(newnick) {
var irc = this.irc,
allusers = irc.users,
oldnick = this.nick,
user = allusers[oldnick],
userchans = user.channels,
allchans = irc.channels;
userchans.forEach(function(channel) {
var chan = allchans[channel],
idx = chan.users.indexOf(oldnick);
if (idx !== -1) {
chan.users[idx] = newnick;
}
});
allusers[newnick] = user;
delete allusers[oldnick];
console.log("CHANGENICK: ", allusers);
};
User.prototype.join = function(channel) {
if (!channel) {
console.log("FAIL USER JOIN: ", this.nick);
return;
}
var channels = this.channels,
chan = this.irc.channels[channel];
if (!this.isOn(channel)) {
this.channels.push(channel);
chan.users.push(this.nick);
}
};
User.prototype.part = function(/* string or Channel object */ channel) {
if (typeof channel === "object") {
channel = channel.name;
}
if (!channel) {
console.log("FAIL USER PART: ", this.nick);
return;
}
var channels = this.channels,
irc = this.irc,
allchans = irc.channels,
allusers = irc.users,
chan = allchans[channel];
if (this.isOn(channel)) {
chan.users.splice(chan.users.indexOf(this.nick), 1);
channels.splice(channels.indexOf(channel), 1);
if (chan.users.length === 0) {
delete allchans[channel];
}
}
if (this.channels.length == 0 && this.nick !== irc.nick) {
delete allusers[this.nick];
}
};
User.prototype.isOn = function(channel) {
var chans = this.channels;
return !(chans.indexOf(channel) === -1);
};
User.prototype.msg = function(target, msg) {
this.irc.send(target, msg);
};
User.prototype.send = function(msg) {
this.irc.send(this.nick, msg);
};