new parser & cleanup

This commit is contained in:
Flummi 2017-11-19 22:11:23 +01:00
parent 08eb84842a
commit c5300e4edf

View File

@ -21,7 +21,6 @@ class irc {
this._recachetime = 60 * 5; // 10 minutes
this.server = {
motd: "",
sasl: {},
me: {},
channel: [],
user: new Map()
@ -33,59 +32,33 @@ class irc {
}, () => {
this.send(`NICK ${this.nickname}`);
this.send(`USER ${this.username} 0 * : ${this.realname}`);
if(this.options.sasl) {
if(this.options.sasl)
this.send("CAP LS");
//this.send("CAP REQ sasl");
}
});
this.socket.setEncoding("utf-8");
this.socket.on("data", msg => {
msg = msg.replace("\r", "").split(/\n/);
msg = msg.split(/\r?\n|\r/);
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++) {
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
parse(data, [a, ...b] = data.split(/ +:/)) {
console.log(a, b);
let tmp = a.split(" ").concat(b);
return data.charAt(0) === ":" ? {
prefix: tmp.shift(),
command: tmp.shift(),
params: tmp
} : {
prefix: null,
command: tmp.shift(),
params: tmp
};
}
cmd(msg) {
@ -94,7 +67,6 @@ class irc {
let chans = [];
let prefix = "";
switch (msg.command) { // auslagern!
// WHOIS BEGIN
case "307": // Rizon Account
tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
@ -161,7 +133,6 @@ class irc {
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);
@ -197,18 +168,14 @@ class irc {
case "PING":
this.send(`PONG ${msg.params.join``}`);
break;
case "JOIN":
case "JOIN": // wip
this.send(`WHO ${msg.params[0]}`);
break;
case "PART":
case "PART": // wip
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)]);
this.emit("data", msg.params[1] === "\u0001VERSION\u0001" ? ["ctcp:version", this.reply(msg)] : ["message", this.reply(msg)]);
break;
case "NOTICE":
this.emit("data", ["notice", msg.params[1]]);
@ -219,13 +186,10 @@ class irc {
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(" ")}`);
this.send(`CAP REQ :${msg.params[2]}`);
break;
case "ACK": // success
this.send("AUTHENTICATE PLAIN");
@ -238,9 +202,6 @@ class irc {
break;
case "900":
this.send("CAP END");
break;
default:
break;
}
}
@ -253,20 +214,11 @@ class irc {
message: tmp.params[1],
time: ~~(Date.now() / 1000),
raw: tmp,
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}`);
},
// act chan
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}`),
_chan: this.server.channel[tmp.params[0]],
_user: this.server.user,
sasl: this.server.sasl,
// commands
join: chan => this.join(chan),
part: (chan, msg) => this.part(chan, msg),
whois: user => this.whois(user),
@ -307,14 +259,12 @@ Map.prototype.hasi = function(val) {
return true;
return false;
};
Map.prototype.geti = function(val) {
for (let [key, value] of this)
if(key.toLowerCase() === val.toLowerCase())
return value;
return false;
};
Map.prototype.deli = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())