This commit is contained in:
Flummi 2016-09-07 11:03:13 +00:00
parent 958bc1d1c0
commit 33f7b0b4a3
4 changed files with 80 additions and 43 deletions

View File

@ -7,21 +7,22 @@ var crypto = require('crypto');
var Mime = require('mime'); var Mime = require('mime');
var cfg; var cfg;
var bot, sql, debug; var bot, sql;
module.exports = Lib; module.exports = Lib;
function Lib(tbot, tsql) { function Lib(tbot, tsql) {
this.bot = bot = tbot; this.bot = bot = tbot;
this.sql = sql = tsql; this.sql = sql = tsql;
this.debug = debug = false;
this.cfg = cfg = require('../cfg/main.json'); this.cfg = cfg = require('../cfg/main.json');
this.admins = [];
Lib.prototype.loadUser((cb) => { Lib.prototype.loadUser((cb) => {
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"); console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
}); });
} }
Lib.prototype.admins = [];
Lib.prototype.debug = false;
Lib.prototype.getUser = (e, cbgu) => { Lib.prototype.getUser = (e, cbgu) => {
var u = e.user.getNick(); var u = e.user.getNick();
var n = e.network; var n = e.network;
@ -60,8 +61,8 @@ Lib.prototype.getUserlevel = (e, cb) => {
var host = cbgu.username+'@'+cbgu.hostname; var host = cbgu.username+'@'+cbgu.hostname;
var lvl_channel = (cbgu.nick in e.channel.names)? cfg.level[ e.channel.names[cbgu.nick] ] : 0; var lvl_channel = (cbgu.nick in e.channel.names)? cfg.level[ e.channel.names[cbgu.nick] ] : 0;
var lvl_db = 0; var lvl_db = 0;
if(host in this.admins) if(host in Lib.prototype.admins)
lvl_db = (this.admins[host].server == e.network)? this.admins[host].level : 0; lvl_db = (Lib.prototype.admins[host].server == e.network)? Lib.prototype.admins[host].level : 0;
cb({ cb({
'channel': lvl_channel, 'channel': lvl_channel,
'db': lvl_db, 'db': lvl_db,
@ -70,11 +71,11 @@ Lib.prototype.getUserlevel = (e, cb) => {
}); });
}; };
Lib.prototype.loadUser = (cb) => { Lib.prototype.loadUser = (cb) => {
this.admins = []; Lib.prototype.admins = [];
sql.query("select * from `f0ck`.`user`", (err, rows, fields) => { sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
rows.forEach((e,i,a) => { rows.forEach(e => {
this.admins.push(e.nick); Lib.prototype.admins.push(e.vhost);
this.admins[e.vhost] = { Lib.prototype.admins[e.vhost] = {
'id': e.id, 'id': e.id,
'nick': e.nick, 'nick': e.nick,
'vhost': e.vhost, 'vhost': e.vhost,
@ -82,9 +83,9 @@ Lib.prototype.loadUser = (cb) => {
'server': e.server 'server': e.server
}; };
}); });
cb(true); if(cb) cb(true);
}).on('error', () => { }).on('error', () => {
cb(false); if(cb) cb(false);
}); });
} }
Lib.prototype.checkRepost = (url, cbcr) => { Lib.prototype.checkRepost = (url, cbcr) => {
@ -182,16 +183,6 @@ Lib.prototype.generateThumbs = () => {
}); });
}; };
Lib.prototype.log = (msg) => { Lib.prototype.log = (msg) => {
if(debug) if(Lib.prototype.debug)
bot.send("#f0ck", msg, 'n0xy'); bot.send("#f0ck", msg, 'n0xy');
};
Lib.prototype.toggleDebug = () => {
if(debug) {
debug = false;
return 'debugmode deactivated';
}
else {
debug = true;
return 'debugmode activated';
}
}; };

View File

@ -46,7 +46,7 @@ bot.on('message', (e) => {
lib.log('trigger: '+entry.name); lib.log('trigger: '+entry.name);
lib.getUserlevel(e, (cb) => { lib.getUserlevel(e, (cb) => {
if(cb.level >= entry.level) if(cb.level >= entry.level)
entry.func(e); entry.func(e, cb.level);
else else
e.reply('no permission, min level '+entry.level+' required'); e.reply('no permission, min level '+entry.level+' required');
}); });

View File

@ -5,7 +5,14 @@ module.exports = (bot, trigger, lib) => {
level: 100, level: 100,
active: 1, active: 1,
func: (e) => { func: (e) => {
e.reply(lib.toggleDebug()); if(lib.debug) {
lib.debug = false;
e.reply('debugmode disabled');
}
else {
lib.debug = true;
e.reply('debugmode enabled');
}
}, },
desc: 'toggle debug' desc: 'toggle debug'
}); });

View File

@ -4,27 +4,66 @@ module.exports = (bot, trigger, lib) => {
call: /^\!level(?: +\@.*)?/i, call: /^\!level(?: +\@.*)?/i,
level: 0, level: 0,
active: 1, active: 1,
func: (e) => { func: (e, userlevel) => {
var user, etmp; var args = e.message.split(' ');
if(user = e.message.match(/^\!level(?: +@(.*))?/i)[1]) {
// e nachbauen xD if(Number.isInteger(parseInt(args[2]))) {
etmp = { if(userlevel >= 100) {
network: e.network, var name = args[1].replace('@','');
user: { var level = (args[2]>=100)?100:args[2];
getNick: () => {
return user.trim(); if( name in e.channel.names ) {
} lib.getUser( { network: e.network, user: { getNick: () => { return name.trim(); } }, channel: { names: e.channel.names } }, cb => {
}, var host = cb.username+'@'+cb.hostname;
channel: { if( host in lib.admins ) {
names: e.channel.names if(level <= 0) {
lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[host].id], () => {
lib.loadUser();
e.reply('deleted user '+cb.nick+' ('+host+')');
});
}
else {
lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[host].id], () => {
lib.loadUser();
e.reply('changed user '+cb.nick+' ('+host+') to level '+level);
});
}
}
else {
lib.sql.query('insert into `f0ck`.`user` (`nick`,`vhost`,`level`,`server`) values (?,?,?,?)', [cb.nick, host, level, e.network], () => {
lib.loadUser();
e.reply('added new user '+cb.nick+' ('+host+') with level '+level);
});
}
});
} }
}; else
e.reply('user '+name+' not found');
}
else
e.reply('no permission, min level 100 required');
}
else {
var user, etmp;
if(user = e.message.match(/^\!level(?: +@(.*))?/i)[1]) {
etmp = {
network: e.network,
user: {
getNick: () => {
return user.trim();
}
},
channel: {
names: e.channel.names
}
};
}
else
etmp = e;
lib.getUserlevel(etmp, (cb) => {
e.reply("level from "+etmp.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")");
});
} }
else
etmp = e;
lib.getUserlevel(etmp, (cb) => {
e.reply("level from "+etmp.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")");
});
}, },
desc: 'get Userlevel' desc: 'get Userlevel'
}); });