codeoverhaul

This commit is contained in:
Flummi 2016-09-15 00:05:18 +02:00
parent e87499e705
commit 32b830f929
20 changed files with 130 additions and 150 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules/
cfg/*.json cfg/*.json
b/ b/
log/*.log log/*.log
t/

4
bot.js
View File

@ -16,6 +16,7 @@ var getDateTimeString = () => {
}; };
var log_forever = './log/forever_'+getDateTimeString()+'.log'; var log_forever = './log/forever_'+getDateTimeString()+'.log';
var log_bot = './log/bot_'+getDateTimeString()+'.log'; var log_bot = './log/bot_'+getDateTimeString()+'.log';
var err_bot = './log/err_'+getDateTimeString()+'.log';
var putlog = (msg) => { var putlog = (msg) => {
fs.appendFileSync(log_forever, msg, 'utf8'); fs.appendFileSync(log_forever, msg, 'utf8');
}; };
@ -31,9 +32,8 @@ var child = new (forever.Monitor)('./src/main.js', {
'*.log', 'b/*', 't/*', 'node_modules/*' '*.log', 'b/*', 't/*', 'node_modules/*'
], ],
watchDirectory: __dirname, watchDirectory: __dirname,
logFile: log_bot,
outFile: log_bot, outFile: log_bot,
errFile: log_bot errFile: err_bot
}); });
child.on('watch:restart', (info) => { child.on('watch:restart', (info) => {

View File

@ -1,6 +1,6 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
bot.on('join', (e) => { lib.bot.on('join', (e) => {
bot.whois(e.user.getNick(), e.network, (err, data) => { lib.bot.whois(e.user.getNick(), e.network, (err, data) => {
if(data.account !== null) { if(data.account !== null) {
lib.getUserlevel(e.user.getNick(), e.network, e.channel, (cb) => { lib.getUserlevel(e.user.getNick(), e.network, e.channel, (cb) => {
var modes = false; var modes = false;
@ -13,7 +13,7 @@ module.exports = (bot, lib) => {
else if(cb.level >= 10) else if(cb.level >= 10)
modes = "+v"; modes = "+v";
if(modes !== false) if(modes !== false)
bot.mode(e.channel, modes + (' '+e.user.getNick()).repeat(modes.length-1), e.network); lib.bot.mode(e.channel, modes + (' '+e.user.getNick()).repeat(modes.length-1), e.network);
}); });
} }
}); });

15
src/events/message.js Normal file
View File

@ -0,0 +1,15 @@
module.exports = (lib) => {
lib.bot.on('message', (e) => {
lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call) && entry.active == 1) {
lib.log('trigger: '+entry.name);
lib.getUserlevel(e.user.getNick(), e.network, e.channel.getName(), (cb) => {
if(cb.level >= entry.level)
entry.func(e, cb.level);
else
e.reply('no permission, min level '+entry.level+' required');
});
}
});
});
};

6
src/events/motd.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = (lib) => {
lib.bot.on('motd', (e) => {
console.log("motd von "+e.network+" erhalten");
lib.bot.mode(lib.bot.me.nick, "+B", e.network);
});
};

View File

@ -4,8 +4,24 @@ var https = require('https');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var crypto = require('crypto'); var crypto = require('crypto');
var request = require('request'); var request = require('request');
var mysql = require('mysql');
var irccfg = require('../cfg/irc.json');
var bot, sql; Lib.prototype.bot = require('coffea')();
irccfg.forEach(e => {
Lib.prototype.bot.add({
"name": e.name,
"host": e.host,
"port": e.port,
"ssl": e.ssl,
"ssl_allow_invalid": e.ssl_allow_invalid,
"pass": e.pass,
"nick": e.nick,
"username": e.username,
"realname": e.realname
});
console.log("Server "+e.name+" wurde geladen");
});
Lib.prototype.admins = []; Lib.prototype.admins = [];
Lib.prototype.debug = false; Lib.prototype.debug = false;
@ -22,32 +38,34 @@ Lib.prototype.trigger = {
}); });
} }
}; };
var haDC = () => {
Lib.prototype.sql = mysql.createConnection(require('../cfg/mysql.json'));
Lib.prototype.sql.connect((err) => {
if(err) setTimeout(haDC, 2000);
});
Lib.prototype.sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
module.exports = Lib; module.exports = Lib;
function Lib(tbot, tsql) { function Lib() {
this.bot = bot = tbot;
Lib.prototype.sql = tsql;
Lib.prototype.cfg = require('../cfg/main.json'); Lib.prototype.cfg = require('../cfg/main.json');
Lib.prototype.debug = Lib.prototype.cfg.debug; Lib.prototype.debug = Lib.prototype.cfg.debug;
Lib.prototype.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
Lib.prototype.loadUser((cb) => {
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
});
Lib.prototype.loadTrigger(); Lib.prototype.loadTrigger();
Lib.prototype.loadEvents(); Lib.prototype.loadEvents();
} }
Lib.prototype.getUserlevel = (user, network, channel, cb) => { Lib.prototype.getUserlevel = (user, network, channel, cb) => {
bot.whois(user, network, (err, data) => { Lib.prototype.bot.whois(user, network, (err, data) => {
var host = data.username+'@'+data.hostname; Lib.prototype.bot.names(channel, network, (names) => {
bot.names(channel, network, (names) => {
var lvl_channel = (data.nick in names)? Lib.prototype.cfg.level[ names[data.nick] ] : 0; var lvl_channel = (data.nick in names)? Lib.prototype.cfg.level[ names[data.nick] ] : 0;
var lvl_db = 0; var lvl_db = 0;
if(host in Lib.prototype.admins) if(data.account in Lib.prototype.admins)
lvl_db = (Lib.prototype.admins[host].server == network)? Lib.prototype.admins[host].level : 0; lvl_db = (Lib.prototype.admins[data.account].network === network)? Lib.prototype.admins[data.account].level : 0;
cb({ cb({
'channel': lvl_channel, 'channel': lvl_channel,
'db': lvl_db, 'db': lvl_db,
@ -60,13 +78,11 @@ Lib.prototype.loadUser = (cb) => {
Lib.prototype.admins = []; Lib.prototype.admins = [];
Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => { Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
rows.forEach(e => { rows.forEach(e => {
Lib.prototype.admins.push(e.vhost); Lib.prototype.admins[e.account] = {
Lib.prototype.admins[e.vhost] = { id: e.id,
'id': e.id, account: e.account,
'nick': e.nick, level: e.level,
'vhost': e.vhost, network: e.network
'level': e.level,
'server': e.server
}; };
}); });
if(cb) cb(true); if(cb) cb(true);
@ -90,13 +106,9 @@ Lib.prototype.formatSize = (size) => {
}; };
Lib.prototype.getCheckSum = (file, cbcs) => { Lib.prototype.getCheckSum = (file, cbcs) => {
var sha256sum = crypto.createHash('sha256'); var sha256sum = crypto.createHash('sha256');
var s = fs.ReadStream(file); fs.ReadStream(file)
s.on('data', (d) => { .on('data', d => sha256sum.update(d))
sha256sum.update(d); .on('end', () => cbcs(sha256sum.digest('hex')));
});
s.on('end', () => {
cbcs(sha256sum.digest('hex'));
});
}; };
Lib.prototype.generateThumbs = () => { Lib.prototype.generateThumbs = () => {
var outdir = './t'; var outdir = './t';
@ -117,14 +129,10 @@ Lib.prototype.generateThumbs = () => {
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+')'); Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+')');
fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail
} }
else { else
exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => { exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
if(error) Lib.prototype.log((error?'failed':'generated') + ' thumbnail for '+e.id+' ('+e.mime+')');
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 2');
else
Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")");
}); });
}
}); });
break; break;
case "image/png": case "image/png":
@ -151,14 +159,14 @@ Lib.prototype.generateThumbs = () => {
}; };
Lib.prototype.log = (msg) => { Lib.prototype.log = (msg) => {
if(Lib.prototype.debug) if(Lib.prototype.debug)
bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy'); Lib.prototype.bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy');
}; };
Lib.prototype.loadTrigger = () => { Lib.prototype.loadTrigger = () => {
var files = fs.readdirSync(__dirname+'/trigger/'); var files = fs.readdirSync(__dirname+'/trigger/');
files.forEach((file) => { files.forEach((file) => {
if(file.substr(-3, 3) === '.js') { if(file.substr(-3, 3) === '.js') {
console.log('Loading trigger', file); console.log('Loading trigger', file);
require(__dirname+'/trigger/' + file)(bot, Lib.prototype); require(__dirname+'/trigger/' + file)(Lib.prototype);
} }
}); });
}; };
@ -167,7 +175,7 @@ Lib.prototype.loadEvents = () => {
files.forEach((file) => { files.forEach((file) => {
if(file.substr(-3, 3) === '.js') { if(file.substr(-3, 3) === '.js') {
console.log('Loading event', file); console.log('Loading event', file);
require(__dirname+'/events/' + file)(bot, Lib.prototype); require(__dirname+'/events/' + file)(Lib.prototype);
} }
}); });
}; };

View File

@ -1,55 +1,5 @@
var cfg = require('../cfg/irc.json');
var fs = require('fs-extra');
var mysql = require('mysql');
var bot = require('coffea')();
var Lib = require('./lib.js'); var Lib = require('./lib.js');
var Websrv = require('./websrv.js'); var Websrv = require('./websrv.js');
var sql; var lib = new Lib();
var haDC = () => { var websrv = new Websrv(lib);
sql = mysql.createConnection(require('../cfg/mysql.json'));
sql.connect((err) => {
if(err) setTimeout(haDC, 2000);
});
sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
var lib = new Lib(bot, sql);
var websrv = new Websrv(bot, sql, lib);
cfg.forEach((e,i,a) => {
bot.add({
"name": e.name,
"host": e.host,
"port": e.port,
"ssl": e.ssl,
"ssl_allow_invalid": e.ssl_allow_invalid,
"pass": e.pass,
"nick": e.nick,
"username": e.username,
"realname": e.realname
});
console.log("Server "+e.name+" wurde geladen");
});
bot.on('motd', (e) => {
console.log("motd von "+e.network+" erhalten");
bot.mode(bot.me, "+B", e.network);
});
bot.on('message', (e) => {
lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call) && entry.active == 1) {
lib.log('trigger: '+entry.name);
lib.getUserlevel(e.user.getNick(), e.network, e.channel.getName(), (cb) => {
if(cb.level >= entry.level)
entry.func(e, cb.level);
else
e.reply('no permission, min level '+entry.level+' required');
});
}
});
});

View File

@ -1,6 +1,6 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'clean', name: 'clean',
call: /^\!clean/i, call: /^\!clean/i,

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'debug', name: 'debug',
call: /^\!debug$/i, call: /^\!debug$/i,

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'delete', name: 'delete',
call: /^\!del (\d+)$/i, call: /^\!del (\d+)$/i,

View File

@ -1,6 +1,6 @@
var exec = require('child_process').exec; var exec = require('child_process').exec;
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'gitpull', name: 'gitpull',
call: /^\!pull$/, call: /^\!pull$/,

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'level', name: 'level',
call: /^\!level(?: +\@.*)?/i, call: /^\!level(?: +\@.*)?/i,
@ -13,32 +13,34 @@ module.exports = (bot, lib) => {
var level = (args[2]>=100)?100:args[2]; var level = (args[2]>=100)?100:args[2];
if( name in e.channel.names ) { if( name in e.channel.names ) {
bot.whois(name.trim(), e.network, (err, data) => { console.log(lib.admins);
if(data.account !== null) { lib.bot.whois(name.trim(), e.network, (err, data) => {
var host = data.username+'@'+data.hostname; if(!err) {
if(host in lib.admins) { if(data.account !== null) {
if(level <= 0) { if(data.account in lib.admins) {
lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[host].id], () => { if(level <= 0) {
lib.loadUser(); lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[data.account].id], () => {
e.reply('deleted user '+data.nick+' ('+host+')'); lib.loadUser();
}); e.reply('deleted user '+data.nick+' ('+data.account+')');
});
}
else {
lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[data.account].id], () => {
lib.loadUser();
e.reply('changed user '+data.nick+' ('+data.account+') to level '+level);
});
}
} }
else { else {
lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[host].id], () => { lib.sql.query('insert into `f0ck`.`user` (`account`,`level`,`network`) values (?,?,?)', [data.account, level, e.network], () => {
lib.loadUser(); lib.loadUser();
e.reply('changed user '+data.nick+' ('+host+') to level '+level); e.reply('added new user '+data.nick+' ('+data.account+') with level '+level);
}); });
} }
} }
else { else
lib.sql.query('insert into `f0ck`.`user` (`nick`,`vhost`,`level`,`server`) values (?,?,?,?)', [data.nick, host, level, e.network], () => { e.reply('user '+data.nick+' isn\'t registered');
lib.loadUser();
e.reply('added new user '+data.nick+' ('+host+') with level '+level);
});
}
} }
else
e.reply('user '+data.nick+' isn\'t registered');
}); });
} }
else else

View File

@ -9,7 +9,7 @@ var ytdl = require('ytdl-core');
var Readable = require('stream').Readable; var Readable = require('stream').Readable;
var cfg = require('../../cfg/main.json'); var cfg = require('../../cfg/main.json');
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'parser', name: 'parser',
call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi, call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi,
@ -25,7 +25,7 @@ module.exports = (bot, lib) => {
if(cb.success === true) { if(cb.success === true) {
fs.move(cb.file, cb.file + '.' + cb.info.ext, (err) => { fs.move(cb.file, cb.file + '.' + cb.info.ext, (err) => {
if(!err) { if(!err) {
bot.whois(e.user.getNick(), e.network, (err, cbgu) => { lib.bot.whois(e.user.getNick(), e.network, (err, cbgu) => {
lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`,`thumb`) values (?,?,?,?,?,?,?,?,?,?,?)", [ lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`,`thumb`) values (?,?,?,?,?,?,?,?,?,?,?)", [
entry, entry,
cb.file + '.' + cb.info.ext, cb.file + '.' + cb.info.ext,

View File

@ -1,6 +1,6 @@
var du = require('du'); var du = require('du');
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'stats', name: 'stats',
call: /^\!stats$/i, call: /^\!stats$/i,

View File

@ -1,19 +1,19 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'sync', name: 'sync',
call: /^!sync$/i, call: /^!sync$/i,
level: 100, level: 100,
active: 1, active: 1,
func: (e) => { func: (e) => {
bot.names(e.channel, e.network, (names) => { lib.bot.names(e.channel, e.network, (names) => {
Object.keys(names).forEach(key => { Object.keys(names).forEach(key => {
bot.whois(key, e.network, (err, data) => { lib.bot.whois(key, e.network, (err, data) => {
if(data.nick != bot.me.nick) { if(data.nick != lib.bot.me.nick) {
bot.mode(e.channel, '-qaohv' + (' '+data.nick).repeat(5), e.network); lib.bot.mode(e.channel, '-qaohv' + (' '+data.nick).repeat(5), e.network);
var level = 0; var level = 0;
var modes = false; var modes = false;
if((data.username+'@'+data.hostname) in lib.admins) { if(data.account in lib.admins) {
level = lib.admins[data.username+'@'+data.hostname].level; level = lib.admins[data.account].level;
if(level >= 70) if(level >= 70)
modes = "+ao"; modes = "+ao";
else if(level >= 60) else if(level >= 60)
@ -24,7 +24,7 @@ module.exports = (bot, lib) => {
modes = "+v"; modes = "+v";
} }
if(modes !== false) if(modes !== false)
bot.mode(e.channel, modes + (' '+data.nick).repeat(modes.length-1), e.network); lib.bot.mode(e.channel, modes + (' '+data.nick).repeat(modes.length-1), e.network);
} }
}); });
}); });

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'test', name: 'test',
call: /^!test$/i, call: /^!test$/i,

View File

@ -1,6 +1,6 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'thumbnailer', name: 'thumbnailer',
call: /^\!thumb(.*\d+)?$/i, call: /^\!thumb(.*\d+)?$/i,

View File

@ -2,7 +2,7 @@ var allowedModes = [
'q','a','o','h','v' 'q','a','o','h','v'
]; ];
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'umode', name: 'umode',
call: /^\!umode .*/i, call: /^\!umode .*/i,
@ -17,7 +17,7 @@ module.exports = (bot, lib) => {
else else
tmp.pop(); tmp.pop();
var modes; var modes;
if(user.toLowerCase() != bot.me.nick.toLowerCase()) { if(user.toLowerCase() != lib.bot.me.nick.toLowerCase()) {
var modes = processModes(tmp.join('')); var modes = processModes(tmp.join(''));
if(modes.length) { if(modes.length) {
if(checkModes(modes, allowedModes)) { if(checkModes(modes, allowedModes)) {
@ -32,7 +32,7 @@ module.exports = (bot, lib) => {
modesToApply.push(currentPrefix + queuedModes + (' ' + user).repeat(queuedModes.length)); modesToApply.push(currentPrefix + queuedModes + (' ' + user).repeat(queuedModes.length));
for(var i = 0; i < queuedModes.length; i++) modes.shift(); for(var i = 0; i < queuedModes.length; i++) modes.shift();
} }
modesToApply.forEach(modeStr => bot.mode(e.channel, modeStr, e.network)); modesToApply.forEach(modeStr => lib.bot.mode(e.channel, modeStr, e.network));
} }
else else
e.reply('Error! Allowed modes are: ' + allowedModes.join(',')); e.reply('Error! Allowed modes are: ' + allowedModes.join(','));

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => { module.exports = (lib) => {
lib.trigger.add({ lib.trigger.add({
name: 'user', name: 'user',
call: /^\!user( \@.*)?/i, call: /^\!user( \@.*)?/i,
@ -9,7 +9,7 @@ module.exports = (bot, lib) => {
user = e.user.getNick(); user = e.user.getNick();
else else
user = user.trim(); user = user.trim();
bot.whois(user, e.network, (err, data) => { lib.bot.whois(user, e.network, (err, data) => {
e.reply(data); e.reply(data);
}); });
}, },

View File

@ -7,12 +7,10 @@ var cfg = require('../cfg/websrv.json');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var templates = {}; var templates = {};
var bot, sql, lib; var lib;
module.exports = Websrv; module.exports = Websrv;
function Websrv(tbot, tsql, tlib) { function Websrv(tlib) {
this.bot = bot = tbot;
this.sql = sql = tsql;
this.lib = lib = tlib; this.lib = lib = tlib;
Websrv.prototype.getTpls(); Websrv.prototype.getTpls();
@ -43,12 +41,12 @@ function Websrv(tbot, tsql, tlib) {
catch(ex) { catch(ex) {
commit = body.commits[body.commits.length-2].message.replace('\n','').trim(); commit = body.commits[body.commits.length-2].message.replace('\n','').trim();
} }
bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+')', 'n0xy' ); lib.bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+')', 'n0xy' );
if( lib.cfg.debugchannel === '#f0ck' ) { if( lib.cfg.debugchannel === '#f0ck' ) {
exec('cd ../ & git pull', (error, stdout) => { exec('cd ../ & git pull', (error, stdout) => {
if(error === null) if(error === null)
bot.send( lib.cfg.debugchannel, stdout, 'n0xy' ); lib.bot.send( lib.cfg.debugchannel, stdout, 'n0xy' );
}); });
} }
}); });
@ -81,7 +79,7 @@ function Websrv(tbot, tsql, tlib) {
items: [], items: [],
last: 10000 last: 10000
}; };
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => {
rows.forEach((e,i,a) => { rows.forEach((e,i,a) => {
data.items.push({ "id": e.id, "mime": e.mime }); data.items.push({ "id": e.id, "mime": e.mime });
data.last = e.id; data.last = e.id;
@ -94,7 +92,7 @@ function Websrv(tbot, tsql, tlib) {
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item + "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item + "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url, url, url], (err, rows, fields) => { lib.sql.query(query, [url, url, url], (err, rows, fields) => {
var tpl = swig.compile(templates.item); var tpl = swig.compile(templates.item);
var data = { var data = {
id: '', id: '',
@ -149,7 +147,7 @@ function Websrv(tbot, tsql, tlib) {
}); });
} }
else if(filePath == "./random") { else if(filePath == "./random") {
sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => { lib.sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.writeHead(301, { res.writeHead(301, {
'Cache-Control': 'no-cache, public', 'Cache-Control': 'no-cache, public',
'Location': '/' + rows[0].id 'Location': '/' + rows[0].id
@ -237,7 +235,7 @@ function Websrv(tbot, tsql, tlib) {
var url = filePath.split('/'); var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage if(url[2] === undefined) { // Mainpage
var query = "select * from `f0ck`.`items`"; var query = "select * from `f0ck`.`items`";
sql.query(query, (err, rows, fields) => { lib.sql.query(query, (err, rows, fields) => {
var items = []; var items = [];
rows.forEach((e,i,a) => { rows.forEach((e,i,a) => {
items.push({ items.push({
@ -253,7 +251,7 @@ function Websrv(tbot, tsql, tlib) {
var eps = 50; var eps = 50;
var id = url[3]; var id = url[3];
sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
var items = { var items = {
"items": [], "items": [],
"last": id "last": id
@ -273,7 +271,7 @@ function Websrv(tbot, tsql, tlib) {
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item + "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item + "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => {
var data; var data;
if(rows[0].length) { if(rows[0].length) {
var e = rows[0][0]; var e = rows[0][0];