es2015 bitches

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

View File

@ -3,109 +3,107 @@ 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 {
EventEmitter.call(this); constructor(options) {
this.options = options || {}; EventEmitter.call(this);
this.options.channels = this.options.channels || []; this.options = options || {};
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({
host: this.options.host,
this.socket = (this.options.ssl?tls:net).connect({ port: this.options.port,
host: this.options.host, rejectUnauthorized: !this.options.selfSigned
port: this.options.port, }, () => {
rejectUnauthorized: !this.options.selfSigned this.send("NICK ", this.nickname);
}, () => { this.send("USER ", this.username, ' 0 * :', this.realname);
this.send("NICK ", this.nickname); });
this.send("USER ", this.username, ' 0 * :', this.realname); this.socket.setEncoding('utf-8');
}); this.socket.on('data', msg => {
this.socket.setEncoding('utf-8'); msg = this.parse(msg);
switch (msg.command) {
this.socket.on('data', msg => { case "464":// Password required
msg = this.parse(msg); if (this.options.password.length > 0)
switch(msg.command) { this.send("PASS ", this.options.password); // pwd
case "464": // Password required break;
if(this.options.password.length > 0) case "PING":
this.send("PASS ", this.options.password); // pwd this.send("PONG ", msg.params.join``);
break; break;
case "PING": case "PRIVMSG":
this.send("PONG ", msg.params.join``); this.emit('message', this.reply(msg));
break; break;
case "PRIVMSG": }
this.emit('message', this.reply(msg)); });
break;
}
});
}
irc.prototype.send = function send() {
for(let i = 0; i < arguments.length; i++)
this.socket.write(arguments[i]);
this.socket.write('\n');
}
irc.prototype.parse = function parse(data) {
var raw = data;
if(data.charAt(0) === ':') {
var i = data.indexOf(' ');
var prefix = data.slice(1, i);
data = data.slice(i + 1);
} }
var i = data.indexOf(' '); send() {
if(i === -1) i = data.length; for (let i = 0; i < arguments.length; i++)
var command = data.slice(0, i); this.socket.write(arguments[i]);
data = data.slice(i + 1); this.socket.write('\n');
var params = [];
while(data && data.charAt(0) !== ':') {
var i = data.indexOf(' ');
if (i === -1) i = data.length;
params.push(data.slice(0, i));
data = data.slice(i + 1);
} }
if (data) params.push(data.slice(1)); parse(data) {
return { var raw = data;
raw: raw, if (data.charAt(0) === ':') {
prefix: prefix, var i = data.indexOf(' ');
command: command, var prefix = data.slice(1, i);
params: params data = data.slice(i + 1);
};
}
irc.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark',
let usertmp = tmp.prefix.split("!");
let hosttmp = usertmp[1].split("@");
let user = {
nick: usertmp[0],
username: hosttmp[0],
hostname: hosttmp[1]
};
return {
type: "irc",
network: this.network,
channel: tmp.params[0],
user: user,
message: tmp.params[1].replace(/\r?\n|\r/g, ''),
time: Date.now(),
reply: msg => {
this.send(`PRIVMSG ${tmp.params[0]} ${msg}`);
},
replyAction: msg => {
this.send(`PRIVMSG ${tmp.params[0]} \u0001ACTION ${msg}\u0001`);
},
replyNotice: msg => {
this.send(`NOTICE ${tmp.params[0]} ${msg}`);
//rpc.emit('call', this.network, 'notice', [this.channel, msg]);
} }
}; var i = data.indexOf(' ');
}; if (i === -1)
i = data.length;
var command = data.slice(0, i);
data = data.slice(i + 1);
var params = [];
while (data && data.charAt(0) !== ':') {
var i = data.indexOf(' ');
if (i === -1)
i = data.length;
params.push(data.slice(0, i));
data = data.slice(i + 1);
}
if (data)
params.push(data.slice(1));
return {
raw: raw,
prefix: prefix,
command: command,
params: params
};
}
reply(tmp) {
let usertmp = tmp.prefix.split("!");
let hosttmp = usertmp[1].split("@");
let user = {
nick: usertmp[0],
username: hosttmp[0],
hostname: hosttmp[1]
};
return {
type: "irc",
network: this.network,
channel: tmp.params[0],
user: user,
message: tmp.params[1].replace(/\r?\n|\r/g, ''),
time: Date.now(),
reply: msg => {
this.send(`PRIVMSG ${tmp.params[0]} ${msg}`);
},
replyAction: msg => {
this.send(`PRIVMSG ${tmp.params[0]} \u0001ACTION ${msg}\u0001`);
},
replyNotice: msg => {
this.send(`NOTICE ${tmp.params[0]} ${msg}`);
//rpc.emit('call', this.network, 'notice', [this.channel, msg]);
}
};
}
}
module.exports = irc; module.exports = irc;
util.inherits(irc, EventEmitter); util.inherits(irc, EventEmitter);