dynamic modimport in ircmodule

This commit is contained in:
Flummi 2019-08-21 10:07:25 +00:00
parent 19782dd6f8
commit 267d8cf798
3 changed files with 57 additions and 85 deletions

View File

@ -1,25 +1,19 @@
import modules from "./irc/index"; import _fs from "fs";
import { fileURLToPath } from "url";
import { dirname } from "path";
import net from "net"; import net from "net";
import tls from "tls"; import tls from "tls";
import EventEmitter from "events"; import EventEmitter from "events";
const fs = _fs.promises;
const __dirname = dirname(fileURLToPath(import.meta.url));
const colors = [ const colors = [
"white", "white", "black", "navy",
"black", "green", "red", "brown",
"navy", "purple", "orange", "yellow",
"green", "lightgreen", "teal", "cyan",
"red", "blue", "magenta", "gray",
"brown",
"purple",
"orange",
"yellow",
"lightgreen",
"teal",
"cyan",
"blue",
"magenta",
"gray",
"lightgray" "lightgray"
].reduce((a, b, i) => ({...a, ...{[b]: i.toString().padStart(2, 0)}}), {}); ].reduce((a, b, i) => ({...a, ...{[b]: i.toString().padStart(2, 0)}}), {});
@ -54,8 +48,6 @@ export default class irc extends EventEmitter {
this._recachetime = 60 * 30; // 30 minutes this._recachetime = 60 * 30; // 30 minutes
this._cmd = new Map(); this._cmd = new Map();
modules.forEach(mod => mod(this));
this.server = { this.server = {
set: this.set, set: this.set,
motd: "", motd: "",
@ -70,7 +62,7 @@ export default class irc extends EventEmitter {
}, () => { }, () => {
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.socket.setEncoding("utf-8"); this.socket.setEncoding("utf-8");
@ -81,20 +73,23 @@ export default class irc extends EventEmitter {
this._cmd.get(cmd.command)(cmd); this._cmd.get(cmd.command)(cmd);
}) })
}); });
return (async () => {
(await fs.readdir(`${__dirname}/irc`)).filter(f => f.endsWith(".mjs")).forEach(async mod => (await import(`./irc/${mod}`)).default(this));
return this;
})();
} }
send(data) { send(data) {
this.socket.write(`${data}\n`); this.socket.write(`${data}\n`);
} }
sendmsg(mode, recipient, msg) { sendmsg(mode, recipient, msg) {
msg = msg.split(/\r?\n/); msg = msg.split(/\r?\n/);
if(msg.length > 6) if (msg.length > 6)
return false; return this.emit("data", ["error", "too many lines"]);
msg.forEach(e => { msg.forEach(e => this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", e) ));
this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", e) );
});
} }
parse(data, [a, ...b] = data.split(/ +:/), tmp = a.split(" ").concat(b)) { parse(data, [a, ...b] = data.split(/ +:/), tmp = a.split(" ").concat(b)) {
let prefix = data.charAt(0) === ":" ? tmp.shift() : null const prefix = data.charAt(0) === ":" ? tmp.shift() : null
, command = tmp.shift() , command = tmp.shift()
, params = command.toLowerCase() === "privmsg" ? [ tmp.shift(), tmp.join(" :") ] : tmp; , params = command.toLowerCase() === "privmsg" ? [ tmp.shift(), tmp.join(" :") ] : tmp;
return { return {
@ -109,14 +104,10 @@ export default class irc extends EventEmitter {
network: this.network, network: this.network,
channel: tmp.params[0], channel: tmp.params[0],
channelid: tmp.params[0], channelid: tmp.params[0],
user: Object.assign(this.parsePrefix(tmp.prefix), { user: { ...this.parsePrefix(tmp.prefix), ...{
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix,
/*level: getLevel(this.network, Object.assign(this.parsePrefix(tmp.prefix), {
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account, account: this.server.user.geti(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,
@ -137,15 +128,15 @@ 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}`);
} }
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(user, force = false) {
user = user.toLowerCase(); user = user.toLowerCase();
let tmpuser = {}; let tmpuser = {};
if(this.server.user.has(user) && !force) { if (this.server.user.has(user) && !force) {
tmpuser = this.server.user.get(user); tmpuser = this.server.user.get(user);
if(tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime) if (tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime)
return; return;
} }
@ -166,8 +157,8 @@ export default class irc extends EventEmitter {
} }
parsePrefix(prefix) { parsePrefix(prefix) {
prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix); prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix);
if(!prefix) if (!prefix)
return false; //this.parsePrefix(arguments); return false;
return { return {
nick: prefix[1], nick: prefix[1],
username: prefix[2], username: prefix[2],

View File

@ -1,44 +0,0 @@
import cap from "./cap";
import invite from "./invite";
import join from "./join";
import motd from "./motd";
import msg from "./msg";
import nick from "./nick";
import part from "./part";
import ping from "./ping";
import pwdreq from "./pwdreq";
import welcome from "./welcome";
import who from "./who";
import whois from "./whois";
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);
};
export default [
cap, invite, join,
motd, msg, nick,
part, ping, pwdreq,
welcome, who, whois
];

View File

@ -38,3 +38,28 @@ 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);
};