diff --git a/src/bot.js b/src/bot.js index 0b1ea77..81ba636 100644 --- a/src/bot.js +++ b/src/bot.js @@ -4,29 +4,41 @@ const safeEval = require('safe-eval'); read().then(() => { let bot = new wrapper(); - bot.on('message', e => { - //if(e.type === "tg") - // clients[0].client.send(`PRIVMSG #kbot-dev ${e.user.nick}: ${e.message}`); + bot.on('message', e => { // Todo: eventhandler + let orig = 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 - let args = e.message.substring(3); - var context = { - e: e - } - try { - var output = safeEval(args, context); - if (typeof output !== undefined && output !== 'undefined' && output) { - let blah = JSON.stringify(output); - 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); - } - } - catch (err) { - e.reply(err.message); + if (e.message.match(/^\.js /)) { // JS-Sandbox + args = e.message.substring(3); + var context = { + e: e, + gf: "nogf" + } + try { + var output = safeEval(args, context); + if (typeof output !== undefined && output !== 'undefined' && output) { + let blah = JSON.stringify(output); + if (blah != "Converting circular structure to JSON") + e.reply(blah.length > 100 ? `holy fuck, Ausgabe wäre viel zu lang! (${blah.length} Zeichen :DDDDDD)` : blah); } } + 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`) + }); }); \ No newline at end of file diff --git a/src/inc/clients/irc.js b/src/inc/clients/irc.js index 1935342..f2cd1dc 100644 --- a/src/inc/clients/irc.js +++ b/src/inc/clients/irc.js @@ -12,6 +12,7 @@ class irc { 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"; @@ -36,7 +37,7 @@ class irc { case "001": // welcome this.server.me = msg.params[0]; this.join(this.options.channels); - this.emit('connected', msg.params[1]); + this.emit('data', ['connected', msg.params[1]]); break; case "372": // motd this.server.motd += `${msg.params[1]}\n`; @@ -46,7 +47,7 @@ class irc { break; case "376": // motd_end this.server.motd += `${msg.params[1]}\n`; - this.emit('motd', this.server.motd); + this.emit('data', ['motd', this.server.motd]); break; case "464": // Password required if (this.options.password.length > 0) @@ -56,10 +57,13 @@ class irc { this.send("PONG ", msg.params.join``); break; 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; case "NOTICE": - this.emit('notice', msg.params[1]); + this.emit('data', ['notice', msg.params[1]]); break; default: console.log(msg); @@ -130,16 +134,10 @@ class irc { }; } join(channel) { - if(typeof channel === "object") - this.send(`JOIN ${channel.join(',')}`); - else - this.send(`JOIN ${channel}`); + this.send(`JOIN ${(typeof channel === "object") ? channel.join(',') : channel}`); } part(channel, msg=false) { - if (typeof channel === "object") - this.send(`PART ${channel.join(',')}${msg ? " " + msg : ""}`); - else - this.send(`PART ${channel}${msg ? " " + msg : ""}`); + this.send(`PART ${(typeof channel === "object") ? channel.join(',') : channel}${msg ? " " + msg : "part"}`); } } diff --git a/src/inc/clients/tg.js b/src/inc/clients/tg.js index 054af83..358b071 100644 --- a/src/inc/clients/tg.js +++ b/src/inc/clients/tg.js @@ -10,17 +10,17 @@ class tg { this.options.polling = options.polling || true; this.client = new tgapi(this.options.token, this.options); - this.client.on('message', msg => { - let time = ~~(Date.now() / 1000); - if (msg.date >= (time - 10)) - this.emit('message', this.reply(msg)); + this.client.onText(/.*/, msg => { + if (msg.date >= (~~(Date.now() / 1000) - 10)) { + msg.text = msg.text.replace(/^\//g, "\."); + this.emit('data', ['message', this.reply(msg)]); + } }); } send(id, msg) { this.client.sendMessage(id, msg); } reply(tmp) { - console.log("Telegram", tmp); return { type: "tg", network: "Telegram", @@ -34,13 +34,13 @@ class tg { message: tmp.text, time: tmp.date, raw: tmp, - reply: function (msg) { + reply: msg => { this.send(tmp.chat.id, msg); }, - replyAction: function (msg) { - this.send(tmp.chat.id, msg); + replyAction: msg => { + this.send(tmp.chat.id, `Uwe ${msg}`); }, - replyNotice: function (msg) { + replyNotice: msg => { this.send(tmp.chat.id, msg); } }; diff --git a/src/inc/lib.js b/src/inc/lib.js deleted file mode 100644 index 4a0b740..0000000 --- a/src/inc/lib.js +++ /dev/null @@ -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 }; \ No newline at end of file diff --git a/src/inc/sql.js b/src/inc/sql.js index 419e9ae..66601f8 100644 --- a/src/inc/sql.js +++ b/src/inc/sql.js @@ -1,61 +1,4 @@ import mysql from 'nodejs-mysql'; const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) ); -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); - } - }); - }); -};*/ \ No newline at end of file +export default sql; \ No newline at end of file diff --git a/src/inc/wrapper.js b/src/inc/wrapper.js index 1753712..4782e45 100644 --- a/src/inc/wrapper.js +++ b/src/inc/wrapper.js @@ -1,16 +1,12 @@ import { cfg, read } from './cfg.js'; import { loadEvents } from './lib.js'; -let irclib = require('./clients/irc.js'); -let tglib = require('./clients/tg.js') -//let tglib = require('node-telegram-bot-api'); -//const safeEval = require('safe-eval'); +const irclib = require('./clients/irc.js'); +const tglib = require('./clients/tg.js') const util = require('util'); -//const fs = require('fs'); -//const ytdl = util.promisify(require('youtube-dl').getInfo); -var EventEmitter = require('events').EventEmitter; -let clients = []; +const EventEmitter = require('events').EventEmitter; +const clients = []; const wrapper = function () { for (let srv in cfg.client) { @@ -33,8 +29,8 @@ const wrapper = function () { } clients.forEach(client => { - client.client.on("message", e => { - this.emit('message', e); + client.client.on("data", e => { + this.emit(e[0], e[1]); }); }); };