From a76563a7bcebd2eeac5fbd409585e112fcbc20cd Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 8 Nov 2017 20:30:21 +0100 Subject: [PATCH] irc & tg --- src/bot.js | 3 +- src/inc/irc/irc.js | 23 ++++++++-------- src/inc/wrapper.js | 69 ++++++++++++++++++++++++---------------------- 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/bot.js b/src/bot.js index 172cd25..b2c1cf5 100644 --- a/src/bot.js +++ b/src/bot.js @@ -4,7 +4,6 @@ import { wrapper } from './inc/wrapper.js'; read().then(() => { let bot = new wrapper(); bot.on('message', e => { - //console.log( e ); - e.reply( e.message ); + console.log( e ); }); }); \ No newline at end of file diff --git a/src/inc/irc/irc.js b/src/inc/irc/irc.js index 9e82b45..1cfcdfb 100644 --- a/src/inc/irc/irc.js +++ b/src/inc/irc/irc.js @@ -3,7 +3,7 @@ var tls = require("tls"); var EventEmitter = require("events").EventEmitter; var util = require("util"); -function Client(options) { +function irc(options) { EventEmitter.call(this); this.options = options || {}; this.options.channels = this.options.channels || []; @@ -46,13 +46,13 @@ function Client(options) { }); } -Client.prototype.send = function send() { +irc.prototype.send = function send() { for(let i = 0; i < arguments.length; i++) this.socket.write(arguments[i]); this.socket.write('\n'); } -Client.prototype.parse = function parse(data) { +irc.prototype.parse = function parse(data) { var raw = data; if(data.charAt(0) === ':') { var i = data.indexOf(' '); @@ -79,7 +79,7 @@ Client.prototype.parse = function parse(data) { }; } -Client.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark', +irc.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark', let usertmp = tmp.prefix.split("!"); let hosttmp = usertmp[1].split("@"); let user = { @@ -87,8 +87,8 @@ Client.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark', username: hosttmp[0], hostname: hosttmp[1] }; - console.log(tmp); return { + type: "irc", network: this.network, channel: tmp.params[0], user: user, @@ -97,14 +97,15 @@ Client.prototype.reply = function(tmp) { // prefix: 'Flummi!~bark@bark', reply: msg => { this.send(`PRIVMSG ${tmp.params[0]} ${msg}`); }, - /*replyAction: msg => { - rpc.emit('call', this.network, 'privmsg', [this.channel, '\u0001' + 'ACTION ' + msg + '\u0001']); + replyAction: msg => { + this.send(`PRIVMSG ${tmp.params[0]} \u0001ACTION ${msg}\u0001`); }, replyNotice: msg => { - rpc.emit('call', this.network, 'notice', [this.channel, msg]); - }*/ + this.send(`NOTICE ${tmp.params[0]} ${msg}`); + //rpc.emit('call', this.network, 'notice', [this.channel, msg]); + } }; }; -module.exports = Client; -util.inherits(Client, EventEmitter); \ No newline at end of file +module.exports = irc; +util.inherits(irc, EventEmitter); \ No newline at end of file diff --git a/src/inc/wrapper.js b/src/inc/wrapper.js index 22d4fa3..d43da84 100644 --- a/src/inc/wrapper.js +++ b/src/inc/wrapper.js @@ -8,43 +8,46 @@ const util = require('util'); //const ytdl = util.promisify(require('youtube-dl').getInfo); var EventEmitter = require('events').EventEmitter; +let clients = []; const wrapper = function() { - let irc = new irclib({ - network: "n0xy", - host: "31.172.14.83", - port: 6669, //6669, - ssl: true, - selfSigned: true, - nickname: "kbotv3", - username: "kbotv2/n0xy", - password: "blah", - realname: "kbotv3", - channels: [ - "#kbot-dev" - ] - }); - - //const tg = new (require('node-telegram-bot-api'))('381368731:AAFalG-LknIbtBDuOvRXcxHUEK9Jg_o1UCw', { polling: true }); - - irc.on("message", (msg) => { - this.emit('message', msg); - //if( msg.event[1] === "privmsg" ) { - // let e = reply( msg ); - // console.log( `IRC: (${e.time}) ${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}` ); - //} + clients.push({ + name: "n0xy", + type: "irc", + client: new irclib({ + network: "n0xy", + host: "31.172.14.83", + port: 6669, //6669, + ssl: true, + selfSigned: true, + nickname: "kbotv3", + username: "kbotv2/n0xy", + password: "blah", + realname: "kbotv3", + channels: [ + "#kbot-dev" + ] + }) }); - /*tg.on('message', msg => { - this.emit('message', msg); - //let e = replytg(tg, msg); - //console.log( `TG: (${e.time}) ${e.channel} -> ${e.user.nick}: ${e.message}` ); - });*/ + clients.push({ + name: "tg", + type: "tg", + client: new (require('node-telegram-bot-api'))('381368731:AAFalG-LknIbtBDuOvRXcxHUEK9Jg_o1UCw', { polling: true }) + }); + clients.forEach(client => { + client.client.on("message", e => { + if(client.type === "tg") + e = replytg(client.client, e); + this.emit('message', e); + }); + }); }; -const replytg = (bot, tmp) => { +const replytg = (bot, tmp) => { // muss noch hier raus return { + type: "tg", channel: tmp.chat.title, channelid: tmp.chat.id, user: { @@ -54,13 +57,13 @@ const replytg = (bot, tmp) => { message: tmp.text, time: tmp.date, reply: function(msg) { - bot.sendMessage(this.channelid, msg); - }.bind(this), + bot.sendMessage(tmp.chat.id, msg); + }, replyAction: function(msg) { - bot.sendMessage(this.channelid, msg); + bot.sendMessage(tmp.chat.id, msg); }, replyNotice: function(msg) { - bot.sendMessage(this.channelid, msg); + bot.sendMessage(tmp.chat.id, msg); } }; };