This commit is contained in:
NatureFreshMilk 2020-01-20 10:43:21 +01:00
parent b8a83261b0
commit 715e007cdd
12 changed files with 133 additions and 128 deletions

View File

@ -1,8 +1,20 @@
{
"host": "chat.freenode.net",
"username": "pandorabot_test",
"password": "abcxyz",
"channels": {
"main": "pandorabox-test"
}
"debug": true,
"remotes": [{
"name": "freenode",
"type": "irc",
"host": "chat.freenode.net",
"username": "pandorabot_test",
"password": "abcxyz",
"channels": {
"main": "pandorabox-test"
}
},{
"name": "discord",
"type": "discord",
"channels": {
"main": "myToken"
}
}]
}

View File

@ -2,30 +2,19 @@ const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
const app = require("../app");
const client_map = require("../client_map");
const events = require("../events");
// mod -> web
app.post('/', jsonParser, function(req, res){
// req.body = { channel = "", playername = "", message = "" }
client_map.then(channels => {
if (!req.body.message){
return;
}
const channel = channels[req.body.source_channel];
const main_channel = channels.main;
if (channel){
// player message
channel.say(
(req.body.source ? `<${req.body.source}> ` : "") +
req.body.message
);
}
})
.catch(e => console.log(e));
// delegate to events bus
events.emit("message-in", {
type: "minetest",
name: "minetest",
username: req.body.username,
channel: req.body.channel,
message: req.body.message
});
res.end();
});

View File

@ -4,43 +4,24 @@ const cfg = require("../config");
const events = require("../events");
var buffer = [];
events.on("message", e => buffer.push(e));
events.on("message-out", e => {
if (e.name == "minetest")
buffer.push(e);
});
// web -> mod
app.get('/', function(req, res){
// buffer full, flush
if (buffer.length > 20){
if (buffer.length > 50){
buffer = [];
}
function sendEvent(event){
if (!event) {
return;
}
var channel_name;
Object.keys(cfg.channels).forEach(ingame_name => {
const irc_name = cfg.channels[ingame_name];
if (event.target == "#" + irc_name){
channel_name = ingame_name;
}
});
res.json({
source_system: event.source_system,
target: channel_name, // mapped channel name: "#main", "#lag"
source: event.nick, // "somedude"
message: event.message
});
}
// buffered case
if (buffer.length > 0){
sendEvent(buffer.shift());
res.json(buffer);
buffer = [];
return;
}
@ -48,16 +29,19 @@ app.get('/', function(req, res){
var handle;
// async event case
function evtHandler(){
clearTimeout(handle);
sendEvent(buffer.shift());
function evtHandler(e){
if (e.name == "minetest"){
clearTimeout(handle);
res.json(buffer);
buffer = [];
}
}
// timeout case
handle = setTimeout(() => {
res.json({});
events.removeListener("message", evtHandler);
res.json([]);
events.removeListener("message-out", evtHandler);
}, 20000);
events.once("message", evtHandler);
events.once("message-out", evtHandler);
});

View File

@ -1,18 +0,0 @@
const irc_client = require('./irc_client');
const cfg = require("./config");
module.exports = new Promise(resolve => {
var channels = {}; // name -> channelObj
irc_client.on('registered', function() {
Object.keys(cfg.channels).forEach(ingame_name => {
const irc_name = cfg.channels[ingame_name];
var channel = irc_client.channel("#" + irc_name);
channel.join();
channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`);
channels[ingame_name] = channel;
});
resolve(channels);
});
});

View File

@ -1,4 +0,0 @@
modules.exports = function(){
}

View File

@ -2,14 +2,4 @@
const fs = require('fs');
const cfg = JSON.parse(fs.readFileSync('beerchat.json', 'utf8'));
// Default config
module.exports = Object.assign({
host: "chat.freenode.net",
port: 6667,
username: "beerchat_test",
password: "xyz",
debug: false,
channels: {
main: "beerchat-test"
}
}, cfg);
module.exports = cfg;

View File

@ -3,21 +3,21 @@
Events:
## "message"
## "message-in" / "message-out"
{
// source messaging system
// "irc", "discord", "minetest", etc
source_system: "irc"
type: "irc",
// the target channel/username
target: "#main",
// the name of the remote system
name: "freenode",
// the source username
source; "SomeDude",
username: "SomeDude",
// the source channel: "#main", "#lag", etc
source_channel: "",
// the ingame channel: "#main", "#lag", etc
channel: "main",
//the actual message
message: "xyz",

4
src/handler/discord.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = function(){
}

49
src/handler/irc.js Normal file
View File

@ -0,0 +1,49 @@
const IRC = require('irc-framework');
module.exports = function(remote, events){
const client = new IRC.Client();
client.connect({
host: remote.host,
port: remote.port,
nick: remote.username,
password: remote.password,
auto_reconnect: true
});
if (remote.debug) {
client.on("debug", function(e){
console.log(e);
});
}
var channels = {}; // name -> channelObj
// map channels
client.on('registered', function() {
Object.keys(remote.channels).forEach(ingame_name => {
const irc_name = remote.channels[ingame_name];
var channel = client.channel("#" + irc_name);
channel.join();
channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`);
channels[ingame_name] = channel;
});
resolve(channels);
});
client.on('message', function(event) {
//TODO: mapping and events.emit("message-in", {})
console.log("irc-message-in", event)
});
events.on("message-out", function(event){
if (event.name != remote.name)
//not meant for this remote, ignore
return;
const channel = channels[event.channel]
if (channel) {
channel.say(`<${event.username}> ${event.message}`)
}
});
}

View File

@ -1,6 +1,26 @@
const app = require("./app");
require("./api/rx.js");
require("./api/tx.js");
// load minetest handler api's
require("./api/rx");
require("./api/tx");
const cfg = require("./config");
const events = require("./events");
const router = require("./router");
const handlers = {
irc: require("./handler/irc"),
discord: require("./handler/discord")
}
cfg.remotes.forEach(remote => {
const handler = handlers[remote.type]
console.log(`Setting up remote: ${remote.name} with type: ${remote.type}`)
handler(remote, events);
})
console.log("Starting message router");
router(cfg.remotes, events);
app.listen(8080, () => console.log('Listening on http://127.0.0.1:8080'));

View File

@ -1,28 +0,0 @@
const cfg = require("./config");
const IRC = require('irc-framework');
const events = require("./events");
const client = new IRC.Client();
client.connect({
host: cfg.host,
port: cfg.port,
nick: cfg.username,
password: cfg.password,
auto_reconnect: true
});
if (cfg.debug) {
client.on("debug", function(e){
console.log(e);
});
}
client.on('message', function(event) {
if (event.type != "privmsg")
return;
events.emit("message", event);
});
module.exports = client;

7
src/router.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = function(remotes, events){
events.on("message-in", function(event){
// dispatch to other systems as message-out event with their names
});
}