This commit is contained in:
Flummi 2020-04-03 13:12:35 +02:00
parent d39deeb038
commit a202ae2e69
11 changed files with 297 additions and 16 deletions

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
node_modules/ node_modules/
logs/ logs/*.log
config.json config.json
public/b/* public/b/*
public/t/* public/t/*
tmp/*

0
logs/.empty Normal file
View File

View File

@ -22,8 +22,7 @@ export default async bot => {
t[1].call.exec(e.message) && t[1].call.exec(e.message) &&
t[1].clients.includes(e.type) && t[1].clients.includes(e.type) &&
t[1].active && t[1].active &&
t[1].level <= getLevel(e.network, e.user).level && t[1].level <= getLevel(e.user).level
!((e.self.set !== "all" && e.self.set !== t[1].set) && t[1].set !== "all")
); );
trigger.forEach(async t => { trigger.forEach(async t => {
@ -31,9 +30,9 @@ export default async bot => {
await t[1].f({ ...e, ...parseArgs(e.message) }); await t[1].f({ ...e, ...parseArgs(e.message) });
console.log(`triggered > ${t[0]}`); console.log(`triggered > ${t[0]}`);
} }
catch(error) { catch(err) {
e.reply(`${t[0]}: An error occured.`); e.reply(`${t[0]}: An error occured.`);
logger.error(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${error.toString ? error : JSON.stringify(error)}`); logger.error(`${e.network} -> ${e.channel} -> ${e.user.nick}: ${err.toString ? err : JSON.stringify(err)}`);
} }
}); });
} }

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

@ -0,0 +1,5 @@
export default new class {
formatSize(size, i = ~~(Math.log(size) / Math.log(1024))) {
return (size / Math.pow(1024, i)).toFixed(2) * 1 + " " + ["B", "kB", "MB", "GB", "TB"][i];
}
};

View File

@ -1,4 +1,4 @@
import { promises as fs } from "fs"; import fs from "fs";
import path from "path"; import path from "path";
export default new class Router { export default new class Router {
@ -19,23 +19,61 @@ export default new class Router {
async static({ dir = path.resolve() + "/public", route = /^\/public/ }) { async static({ dir = path.resolve() + "/public", route = /^\/public/ }) {
if(!this.#mimes) { if(!this.#mimes) {
this.#mimes = new Map(); this.#mimes = new Map();
(await fs.readFile("/etc/mime.types", "utf-8")) (await fs.promises.readFile("/etc/mime.types", "utf-8"))
.split("\n") .split("\n")
.filter(e => !e.startsWith("#") && e) .filter(e => !e.startsWith("#") && e)
.map(e => e.split(/\s{2,}/)) .map(e => e.split(/\s{2,}/))
.filter(e => e.length > 1) .filter(e => e.length > 1)
.forEach(m => m[1].split(" ").forEach(ext => this.#mimes.set(ext, m[0]))); .forEach(m => m[1].split(" ").forEach(ext => this.#mimes.set(ext, m[0])));
} }
this.get(route, async (req, res) => { this.get(route, async (req, res) => {
try { try {
const mime = this.#mimes.get(req.url.path.split(".").pop());
const file = path.join(dir, req.url.path.replace(route, ""));
let stat;
try {
stat = await fs.promises.stat(file);
} catch {
return res.reply({
code: 404,
body: "404 - file not found."
});
}
if(!mime.startsWith("video") && !mime.startsWith("audio")) {
return res.reply({
type: this.#mimes.get(req.url.path.split(".").pop()).toLowerCase(),
body: await fs.promises.readFile(path.join(dir, req.url.path.replace(route, "")))
});
}
if(req.headers.range) {
const parts = req.headers.range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : stat.size - 1;
res.writeHead(206, {
"Content-Range": `bytes ${start}-${end}/${stat.size}`,
"Accept-Ranges": "bytes",
"Content-Length": (end - start) + 1,
"Content-Type": mime,
});
const stream = fs.createReadStream(file, { start: start, end: end })
.on("open", () => stream.pipe(res))
.on("error", err => res.end(err));
}
else {
res.writeHead(200, {
"Content-Length": stat.size,
"Content-Type": mime,
});
fs.createReadStream(file).pipe(res);
}
} catch(err) {
console.error(err);
return res.reply({ return res.reply({
type: this.#mimes.get(req.url.path.split(".").pop()).toLowerCase(), code: 500,
body: await fs.readFile(path.join(dir, req.url.path.replace(route, ""))) body: "500 - f0ck hat keinen b0ck"
});
} catch {
return res.reply({
code: 404,
body: "404 - file not found"
}); });
} }
}); });

View File

@ -17,7 +17,7 @@ router.get("/", async (req, res) => {
const query = await sql.query("select id, mime from items order by id desc limit 300"); const query = await sql.query("select id, mime from items order by id desc limit 300");
const data = { const data = {
items: query, items: query,
last: query[0].id last: query[query.length - 1].id
}; };
res.reply({ res.reply({

View File

@ -0,0 +1,35 @@
import { promises as fs } from "fs";
import sql from "../sql.mjs";
export default async bot => {
return [{
name: "delete",
call: /^\!(del|rm) .*/i,
active: true,
level: 100,
f: async e => {
const ret = (await Promise.all(e.args.map(async id => {
id = +id;
if(id <= 0)
return false;
const f0ck = await sql.query("select dest from items where id = ? limit 1", [ id ]);
if(f0ck.length === 0)
return false;
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
await fs.unlink(`./public/t/${id}`).catch(_=>{});
await sql.query("delete from items where id = ? limit 1", [ id ]);
return id;
}))).filter(d => d);
if(ret.length > 0)
e.reply(`deleted ${ret.length}/${e.args.length} (${ret.join(",")}) f0cks`);
else
e.reply(`oof`)
}
}]
};

32
src/inc/trigger/f0ck.mjs Normal file
View File

@ -0,0 +1,32 @@
import { promises as fs } from "fs";
import cfg from "../../../config.json";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
export default async bot => {
return [{
name: "f0ck",
call: /^\!f0ck .*/i,
active: true,
level: 100,
f: async e => {
switch(e.args[0]) {
case "stats":
const dirs = {
b: await fs.readdir("./public/b"),
t: await fs.readdir("./public/t")
};
const sizes = {
b: (await Promise.all(dirs.b.map(async file => (await fs.stat(`./public/b/${file}`)).size))).reduce((a, b) => b + a),
t: (await Promise.all(dirs.t.map(async file => (await fs.stat(`./public/t/${file}`)).size))).reduce((a, b) => b + a),
};
return e.reply(`${dirs.b.length} f0cks: ${sizes.b}, ${dirs.t.length} thumbnails: ${sizes.t}`);
case "limit":
return e.reply(`up to ${lib.formatSize(cfg.main.maxfilesize)} (${lib.formatSize(cfg.main.maxfilesize * 2.5)} for admins)`);
default:
return e.reply("lul");
}
}
}]
};

View File

@ -0,0 +1,40 @@
import cfg from "../../../config.json";
import sql from "../sql.mjs";
import lib from "../lib.mjs";
const _query = "select id, mime, size, username, userchannel, usernetwork, stamp from items where ";
const regex = /https\:\/\/f0ck\.me\/(\d+|(?:b\/)(\w{8})\.(jpg|webm|gif|mp4|png|mov|mp3|ogg|flac))/gi;
export default async bot => {
return [{
name: "f0ckgag",
call: regex,
active: true,
f: async e => {
const dat = e.message.match(regex)[0].split(/\//).pop();
let query, arg;
if(dat.includes(".")) {
query = _query + "dest like ?";
arg = `%${dat}%`;
}
else {
query = _query + "id = ?";
arg = dat;
}
const rows = await sql.query(query, [ arg ]);
if(rows.length === 0)
return e.reply("no f0cks given! lol D:");
e.reply([
`${cfg.main.url}/${rows[0].id}`,
`user: ${rows[0].username} @ ${rows[0].usernetwork} ${rows[0].userchannel}`,
`~${lib.formatSize(rows[0].size)}`,
rows[0].mime,
new Date(rows[0].stamp * 1e3).toString().slice(0, 24)
].join(" - "));
}
}];
};

View File

@ -0,0 +1,39 @@
import sql from "../sql.mjs";
import lib from "../lib.mjs";
import cfg from "../../../config.json";
export default async bot => {
return [{
name: "f0ckrand",
call: /^gib f0ck/i,
active: true,
f: async e => {
let args = e.args.slice(2);
let params = {
inc: [],
exc: []
};
for(let i = 0; i < args.length; i++) {
let name = args[0];
if(name.charAt(0) === "!")
params.exc.push(name.slice(1));
else
params.inc.push(name);
}
let vars = params.inc.concat(params.inc.length == 0 ? params.exc : []);
params.inc = new Array(params.inc.length).fill("username like ?");
params.exc = new Array(params.inc.length == 0 ? params.exc.length : 0).fill("username not like ?");
let where = params.inc.concat(params.exc).join(" || ");
let query = `select id, username, mime, size from items ${where.length > 0 ? `where ${where}` : ""} order by rand() limit 1`;
const rows = await sql.query(query, vars);
if(rows.length === 0)
return e.reply("nothing found, f0cker");
e.reply(`f0ckrnd: ${cfg.main.url}/${rows[0].id} by: ${rows[0].username} (${rows[0].mime}, ~${lib.formatSize(rows[0].size)})`);
}
}];
};

View File

@ -0,0 +1,92 @@
import cfg from "../../../config.json";
import sql from "../sql.mjs";
import fs from "fs";
import { exec as _exec } from "child_process";
import { promisify } from "util";
const exec = promisify(_exec);
const regex = /https?:\/\/[\w\S(\.|:|/)]+/gi;
export default async bot => {
return [{
name: "parser",
call: regex,
active: true,
f: e => {
const links = e.message.match(regex)?.filter(link => {
return (
!link.includes("f0ck.me")
);
});
if(links.length === 0)
return false;
e.reply(`parsing ${links.length} link${links.length > 1 ? "s" : ""}...`);
links.forEach(async link => {
// check repost (link)
const q_repost = await sql.query("select id from items where src = ?", [ link ]);
if(q_repost.length > 0)
return e.reply(`repost motherf0cker (link): ${cfg.main.url}/${q_repost[0].id}`);
// generate uuid
const uuid = (await sql.query("select left(uuid(), 8) as uuid"))[0].uuid;
// read metadata
const meta = JSON.parse((await exec(`youtube-dl --skip-download --dump-json "${link}"`)).stdout);
const filename = `${uuid}.${meta.ext}`;
// download data
const source = (await exec(`youtube-dl "${link}" --max-filesize 100m -o ./tmp/${filename}`)).stdout.trim();
if(source.match(/larger than/))
return e.reply("too large lol");
// generate checksum
const checksum = (await exec(`sha256sum ./tmp/${filename}`)).stdout.trim().split(" ")[0];
const size = fs.statSync(`./tmp/${filename}`).size;
// mime check
const mime = (await exec(`file --mime-type -b ./tmp/${filename}`)).stdout.trim();
if(!cfg.allowed.includes(mime))
return e.reply(`lol, go f0ck yourself (${mime})`);
// check repost (checksum)
const q_repostc = await sql.query("select id from items where checksum = ?", [ checksum ]);
if(q_repostc.length > 0)
return e.reply(`repost motherf0cker (checksum): ${cfg.main.url}/${q_repostc[0].id}`);
await fs.promises.copyFile(`./tmp/${filename}`, `./public/b/${filename}`);
await fs.promises.unlink(`./tmp/${filename}`).catch(_=>{});
const insertq = await sql.query(
"insert into items (src, dest, mime, size, checksum, username, userchannel, usernetwork, stamp, active) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[ link, filename, mime, size, checksum, e.user.username, e.channel, e.network, ~~(new Date() / 1000), 1 ]
);
// generate thumbnail
let thumb_orig = (await exec(`youtube-dl --get-thumbnail "${link}"`)).stdout.trim();
if(!thumb_orig.startsWith("http")) {
if(mime.startsWith("image") && mime !== "image/gif")
thumb_orig = `./public/b/${filename}`;
else {
await exec(`ffmpegthumbnailer -i./public/b/${filename} -s1024 -o./tmp/${insertq.insertId}`);
thumb_orig = `./tmp/${insertq.insertId}`;
}
}
await exec(`convert "${thumb_orig}" -resize "200x200^" -gravity center -crop 128x128+0+0 +repage ./public/t/${insertq.insertId}.png`);
await fs.promises.unlink(`./tmp/${insertq.insertId}`).catch(_=>{});
e.reply([
`title: ${meta.fulltitle}`,
`name: ${filename}`,
`size: ${size}`,
`link: ${cfg.main.url}/${insertq.insertId}`
]);
});
}
}];
};