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 { 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);
}
}
}
});
});

View File

@ -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,7 +32,22 @@ class irc {
this.socket.setEncoding('utf-8');
this.socket.on('data', msg => {
msg = this.parse(msg);
switch (msg.command) {
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
@ -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);
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);
}
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);
},