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

71
package-lock.json generated
View File

@ -1424,6 +1424,21 @@
"loose-envify": "1.3.1" "loose-envify": "1.3.1"
} }
}, },
"irc-message": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/irc-message/-/irc-message-3.0.2.tgz",
"integrity": "sha1-U3eyNcVMTjzlZTbJ1AxSkXmN+d8=",
"requires": {
"irc-prefix-parser": "1.0.1",
"iso8601-convert": "1.0.0",
"through2": "0.6.5"
}
},
"irc-prefix-parser": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/irc-prefix-parser/-/irc-prefix-parser-1.0.1.tgz",
"integrity": "sha1-Vs5g148w6bgLD88SP075+PLmLgs="
},
"is-binary-path": { "is-binary-path": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-1.0.1.tgz", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-1.0.1.tgz",
@ -1545,6 +1560,11 @@
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
}, },
"iso8601-convert": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/iso8601-convert/-/iso8601-convert-1.0.0.tgz",
"integrity": "sha1-inCYB8mOYEZtnnlNgRJlyQpBVjU="
},
"isobject": { "isobject": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/isobject/-/isobject-2.1.0.tgz", "resolved": "https://registry.npmjs.org/isobject/-/isobject-2.1.0.tgz",
@ -2249,12 +2269,12 @@
"dev": true "dev": true
}, },
"through2": { "through2": {
"version": "0.5.1", "version": "0.6.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-0.5.1.tgz", "resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz",
"integrity": "sha1-390BLrnHAOIyP9M084rGIqs3Lac=", "integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=",
"requires": { "requires": {
"readable-stream": "1.0.34", "readable-stream": "1.0.34",
"xtend": "3.0.0" "xtend": "4.0.1"
}, },
"dependencies": { "dependencies": {
"isarray": { "isarray": {
@ -2287,6 +2307,43 @@
"requires": { "requires": {
"inherits": "2.0.3", "inherits": "2.0.3",
"through2": "0.5.1" "through2": "0.5.1"
},
"dependencies": {
"isarray": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
"integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
},
"readable-stream": {
"version": "1.0.34",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz",
"integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=",
"requires": {
"core-util-is": "1.0.2",
"inherits": "2.0.3",
"isarray": "0.0.1",
"string_decoder": "0.10.31"
}
},
"string_decoder": {
"version": "0.10.31",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
},
"through2": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/through2/-/through2-0.5.1.tgz",
"integrity": "sha1-390BLrnHAOIyP9M084rGIqs3Lac=",
"requires": {
"readable-stream": "1.0.34",
"xtend": "3.0.0"
}
},
"xtend": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-3.0.0.tgz",
"integrity": "sha1-XM50B7r2Qsunvs2laBEcST9ZZlo="
}
} }
}, },
"to-fast-properties": { "to-fast-properties": {
@ -2367,9 +2424,9 @@
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
}, },
"xtend": { "xtend": {
"version": "3.0.0", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-3.0.0.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
"integrity": "sha1-XM50B7r2Qsunvs2laBEcST9ZZlo=" "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68="
}, },
"youtube-dl": { "youtube-dl": {
"version": "1.12.2", "version": "1.12.2",

View File

@ -14,7 +14,6 @@
"dependencies": { "dependencies": {
"node-telegram-bot-api": "^0.29.0", "node-telegram-bot-api": "^0.29.0",
"nodejs-mysql": "^0.1.3", "nodejs-mysql": "^0.1.3",
"parse-irc": "^0.2.4",
"safe-eval": "^0.3.0", "safe-eval": "^0.3.0",
"youtube-dl": "^1.12.2" "youtube-dl": "^1.12.2"
}, },

View File

@ -1,8 +1,7 @@
const net = require("net") const net = require("net")
, tls = require("tls") , tls = require("tls")
, EventEmitter = require("events").EventEmitter , EventEmitter = require("events").EventEmitter
, util = require("util") , util = require("util");
, parser = require("parse-irc");
class irc { class irc {
constructor(options) { constructor(options) {
@ -20,7 +19,6 @@ class irc {
this.realname = this.options.realname || "test"; this.realname = this.options.realname || "test";
this.channels = this.options.channels || []; this.channels = this.options.channels || [];
this._recachetime = 60 * 5; // 10 minutes this._recachetime = 60 * 5; // 10 minutes
this.stream = parser();
this.server = { this.server = {
motd: "", motd: "",
sasl: {}, sasl: {},
@ -39,169 +37,13 @@ class irc {
this.send("CAP LS"); this.send("CAP LS");
//this.send("CAP REQ sasl"); //this.send("CAP REQ sasl");
} }
}).pipe(this.stream); });
this.socket.setEncoding("utf-8"); this.socket.setEncoding("utf-8");
this.stream.on("data", msg => { this.socket.on("data", msg => {
//msg = this.parse(msg); msg = msg.replace("\r", "").split(/\n/);
let tmpuser = {}; for(let tmp in msg)
let tmpchan = {}; if(msg[tmp].length > 0)
let chans = []; this.cmd(this.parse(msg[tmp]));
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;
}
}); });
} }
send() { send() {
@ -210,13 +52,13 @@ class irc {
this.socket.write(arguments[i]); this.socket.write(arguments[i]);
args += arguments[i]; args += arguments[i];
} }
console.log(this.options.network, "sending", args); console.log(this.options.network, "send", args);
this.socket.write("\n"); this.socket.write("\n");
} }
parse(data) { parse(data) {
let raw = data; //let raw = data
let i = 0; let i = 0
let prefix = ""; , prefix = "";
if (data.charAt(0) === ":") { if (data.charAt(0) === ":") {
i = data.indexOf(" "); i = data.indexOf(" ");
prefix = data.slice(1, i); prefix = data.slice(1, i);
@ -240,12 +82,168 @@ class irc {
params.push(data.slice(1)); params.push(data.slice(1));
} }
return { return {
raw: raw, //raw: raw,
prefix: prefix, prefix: prefix,
command: command, command: command,
params: params 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) { reply(tmp) {
return { return {
type: "irc", type: "irc",
@ -283,14 +281,9 @@ class irc {
} }
whois(user, force = false) { whois(user, force = false) {
user = user.toLowerCase(); user = user.toLowerCase();
if(this.server.user.hasi(user) && !force) { if(this.server.user.hasi(user) && !force)
if(this.server.user.geti(user).cached >= ~~(Date.now() / 1000) - this._recachetime) { 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");
return; return;
}
}
//console.log("get whois for", user);
this.send(`WHOIS ${user}`); this.send(`WHOIS ${user}`);
} }
} }