irgendwas mit overhaul

This commit is contained in:
Flummi 2019-08-22 22:05:18 +00:00
parent acf7ac3d49
commit 0d45670ae4
14 changed files with 132 additions and 174 deletions

View File

@ -52,7 +52,7 @@ export default class irc extends EventEmitter {
set: this.set, set: this.set,
motd: "", motd: "",
me: {}, me: {},
channel: [], channel: new Map(),
user: new Map() user: new Map()
}; };
this.socket = (this.options.ssl ? tls : net).connect({ this.socket = (this.options.ssl ? tls : net).connect({
@ -105,17 +105,17 @@ export default class irc extends EventEmitter {
channel: tmp.params[0], channel: tmp.params[0],
channelid: tmp.params[0], channelid: tmp.params[0],
user: { ...this.parsePrefix(tmp.prefix), ...{ user: { ...this.parsePrefix(tmp.prefix), ...{
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account, account: this.server.user.get(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix
}}, }},
message: tmp.params[1].replace(/\u0002/, ""), message: tmp.params[1].replace(/\u0002/, ""),
time: ~~(Date.now() / 1000), time: ~~(Date.now() / 1000),
raw: tmp, raw: tmp,
reply: msg => this.sendmsg("normal", tmp.params[0], msg), reply: msg => this.sendmsg("normal", tmp.params[0], msg),
replyAction: msg => this.sendmsg("action", tmp.params[0], this.format(""+msg)), replyAction: msg => this.sendmsg("action", tmp.params[0], msg),
replyNotice: msg => this.sendmsg("notice", tmp.params[0], this.format(""+msg)), replyNotice: msg => this.sendmsg("notice", tmp.params[0], msg),
self: this.server, self: this.server,
_chan: this.server.channel[tmp.params[0]], _chan: this.server.channel.get(tmp.params[0]),
_user: this.server.user, _user: this.server.user,
_cmd: this._cmd, _cmd: this._cmd,
join: chan => this.join(chan), join: chan => this.join(chan),
@ -128,32 +128,22 @@ export default class irc extends EventEmitter {
join(channel) { join(channel) {
this.send(`JOIN ${(typeof channel === "object") ? channel.join(",") : channel}`); this.send(`JOIN ${(typeof channel === "object") ? channel.join(",") : channel}`);
} }
who(channel) {
this.send(`WHO ${channel}`);
}
part(channel, msg = false) { part(channel, msg = false) {
this.send(`PART ${(typeof channel === "object") ? channel.join(",") : channel}${msg ? " " + msg : " part"}`); this.send(`PART ${(typeof channel === "object") ? channel.join(",") : channel}${msg ? " " + msg : " part"}`);
} }
whois(user, force = false) { whois(userlist, force = false, whois = []) {
user = user.toLowerCase(); for(const u of (typeof userlist === "object") ? userlist : userlist.split(",")) {
let tmpuser = {}; let tmpuser = { cached: 0 };
if (this.server.user.has(user) && !force) { if (this.server.user.has(u) && !force)
tmpuser = this.server.user.get(user); tmpuser = this.server.user.get(u);
if (tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime) if (tmpuser.cached < ~~(Date.now() / 1000) - this._recachetime)
return; whois.push(u);
} }
this.emit("data", ["info", `whois > ${whois}`]);
tmpuser = { this.send(`WHOIS ${whois}`);
nickname: tmpuser.nickname || false,
username: tmpuser.username || false,
hostname: tmpuser.hostname || false,
realname: tmpuser.realname || false,
account: tmpuser.account || false,
prefix: tmpuser.prefix || false,
registered: tmpuser.registered || false,
oper: tmpuser.oper || false,
channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000)
};
this.server.user.set(user, tmpuser);
this.send(`WHOIS ${user}`);
} }
parsePrefix(prefix) { parsePrefix(prefix) {
prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix); prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix);

View File

@ -1,21 +1,21 @@
export default client => { export default bot => {
client._cmd.set("CAP", function (msg) { // capkram bot._cmd.set("CAP", msg => { // capkram
switch (msg.params[1]) { switch (msg.params[1]) {
case "LS": // list case "LS": // list
this.send(`CAP REQ :${msg.params[2]}`); bot.send(`CAP REQ :${msg.params[2]}`);
break; break;
case "ACK": // success case "ACK": // success
this.send("AUTHENTICATE PLAIN"); bot.send("AUTHENTICATE PLAIN");
break; break;
} }
}.bind(client)); });
client._cmd.set("AUTHENTICATE", function (msg) { // auth bot._cmd.set("AUTHENTICATE", msg => { // auth
if (msg.params[0].match(/\+/)) if (msg.params[0].match(/\+/))
this.send(`AUTHENTICATE ${new Buffer(this.username + "\u0000" + this.username + "\u0000" + this.options.password).toString("base64")}`); bot.send(`AUTHENTICATE ${new Buffer(bot.username + "\u0000" + bot.username + "\u0000" + bot.options.password).toString("base64")}`);
}.bind(client)); });
client._cmd.set("900", function (msg) { // cap end bot._cmd.set("900", msg => { // cap end
this.send("CAP END"); bot.send("CAP END");
}.bind(client)); });
}; };

View File

@ -1,13 +1,13 @@
export default client => { export default bot => {
client._cmd.set("INVITE", function (msg) { // invite bot._cmd.set("INVITE", msg => { // invite
const user = this.parsePrefix(msg.prefix); const user = bot.parsePrefix(msg.prefix);
const channel = msg.params[1]; const channel = msg.params[1];
if(!this.server.channel.includes(channel)) { if(!bot.server.channel.includes(channel)) {
this.join(channel); bot.join(channel);
setTimeout(() => { setTimeout(() => {
this.send(`PRIVMSG ${channel} :Hi. Wurde von ${user.nick} eingeladen.`); bot.send(`PRIVMSG ${channel} :Hi. Wurde von ${user.nick} eingeladen.`);
}, 1000); }, 1000);
} }
}.bind(client)); });
}; };

View File

@ -1,5 +1,5 @@
export default client => { export default bot => {
client._cmd.set("JOIN", function (msg) { // join bot._cmd.set("JOIN", msg => { // join
this.send(`WHO ${msg.params[0]}`); bot.send(`WHO ${msg.params[0]}`);
}.bind(client)); });
}; };

View File

@ -1,14 +1,14 @@
export default client => { export default bot => {
client._cmd.set("372", function (msg) { // motd_entry bot._cmd.set("372", msg => { // motd_entry
this.server.motd += `${msg.params[1]}\n`; bot.server.motd += `${msg.params[1]}\n`;
}.bind(client)); });
client._cmd.set("375", function (msg) { // motd_start bot._cmd.set("375", msg => { // motd_start
this.server.motd = `${msg.params[1]}\n`; bot.server.motd = `${msg.params[1]}\n`;
}.bind(client)); });
client._cmd.set("376", function (msg) { // motd_end bot._cmd.set("376", msg => { // motd_end
this.server.motd += `${msg.params[1]}\n`; bot.server.motd += `${msg.params[1]}\n`;
this.emit("data", ["motd", this.server.motd]); bot.emit("data", ["motd", bot.server.motd]);
}.bind(client)); });
}; };

View File

@ -1,14 +1,14 @@
export default client => { export default bot => {
client._cmd.set("PRIVMSG", function (msg) { // privmsg bot._cmd.set("PRIVMSG", msg => { // privmsg
if(msg.params[1] === "\u0001VERSION\u0001") if(msg.params[1] === "\u0001VERSION\u0001")
return this.emit("data", ["ctcp:version", this.reply(msg)]); return bot.emit("data", ["ctcp:version", bot.reply(msg)]);
else if(msg.params[1].match(/^\u0001PING .*\u0001/i)) else if(msg.params[1].match(/^\u0001PING .*\u0001/i))
return this.emit("data", ["ctcp:ping", this.reply(msg)]); return bot.emit("data", ["ctcp:ping", bot.reply(msg)]);
else else
this.emit("data", ["message", this.reply(msg)]); bot.emit("data", ["message", bot.reply(msg)]);
}.bind(client)); });
client._cmd.set("NOTICE", function (msg) { // notice bot._cmd.set("NOTICE", msg => { // notice
this.emit("data", ["notice", msg.params[1]]); bot.emit("data", ["notice", msg.params[1]]);
}.bind(client)); });
}; };

View File

@ -1,8 +1,8 @@
export default client => { export default bot => {
client._cmd.set("NICK", function (msg) { // nickchange bot._cmd.set("NICK", msg => { // nickchange
let prefix = this.parsePrefix(msg.prefix); let prefix = bot.parsePrefix(msg.prefix);
if (this.server.user.hasi(prefix.nick)) if (bot.server.user.has(prefix.nick))
this.server.user.deli(prefix.nick); bot.server.user.del(prefix.nick);
this.whois(msg.params[0], true); // force bot.whois(msg.params[0], true); // force
}.bind(client)); });
}; };

View File

@ -1,5 +1,5 @@
export default client => { export default bot => {
client._cmd.set("PART", function (msg) { // part bot._cmd.set("PART", msg => { // part
//delete this.server.user[msg.params[0]]; //delete this.server.user[msg.params[0]];
}.bind(client)); });
}; };

View File

@ -1,5 +1,5 @@
export default client => { export default bot => {
client._cmd.set("PING", function (msg) { // ping bot._cmd.set("PING", msg => { // ping
this.send(`PONG ${msg.params.join``}`); bot.send(`PONG ${msg.params.join``}`);
}.bind(client)); });
}; };

View File

@ -1,6 +1,6 @@
export default client => { export default bot => {
client._cmd.set("464", function (msg) { // motd_entry bot._cmd.set("464", msg => { // motd_entry
if (this.options.password.length > 0 && !this.options.sasl) if (bot.options.password.length > 0 && !bot.options.sasl)
this.send(`PASS ${this.options.password}`); bot.send(`PASS ${bot.options.password}`);
}.bind(client)); });
}; };

View File

@ -1,6 +1,6 @@
export default client => { export default bot => {
client._cmd.set("001", function (msg) { // welcome bot._cmd.set("001", msg => { // welcome
this.join(this.options.channels); bot.join(bot.options.channels);
this.emit("data", ["connected", msg.params[1]]); bot.emit("data", ["connected", msg.params[1]]);
}.bind(client)); });
}; };

View File

@ -1,27 +1,17 @@
const max = 400; const max = 400;
let whois = []; let whois = [];
let chan;
export default client => { export default bot => {
client._cmd.set("352", function (msg) { // who_entry bot._cmd.set("352", msg => { // who_entry
if (!this.server.channel[msg.params[1]]) chan = 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]
});
whois.push(msg.params[5]); whois.push(msg.params[5]);
}.bind(client)); });
client._cmd.set("315", function (msg) { // who_end bot._cmd.set("315", msg => { // who_end
this.whois(whois.reduce((a, b) => { bot.server.channel.set(chan, whois);
a += `${b},`; whois = [...new Set(whois)];
if(a.length >= max) { Array(Math.ceil(whois.length / 10)).fill().map(_ => whois.splice(0, 10)).forEach(l => bot.whois(l));
this.whois(a.slice(0, -1));
a = "";
}
return a;
}, "").slice(0, -1));
whois = []; whois = [];
}.bind(client)); });
}; };

View File

@ -1,37 +1,38 @@
export default client => { export default bot => {
client._cmd.set("307", function (msg) { // whois_identified (ircd-hybrid) bot._cmd.set("307", msg => { // whois_identified (ircd-hybrid)
let tmpuser = {}; let tmpuser = {};
if (this.server.user.hasi(msg.params[1])) if (bot.server.user.has(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]); tmpuser = bot.server.user.get(msg.params[1]);
tmpuser.account = msg.params[1]; tmpuser.account = msg.params[1];
tmpuser.registered = true; tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser); bot.server.user.set(msg.params[1], tmpuser);
}.bind(client)); });
client._cmd.set("311", function (msg) { // whois_userdata bot._cmd.set("311", msg => { // whois_userdata
let tmpuser = {}; let tmpuser = {};
if (this.server.user.hasi(msg.params[1])) if (bot.server.user.has(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]); tmpuser = bot.server.user.get(msg.params[1]);
tmpuser.nickname = msg.params[1]; tmpuser.nickname = msg.params[1];
tmpuser.username = msg.params[2]; tmpuser.username = msg.params[2];
tmpuser.hostname = msg.params[3]; tmpuser.hostname = msg.params[3];
tmpuser.realname = msg.params[5]; tmpuser.realname = msg.params[5];
tmpuser.prefix = `${msg.params[1]}!${msg.params[2]}@${msg.params[3]}`; tmpuser.prefix = `${msg.params[1]}!${msg.params[2]}@${msg.params[3]}`;
this.server.user.set(msg.params[1], tmpuser); bot.server.user.set(msg.params[1], tmpuser);
}.bind(client)); });
client._cmd.set("313", function (msg) { // whois_oper bot._cmd.set("313", msg => { // whois_oper
let tmpuser = {}; let tmpuser = {};
if (this.server.user.hasi(msg.params[1])) if (bot.server.user.has(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]); tmpuser = bot.server.user.get(msg.params[1]);
tmpuser.oper = true; tmpuser.oper = true;
this.server.user.set(msg.params[1], tmpuser); bot.server.user.set(msg.params[1], tmpuser);
}.bind(client)); });
client._cmd.set("318", function (msg) { // whois_end bot._cmd.set("318", msg => { // whois_end
let tmpuser = {}; let tmpuser = {};
if (this.server.user.hasi(msg.params[1])) bot.emit("data", ["info", `whois < ${msg.params[1]}`]);
tmpuser = this.server.user.geti(msg.params[1]); if (bot.server.user.has(msg.params[1]))
tmpuser = bot.server.user.get(msg.params[1]);
tmpuser = { tmpuser = {
nickname: tmpuser.nickname || false, nickname: tmpuser.nickname || false,
username: tmpuser.username || false, username: tmpuser.username || false,
@ -44,18 +45,20 @@ export default client => {
channels: tmpuser.channels || [], channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000) cached: ~~(Date.now() / 1000)
}; };
if(msg.params[0] === msg.params[1]) bot.server.user.set(msg.params[1], tmpuser);
this.server.me = tmpuser; if(msg.params[0] == msg.params[1]) {
this.server.user.set(msg.params[1], tmpuser); bot.server.me = tmpuser;
}.bind(client)); bot.server.user.delete(msg.params[1]);
}
});
client._cmd.set("319", function (msg) { // whois_chanlist bot._cmd.set("319", msg => { // whois_chanlist
let tmpchan = new Map() let tmpchan = new Map()
, tmpuser = {}; , tmpuser = {};
if (this.server.user.hasi(msg.params[1])) { if (bot.server.user.has(msg.params[1])) {
tmpuser = this.server.user.geti(msg.params[1]); tmpuser = bot.server.user.get(msg.params[1]);
if (tmpuser.channels) if (tmpuser.channels)
tmpchan = new Map(tmpuser.channels); tmpchan = new Map(tmpuser.channels);
} }
let chans = msg.params[2].trim().split(" "); let chans = msg.params[2].trim().split(" ");
for (let chan in chans) { for (let chan in chans) {
@ -63,15 +66,15 @@ export default client => {
tmpchan.set(`#${chan[1]}`, chan[0]); tmpchan.set(`#${chan[1]}`, chan[0]);
} }
tmpuser.channels = tmpchan; tmpuser.channels = tmpchan;
this.server.user.set(msg.params[1], tmpuser); bot.server.user.set(msg.params[1], tmpuser);
}.bind(client)); });
client._cmd.set("330", function (msg) { // whois_authed_as (snircd) bot._cmd.set("330", msg => { // whois_authed_as (snircd)
let tmpuser = {}; let tmpuser = {};
if (this.server.user.hasi(msg.params[1])) if (bot.server.user.has(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]); tmpuser = bot.server.user.get(msg.params[1]);
tmpuser.account = msg.params[2]; tmpuser.account = msg.params[2];
tmpuser.registered = true; tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser); bot.server.user.set(msg.params[1], tmpuser);
}.bind(client)); });
}; };

View File

@ -38,28 +38,3 @@ export default class cuffeo extends EventEmitter {
}); });
} }
}; };
Map.prototype.hasi = function(val) {
try {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
return true;
return false;
} catch(err) {
console.log("das übliche mit tolowercase()");
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())
this.delete(key);
};