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