-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitch-irc.js
265 lines (233 loc) · 7.41 KB
/
twitch-irc.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
var nt = require('net'),
rq = require('request'),
us = require('underscore');
var users = {};
/**
* Main function.
*/
var connect = function(conf, callback) {
var self = this;
/**
* Default configuration.
*/
self.config = {
autoreconnect: conf.autoreconnect || true,
channels: conf.channels || [],
names: conf.names || false,
server: conf.server || 'irc.twitch.tv',
port: conf.port || 6667,
nickname: conf.nickname || 'justinfan'+Math.floor((Math.random() * 80000) + 1000),
oauth: conf.oauth || '',
debug: conf.debug || false
};
/**
* Connect and send basic informations to the server.
*/
_connect(self.config);
/**
* Emitted when the server closes.
* Note that if connections exist, this event is not emitted until all connections are ended.
*/
connect.on('close', function() {
connect.emit('disconnected', 'Got disconnected from server.');
});
/**
* Emitted when an error occurs. The 'close' event will be called directly following this event.
*
* @param Error Object
*/
connect.on('error', function(err) {
connect.emit('disconnected', err.code);
});
/**
* Half-closes the socket. i.e., it sends a FIN packet.
* It is possible the server will still send some data.
*/
connect.on('end', function() {
connect.emit('disconnected', 'Client closed connection.');
});
connect.on("connected", function () {
connect.write('TWITCHCLIENT 3\r\n');
self.config.channels.forEach(function(channel) {
if (channel.charAt(0) !== '#') { channel = '#'+channel; }
connect.write('JOIN '+channel.toLowerCase()+'\r\n');
});
});
connect.on("disconnected", function (reason) {
connected = false;
if (self.config.autoreconnect) {
setTimeout( function() { _connect(self.config); }, 5000);
}
});
connect.on("join", function (channel) {
if (self.config.names) {
_chatters(channel, function(err, result) {
if (!err) {
var names = result.chatters;
connect.emit('names', channel, names);
}
});
}
});
var buffer = '';
connect.on("data", function (chunk) {
buffer += chunk;
var lines = buffer.split("\r\n");
buffer = lines.pop();
lines.forEach(function (line) {
var message = _handleMsg(line, self.config.debug);
try {
// Callback - Connected or not ?
if (message.indexOf('You are in a maze of twisty passages') >= 0) {
callback(null, connect);
connected = true;
connect.emit('connected');
}
if (message.indexOf('Login unsuccessful') >= 0) {
callback('Unable to connect to server. Verify your credentials.', connect);
connect.emit('disconnected', 'Unable to connect to server. Verify your credentials.');
}
connect.emit('raw', message);
} catch (e) {
throw e;
}
});
});
process.EventEmitter.call(this);
}
/**
* Connect and send basic informations to the server.
*/
function _connect(config) {
connect = nt.createConnection(config.port, config.server);
if (config.oauth !== '') { connect.write('PASS '+config.oauth+'\r\n'); }
connect.write('NICK '+config.nickname+'\r\n');
connect.write('USER '+config.nickname+' 8 * :'+config.nickname+'\r\n');
}
/**
* Add basic informations about a user.
*/
function _createUser(username) {
if (!users[username]) {
users[username] = {
username: username,
special: [],
color: '#696969',
emote: []
};
}
}
/**
* Handle RAW messages.
*/
function _handleMsg(line, debug) {
if (debug) {
console.log(line);
}
// Commands.
switch(line.split(" ")[0]) {
case 'PING':
connect.write('PONG\r\n');
break;
}
// Private messages and modes.
switch(line.split(" ")[1]) {
case 'PRIVMSG':
var from = line.split(" ")[0].split("!")[0].replace(':','');
var channel = line.split(" ")[2];
var msg = line.substr(line.indexOf(":",2) + 1);
_createUser(from);
if (from === 'twitchnotify' && msg.indexOf('just subscribed!')) {
connect.emit('subscribe', channel, msg.split(" ")[0]);
}
else if (from === 'jtv') {
if (msg.split(" ")[0] === 'SPECIALUSER') {
_createUser(msg.split(" ")[1]);
var special = users[msg.split(" ")[1]].special;
if (us.indexOf(special,msg.split(" ")[2]) < 0) { special.push(msg.split(" ")[2]); }
}
else if (msg.split(" ")[0] === 'USERCOLOR') {
_createUser(msg.split(" ")[1]);
users[msg.split(" ")[1]].color = msg.split(" ")[2];
}
else if (msg.split(" ")[0] === 'EMOTESET') {
_createUser(msg.split(" ")[1]);
users[msg.split(" ")[1]].emote = msg.split(" ")[2];
}
else if (msg.split(" ")[0] === 'CLEARCHAT') {
if (msg.split(" ")[1]) {
connect.emit('timeout', channel, msg.split(" ")[1]);
} else {
connect.emit('clearchat', channel);
}
}
else if (msg === 'This room is now in subscribers-only mode.') { connect.emit('submode', channel, true); }
else if (msg === 'This room is no longer in subscribers-only mode.') { connect.emit('submode', channel, false); }
else if (msg.indexOf('This room is now in slow mode. You may send messages every') === 0) { connect.emit('slowmode', channel, true); }
else if (msg === 'This room is no longer in slow mode.') { connect.emit('slowmode', channel, false); }
else if (msg.indexOf('This room is now in r9k mode.') === 0) { connect.emit('r9kmode', channel, true); }
else if (msg === 'This room is no longer in r9k mode.') { connect.emit('r9kmode', channel, false); }
}
else {
if (msg.split(" ")[0] === '\u0001ACTION') {
connect.emit('action', users[from], channel, msg.replace('\u0001ACTION ', '').replace('\u0001', ''));
} else {
connect.emit('chat', users[from], channel, msg);
}
}
break;
case 'MODE':
var channel = line.split(" ")[2];
var mode = line.split(" ")[3];
var username = line.split(" ")[4];
connect.emit('mode', channel, mode, username);
break;
case 'JOIN':
var channel = line.split(" ")[2];
connect.emit('join', channel);
break;
case 'PART':
var channel = line.split(" ")[2];
connect.emit('part', channel);
break;
}
return line;
}
/**
* Returns a list of all users connected on a channel.
*
* e.g: http://tmi.twitch.tv/group/user/lirik/chatters
*/
function _chatters(channel, cb) {
rq('http://tmi.twitch.tv/group/user/'+channel.replace('#','').toLowerCase()+'/chatters', function (error, response, body) {
var codes = [500,501,502,503,504,505];
if (!error && response.statusCode === 200 && us.indexOf(codes, response.statusCode) === -1) {
cb(null, JSON.parse(body));
} else { cb(response.statusCode, null); }
});
}
/**
* Commands..
*/
connect.prototype.action = function(channel, message) {
if (channel.charAt(0) !== '#') { channel = '#'+channel; }
connect.write('PRIVMSG '+channel.toLowerCase()+' :/me '+message+'\r\n');
}
connect.prototype.join = function(channel) {
if (channel.charAt(0) !== '#') { channel = '#'+channel; }
connect.write('JOIN '+channel.toLowerCase()+'\r\n');
}
connect.prototype.part = function(channel) {
if (channel.charAt(0) !== '#') { channel = '#'+channel; }
connect.write('PART '+channel.toLowerCase()+'\r\n');
}
connect.prototype.say = function(channel, message) {
if (channel.charAt(0) !== '#') { channel = '#'+channel; }
connect.write('PRIVMSG '+channel.toLowerCase()+' :'+message+'\r\n');
}
connect.prototype.send = function(channel, command) {
if (channel.charAt(0) !== '#') { channel = '#'+channel; }
if (command.charAt(0) !== '/') { command = '/'+command; }
connect.write('PRIVMSG '+channel.toLowerCase()+' :'+command+'\r\n');
}
exports.connect = connect;