irc-replies omfg

This commit is contained in:
Flummi 2017-11-09 12:21:40 +01:00
parent 8f3624d75f
commit e10c453716
4 changed files with 96 additions and 25 deletions

View File

@ -1,5 +1,6 @@
import { cfg, read } from './inc/cfg.js'; import { cfg, read } from './inc/cfg.js';
import { wrapper, clients } from './inc/wrapper.js'; import { wrapper, clients } from './inc/wrapper.js';
const safeEval = require('safe-eval');
read().then(() => { read().then(() => {
let bot = new wrapper(); let bot = new wrapper();
@ -7,7 +8,25 @@ read().then(() => {
//if(e.type === "tg") //if(e.type === "tg")
// clients[0].client.send(`PRIVMSG #kbot-dev ${e.user.nick}: ${e.message}`); // 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);
}
}
}
}); });
}); });

View File

@ -17,6 +17,10 @@ class irc {
this.username = this.options.username || "test"; this.username = this.options.username || "test";
this.realname = this.options.realname || "test"; this.realname = this.options.realname || "test";
this.channels = this.options.channels || []; this.channels = this.options.channels || [];
this.server = {
motd: "",
me: {}
};
this.socket = (this.options.ssl ? tls : net).connect({ this.socket = (this.options.ssl ? tls : net).connect({
host: this.options.host, host: this.options.host,
port: this.options.port, port: this.options.port,
@ -28,8 +32,23 @@ class irc {
this.socket.setEncoding('utf-8'); this.socket.setEncoding('utf-8');
this.socket.on('data', msg => { this.socket.on('data', msg => {
msg = this.parse(msg); msg = this.parse(msg);
switch (msg.command) { switch (msg.command) { // auslagern!
case "464":// Password required 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) if (this.options.password.length > 0)
this.send("PASS ", this.options.password); // pwd this.send("PASS ", this.options.password); // pwd
break; break;
@ -39,8 +58,16 @@ class irc {
case "PRIVMSG": case "PRIVMSG":
this.emit('message', this.reply(msg)); this.emit('message', this.reply(msg));
break; 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() { send() {
for (let i = 0; i < arguments.length; i++) for (let i = 0; i < arguments.length; i++)
@ -67,8 +94,10 @@ class irc {
params.push(data.slice(0, i)); params.push(data.slice(0, i));
data = data.slice(i + 1); data = data.slice(i + 1);
} }
if (data) if (data) {
data = data.replace(/\r?\n|\r/g, '');
params.push(data.slice(1)); params.push(data.slice(1));
}
return { return {
raw: raw, raw: raw,
prefix: prefix, prefix: prefix,
@ -77,33 +106,52 @@ class irc {
}; };
} }
reply(tmp) { reply(tmp) {
let usertmp = tmp.prefix.split("!");
let hosttmp = usertmp[1].split("@");
let user = {
nick: usertmp[0],
username: hosttmp[0],
hostname: hosttmp[1]
};
return { return {
type: "irc", type: "irc",
network: this.network, network: this.network,
channel: tmp.params[0], channel: tmp.params[0],
user: user, user: parsePrefix(tmp.prefix),
message: tmp.params[1].replace(/\r?\n|\r/g, ''), message: tmp.params[1],
time: ~~(Date.now() / 1000), time: ~~(Date.now() / 1000),
raw: tmp,
reply: msg => { reply: msg => {
this.send(`PRIVMSG ${tmp.params[0]} ${msg}`); this.send(`PRIVMSG ${tmp.params[0]} :${msg}`);
}, },
replyAction: msg => { replyAction: msg => {
this.send(`PRIVMSG ${tmp.params[0]} \u0001ACTION ${msg}\u0001`); this.send(`PRIVMSG ${tmp.params[0]} :\u0001ACTION ${msg}\u0001`);
}, },
replyNotice: msg => { replyNotice: msg => {
this.send(`NOTICE ${tmp.params[0]} ${msg}`); this.send(`NOTICE ${tmp.params[0]} :${msg}`);
//rpc.emit('call', this.network, 'notice', [this.channel, 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; module.exports = irc;
util.inherits(irc, EventEmitter); 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]
};
}

View File

@ -20,16 +20,20 @@ class tg {
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",
channel: tmp.chat.title, channel: tmp.chat.title,
channelid: tmp.chat.id, channelid: tmp.chat.id,
user: { user: {
nick: tmp.from.first_name, nick: tmp.from.first_name,
username: tmp.from.username username: tmp.from.username,
hostname: tmp.from.id
}, },
message: tmp.text, message: tmp.text,
time: tmp.date, time: tmp.date,
raw: tmp,
reply: function (msg) { reply: function (msg) {
this.send(tmp.chat.id, msg); this.send(tmp.chat.id, msg);
}, },

View File

@ -12,23 +12,23 @@ const util = require('util');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
let clients = []; let clients = [];
const wrapper = function() { const wrapper = function () {
for(let srv in cfg.client) { for (let srv in cfg.client) {
switch(cfg.client[srv].type) { switch (cfg.client[srv].type) {
case "irc": case "irc":
clients.push({ clients.push({
name: cfg.client[srv].network, name: cfg.client[srv].network,
type: "irc", type: "irc",
client: new irclib(cfg.client[srv]) client: new irclib(cfg.client[srv])
}); });
break; break;
case "tg": case "tg":
clients.push({ clients.push({
name: "tg", name: "tg",
type: "tg", type: "tg",
client: new tglib(cfg.client[srv]) client: new tglib(cfg.client[srv])
}); });
break; break;
} }
} }