w0bmircbot/chatCommands.js

235 lines
6.8 KiB
JavaScript

"use strict";
let crypto = require("crypto");
let Sandbox = require("sandbox");
let google = require("google");
google.resultsPerPage = 1;
google.lang = "de";
google.tld = "de";
google.nextText = "Weiter";
const googleSearch = function (client, o) {
google(o.args, function (err, res) {
if (err) return;
const links = res.links;
if (links.length === 0) {
client.say(o.to, o.from + ": could not find anything D:");
}
else {
client.say(o.to, o.from + ": " + links[0].link + " " + links[0].title + " - " + links[0].description.replace(/(\r\n|\r|\n|\t)/g, " "));
}
});
};
let s = new Sandbox({
timeout: 1000
});
setInterval(x=>{
s.run("true", x=>{});
}, 10000);
let sbhead = "\"use strict\";const mett=9000,belst=Math.random()*mett|0;";
sbhead += Object.getOwnPropertyNames(Math).map(function(v){return "const " + v + "=Math." + v + ";"}).join("");
let handleChatCommands = function (irc, db) {
this.irc = irc;
this.db = db;
};
let p = handleChatCommands.prototype;
const userLevels = ["","+","%","@","&","~"];
const userLevelNames = ["user", "voiced", "halfop", "op", "admin", "owner"];
const getLevel = name => userLevelNames.indexOf(name);
const exec = require("child_process").exec;
p.run = function (o) {
let client = this.irc.client;
let db = this.db;
//let level = client.chans[o.to] ? client.chans[o.to].users[o.from] : undefined;
let level = client.chans["#w0bm"] ? client.chans["#w0bm"].users[o.from] : undefined;
level = level !== undefined ? level.split("").map(x=>userLevels.indexOf(x)).sort().pop() : -1;
if (level === null || level === undefined) level = -1;
switch(o.cmd) {
case ".level":
client.say(o.to, o.from + " dein level: " + userLevelNames[level]);
return true;
case ".reboot":
if (level < getLevel("admin") || o.args != "yourself") {
client.say(o.to, o.from + " go kill yourself");
break;
}
client.say(o.to, "kthx bai");
setTimeout(process.exit, 100);
return true;
case ".reg":
if (level < getLevel("admin")) {
client.say(o.to, o.from + ": dein level: " + userLevelNames[level] + ", benötigtes level: admin");
return true;
}
if (!~["open", "close"].indexOf(o.args)) {
client.say(o.to, "momentane befehle: open und close");
return true;
}
client.say(o.to, "kleinen moment bitte");
exec("/bin/bash /var/www/w0bm.com/resources/views/"+o.args+".sh", (err, sout, serr)=>{
client.say(o.to, "registrierung erfolgreich " + (o.args == "open" ? "geöffnet" : "geschlossen"));
});
break;
case ".status":
/*if (level < getLevel("admin")) {
client.say(o.to, o.from + ": dein level: " + userLevelNames[level] + ", benötigtes level: admin");
return true;
}*/
db.getConnection((err, con) => {
if (err) {
client.say(o.to, "sorry.. datenbank is kapott D: BKA belst gz");
con.release();
return;
}
con.query("select disabled, deleted_at, banend, banreason, verified from users where username like ?", o.args, (err, rows, fields) => {
if (err) {
client.say(o.to, "ich bin zu doof zum coden.. hier hat sich ein fehler eingeschlichen D:");
con.release();
return;
}
if (rows.length == 0) {
client.say(o.to, "konnte keinen benutzer mit dem namen finden D:");
con.release();
return;
}
if (rows.length > 1) {
client.say(o.to, "fick dich. hör auf wildcards zu nutzen… oder… wir haben ein datenbank problem…");
con.release();
return;
}
const row = rows[0];
const stati = [];
if (row.disabled) stati.push("deaktiviert");
if (!row.disabled) stati.push("aktiviert");
if (!row.verified) stati.push("nicht verifiziert");
if (row.banend) stati.push("gebannt mit grund: " + row.banreason + ", bis: " + row.banend);
if (row.deleted_at) stati.push("gelöscht seit: " + row.deleted_at);
client.say(o.to, o.args + " ist: " + stati.join(", "));
/*var vid = rows[0];
if (!vid.id) {
client.say(to, from + ": konnte kein video finden D:");
con.release();
return;
}
var txt = [];
if (vid.deleted_at) txt.push("\x02gelöscht\x0f");
if (webm) txt.push("Link: \x02https://w0bm.com/" + vid.id + "\x0f");
["interpret", "songtitle", "category", "username", "comments"].forEach((v, i) => {
if (vid[v]) {
txt.push(
["Interpret", "Titel", "Kategorie", "Uploader", "Kommentare"][i] + ": "
+"\x02" + vid[v] + "\x0f");
}
});
client.say(to, txt.join(" | "));*/
con.release();
});
});
return true;
case "md5sum":
case "sha1sum":
case "sha256sum":
case "sha512sum":
o.cmd = o.cmd.substr(0, o.cmd.length -3);
case "md5":
case "sha1":
case "sha256":
case "sha512":
let crypt = crypto.createHash(o.cmd);
crypt.update(o.args);
client.say(o.to, "teh " + o.cmd + " sum iz: " + crypt.digest("hex"));
return true;
case "js":
/*if (level < getLevel("voiced")) {
client.say(o.to, o.from + ": dein level: " + userLevelNames[level] + ", benötigtes level: voiced");
return true;
}*/
s.run(sbhead + o.args + "+\"\"", function (out) {
let con = out.console.length > 0 ? " and printed to console: " + out.console : "";
let text = (out.result + con).replace(/[\r\n\t]/g, " ");
client.say(o.to, o.from + " it returned: " + (text.length > 400 ? "fuck you" : text));
});
return true;
case "throw":
if (o.args != "a dice") break;
client.say(o.to, o.from + ": the magic dice shows " + ((Math.random()*6|0)+1) + " eyes.");
return true;
case ".choose":
case "choose:":
case "choice:":
let choices = o.args.split(",").map((str)=>str.trim());
if (choices.length < 2) {
client.say(o.to, "fuck you " + o.from + "!");
return true;
};
let choice = ~choices.indexOf("w0bm") ? "w0bm of course!" : choices[Math.random() * choices.length | 0];
client.say(o.to, o.from + ": i'd choose: " + choice);
return true;
case ".mdn":
case "mdn":
o.args += " site:developer.mozilla.org";
googleSearch(client, o);
return true;
case "hoogle":
case ".hs":
case "hs":
case "hss":
o.args += " site:www.haskell.org/hoogle/";
googleSearch(client, o);
return true;
case ".die":
o.args += " site:die.net";
googleSearch(client, o);
return true;
//case "google":
case ".google":
googleSearch(client, o);
return true;
case "b2o":
case "b2d":
case "b2h":
case "o2b":
case "o2d":
case "o2h":
case "d2b":
case "d2o":
case "d2h":
case "h2b":
case "h2o":
case "h2d":
if ({b:/[^01]/,o:/[^0-7]/,d:/[^\d]/,h:/[^\da-fA-F]/}[o.cmd[0]].test(o.args)) return false;
client.say(o.to, o.from + ": " + parseInt(o.args, {b:2,o:8,d:10,h:16}[o.cmd[0]]).toString({b:2,o:8,d:10,h:16}[o.cmd[2]]));
return true;
}
return false;
};
module.exports = function (irc, db) {
let hcc = new handleChatCommands(irc, db);
return function (o) {
hcc.run(o);
}
};