fick ircparser.. fix'd

This commit is contained in:
Flummi
2017-11-19 20:16:25 +01:00
parent 9553ce1701
commit 08eb84842a
3 changed files with 234 additions and 185 deletions

View File

@ -1,8 +1,7 @@
const net = require("net")
, tls = require("tls")
, EventEmitter = require("events").EventEmitter
, util = require("util")
, parser = require("parse-irc");
, util = require("util");
class irc {
constructor(options) {
@ -20,7 +19,6 @@ class irc {
this.realname = this.options.realname || "test";
this.channels = this.options.channels || [];
this._recachetime = 60 * 5; // 10 minutes
this.stream = parser();
this.server = {
motd: "",
sasl: {},
@ -39,169 +37,13 @@ class irc {
this.send("CAP LS");
//this.send("CAP REQ sasl");
}
}).pipe(this.stream);
});
this.socket.setEncoding("utf-8");
this.stream.on("data", msg => {
//msg = this.parse(msg);
let tmpuser = {};
let tmpchan = {};
let chans = [];
let prefix = "";
if(msg.command != "372")
console.log(msg);
switch (msg.command) { // auslagern!
// WHOIS BEGIN
case "307": // Rizon Account
tmpuser = {};
if(this.server.user.hasi( msg.params[1] ))
tmpuser = this.server.user.geti( msg.params[1] );
tmpuser.account = msg.params[1];
tmpuser.registered = true;
this.server.user.set( msg.params[1], tmpuser );
break;
case "311": // first
tmpuser = {};
if (this.server.user.hasi( msg.params[1] ))
tmpuser = this.server.user.geti( msg.params[1] );
tmpuser.nickname = msg.params[1];
tmpuser.username = msg.params[2];
tmpuser.hostname = msg.params[3];
tmpuser.realname = msg.params[5];
this.server.user.set( msg.params[1], tmpuser );
break;
case "313": // Oper
tmpuser = {};
if (this.server.user.hasi( msg.params[1] ))
tmpuser = this.server.user.geti( msg.params[1] );
tmpuser.oper = true;
this.server.user.set( msg.params[1], tmpuser );
break;
case "318": // last (check Data)
tmpuser = {};
if (this.server.user.hasi( msg.params[1] ))
tmpuser = this.server.user.geti( msg.params[1] );
tmpuser = {
nickname: tmpuser.nickname || false,
username: tmpuser.username || false,
hostname: tmpuser.hostname || false,
realname: tmpuser.realname || false,
account: tmpuser.account || false,
registered: tmpuser.registered || false,
oper: tmpuser.oper || false,
channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000)
};
this.server.user.set( msg.params[1], tmpuser );
break;
case "319": // Chanlist Todo
tmpchan = new Map();
tmpuser = {};
if (this.server.user.hasi( msg.params[1] )) {
tmpuser = this.server.user.geti( msg.params[1] );
if(tmpuser.channels)
tmpchan = tmpuser.channels;
}
chans = msg.params[2].trim().split(" ");
for(let chan in chans) {
chan = chans[chan].split("#");
tmpchan.set(`#${chan[1]}`, chan[0]);
}
tmpuser.channels = tmpchan;
this.server.user.set( msg.params[1], tmpuser );
break;
case "330": // Quarknet
tmpuser = {};
if (this.server.user.hasi( msg.params[1] ))
tmpuser = this.server.user.geti( msg.params[1] );
tmpuser.account = msg.params[2];
tmpuser.registered = true;
this.server.user.set( msg.params[1], tmpuser );
break;
// WHOIS END
case "001": // welcome
this.server.me = msg.params[0];
this.join(this.options.channels);
this.emit("data", ["connected", msg.params[1]]);
break;
case "352": // who_entry
if(!this.server.channel[msg.params[1]])
this.server.channel[msg.params[1]] = new Map();
this.server.channel[msg.params[1]].set(msg.params[5], { // chan
nick: msg.params[5],
username: msg.params[2],
hostname: msg.params[3]
});
this.whois(msg.params[5]);
break;
case "315": // who_end
//console.log(this.server.channel);
break;
case "372": // motd
this.server.motd += `${msg.params[1]}\n`;
break;
case "375": // motd_start
this.server.motd = `${msg.params[1]}\n`;
break;
case "376": // motd_end
this.server.motd += `${msg.params[1]}\n`;
this.emit("data", ["motd", this.server.motd]);
break;
case "464": // Password required
if (this.options.password.length > 0 && !this.options.sasl)
this.send(`PASS ${this.options.password}`); // pwd
break;
case "PING":
this.send(`PONG ${msg.params.join``}`);
break;
case "JOIN":
this.send(`WHO ${msg.params[0]}`);
break;
case "PART":
delete this.server.user[msg.params[0]];
//this.whois(msg.params[0], true); // force whois
break;
case "PRIVMSG":
if (msg.params[1] === "\u0001VERSION\u0001")
this.emit("data", ["ctcp:version", this.reply(msg)]);
else
this.emit("data", ["message", this.reply(msg)]);
break;
case "NOTICE":
this.emit("data", ["notice", msg.params[1]]);
break;
case "NICK": // nickchange
prefix = parsePrefix(msg.prefix);
if(this.server.user.hasi(prefix.nick))
this.server.user.deli(prefix.nick);
this.whois(msg.params[0], true); // force
break;
/* CAPKACKE */
case "CAP":
console.log("CAP", msg);
switch(msg.params[1]) {
case "LS":
this.server.sasl.ls = msg.params[2].split(" ");
this.send(`CAP REQ :${this.server.sasl.ls.join(" ")}`);
break;
case "ACK": // success
this.send("AUTHENTICATE PLAIN");
break;
}
break;
case "AUTHENTICATE":
if(msg.params[0].match(/\+/))
this.send(`AUTHENTICATE ${new Buffer(this.username+"\u0000"+this.username+"\u0000"+this.options.password).toString("base64")}`);
break;
case "900":
this.send("CAP END");
this.join(this.options.channels); // tmp
break;
default:
break;
}
this.socket.on("data", msg => {
msg = msg.replace("\r", "").split(/\n/);
for(let tmp in msg)
if(msg[tmp].length > 0)
this.cmd(this.parse(msg[tmp]));
});
}
send() {
@ -210,13 +52,13 @@ class irc {
this.socket.write(arguments[i]);
args += arguments[i];
}
console.log(this.options.network, "sending", args);
console.log(this.options.network, "send", args);
this.socket.write("\n");
}
parse(data) {
let raw = data;
let i = 0;
let prefix = "";
//let raw = data
let i = 0
, prefix = "";
if (data.charAt(0) === ":") {
i = data.indexOf(" ");
prefix = data.slice(1, i);
@ -240,12 +82,168 @@ class irc {
params.push(data.slice(1));
}
return {
raw: raw,
//raw: raw,
prefix: prefix,
command: command,
params: params
};
}
cmd(msg) {
let tmpuser = {};
let tmpchan = {};
let chans = [];
let prefix = "";
switch (msg.command) { // auslagern!
// WHOIS BEGIN
case "307": // Rizon Account
tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.account = msg.params[1];
tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser);
break;
case "311": // first
tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.nickname = msg.params[1];
tmpuser.username = msg.params[2];
tmpuser.hostname = msg.params[3];
tmpuser.realname = msg.params[5];
this.server.user.set(msg.params[1], tmpuser);
break;
case "313": // Oper
tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.oper = true;
this.server.user.set(msg.params[1], tmpuser);
break;
case "318": // last (check Data)
tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser = {
nickname: tmpuser.nickname || false,
username: tmpuser.username || false,
hostname: tmpuser.hostname || false,
realname: tmpuser.realname || false,
account: tmpuser.account || false,
registered: tmpuser.registered || false,
oper: tmpuser.oper || false,
channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000)
};
this.server.user.set(msg.params[1], tmpuser);
break;
case "319": // Chanlist Todo
tmpchan = new Map();
tmpuser = {};
if (this.server.user.hasi(msg.params[1])) {
tmpuser = this.server.user.geti(msg.params[1]);
if (tmpuser.channels)
tmpchan = tmpuser.channels;
}
chans = msg.params[2].trim().split(" ");
for (let chan in chans) {
chan = chans[chan].split("#");
tmpchan.set(`#${chan[1]}`, chan[0]);
}
tmpuser.channels = tmpchan;
this.server.user.set(msg.params[1], tmpuser);
break;
case "330": // Quarknet
tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.account = msg.params[2];
tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser);
break;
// WHOIS END
case "001": // welcome
this.server.me = msg.params[0];
this.join(this.options.channels);
this.emit("data", ["connected", msg.params[1]]);
break;
case "352": // who_entry
if (!this.server.channel[msg.params[1]])
this.server.channel[msg.params[1]] = new Map();
this.server.channel[msg.params[1]].set(msg.params[5], { // chan
nick: msg.params[5],
username: msg.params[2],
hostname: msg.params[3]
});
this.whois(msg.params[5]);
break;
case "315": // who_end
//console.log(this.server.channel);
break;
case "372": // motd
this.server.motd += `${msg.params[1]}\n`;
break;
case "375": // motd_start
this.server.motd = `${msg.params[1]}\n`;
break;
case "376": // motd_end
this.server.motd += `${msg.params[1]}\n`;
this.emit("data", ["motd", this.server.motd]);
break;
case "464": // Password required
if (this.options.password.length > 0 && !this.options.sasl)
this.send(`PASS ${this.options.password}`); // pwd
break;
case "PING":
this.send(`PONG ${msg.params.join``}`);
break;
case "JOIN":
this.send(`WHO ${msg.params[0]}`);
break;
case "PART":
delete this.server.user[msg.params[0]];
//this.whois(msg.params[0], true); // force whois
break;
case "PRIVMSG":
if (msg.params[1] === "\u0001VERSION\u0001")
this.emit("data", ["ctcp:version", this.reply(msg)]);
else
this.emit("data", ["message", this.reply(msg)]);
break;
case "NOTICE":
this.emit("data", ["notice", msg.params[1]]);
break;
case "NICK": // nickchange
prefix = parsePrefix(msg.prefix);
if(this.server.user.hasi(prefix.nick))
this.server.user.deli(prefix.nick);
this.whois(msg.params[0], true); // force
break;
/* CAPKACKE */
case "CAP":
console.log("CAP", msg);
switch (msg.params[1]) {
case "LS":
this.server.sasl.ls = msg.params[2].split(" ");
this.send(`CAP REQ :${this.server.sasl.ls.join(" ")}`);
break;
case "ACK": // success
this.send("AUTHENTICATE PLAIN");
break;
}
break;
case "AUTHENTICATE":
if (msg.params[0].match(/\+/))
this.send(`AUTHENTICATE ${new Buffer(this.username + "\u0000" + this.username + "\u0000" + this.options.password).toString("base64")}`);
break;
case "900":
this.send("CAP END");
break;
default:
break;
}
}
reply(tmp) {
return {
type: "irc",
@ -283,14 +281,9 @@ class irc {
}
whois(user, force = false) {
user = user.toLowerCase();
if(this.server.user.hasi(user) && !force) {
if(this.server.user.geti(user).cached >= ~~(Date.now() / 1000) - this._recachetime) {
//console.log(this.server.user.geti(user).cached, ~~(Date.now() / 1000) - this._recachetime);
//console.log(user, "already cached");
if(this.server.user.hasi(user) && !force)
if(this.server.user.geti(user).cached >= ~~(Date.now() / 1000) - this._recachetime)
return;
}
}
//console.log("get whois for", user);
this.send(`WHOIS ${user}`);
}
}