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"
}
},
"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": {
"version": "1.0.1",
"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",
"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": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/isobject/-/isobject-2.1.0.tgz",
@ -2249,12 +2269,12 @@
"dev": true
},
"through2": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/through2/-/through2-0.5.1.tgz",
"integrity": "sha1-390BLrnHAOIyP9M084rGIqs3Lac=",
"version": "0.6.5",
"resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz",
"integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=",
"requires": {
"readable-stream": "1.0.34",
"xtend": "3.0.0"
"xtend": "4.0.1"
},
"dependencies": {
"isarray": {
@ -2287,6 +2307,43 @@
"requires": {
"inherits": "2.0.3",
"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": {
@ -2367,9 +2424,9 @@
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
},
"xtend": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-3.0.0.tgz",
"integrity": "sha1-XM50B7r2Qsunvs2laBEcST9ZZlo="
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
"integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68="
},
"youtube-dl": {
"version": "1.12.2",

View File

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

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,47 +37,93 @@ 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);
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() {
let args = "";
for (let i = 0; i < arguments.length; i++) {
this.socket.write(arguments[i]);
args += arguments[i];
}
console.log(this.options.network, "send", args);
this.socket.write("\n");
}
parse(data) {
//let raw = data
let i = 0
, prefix = "";
if (data.charAt(0) === ":") {
i = data.indexOf(" ");
prefix = data.slice(1, i);
data = data.slice(i + 1);
}
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) !== ":") {
i = data.indexOf(" ");
if (i === -1)
i = data.length;
params.push(data.slice(0, i));
data = data.slice(i + 1);
}
if (data) {
data = data.replace(/\r?\n|\r/g, "");
params.push(data.slice(1));
}
return {
//raw: raw,
prefix: prefix,
command: command,
params: params
};
}
cmd(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] );
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 );
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] );
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 );
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] );
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 );
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] );
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,
@ -91,33 +135,31 @@ class irc {
channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000)
};
this.server.user.set( msg.params[1], tmpuser );
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)
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) {
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 );
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] );
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 );
this.server.user.set(msg.params[1], tmpuser);
break;
// WHOIS END
case "001": // welcome
@ -126,7 +168,7 @@ class irc {
this.emit("data", ["connected", msg.params[1]]);
break;
case "352": // who_entry
if(!this.server.channel[msg.params[1]])
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],
@ -180,7 +222,7 @@ class irc {
/* CAPKACKE */
case "CAP":
console.log("CAP", msg);
switch(msg.params[1]) {
switch (msg.params[1]) {
case "LS":
this.server.sasl.ls = msg.params[2].split(" ");
this.send(`CAP REQ :${this.server.sasl.ls.join(" ")}`);
@ -191,60 +233,16 @@ class irc {
}
break;
case "AUTHENTICATE":
if(msg.params[0].match(/\+/))
this.send(`AUTHENTICATE ${new Buffer(this.username+"\u0000"+this.username+"\u0000"+this.options.password).toString("base64")}`);
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() {
let args = "";
for (let i = 0; i < arguments.length; i++) {
this.socket.write(arguments[i]);
args += arguments[i];
}
console.log(this.options.network, "sending", args);
this.socket.write("\n");
}
parse(data) {
let raw = data;
let i = 0;
let prefix = "";
if (data.charAt(0) === ":") {
i = data.indexOf(" ");
prefix = data.slice(1, i);
data = data.slice(i + 1);
}
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) !== ":") {
i = data.indexOf(" ");
if (i === -1)
i = data.length;
params.push(data.slice(0, i));
data = data.slice(i + 1);
}
if (data) {
data = data.replace(/\r?\n|\r/g, "");
params.push(data.slice(1));
}
return {
raw: raw,
prefix: prefix,
command: command,
params: params
};
}
reply(tmp) {
return {
@ -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}`);
}
}