-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
65 lines (62 loc) · 1.62 KB
/
index.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
const tcp = require('net');
const Parser = require('./parser');
const EventEmitter = require('events');
const to = require('flush-write-stream');
class IRC extends EventEmitter {
constructor(options) {
super();
Object.assign(this, options);
this.socket = new tcp.Socket();
this.socket.pipe(Parser()).pipe(to.obj((message, enc, cb) => {
const { prefix, command, parameters } = message;
this.emit(command, parameters);
cb()
}));
}
connect() {
const { host, port } = this;
return new Promise(resolve => {
this.socket.connect(port, host, resolve);
});
}
write(command, parameters) {
this.socket.write([command].concat(parameters).join(' ') + '\r\n');
return this;
}
nick() {
const user = this;
return this.write('NICK', [user.nickname]);
}
user() {
const user = this;
const { username, hostname = '0', servername = '*', realname } = user;
return this.write('USER', [username, hostname, servername, `:${realname}`]);
}
join(channel) {
return this.write('JOIN', [channel]);
}
send(message, to) {
return this.write('PRIVMSG', [to, `:${message}`]);
}
ping() {
const now = Date.now();
return this.write('PING', [now]);
}
pong(x) {
return this.write('PING', x);
}
notice(message, to) {
return this.write('NOTICE', [to, `:${message}`]);
}
part(channel, message) {
return this.write('PART', [channel, message]);
}
quit(message) {
return this.write('QUIT', [`:${message}`]);
}
}
IRC.Server = require('./server');
IRC.createServer = options => {
return new IRC.Server(options);
};
module.exports = IRC;