This commit is contained in:
Flummi 2017-11-09 15:34:21 +01:00
parent e10c453716
commit 2973e3178f
6 changed files with 58 additions and 122 deletions

View File

@ -4,29 +4,41 @@ const safeEval = require('safe-eval');
read().then(() => { read().then(() => {
let bot = new wrapper(); let bot = new wrapper();
bot.on('message', e => { bot.on('message', e => { // Todo: eventhandler
//if(e.type === "tg") let orig = e.message;
// clients[0].client.send(`PRIVMSG #kbot-dev ${e.user.nick}: ${e.message}`); let tmp = orig.split(" ");
let cmd = tmp[0].toLowerCase();
tmp.shift();
let args = tmp;
args[0] = (args[0] == String.empty || typeof args[0] === 'undefined' || args[0] == "") ? e.user.nick : args[0];
if(e.type === "irc") { if (e.message.match(/^\.js /)) { // JS-Sandbox
if (e.message.match(/^\.js /)) { // JS-Sandbox args = e.message.substring(3);
let args = e.message.substring(3); var context = {
var context = { e: e,
e: e gf: "nogf"
} }
try { try {
var output = safeEval(args, context); var output = safeEval(args, context);
if (typeof output !== undefined && output !== 'undefined' && output) { if (typeof output !== undefined && output !== 'undefined' && output) {
let blah = JSON.stringify(output); let blah = JSON.stringify(output);
if (blah != "Converting circular structure to JSON") if (blah != "Converting circular structure to JSON")
e.reply(blah.length > 250 ? `holy fuck, Ausgabe wäre viel zu lang! (${blah.length} Zeichen :DDDDDD)` : blah); e.reply(blah.length > 100 ? `holy fuck, Ausgabe wäre viel zu lang! (${blah.length} Zeichen :DDDDDD)` : blah);
}
}
catch (err) {
e.reply(err.message);
} }
} }
catch (err) {
e.reply(err.message);
}
}
else if (e.message.match(/^\.kaffee/)) {
var Texte = [
`serviert ${args.join(" ")} einen frisch gebrühten Kaffee aus aromatisch hochwertigen und laktosefreien Kaffeebohnen, die nach dem Vorbild der kolonisierten Ausbeutung herangewachsen und importiert worden sind`,
`serviert ${args.join(" ")} einen frisch gebrühten Kaffee aus aromatisch minderwertigen Kaffeebohnen, die zu einem überhöhten Preis und nach dem Prinzip des Fairtrade® herangewachsen und importiert worden sind`
];
e.replyAction(`${Texte[~~((Math.random() * Texte.length))]}; Prost!`);
} }
}); });
bot.on('ctcp:version', e => {
e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`)
});
}); });

View File

@ -12,6 +12,7 @@ class irc {
this.options.port = this.options.port || 6667; this.options.port = this.options.port || 6667;
this.options.ssl = this.options.ssl || false; this.options.ssl = this.options.ssl || false;
this.options.selfSigned = this.options.selfSigned || false; this.options.selfSigned = this.options.selfSigned || false;
this.options.sasl = this.options.sasl || false;
this.network = this.options.network || "test"; this.network = this.options.network || "test";
this.nickname = this.options.nickname || "test"; this.nickname = this.options.nickname || "test";
this.username = this.options.username || "test"; this.username = this.options.username || "test";
@ -36,7 +37,7 @@ class irc {
case "001": // welcome case "001": // welcome
this.server.me = msg.params[0]; this.server.me = msg.params[0];
this.join(this.options.channels); this.join(this.options.channels);
this.emit('connected', msg.params[1]); this.emit('data', ['connected', msg.params[1]]);
break; break;
case "372": // motd case "372": // motd
this.server.motd += `${msg.params[1]}\n`; this.server.motd += `${msg.params[1]}\n`;
@ -46,7 +47,7 @@ class irc {
break; break;
case "376": // motd_end case "376": // motd_end
this.server.motd += `${msg.params[1]}\n`; this.server.motd += `${msg.params[1]}\n`;
this.emit('motd', this.server.motd); this.emit('data', ['motd', this.server.motd]);
break; break;
case "464": // Password required case "464": // Password required
if (this.options.password.length > 0) if (this.options.password.length > 0)
@ -56,10 +57,13 @@ class irc {
this.send("PONG ", msg.params.join``); this.send("PONG ", msg.params.join``);
break; break;
case "PRIVMSG": case "PRIVMSG":
this.emit('message', this.reply(msg)); if (msg.params[1] === "\u0001VERSION\u0001")
this.emit('data', ['ctcp:version', this.reply(msg)]);
else
this.emit('data', ['message', this.reply(msg)]);
break; break;
case "NOTICE": case "NOTICE":
this.emit('notice', msg.params[1]); this.emit('data', ['notice', msg.params[1]]);
break; break;
default: default:
console.log(msg); console.log(msg);
@ -130,16 +134,10 @@ class irc {
}; };
} }
join(channel) { join(channel) {
if(typeof channel === "object") this.send(`JOIN ${(typeof channel === "object") ? channel.join(',') : channel}`);
this.send(`JOIN ${channel.join(',')}`);
else
this.send(`JOIN ${channel}`);
} }
part(channel, msg=false) { part(channel, msg=false) {
if (typeof channel === "object") this.send(`PART ${(typeof channel === "object") ? channel.join(',') : channel}${msg ? " " + msg : "part"}`);
this.send(`PART ${channel.join(',')}${msg ? " " + msg : ""}`);
else
this.send(`PART ${channel}${msg ? " " + msg : ""}`);
} }
} }

View File

@ -10,17 +10,17 @@ class tg {
this.options.polling = options.polling || true; this.options.polling = options.polling || true;
this.client = new tgapi(this.options.token, this.options); this.client = new tgapi(this.options.token, this.options);
this.client.on('message', msg => { this.client.onText(/.*/, msg => {
let time = ~~(Date.now() / 1000); if (msg.date >= (~~(Date.now() / 1000) - 10)) {
if (msg.date >= (time - 10)) msg.text = msg.text.replace(/^\//g, "\.");
this.emit('message', this.reply(msg)); this.emit('data', ['message', this.reply(msg)]);
}
}); });
} }
send(id, msg) { send(id, msg) {
this.client.sendMessage(id, msg); this.client.sendMessage(id, msg);
} }
reply(tmp) { reply(tmp) {
console.log("Telegram", tmp);
return { return {
type: "tg", type: "tg",
network: "Telegram", network: "Telegram",
@ -34,13 +34,13 @@ class tg {
message: tmp.text, message: tmp.text,
time: tmp.date, time: tmp.date,
raw: tmp, raw: tmp,
reply: function (msg) { reply: msg => {
this.send(tmp.chat.id, msg); this.send(tmp.chat.id, msg);
}, },
replyAction: function (msg) { replyAction: msg => {
this.send(tmp.chat.id, msg); this.send(tmp.chat.id, `Uwe ${msg}`);
}, },
replyNotice: function (msg) { replyNotice: msg => {
this.send(tmp.chat.id, msg); this.send(tmp.chat.id, msg);
} }
}; };

View File

@ -1,13 +0,0 @@
import { cfg } from './cfg.js';
const loadEvents = () => {
var files = fs.readdirSync(__dirname+'/events/');
files.forEach(file => {
if(file.substr(-3, 3) === '.js') {
console.log('Loading event', file);
require(__dirname+'/events/' + file)(self);
}
});
};
export { loadEvents };

View File

@ -1,61 +1,4 @@
import mysql from 'nodejs-mysql'; import mysql from 'nodejs-mysql';
const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) ); const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) );
export default sql; export default sql;
/*var mysql = require('mysql');
var self = Cfg.prototype;
module.exports = Cfg;
var haDC = () => {
self.sql = mysql.createConnection(require(__dirname+'/../cfg/mysql.json'));
self.sql.connect(err => {
if(err) setTimeout(haDC, 2000);
});
self.sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
function Cfg() {
self.cfg = {};
}
self.read = (kat, key) => {
return new Promise((resolve, reject) => {
var out = {
irc: {},
main: {},
websrv: {},
trigger: {}
};
self.sql.query("select * from `cfg`" + (kat?" where `class` = '"+kat+"'"+(key?" && `key` = '"+key+"'":""):""), (err, rows) => {
if(err || rows.length < 1)
reject( err );
else {
rows.forEach(e => {
out[e.class][e.key] = ((type, value) => {
switch(type) {
case 'string':
return value;
break;
case 'int':
return parseInt(value);
break;
case 'bool':
return (value === 'true')?true:false;
break;
case 'json':
return JSON.parse(value);
break;
}
})(e.type, e.value);
});
resolve(key?out[kat][key]:out);
}
});
});
};*/

View File

@ -1,16 +1,12 @@
import { cfg, read } from './cfg.js'; import { cfg, read } from './cfg.js';
import { loadEvents } from './lib.js'; import { loadEvents } from './lib.js';
let irclib = require('./clients/irc.js'); const irclib = require('./clients/irc.js');
let tglib = require('./clients/tg.js') const tglib = require('./clients/tg.js')
//let tglib = require('node-telegram-bot-api');
//const safeEval = require('safe-eval');
const util = require('util'); const util = require('util');
//const fs = require('fs');
//const ytdl = util.promisify(require('youtube-dl').getInfo);
var EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
let clients = []; const clients = [];
const wrapper = function () { const wrapper = function () {
for (let srv in cfg.client) { for (let srv in cfg.client) {
@ -33,8 +29,8 @@ const wrapper = function () {
} }
clients.forEach(client => { clients.forEach(client => {
client.client.on("message", e => { client.client.on("data", e => {
this.emit('message', e); this.emit(e[0], e[1]);
}); });
}); });
}; };