es2015 bitches

This commit is contained in:
Flummi 2017-11-08 21:59:54 +01:00
parent fdea6de52c
commit a32efd38b4

View File

@ -3,23 +3,21 @@ var tls = require("tls");
var EventEmitter = require("events").EventEmitter; var EventEmitter = require("events").EventEmitter;
var util = require("util"); var util = require("util");
function irc(options) { class irc {
constructor(options) {
EventEmitter.call(this); EventEmitter.call(this);
this.options = options || {}; this.options = options || {};
this.options.channels = this.options.channels || []; this.options.channels = this.options.channels || [];
this.options.host = this.options.host || "127.0.0.1"; this.options.host = this.options.host || "127.0.0.1";
this.options.port = this.options.port || 6667; this.options.port = this.options.port || 6667;
this.options.ssl = this.options.ssl || false; this.options.ssl = this.options.ssl || false;
this.options.selfSigned = this.options.selfSigned || false; this.options.selfSigned = this.options.selfSigned || false;
this.network = this.options.network || "test"; this.network = this.options.network || "test";
this.nickname = this.options.nickname || "test"; this.nickname = this.options.nickname || "test";
this.username = this.options.username || "test"; this.username = this.options.username || "test";
this.realname = this.options.realname || "test"; this.realname = this.options.realname || "test";
this.channels = this.options.channels || []; this.channels = this.options.channels || [];
this.socket = (this.options.ssl ? tls : net).connect({
this.socket = (this.options.ssl?tls:net).connect({
host: this.options.host, host: this.options.host,
port: this.options.port, port: this.options.port,
rejectUnauthorized: !this.options.selfSigned rejectUnauthorized: !this.options.selfSigned
@ -28,12 +26,11 @@ function irc(options) {
this.send("USER ", this.username, ' 0 * :', this.realname); this.send("USER ", this.username, ' 0 * :', this.realname);
}); });
this.socket.setEncoding('utf-8'); this.socket.setEncoding('utf-8');
this.socket.on('data', msg => { this.socket.on('data', msg => {
msg = this.parse(msg); msg = this.parse(msg);
switch(msg.command) { switch (msg.command) {
case "464": // Password required case "464":// Password required
if(this.options.password.length > 0) if (this.options.password.length > 0)
this.send("PASS ", this.options.password); // pwd this.send("PASS ", this.options.password); // pwd
break; break;
case "PING": case "PING":
@ -44,42 +41,42 @@ function irc(options) {
break; break;
} }
}); });
} }
send() {
irc.prototype.send = function send() { for (let i = 0; i < arguments.length; i++)
for(let i = 0; i < arguments.length; i++)
this.socket.write(arguments[i]); this.socket.write(arguments[i]);
this.socket.write('\n'); this.socket.write('\n');
} }
parse(data) {
irc.prototype.parse = function parse(data) {
var raw = data; var raw = data;
if(data.charAt(0) === ':') { if (data.charAt(0) === ':') {
var i = data.indexOf(' '); var i = data.indexOf(' ');
var prefix = data.slice(1, i); var prefix = data.slice(1, i);
data = data.slice(i + 1); data = data.slice(i + 1);
} }
var i = data.indexOf(' '); var i = data.indexOf(' ');
if(i === -1) i = data.length; if (i === -1)
i = data.length;
var command = data.slice(0, i); var command = data.slice(0, i);
data = data.slice(i + 1); data = data.slice(i + 1);
var params = []; var params = [];
while(data && data.charAt(0) !== ':') { while (data && data.charAt(0) !== ':') {
var i = data.indexOf(' '); var i = data.indexOf(' ');
if (i === -1) i = data.length; if (i === -1)
i = data.length;
params.push(data.slice(0, i)); params.push(data.slice(0, i));
data = data.slice(i + 1); data = data.slice(i + 1);
} }
if (data) params.push(data.slice(1)); if (data)
params.push(data.slice(1));
return { return {
raw: raw, raw: raw,
prefix: prefix, prefix: prefix,
command: command, command: command,
params: params params: params
}; };
} }
reply(tmp) {
irc.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark',
let usertmp = tmp.prefix.split("!"); let usertmp = tmp.prefix.split("!");
let hosttmp = usertmp[1].split("@"); let hosttmp = usertmp[1].split("@");
let user = { let user = {
@ -105,7 +102,8 @@ irc.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark',
//rpc.emit('call', this.network, 'notice', [this.channel, msg]); //rpc.emit('call', this.network, 'notice', [this.channel, msg]);
} }
}; };
}; }
}
module.exports = irc; module.exports = irc;
util.inherits(irc, EventEmitter); util.inherits(irc, EventEmitter);