-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathirc-socket.js
182 lines (147 loc) · 4.91 KB
/
irc-socket.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
/**
*
* IRC Socket
*
* Socket that connects to an IRC network and emits each line from the server.
*
* Send messages to server with .raw(String) method.
*/
var net = require('net');
var tls = require('tls');
var events = require('events');
var util = require('util');
// var log = function (input, msg) {
// var date = new Date();
// console.log(Date().toString() + "|" + (input ? "<-" : "->") + "|" + msg);
// };
var create = function (prototype, properties) {
if (typeof properties == 'object') {
var props = {};
Object.keys(properties).forEach(function (key) {
props[key] = { value: properties[key] };
});
}
return Object.create(prototype, props);
};
var Socket = module.exports = function Socket (network, NetSocket) {
NetSocket = NetSocket || net.Socket;
var socket = create(Socket.prototype);
socket.port = network.port || 6667;
socket.netname = network.server;
socket.secure = network.secure || false;
socket.capab = network.capab || false;
socket.password = network.password || null;
socket.network = network;
socket.impl = new NetSocket();
socket.connected = false;
socket._setupEvents = function () {
var onData = function onData (data) {
lines = data.split('\r\n');
if (onData.buffer) {
lines[0] = onData.buffer + lines[0];
onData.buffer = null;
}
if (lines[lines.length - 1] !== "") {
onData.buffer = lines.pop();
}
lines
.filter(function (line) { return line !== ''; })
.filter(function (line) {
if (line.slice(0, 4) === 'PING') {
socket.raw(['PONG', line.slice(line.indexOf(':'))]);
return false;
}
return true;
})
.forEach(function (line) {
//log(true, line);
socket.emit('data', line);
});
};
onData.buffer = null;
void function readyEvent () {
var emitWhenReady = function (data) {
if (Socket.isReady(data)) {
socket.emit('ready');
}
};
socket.impl.on('data', emitWhenReady);
socket.on('ready', function remove () {
socket.impl.removeListener('data', emitWhenReady);
});
}();
void function connectEvent () {
var emitEvent = (socket.secure) ? 'secureConnect' : 'connect';
var emitWhenConnected = function () {
socket.connected = true;
if (socket.capab) {
socket.raw(["CAP", "LS"]);
}
if (socket.password !== null) {
socket.raw(["PASS", socket.password]);
}
socket.raw(["NICK", socket.network.nick]);
socket.raw(["USER", socket.network.user || "user", "8 * :" + socket.network.realname]);
};
socket.impl.on(emitEvent, emitWhenConnected);
}();
socket.impl.on('error', function () {
socket.connected = false;
socket.emit('error');
});
socket.impl.on('close', function () {
socket.connected = false;
socket.emit('close');
});
socket.impl.on('data', onData);
socket.impl.setEncoding('utf-8');
socket.impl.setNoDelay();
};
socket._setupEvents();
return socket;
};
Socket.isReady = function (data) {
// We are 'ready' when we get a 001 message.
return data.split('\r\n')
.filter(function (line) { return line !== ''; })
.some(function (line) { return line.split(' ')[1] === '001'; });
};
Socket.prototype = create(events.EventEmitter.prototype, {
connect : function () {
if (this.isConnected()) {
return;
}
if (this.secure) {
this.impl = tls.connect(this.port, this.netname, {rejectUnauthorized: false});
this._setupEvents();
// set rejectUnauthorized because most IRC servers don't have certified certificates anyway.
} else {
this.impl.connect(this.port, this.netname);
}
},
end : function () {
if (!this.isConnected()) {
return;
}
this.impl.end();
},
raw : function (message) {
if (!this.connected) {
return;
}
if (util.isArray(message)) {
message = message.join(" ");
}
if (message.indexOf('\n') !== -1) {
throw new Error('Newline detected in message. Use multiple raws instead.');
}
//log(false, message);
this.impl.write(message + '\r\n', 'utf-8');
},
isConnected : function () {
return this.connected;
},
getRealName : function () {
return this._realname;
}
});