diff --git a/src/clients/irc.mjs b/src/clients/irc.mjs index f162a25..9b4bfd2 100644 --- a/src/clients/irc.mjs +++ b/src/clients/irc.mjs @@ -52,7 +52,7 @@ export default class irc extends EventEmitter { set: this.set, motd: "", me: {}, - channel: [], + channel: new Map(), user: new Map() }; this.socket = (this.options.ssl ? tls : net).connect({ @@ -105,17 +105,17 @@ export default class irc extends EventEmitter { channel: tmp.params[0], channelid: tmp.params[0], 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 }}, message: tmp.params[1].replace(/\u0002/, ""), time: ~~(Date.now() / 1000), raw: tmp, - reply: msg => this.sendmsg("normal", tmp.params[0], msg), - replyAction: msg => this.sendmsg("action", tmp.params[0], this.format(""+msg)), - replyNotice: msg => this.sendmsg("notice", tmp.params[0], this.format(""+msg)), + reply: msg => this.sendmsg("normal", tmp.params[0], msg), + replyAction: msg => this.sendmsg("action", tmp.params[0], msg), + replyNotice: msg => this.sendmsg("notice", tmp.params[0], msg), self: this.server, - _chan: this.server.channel[tmp.params[0]], + _chan: this.server.channel.get(tmp.params[0]), _user: this.server.user, _cmd: this._cmd, join: chan => this.join(chan), @@ -128,32 +128,22 @@ export default class irc extends EventEmitter { join(channel) { this.send(`JOIN ${(typeof channel === "object") ? channel.join(",") : channel}`); } + who(channel) { + this.send(`WHO ${channel}`); + } part(channel, msg = false) { this.send(`PART ${(typeof channel === "object") ? channel.join(",") : channel}${msg ? " " + msg : " part"}`); } - whois(user, force = false) { - user = user.toLowerCase(); - let tmpuser = {}; - if (this.server.user.has(user) && !force) { - tmpuser = this.server.user.get(user); - if (tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime) - return; + whois(userlist, force = false, whois = []) { + for(const u of (typeof userlist === "object") ? userlist : userlist.split(",")) { + let tmpuser = { cached: 0 }; + if (this.server.user.has(u) && !force) + tmpuser = this.server.user.get(u); + if (tmpuser.cached < ~~(Date.now() / 1000) - this._recachetime) + whois.push(u); } - - tmpuser = { - 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}`); + this.emit("data", ["info", `whois > ${whois}`]); + this.send(`WHOIS ${whois}`); } parsePrefix(prefix) { prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix); diff --git a/src/clients/irc/cap.mjs b/src/clients/irc/cap.mjs index b29ca52..7f9a560 100644 --- a/src/clients/irc/cap.mjs +++ b/src/clients/irc/cap.mjs @@ -1,21 +1,21 @@ -export default client => { - client._cmd.set("CAP", function (msg) { // capkram +export default bot => { + bot._cmd.set("CAP", msg => { // capkram switch (msg.params[1]) { case "LS": // list - this.send(`CAP REQ :${msg.params[2]}`); + bot.send(`CAP REQ :${msg.params[2]}`); break; case "ACK": // success - this.send("AUTHENTICATE PLAIN"); + bot.send("AUTHENTICATE PLAIN"); break; } - }.bind(client)); + }); - client._cmd.set("AUTHENTICATE", function (msg) { // auth + bot._cmd.set("AUTHENTICATE", msg => { // auth if (msg.params[0].match(/\+/)) - this.send(`AUTHENTICATE ${new Buffer(this.username + "\u0000" + this.username + "\u0000" + this.options.password).toString("base64")}`); - }.bind(client)); + bot.send(`AUTHENTICATE ${new Buffer(bot.username + "\u0000" + bot.username + "\u0000" + bot.options.password).toString("base64")}`); + }); - client._cmd.set("900", function (msg) { // cap end - this.send("CAP END"); - }.bind(client)); + bot._cmd.set("900", msg => { // cap end + bot.send("CAP END"); + }); }; diff --git a/src/clients/irc/invite.mjs b/src/clients/irc/invite.mjs index e42db27..e568b9d 100644 --- a/src/clients/irc/invite.mjs +++ b/src/clients/irc/invite.mjs @@ -1,13 +1,13 @@ -export default client => { - client._cmd.set("INVITE", function (msg) { // invite - const user = this.parsePrefix(msg.prefix); +export default bot => { + bot._cmd.set("INVITE", msg => { // invite + const user = bot.parsePrefix(msg.prefix); const channel = msg.params[1]; - if(!this.server.channel.includes(channel)) { - this.join(channel); + if(!bot.server.channel.includes(channel)) { + bot.join(channel); setTimeout(() => { - this.send(`PRIVMSG ${channel} :Hi. Wurde von ${user.nick} eingeladen.`); + bot.send(`PRIVMSG ${channel} :Hi. Wurde von ${user.nick} eingeladen.`); }, 1000); } - }.bind(client)); + }); }; \ No newline at end of file diff --git a/src/clients/irc/join.mjs b/src/clients/irc/join.mjs index 592098e..829b87c 100644 --- a/src/clients/irc/join.mjs +++ b/src/clients/irc/join.mjs @@ -1,5 +1,5 @@ -export default client => { - client._cmd.set("JOIN", function (msg) { // join - this.send(`WHO ${msg.params[0]}`); - }.bind(client)); +export default bot => { + bot._cmd.set("JOIN", msg => { // join + bot.send(`WHO ${msg.params[0]}`); + }); }; diff --git a/src/clients/irc/motd.mjs b/src/clients/irc/motd.mjs index ac1232d..44fd517 100644 --- a/src/clients/irc/motd.mjs +++ b/src/clients/irc/motd.mjs @@ -1,14 +1,14 @@ -export default client => { - client._cmd.set("372", function (msg) { // motd_entry - this.server.motd += `${msg.params[1]}\n`; - }.bind(client)); +export default bot => { + bot._cmd.set("372", msg => { // motd_entry + bot.server.motd += `${msg.params[1]}\n`; + }); - client._cmd.set("375", function (msg) { // motd_start - this.server.motd = `${msg.params[1]}\n`; - }.bind(client)); + bot._cmd.set("375", msg => { // motd_start + bot.server.motd = `${msg.params[1]}\n`; + }); - client._cmd.set("376", function (msg) { // motd_end - this.server.motd += `${msg.params[1]}\n`; - this.emit("data", ["motd", this.server.motd]); - }.bind(client)); + bot._cmd.set("376", msg => { // motd_end + bot.server.motd += `${msg.params[1]}\n`; + bot.emit("data", ["motd", bot.server.motd]); + }); }; diff --git a/src/clients/irc/msg.mjs b/src/clients/irc/msg.mjs index 4ca3efb..0485fba 100644 --- a/src/clients/irc/msg.mjs +++ b/src/clients/irc/msg.mjs @@ -1,14 +1,14 @@ -export default client => { - client._cmd.set("PRIVMSG", function (msg) { // privmsg +export default bot => { + bot._cmd.set("PRIVMSG", msg => { // privmsg 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)) - return this.emit("data", ["ctcp:ping", this.reply(msg)]); + return bot.emit("data", ["ctcp:ping", bot.reply(msg)]); else - this.emit("data", ["message", this.reply(msg)]); - }.bind(client)); + bot.emit("data", ["message", bot.reply(msg)]); + }); - client._cmd.set("NOTICE", function (msg) { // notice - this.emit("data", ["notice", msg.params[1]]); - }.bind(client)); + bot._cmd.set("NOTICE", msg => { // notice + bot.emit("data", ["notice", msg.params[1]]); + }); }; diff --git a/src/clients/irc/nick.mjs b/src/clients/irc/nick.mjs index 70e4fbb..aac3812 100644 --- a/src/clients/irc/nick.mjs +++ b/src/clients/irc/nick.mjs @@ -1,8 +1,8 @@ -export default client => { - client._cmd.set("NICK", function (msg) { // nickchange - let prefix = this.parsePrefix(msg.prefix); - if (this.server.user.hasi(prefix.nick)) - this.server.user.deli(prefix.nick); - this.whois(msg.params[0], true); // force - }.bind(client)); +export default bot => { + bot._cmd.set("NICK", msg => { // nickchange + let prefix = bot.parsePrefix(msg.prefix); + if (bot.server.user.has(prefix.nick)) + bot.server.user.del(prefix.nick); + bot.whois(msg.params[0], true); // force + }); }; diff --git a/src/clients/irc/part.mjs b/src/clients/irc/part.mjs index f4745a9..f5f1563 100644 --- a/src/clients/irc/part.mjs +++ b/src/clients/irc/part.mjs @@ -1,5 +1,5 @@ -export default client => { - client._cmd.set("PART", function (msg) { // part +export default bot => { + bot._cmd.set("PART", msg => { // part //delete this.server.user[msg.params[0]]; - }.bind(client)); + }); }; diff --git a/src/clients/irc/ping.mjs b/src/clients/irc/ping.mjs index 0d0608d..cec7f1f 100644 --- a/src/clients/irc/ping.mjs +++ b/src/clients/irc/ping.mjs @@ -1,5 +1,5 @@ -export default client => { - client._cmd.set("PING", function (msg) { // ping - this.send(`PONG ${msg.params.join``}`); - }.bind(client)); +export default bot => { + bot._cmd.set("PING", msg => { // ping + bot.send(`PONG ${msg.params.join``}`); + }); }; diff --git a/src/clients/irc/pwdreq.mjs b/src/clients/irc/pwdreq.mjs index 2a28108..845e58a 100644 --- a/src/clients/irc/pwdreq.mjs +++ b/src/clients/irc/pwdreq.mjs @@ -1,6 +1,6 @@ -export default client => { - client._cmd.set("464", function (msg) { // motd_entry - if (this.options.password.length > 0 && !this.options.sasl) - this.send(`PASS ${this.options.password}`); - }.bind(client)); +export default bot => { + bot._cmd.set("464", msg => { // motd_entry + if (bot.options.password.length > 0 && !bot.options.sasl) + bot.send(`PASS ${bot.options.password}`); + }); }; diff --git a/src/clients/irc/welcome.mjs b/src/clients/irc/welcome.mjs index 800cc65..d58e026 100644 --- a/src/clients/irc/welcome.mjs +++ b/src/clients/irc/welcome.mjs @@ -1,6 +1,6 @@ -export default client => { - client._cmd.set("001", function (msg) { // welcome - this.join(this.options.channels); - this.emit("data", ["connected", msg.params[1]]); - }.bind(client)); +export default bot => { + bot._cmd.set("001", msg => { // welcome + bot.join(bot.options.channels); + bot.emit("data", ["connected", msg.params[1]]); + }); }; diff --git a/src/clients/irc/who.mjs b/src/clients/irc/who.mjs index bfba569..2d603d0 100644 --- a/src/clients/irc/who.mjs +++ b/src/clients/irc/who.mjs @@ -1,27 +1,17 @@ const max = 400; let whois = []; +let chan; -export default client => { - client._cmd.set("352", function (msg) { // 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] - }); +export default bot => { + bot._cmd.set("352", msg => { // who_entry + chan = msg.params[1]; whois.push(msg.params[5]); - }.bind(client)); + }); - client._cmd.set("315", function (msg) { // who_end - this.whois(whois.reduce((a, b) => { - a += `${b},`; - if(a.length >= max) { - this.whois(a.slice(0, -1)); - a = ""; - } - return a; - }, "").slice(0, -1)); + bot._cmd.set("315", msg => { // who_end + bot.server.channel.set(chan, whois); + whois = [...new Set(whois)]; + Array(Math.ceil(whois.length / 10)).fill().map(_ => whois.splice(0, 10)).forEach(l => bot.whois(l)); whois = []; - }.bind(client)); + }); }; diff --git a/src/clients/irc/whois.mjs b/src/clients/irc/whois.mjs index 5008b88..fb0a676 100644 --- a/src/clients/irc/whois.mjs +++ b/src/clients/irc/whois.mjs @@ -1,37 +1,38 @@ -export default client => { - client._cmd.set("307", function (msg) { // whois_identified (ircd-hybrid) +export default bot => { + bot._cmd.set("307", msg => { // whois_identified (ircd-hybrid) let tmpuser = {}; - if (this.server.user.hasi(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.account = msg.params[1]; tmpuser.registered = true; - this.server.user.set(msg.params[1], tmpuser); - }.bind(client)); + bot.server.user.set(msg.params[1], tmpuser); + }); - client._cmd.set("311", function (msg) { // whois_userdata + bot._cmd.set("311", msg => { // whois_userdata let tmpuser = {}; - if (this.server.user.hasi(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.nickname = msg.params[1]; tmpuser.username = msg.params[2]; tmpuser.hostname = msg.params[3]; tmpuser.realname = msg.params[5]; tmpuser.prefix = `${msg.params[1]}!${msg.params[2]}@${msg.params[3]}`; - this.server.user.set(msg.params[1], tmpuser); - }.bind(client)); + bot.server.user.set(msg.params[1], tmpuser); + }); - client._cmd.set("313", function (msg) { // whois_oper + bot._cmd.set("313", msg => { // whois_oper let tmpuser = {}; - if (this.server.user.hasi(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.oper = true; - this.server.user.set(msg.params[1], tmpuser); - }.bind(client)); + bot.server.user.set(msg.params[1], tmpuser); + }); - client._cmd.set("318", function (msg) { // whois_end + bot._cmd.set("318", msg => { // whois_end let tmpuser = {}; - if (this.server.user.hasi(msg.params[1])) - tmpuser = this.server.user.geti(msg.params[1]); + bot.emit("data", ["info", `whois < ${msg.params[1]}`]); + if (bot.server.user.has(msg.params[1])) + tmpuser = bot.server.user.get(msg.params[1]); tmpuser = { nickname: tmpuser.nickname || false, username: tmpuser.username || false, @@ -44,18 +45,20 @@ export default client => { channels: tmpuser.channels || [], cached: ~~(Date.now() / 1000) }; - if(msg.params[0] === msg.params[1]) - this.server.me = tmpuser; - this.server.user.set(msg.params[1], tmpuser); - }.bind(client)); + bot.server.user.set(msg.params[1], tmpuser); + if(msg.params[0] == msg.params[1]) { + bot.server.me = tmpuser; + 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() , tmpuser = {}; - if (this.server.user.hasi(msg.params[1])) { - tmpuser = this.server.user.geti(msg.params[1]); - if (tmpuser.channels) - tmpchan = new Map(tmpuser.channels); + if (bot.server.user.has(msg.params[1])) { + tmpuser = bot.server.user.get(msg.params[1]); + if (tmpuser.channels) + tmpchan = new Map(tmpuser.channels); } let chans = msg.params[2].trim().split(" "); for (let chan in chans) { @@ -63,15 +66,15 @@ export default client => { tmpchan.set(`#${chan[1]}`, chan[0]); } tmpuser.channels = tmpchan; - this.server.user.set(msg.params[1], tmpuser); - }.bind(client)); + bot.server.user.set(msg.params[1], tmpuser); + }); - client._cmd.set("330", function (msg) { // whois_authed_as (snircd) + bot._cmd.set("330", msg => { // whois_authed_as (snircd) let tmpuser = {}; - if (this.server.user.hasi(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.account = msg.params[2]; tmpuser.registered = true; - this.server.user.set(msg.params[1], tmpuser); - }.bind(client)); + bot.server.user.set(msg.params[1], tmpuser); + }); }; diff --git a/src/index.mjs b/src/index.mjs index 3db86a5..3baea33 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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); -}; \ No newline at end of file