diff --git a/src/bot.js b/src/bot.js index 5feaf1c..0b1ea77 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,5 +1,6 @@ import { cfg, read } from './inc/cfg.js'; import { wrapper, clients } from './inc/wrapper.js'; +const safeEval = require('safe-eval'); read().then(() => { let bot = new wrapper(); @@ -7,7 +8,25 @@ read().then(() => { //if(e.type === "tg") // clients[0].client.send(`PRIVMSG #kbot-dev ${e.user.nick}: ${e.message}`); - console.log(e.message); + 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); + } + } + } }); }); \ No newline at end of file diff --git a/src/inc/clients/irc.js b/src/inc/clients/irc.js index 949bf55..1935342 100644 --- a/src/inc/clients/irc.js +++ b/src/inc/clients/irc.js @@ -17,6 +17,10 @@ class irc { this.username = this.options.username || "test"; this.realname = this.options.realname || "test"; this.channels = this.options.channels || []; + this.server = { + motd: "", + me: {} + }; this.socket = (this.options.ssl ? tls : net).connect({ host: this.options.host, port: this.options.port, @@ -28,8 +32,23 @@ class irc { this.socket.setEncoding('utf-8'); this.socket.on('data', msg => { msg = this.parse(msg); - switch (msg.command) { - case "464":// Password required + switch (msg.command) { // auslagern! + case "001": // welcome + this.server.me = msg.params[0]; + this.join(this.options.channels); + this.emit('connected', msg.params[1]); + break; + case "372": // motd + this.server.motd += `${msg.params[1]}\n`; + break; + case "375": // motd_start + this.server.motd = `${msg.params[1]}\n`; + break; + case "376": // motd_end + this.server.motd += `${msg.params[1]}\n`; + this.emit('motd', this.server.motd); + break; + case "464": // Password required if (this.options.password.length > 0) this.send("PASS ", this.options.password); // pwd break; @@ -39,8 +58,16 @@ class irc { case "PRIVMSG": this.emit('message', this.reply(msg)); break; + case "NOTICE": + this.emit('notice', msg.params[1]); + break; + default: + console.log(msg); + break; } }); + //if(this.network === "n0xy") + // setTimeout(() => { console.log(this.server.me) }, 5000); } send() { for (let i = 0; i < arguments.length; i++) @@ -67,8 +94,10 @@ class irc { params.push(data.slice(0, i)); data = data.slice(i + 1); } - if (data) + if (data) { + data = data.replace(/\r?\n|\r/g, ''); params.push(data.slice(1)); + } return { raw: raw, prefix: prefix, @@ -77,33 +106,52 @@ class irc { }; } reply(tmp) { - let usertmp = tmp.prefix.split("!"); - let hosttmp = usertmp[1].split("@"); - let user = { - nick: usertmp[0], - username: hosttmp[0], - hostname: hosttmp[1] - }; return { type: "irc", network: this.network, channel: tmp.params[0], - user: user, - message: tmp.params[1].replace(/\r?\n|\r/g, ''), + user: parsePrefix(tmp.prefix), + message: tmp.params[1], time: ~~(Date.now() / 1000), + raw: tmp, reply: msg => { - this.send(`PRIVMSG ${tmp.params[0]} ${msg}`); + this.send(`PRIVMSG ${tmp.params[0]} :${msg}`); }, replyAction: msg => { - this.send(`PRIVMSG ${tmp.params[0]} \u0001ACTION ${msg}\u0001`); + this.send(`PRIVMSG ${tmp.params[0]} :\u0001ACTION ${msg}\u0001`); }, replyNotice: msg => { - this.send(`NOTICE ${tmp.params[0]} ${msg}`); - //rpc.emit('call', this.network, 'notice', [this.channel, msg]); - } + this.send(`NOTICE ${tmp.params[0]} :${msg}`); + }, + // commands + join: chan => this.join(chan), + part: (chan, msg) => this.part(chan, msg), + write: msg => this.send(msg) }; } + join(channel) { + if(typeof channel === "object") + this.send(`JOIN ${channel.join(',')}`); + else + this.send(`JOIN ${channel}`); + } + part(channel, msg=false) { + if (typeof channel === "object") + this.send(`PART ${channel.join(',')}${msg ? " " + msg : ""}`); + else + this.send(`PART ${channel}${msg ? " " + msg : ""}`); + } } module.exports = irc; -util.inherits(irc, EventEmitter); \ No newline at end of file +util.inherits(irc, EventEmitter); + +function parsePrefix(prefix) { + let usertmp = prefix.split("!"); + let hosttmp = usertmp[1].split("@"); + return { + nick: usertmp[0], + username: hosttmp[0], + hostname: hosttmp[1] + }; +} \ No newline at end of file diff --git a/src/inc/clients/tg.js b/src/inc/clients/tg.js index 5a82e54..054af83 100644 --- a/src/inc/clients/tg.js +++ b/src/inc/clients/tg.js @@ -20,16 +20,20 @@ class tg { this.client.sendMessage(id, msg); } reply(tmp) { + console.log("Telegram", tmp); return { type: "tg", + network: "Telegram", channel: tmp.chat.title, channelid: tmp.chat.id, user: { nick: tmp.from.first_name, - username: tmp.from.username + username: tmp.from.username, + hostname: tmp.from.id }, message: tmp.text, time: tmp.date, + raw: tmp, reply: function (msg) { this.send(tmp.chat.id, msg); }, diff --git a/src/inc/wrapper.js b/src/inc/wrapper.js index 2463f49..1753712 100644 --- a/src/inc/wrapper.js +++ b/src/inc/wrapper.js @@ -12,23 +12,23 @@ const util = require('util'); var EventEmitter = require('events').EventEmitter; let clients = []; -const wrapper = function() { - for(let srv in cfg.client) { - switch(cfg.client[srv].type) { +const wrapper = function () { + for (let srv in cfg.client) { + switch (cfg.client[srv].type) { case "irc": clients.push({ name: cfg.client[srv].network, type: "irc", client: new irclib(cfg.client[srv]) }); - break; + break; case "tg": clients.push({ name: "tg", type: "tg", client: new tglib(cfg.client[srv]) }); - break; + break; } }