Initial commit

This commit is contained in:
Flummi
2018-09-02 12:07:40 +02:00
commit 47ccf527ee
30 changed files with 1768 additions and 0 deletions

195
src/clients/irc.mjs Normal file
View File

@ -0,0 +1,195 @@
import { logger } from "../inc/log";
import { getLevel } from "../inc/admin";
import modules from "./irc/index";
import net from "net";
import tls from "tls";
import EventEmitter from "events";
const colors = {
red: "\x0304$1\x0304",
blue: "\x0312$1\x0312",
yellow: "\x0308$1\x0308"
};
const msgmodes = {
normal: "PRIVMSG {recipient} :{msg}",
action: "PRIVMSG {recipient} :\u0001ACTION {msg}\u0001",
notice: "NOTICE {recipient} :{msg}"
};
const replaceColor = (match, color, text) => {
if (colors.hasOwnProperty(color))
return colors[color].replace("\$1", text);
return text;
};
export class irc extends EventEmitter {
constructor(options) {
super();
this.options = options || {};
this.options.channels = this.options.channels || [];
this.options.host = this.options.host || "127.0.0.1";
this.options.port = this.options.port || 6667;
this.options.ssl = this.options.ssl || false;
this.options.selfSigned = this.options.selfSigned || false;
this.options.sasl = this.options.sasl || false;
this.network = this.options.network || "test";
this.nickname = this.options.nickname || "test";
this.username = this.options.username || "test";
this.realname = this.options.realname || "test";
this.channels = this.options.channels || [];
this.set = this.options.set || "all";
this._recachetime = 60 * 30; // 30 minutes
this._cmd = new Map();
modules.forEach(mod => mod(this));
this.server = {
set: this.set,
motd: "",
me: {},
channel: [],
user: new Map()
};
this.socket = (this.options.ssl ? tls : net).connect({
host: this.options.host,
port: this.options.port,
rejectUnauthorized: !this.options.selfSigned
}, () => {
this.send(`NICK ${this.nickname}`);
this.send(`USER ${this.username} 0 * : ${this.realname}`);
if(this.options.sasl)
this.send("CAP LS");
});
this.socket.setEncoding("utf-8");
this.socket.on("data", msg => {
msg.split(/\r?\n|\r/).filter(tmp => tmp.length > 0).forEach(tmp => {
const cmd = this.parse(tmp);
if (this._cmd.has(cmd.command))
this._cmd.get(cmd.command)(cmd);
})
});
}
send(data) {
this.socket.write(`${data}\n`);
logger.debug(`(${this.network}) out: ${data}`);
}
sendmsg(mode, recipient, msg) {
msg = msg.split(/\r?\n/);
if(msg.length > 6)
return false;
msg.forEach(e => {
this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", e) );
});
}
parse(data, [a, ...b] = data.split(/ +:/)) {
let tmp = a.split(" ").concat(b);
logger.debug(`(${this.network}) in: ${[...tmp]}`);
return data.charAt(0) === ":" ? {
prefix: tmp.shift(),
command: tmp.shift(),
params: tmp
} : {
prefix: null,
command: tmp.shift(),
params: tmp
};
}
reply(tmp) {
return {
type: "irc",
network: this.network,
channel: tmp.params[0],
channelid: tmp.params[0],
user: Object.assign(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,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix
}))
}),
message: tmp.params[1],
time: ~~(Date.now() / 1000),
raw: tmp,
reply: msg => this.sendmsg("normal", tmp.params[0], this.format(""+msg)),
replyAction: msg => this.sendmsg("action", tmp.params[0], this.format(""+msg)),
replyNotice: msg => this.sendmsg("notice", tmp.params[0], this.format(""+msg)),
self: this.server,
_chan: this.server.channel[tmp.params[0]],
_user: this.server.user,
_cmd: this._cmd,
join: chan => this.join(chan),
part: (chan, msg) => this.part(chan, msg),
whois: user => this.whois(user),
write: msg => this.send(msg),
socket: this.socket
};
}
join(channel) {
this.send(`JOIN ${(typeof channel === "object") ? channel.join(",") : 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.hasi(user) && !force) {
tmpuser = this.server.user.geti(user);
if(tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime)
return;
}
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}`);
}
parsePrefix(prefix) {
prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix);
if(!prefix)
return false; //this.parsePrefix(arguments);
return {
nick: prefix[1],
username: prefix[2],
hostname: prefix[3]
};
}
format(msg) {
return msg
.replace(/\[b\](.*?)\[\/b\]/g, "\x02$1\x02") // bold
.replace(/\[i\](.*?)\[\/i\]/g, "\x1D$1\x1D") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, replaceColor) // colors
;
}
}
Map.prototype.hasi = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
return true;
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);
};

21
src/clients/irc/cap.mjs Normal file
View File

@ -0,0 +1,21 @@
export default client => {
client._cmd.set("CAP", function (msg) { // capkram
switch (msg.params[1]) {
case "LS": // list
this.send(`CAP REQ :${msg.params[2]}`);
break;
case "ACK": // success
this.send("AUTHENTICATE PLAIN");
break;
}
}.bind(client));
client._cmd.set("AUTHENTICATE", function (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));
client._cmd.set("900", function (msg) { // cap end
this.send("CAP END");
}.bind(client));
};

19
src/clients/irc/index.mjs Normal file
View File

@ -0,0 +1,19 @@
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";
export default [
cap, invite, join,
motd, msg, nick,
part, ping, pwdreq,
welcome, who, whois
];

View File

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

5
src/clients/irc/join.mjs Normal file
View File

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

14
src/clients/irc/motd.mjs Normal file
View File

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

9
src/clients/irc/msg.mjs Normal file
View File

@ -0,0 +1,9 @@
export default client => {
client._cmd.set("PRIVMSG", function (msg) { // privmsg
this.emit("data", msg.params[1] === "\u0001VERSION\u0001" ? ["ctcp:version", this.reply(msg)] : ["message", this.reply(msg)]);
}.bind(client));
client._cmd.set("NOTICE", function (msg) { // notice
this.emit("data", ["notice", msg.params[1]]);
}.bind(client));
};

8
src/clients/irc/nick.mjs Normal file
View File

@ -0,0 +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));
};

5
src/clients/irc/part.mjs Normal file
View File

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

5
src/clients/irc/ping.mjs Normal file
View File

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

View File

@ -0,0 +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));
};

View File

@ -0,0 +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));
};

27
src/clients/irc/who.mjs Normal file
View File

@ -0,0 +1,27 @@
const max = 400;
let whois = [];
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]
});
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));
whois = [];
}.bind(client));
};

95
src/clients/irc/whois.mjs Normal file
View File

@ -0,0 +1,95 @@
export default client => {
client._cmd.set("307", function (msg) { // whois_identified (ircd-hybrid)
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.account = msg.params[1];
tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("311", function (msg) { // whois_userdata
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(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));
client._cmd.set("313", function (msg) { // whois_oper
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.oper = true;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("318", function (msg) { // whois_end
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
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)
};
if(msg.params[0] === msg.params[1])
this.server.me = tmpuser;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("319", function (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);
}
let chans = msg.params[2].trim().split(" ");
for (let chan in chans) {
chan = chans[chan].split("#");
tmpchan.set(`#${chan[1]}`, chan[0]);
}
tmpuser.channels = tmpchan;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("330", function (msg) { // whois_authed_as (snircd)
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.account = msg.params[2];
tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
};
Map.prototype.hasi = function (val) {
for (let [key] of this)
if (key.toLowerCase() === val.toLowerCase())
return true;
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);
};

159
src/clients/tg.mjs Normal file
View File

@ -0,0 +1,159 @@
import { logger } from "../inc/log";
import { getLevel } from "../inc/admin";
import rp from "request-promise-native";
import EventEmitter from "events";
export class tg extends EventEmitter {
constructor(options) {
super();
this.options = options || {};
this.token = options.token || null;
this.options.pollrate = options.pollrate || 1000;
this.set = this.options.set || "all";
this.network = "Telegram";
this.api = `https://api.telegram.org/bot${this.token}`;
this.lastUpdate = 0;
this.lastMessage = 0;
this.server = {
set: this.set,
channel: new Map(),
user: new Map(),
me: {}
};
this.connect().then(() => {
this.poller = setInterval(() => { this.poll(); }, this.options.pollrate);
});
}
connect() {
return new Promise((resolve, reject) => {
rp(`${this.api}/getMe`, { json: true })
.then(res => {
if(res.ok) {
this.me = res.result;
this.server.me = {
nickname: res.result.first_name,
username: res.result.username,
account: res.result.id.toString(),
prefix: `${res.result.username}!${res.result.id.toString()}`,
id: res.result.id.toString()
};
resolve();
}
else {
logger.error(`(${this.network}) ${res}`);
reject();
}
})
.catch(err => {
logger.error(`(${this.network}) ${err.message}`);
reject();
});
});
}
poll() {
rp(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`, { json:true })
.then(res => {
if(res.ok && res.result.length > 0) {
res = res.result[res.result.length-1];
this.lastUpdate = res.update_id + 1;
if (res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) {
this.lastMessage = res.message.message_id;
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
this.server.user.set(res.message.from.username || res.message.from.first_name, {
nick: res.message.from.first_name,
username: res.message.from.username,
account: res.message.from.id.toString(),
prefix: `${res.message.from.username}!${res.message.from.id.toString()}`,
id: res.message.from.id
});
}
this.emit("data", ["message", this.reply(res.message)]);
}
}
})
.catch(err => {
if(err.statusCode !== 409)
logger.error(`(${this.network}) ${err.message}`);
});
}
send(chatid, msg, reply = null) {
if(msg.length === 0 || msg.length > 2048)
return false;
const opts = {
method: 'POST',
uri: `${this.api}/sendMessage`,
body: {
chat_id: chatid,
text: msg,
parse_mode: "HTML"
},
json: true
};
if(reply)
opts.body.reply_to_message_id = reply;
rp(opts)
.then(res => {})
.catch(err => {
logger.error(`(${this.network}) ${err.message}`);
});
}
sendmsg(mode, recipient, msg) {
this.send(recipient, msg);
}
reply(tmp) {
return {
type: "tg",
network: "Telegram",
channel: tmp.chat.title,
channelid: tmp.chat.id,
user: {
prefix: `${tmp.from.username}!${tmp.from.id}`,
nick: tmp.from.first_name,
username: tmp.from.username,
account: tmp.from.id.toString(),
level: getLevel("Telegram", {
prefix: `${tmp.from.username}!${tmp.from.id}`,
nick: tmp.from.first_name,
username: tmp.from.username,
account: tmp.from.id.toString()
})
},
self: this.server,
message: tmp.text,
time: tmp.date,
raw: tmp,
reply: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id),
replyAction: msg => this.send(tmp.chat.id, this.format(`f0ck ${msg}`), tmp.message_id),
replyNotice: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id),
_user: this.server.user
};
}
format(msg) {
return msg.toString()
.split("<").join("&lt;")
.split(">").join("&gt;")
.replace(/\[b\](.*?)\[\/b\]/g, "<b>$1</b>") // bold
.replace(/\[i\](.*?)\[\/i\]/g, "<i>$1</i>") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, "$2")
;
}
}
Map.prototype.hasi = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
return true;
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);
};

42
src/inc/admin.mjs Normal file
View File

@ -0,0 +1,42 @@
import sql from "./sql";
export let admins = [];
export const loadAdmins = () => {
admins = [];
sql.exec(`select * from admins`)
.then(rows => {
rows.forEach(row => {
admins.push({
id: row.id,
prefix: row.prefix,
account: row.account,
network: row.network,
level: row.level
});
});
})
.catch(err => {
console.log("keine Admins vorhanden");
});
};
loadAdmins();
export const getLevel = (network, user) => {
let ret = {
level: 0,
verified: false
};
if (typeof user !== "object")
return "user has to be an object!";
if (!user.account || !user.prefix)
return ret;
for(let admin of admins) {
if (admin.account === user.account.toLowerCase() && admin.network === network.toLowerCase()) {
ret = {
level: admin.level,
verified: user.prefix.toLowerCase() === admin.prefix
};
}
};
return ret;
};

37
src/inc/cfg.mjs Normal file
View File

@ -0,0 +1,37 @@
import sql from "./sql";
let cfg = {
client: {},
main: {},
websrv: {},
trigger: {}
};
const read = () => new Promise((resolve, reject) => {
sql.exec("select * from cfg").then(rows => {
for (let row in rows) {
cfg[rows[row].class][rows[row].key] = {
val: ((type, value) => {
switch (type) {
case "string":
return value;
case "int":
return parseInt(value);
case "bool":
return value === "true";
case "json":
return JSON.parse(value);
}
})(rows[row].type, rows[row].value),
hidden: rows[row].hidden === 1,
type: rows[row].type
}
}
resolve();
})
.catch(err => {
reject("no cfg");
})
});
export { cfg, read };

12
src/inc/events/ctcp.mjs Normal file
View File

@ -0,0 +1,12 @@
import { logger } from "../log";
const versions = [
"AmIRC.1 (8 Bit) for Commodore Amiga 500",
"HexChat 0.72 [x86] / Windows 95c [500MHz]"
];
export default self => {
self.bot.on("ctcp:version", e => {
e.write(`notice ${e.user.nick} :\u0001VERSION ${versions[~~(Math.random() * versions.length)]}\u0001`);
});
};

6
src/inc/events/index.mjs Normal file
View File

@ -0,0 +1,6 @@
import ctcp from "./ctcp";
import message from "./message";
export default [
ctcp, message
];

View File

@ -0,0 +1,45 @@
import { logger } from "../log";
import { cfg } from "../cfg";
const parseArgs = msg => {
let args = msg.trim().split(" ");
let cmd = args.shift();
return {
cmd: cmd.replace(/^(\.|\/|\!)/, ""),
args: args
};
};
export default self => {
self.bot.on("message", e => {
for (var [name, trigger] of self._trigger.entries()) {
if (!trigger.call.exec(e.message))
continue;
if (!trigger.clients.includes(e.type))
continue;
/*let active = false;
if (e.type === "irc" && cfg.trigger[e.network + e.channel]) {
if (cfg.trigger[e.network + e.channel].val[trigger.name])
active = true;
}
else
active = trigger.active;*/
//if (!active)
// continue;
if ((e.self.set !== "all" && e.self.set !== trigger.set) && trigger.set !== "all")
continue;
if (trigger.level > e.user.level.level) {
e.reply(`no permission, min level ${trigger.level} required`);
break;
}
e = Object.assign(e, parseArgs(e.message));
trigger.f(e);
}
logger.info(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}`);
});
};

26
src/inc/log.mjs Normal file
View File

@ -0,0 +1,26 @@
import winston from "winston";
const logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
name: "debug-file",
filename: `./logs/${~~(Date.now() / 1000)}_debug.log`,
level: "debug"
}),
new (winston.transports.File)({
name: "info-file",
filename: `./logs/${~~(Date.now() / 1000)}_info.log`,
level: "info"
}),
new (winston.transports.File)({
name: "error-file",
filename: `./logs/${~~(Date.now() / 1000)}_error.log`,
level: "error"
}),
new (winston.transports.Console)({
level: "info"
})
]
});
export { logger };

5
src/inc/sql.mjs Normal file
View File

@ -0,0 +1,5 @@
import mysql from "nodejs-mysql2";
import { default as cfg } from "../../cfg/sql.json";
const sql = mysql.default.getInstance(cfg);
export default sql;

37
src/inc/trigger/debug.mjs Normal file
View File

@ -0,0 +1,37 @@
import { admins, getLevel } from "../admin";
import vm from "vm";
const maxoutput = 750;
let context = vm.createContext({
e: null,
bot: null,
admins: null,
});
export default bot => {
bot._trigger.set("sandbox_debug", new bot.trigger({
call: /^\!debug (.*)/i,
level: 100,
active: true,
f: e => {
const args = e.message.trim().substring(7);
try {
context.admins = admins;
context.e = e;
context.bot = bot;
context.level = getLevel;
let output = vm.runInContext(args, vm.createContext(context));
if (typeof output !== undefined && output) {
output = JSON.stringify(output);
if (output.length > maxoutput)
return e.reply(`holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)`);
else
return e.reply(output);
}
}
catch (err) {
e.reply(err.message);
}
}
}));
};

View File

@ -0,0 +1,4 @@
import debug from "./debug";
import parser from "./parser";
export default [ debug, parser ];

241
src/inc/trigger/parser.mjs Normal file
View File

@ -0,0 +1,241 @@
import { cfg } from "../cfg";
import sql from "../sql";
import fs from "fs";
import path from "path";
import crypto from "crypto";
import uuid from "uuid";
import cloudscraper from "cloudscraper";
import readChunk from "read-chunk";
import ytdl from "ytdl-core";
import request from "request";
import fileType from "file-type";
import { Readable } from "stream";
const checkRepost = (url, cbcr) => {
sql.exec("select count(id) as count, id from `f0ck`.`items` where `src` = ?", url).then(rows => {
cbcr((rows[0].count == 0)?true:rows[0].id);
});
};
const checkRepostCheckSum = (cs, cbcrcs) => {
sql.exec("select count(id) as count, id from `f0ck`.`items` where `checksum` = ?", cs).then(rows => {
cbcrcs((rows[0].count == 0)?true:rows[0].id);
});
};
const formatSize = (size) => {
const i = ~~(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};
const getCheckSum = (file, cbcs) => {
var sha256sum = crypto.createHash('sha256');
fs.ReadStream(file)
.on('data', d => sha256sum.update(d))
.on('end', () => cbcs(sha256sum.digest('hex')));
};
export default bot => {
bot._trigger.set("parser", new bot.trigger({
call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi,
f: e => {
if(e.channel === "#kbot-dev" || e.message.match(/(!|-)f0ck/i)) {
if(!e.message.match(/(!|-)ignore/)) {
const tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
tmp.forEach((entry,i,a) => {
if(!entry.match(/f0ck\.me/i) && !entry.match(/\.onion/i)) {
getLink(entry, ((e.message.match(/(!|-)force/i) && e.user.level.level >= 100)?true:false), e.user.level.level, cb => {
if(cb.success === true) {
fs.rename(cb.file, cb.file + '.' + cb.info.ext, (err) => {
if(!err) {
sql.exec("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`,`thumb`) values (?,?,?,?,?,?,?,?,?,?,?)", [
entry,
cb.file + '.' + cb.info.ext,
cb.info.mime,
cb.size,
cb.checksum,
e.user.nick,
e.channel,
e.network,
~~(new Date() / 1000),
0,
cb.info.thumb ? cb.info.thumb : ''
]).then(result => {
//lib.generateThumbs();
e.reply(cfg.main.url.val+"/"+result.insertId+" - "+cb.info.title+" ("+cb.info.mime+", ~"+formatSize(cb.size)+") by "+e.user.nick+" ("+e.user.prefix+")");
}).catch(msg => {
e.reply("ups", msg);
});
}
else {
e.reply("Datei konnte nicht verschoben werden D:");
e.reply(`${cb.file} -> ${cb.file}.${cb.info.ext}`);
}
});
}
else {
e.reply(JSON.stringify(cb));
fs.stat(process.cwd() + '/b/' + cb.file, (err, stat) => {
if(cb.msg !== '')
e.reply(cb.msg);
if(!err && stat.isFile())
fs.unlinkSync(process.cwd() + '/b/' + cb.file);
});
}
});
}
});
}
else
e.reply("ignored");
}
else
e.reply("ignore");
}
}));
};
const getLink = (url, force, userlevel, cb) => {
const yt = /https?:\/\/(www\.)?youtu(\.be\/|be\.com\/)((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi;
const sc = /https?:\/\/(www\.)?(soundcloud\.com|snd\.sc)(\/\S*)(\/\S*)/gi;
checkRepost(url, cbcr => {
var tmpdest = uuid.v1().split('-')[0];
if(cbcr === true) {
var dat = fs.createWriteStream(process.cwd() + '/b/' + tmpdest);
var info;
if(url.match(yt)) { // ytdl
ytdl.getInfo(url, (err, inf) => {
if(!err) {
var title = inf.title;
var iurl = inf.thumbnail_url; //JSON.parse(inf.player_response).videoDetails.thumbnail.thumbnails[0].url.split('?')[0];
try {
dlformat = { filter: (format) => { return format.container === 'webm'; } };
ytdl.downloadFromInfo(inf, dlformat)
.on('response', (res) => {
if( ( res.headers['content-length'] <= cfg.main.maxFileSize.val ) || force || ( userlevel >= 60 && res.headers['content-length'] <= (cfg.main.maxFileSize.val * 2) ) ) {
info = {
type: 'youtube',
title: title,
mime: 'video/webm',
ext: 'webm',
thumb: iurl
};
}
else {
res.destroy();
dat.end();
return cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+formatSize(res.headers['content-length'])+'), max '+formatSize( ( userlevel >= 60?(cfg.main.maxFileSize.val*2):cfg.main.maxFileSize.val ) )+' allowed' });
}
})
.on('error', (err) => {
dat.end();
return cb({ success: false, file: tmpdest, msg: err.message });
})
.pipe(dat);
}
catch(ex) {
dat.end();
return cb({ success: false, file: tmpdest, msg: ex.message });
}
}
});
}
else if(url.match(sc)) { // scdl
request('https://api.soundcloud.com/resolve.json?client_id=' + cfg.main.scclientid.val + '&url=' + url, (err, res, body) => {
if(!err && res.statusCode === 200) {
var data = JSON.parse(body);
request(data.stream_url + ((data.stream_url.indexOf('?') === -1)?'?':'&') + 'client_id=' + cfg.main.scclientid.val)
.pipe(dat);
info = {
type: 'soundcloud',
title: data.title,
mime: 'audio/mpeg',
ext: 'mp3',
thumb: (data.artwork_url !== null)?data.artwork_url.replace('large.jpg', 't300x300.jpg'):null
};
}
else {
dat.end();
return cb({ success: false, file: tmpdest, msg: 'f0ck sc-api' });
}
});
}
else { // various
cloudscraper.request({
method: 'GET',
url: url,
encoding: null,
},
(err, res, data) => {
if(!err) {
var type = res.headers['content-type'].split(";")[0];
if(cfg.main.allowedMimes.val.hasOwnProperty(type)) {
if( ( data.length <= cfg.main.maxFileSize.val ) || force || ( userlevel >= 60 && data.length <= (cfg.main.maxFileSize.val * 2) ) ) {
var s = new Readable;
s.push(data);
s.push(null);
s.pipe(dat);
info = {
type: 'other',
title: path.parse(url).base,
mime: type,
ext: cfg.main.allowedMimes[type],
thumb: null
};
}
else {
dat.end();
return cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+formatSize(data.length)+'), max '+formatSize( ( userlevel >= 60?(cfg.main.maxFileSize.val*2):cfg.main.maxFileSize.val ) )+' allowed' });
}
}
else {
dat.end();
return cb({ success: false, file: tmpdest, msg: 'irgendwas mit mime oder so.' });
}
}
else {
dat.end();
return cb({ success: false, file: tmpdest, msg: 'nope.' });
}
});
}
dat
.on('finish', () => {
var size = dat.bytesWritten;
dat.end();
if( ( size <= cfg.main.maxFileSize.val ) || force || ( userlevel >= 60 && size <= (cfg.main.maxFileSize.val * 2) ) ) {
fs.stat(process.cwd() + '/b/' + tmpdest, (err, stat) => {
if(!err && stat.isFile() && stat.size > 300) {
getCheckSum(process.cwd() + '/b/' + tmpdest, (cbcs) => {
checkRepostCheckSum(cbcs, (cbcrcs) => {
if(cbcrcs === true) {
var mime = fileType(readChunk.sync(process.cwd() + '/b/' + tmpdest, 0, 262));
info.ext = mime.ext;
info.mime = mime.mime;
if(cfg.main.allowedMimes.val.hasOwnProperty(mime.mime) || info.type === 'soundcloud')
return cb({ success: true, info: info, size: size, file: process.cwd() + '/b/' + tmpdest, checksum: cbcs });
else
return cb({ success: false, file: tmpdest, msg: 'lol, go f0ck yourself ('+mime.mime+')' });
}
else
return cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+cfg.main.url.val+'/'+cbcrcs });
});
});
}
else
return cb({ success: false, file: tmpdest, msg: 'nope' });
});
}
else
return cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+formatSize(size)+'), max '+formatSize( ( userlevel >= 60?(cfg.main.maxFileSize.val*2):cfg.main.maxFileSize.val ) )+' allowed' });
})
.on('error', err => {
return cb({ success: false, file: tmpdest, msg: err });
});
}
else
return cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+cfg.main.url.val+'/'+cbcr });
});
};

41
src/inc/wrapper.mjs Normal file
View File

@ -0,0 +1,41 @@
import { cfg } from "./cfg";
import { irc as irclib } from "../clients/irc";
import { tg as tglib } from "../clients/tg";
import EventEmitter from "events";
const clients = [];
const wrapper = class wrapper extends EventEmitter {
constructor() {
super();
for (let srv in cfg.client) {
if(cfg.client[srv].val.enabled) {
switch (cfg.client[srv].val.type) {
case "irc":
clients.push({
name: cfg.client[srv].val.network,
type: "irc",
client: new irclib(cfg.client[srv].val)
});
break;
case "tg":
clients.push({
name: "tg",
type: "tg",
client: new tglib(cfg.client[srv].val)
});
break;
}
}
}
clients.forEach(client => {
client.client.on("data", e => {
this.emit(e[0], e[1]);
});
});
}
};
export { wrapper, clients };

28
src/index.mjs Normal file
View File

@ -0,0 +1,28 @@
import { logger } from "./inc/log";
import { read, cfg } from "./inc/cfg";
import { wrapper } from "./inc/wrapper";
import triggers from "./inc/trigger";
import events from "./inc/events";
read().then(() => {
const self = {
_trigger: new Map(),
trigger: function trigger(args) {
this.call = args.call;
this.help = args.help || false;
this.level = args.level || 0;
this.active = args.hasOwnProperty("active") ? args.active : true;
this.set = args.set || "all"; // uwe, nxy, f0ck, all
this.clients = args.clients || ["irc", "tg", "discord"];
this.f = args.f;
},
bot: new wrapper()
};
triggers.forEach(mod => {
console.log(mod);
mod(self);
});
events.forEach(event => event(self));
}).catch(err => logger.error(`(main) ${err.message}`));