fix indendations
This commit is contained in:
parent
10c1ccc207
commit
a894530cb0
34
readme.md
34
readme.md
@ -22,29 +22,29 @@ curl -X POST \
|
||||
beerchat.js
|
||||
```js
|
||||
module.exports = {
|
||||
"debug": true,
|
||||
"debug": true,
|
||||
|
||||
"remotes": [{
|
||||
"name": "IRC",
|
||||
"type": "irc",
|
||||
"remotes": [{
|
||||
"name": "IRC",
|
||||
"type": "irc",
|
||||
"debug": true,
|
||||
"host": "chat.freenode.net",
|
||||
"username": "pandorabot_test",
|
||||
"password": "my-password",
|
||||
"host": "chat.freenode.net",
|
||||
"username": "pandorabot_test",
|
||||
"password": "my-password",
|
||||
"system_channel": "main",
|
||||
"channels": {
|
||||
"main": "pandorabox-test"
|
||||
}
|
||||
},{
|
||||
"name": "Discord",
|
||||
"type": "discord",
|
||||
"channels": {
|
||||
"main": "pandorabox-test"
|
||||
}
|
||||
},{
|
||||
"name": "Discord",
|
||||
"type": "discord",
|
||||
"debug": true,
|
||||
"token": "the-discord-token",
|
||||
"system_channel": "main",
|
||||
"channels": {
|
||||
"main": "test"
|
||||
}
|
||||
}]
|
||||
"channels": {
|
||||
"main": "test"
|
||||
}
|
||||
}]
|
||||
};
|
||||
```
|
||||
|
||||
|
@ -2,19 +2,19 @@ const app = require("../app");
|
||||
const events = require("../events");
|
||||
|
||||
app.ws("/api/ws", function(ws){
|
||||
ws.on('message', msg => {
|
||||
const data = JSON.parse(msg);
|
||||
events.emit(data.event, data.data);
|
||||
});
|
||||
ws.on('message', msg => {
|
||||
const data = JSON.parse(msg);
|
||||
events.emit(data.event, data.data);
|
||||
});
|
||||
|
||||
function inlistener(msg){
|
||||
ws.send(JSON.stringify({ event: "message-in", data: msg }));
|
||||
}
|
||||
function inlistener(msg){
|
||||
ws.send(JSON.stringify({ event: "message-in", data: msg }));
|
||||
}
|
||||
|
||||
events.on("message-in", inlistener);
|
||||
events.on("message-in", inlistener);
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('ws WebSocket was closed');
|
||||
events.off("message-in", inlistener);
|
||||
});
|
||||
ws.on('close', () => {
|
||||
console.log('ws WebSocket was closed');
|
||||
events.off("message-in", inlistener);
|
||||
});
|
||||
});
|
||||
|
@ -6,24 +6,24 @@ Events:
|
||||
## "message-in" / "message-out"
|
||||
|
||||
{
|
||||
// source messaging system
|
||||
// "irc", "discord", "minetest", etc
|
||||
type: "irc",
|
||||
// source messaging system
|
||||
// "irc", "discord", "minetest", etc
|
||||
type: "irc",
|
||||
|
||||
// the name of the remote system
|
||||
name: "freenode",
|
||||
// the name of the remote system
|
||||
name: "freenode",
|
||||
|
||||
// the source username
|
||||
username: "SomeDude",
|
||||
// the source username
|
||||
username: "SomeDude",
|
||||
|
||||
// the ingame channel: "main", "lag", etc
|
||||
channel: "main",
|
||||
// the ingame channel: "main", "lag", etc
|
||||
channel: "main",
|
||||
|
||||
//the actual message
|
||||
message: "xyz",
|
||||
//the actual message
|
||||
message: "xyz",
|
||||
|
||||
// true if PM / direct message
|
||||
direct: false
|
||||
// true if PM / direct message
|
||||
direct: false
|
||||
}
|
||||
|
||||
## 'reconnect'
|
||||
|
@ -1,130 +1,130 @@
|
||||
const IRC = require('irc-framework');
|
||||
|
||||
module.exports = class {
|
||||
destroy() {
|
||||
this.client.quit("bye o/");
|
||||
}
|
||||
destroy() {
|
||||
this.client.quit("bye o/");
|
||||
}
|
||||
|
||||
init(remote, events) {
|
||||
this.client = new IRC.Client();
|
||||
this.client.connect({
|
||||
host: remote.host,
|
||||
port: remote.port,
|
||||
nick: remote.username,
|
||||
password: remote.password,
|
||||
auto_reconnect: true,
|
||||
auto_reconnect_wait: 10000,
|
||||
auto_reconnect_max_retries: 9999
|
||||
});
|
||||
init(remote, events) {
|
||||
this.client = new IRC.Client();
|
||||
this.client.connect({
|
||||
host: remote.host,
|
||||
port: remote.port,
|
||||
nick: remote.username,
|
||||
password: remote.password,
|
||||
auto_reconnect: true,
|
||||
auto_reconnect_wait: 10000,
|
||||
auto_reconnect_max_retries: 9999
|
||||
});
|
||||
|
||||
if (remote.debug) {
|
||||
this.client.on("debug", function(e){
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
if (remote.debug) {
|
||||
this.client.on("debug", function(e){
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
|
||||
var channels = {}; // name -> channelObj
|
||||
var channels = {}; // name -> channelObj
|
||||
|
||||
// map channels
|
||||
this.client.on('registered', () => {
|
||||
let delay = 5000;
|
||||
Object.keys(remote.channels).forEach(ingame_name => {
|
||||
setTimeout(() => {
|
||||
const irc_name = remote.channels[ingame_name];
|
||||
var channel = this.client.channel("#" + irc_name);
|
||||
channel.join();
|
||||
if (remote.announce_channel) {
|
||||
channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`);
|
||||
}
|
||||
channels[ingame_name] = channel;
|
||||
}, delay);
|
||||
delay += 2000;
|
||||
});
|
||||
});
|
||||
// map channels
|
||||
this.client.on('registered', () => {
|
||||
let delay = 5000;
|
||||
Object.keys(remote.channels).forEach(ingame_name => {
|
||||
setTimeout(() => {
|
||||
const irc_name = remote.channels[ingame_name];
|
||||
var channel = this.client.channel("#" + irc_name);
|
||||
channel.join();
|
||||
if (remote.announce_channel) {
|
||||
channel.say(`beerchat_proxy connected! ingame-channel: ${ingame_name}`);
|
||||
}
|
||||
channels[ingame_name] = channel;
|
||||
}, delay);
|
||||
delay += 2000;
|
||||
});
|
||||
});
|
||||
|
||||
this.client.on('message', event => {
|
||||
if (remote.debug){
|
||||
console.log("irc-event-in", event);
|
||||
}
|
||||
this.client.on('message', event => {
|
||||
if (remote.debug){
|
||||
console.log("irc-event-in", event);
|
||||
}
|
||||
|
||||
if (event.type != "privmsg")
|
||||
return;
|
||||
if (event.type != "privmsg")
|
||||
return;
|
||||
|
||||
if (event.target == remote.username){
|
||||
//PM from IRC received
|
||||
events.emit("message-in", {
|
||||
type: "discord",
|
||||
name: remote.name,
|
||||
username: event.nick,
|
||||
message: event.message,
|
||||
target_name: "minetest"
|
||||
});
|
||||
if (event.target == remote.username){
|
||||
//PM from IRC received
|
||||
events.emit("message-in", {
|
||||
type: "discord",
|
||||
name: remote.name,
|
||||
username: event.nick,
|
||||
message: event.message,
|
||||
target_name: "minetest"
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var channel = "";
|
||||
Object.keys(remote.channels).forEach(ingame_channel => {
|
||||
const irc_channel = remote.channels[ingame_channel];
|
||||
if (irc_channel == event.target.substr(1)){
|
||||
channel = ingame_channel;
|
||||
}
|
||||
});
|
||||
var channel = "";
|
||||
Object.keys(remote.channels).forEach(ingame_channel => {
|
||||
const irc_channel = remote.channels[ingame_channel];
|
||||
if (irc_channel == event.target.substr(1)){
|
||||
channel = ingame_channel;
|
||||
}
|
||||
});
|
||||
|
||||
events.emit("message-in", {
|
||||
type: "irc",
|
||||
name: remote.name,
|
||||
username: event.nick,
|
||||
channel: channel,
|
||||
message: event.message
|
||||
});
|
||||
});
|
||||
events.emit("message-in", {
|
||||
type: "irc",
|
||||
name: remote.name,
|
||||
username: event.nick,
|
||||
channel: channel,
|
||||
message: event.message
|
||||
});
|
||||
});
|
||||
|
||||
events.on("message-out", event => {
|
||||
if (event.name == remote.name)
|
||||
//not meant for this remote, ignore
|
||||
return;
|
||||
events.on("message-out", event => {
|
||||
if (event.name == remote.name)
|
||||
//not meant for this remote, ignore
|
||||
return;
|
||||
|
||||
if (event.target_name != null && event.target_name != remote.name){
|
||||
// target_name set but not for this
|
||||
return;
|
||||
}
|
||||
if (event.target_name != null && event.target_name != remote.name){
|
||||
// target_name set but not for this
|
||||
return;
|
||||
}
|
||||
|
||||
if (remote.debug){
|
||||
console.log("irc-message-out", event);
|
||||
}
|
||||
if (remote.debug){
|
||||
console.log("irc-message-out", event);
|
||||
}
|
||||
|
||||
let channel;
|
||||
let channel;
|
||||
|
||||
if (event.target_username != null){
|
||||
// send PM to IRC user
|
||||
this.client.say(event.target_username, event.message);
|
||||
if (event.target_username != null){
|
||||
// send PM to IRC user
|
||||
this.client.say(event.target_username, event.message);
|
||||
|
||||
} else if (event.channel != null) {
|
||||
// channel name sent, map to config channels
|
||||
channel = channels[event.channel];
|
||||
} else if (event.channel != null) {
|
||||
// channel name sent, map to config channels
|
||||
channel = channels[event.channel];
|
||||
|
||||
} else {
|
||||
// no channel sent, assuming system message
|
||||
channel = channels[remote.system_channel];
|
||||
} else {
|
||||
// no channel sent, assuming system message
|
||||
channel = channels[remote.system_channel];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (channel) {
|
||||
if (event.username){
|
||||
// player message
|
||||
if (event.message_type == "me"){
|
||||
// /me message
|
||||
channel.say(`* ${event.username}${event.type == "minetest" ? "" : "@" + event.name} ${event.message}`);
|
||||
} else {
|
||||
// normal message
|
||||
channel.say(`<${event.username}${event.type == "minetest" ? "" : "@" + event.name}> ${event.message}`);
|
||||
}
|
||||
} else {
|
||||
// system message
|
||||
channel.say(`${event.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (channel) {
|
||||
if (event.username){
|
||||
// player message
|
||||
if (event.message_type == "me"){
|
||||
// /me message
|
||||
channel.say(`* ${event.username}${event.type == "minetest" ? "" : "@" + event.name} ${event.message}`);
|
||||
} else {
|
||||
// normal message
|
||||
channel.say(`<${event.username}${event.type == "minetest" ? "" : "@" + event.name}> ${event.message}`);
|
||||
}
|
||||
} else {
|
||||
// system message
|
||||
channel.say(`${event.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
26
src/index.js
26
src/index.js
@ -15,18 +15,18 @@ const events = require("./events");
|
||||
const router = require("./router");
|
||||
|
||||
const handlers = {
|
||||
irc: require("./handler/irc"),
|
||||
discord: require("./handler/discord")
|
||||
irc: require("./handler/irc"),
|
||||
discord: require("./handler/discord")
|
||||
};
|
||||
|
||||
const instances = [];
|
||||
cfg.remotes.forEach(remote => {
|
||||
const Handler = handlers[remote.type];
|
||||
const instance = new Handler();
|
||||
const Handler = handlers[remote.type];
|
||||
const instance = new Handler();
|
||||
|
||||
console.log(`Setting up remote: ${remote.name} with type: ${remote.type}`);
|
||||
instance.init(remote, events);
|
||||
instances.push(instance);
|
||||
console.log(`Setting up remote: ${remote.name} with type: ${remote.type}`);
|
||||
instance.init(remote, events);
|
||||
instances.push(instance);
|
||||
});
|
||||
|
||||
console.log("Starting message router");
|
||||
@ -35,10 +35,10 @@ router(cfg, events);
|
||||
const server = app.listen(8080, () => console.log('Listening on http://127.0.0.1:8080'));
|
||||
|
||||
events.on("shutdown", function(){
|
||||
// close http server
|
||||
server.close();
|
||||
// close clients
|
||||
instances.forEach(i => i.destroy());
|
||||
// forcibly quit
|
||||
process.exit(0);
|
||||
// close http server
|
||||
server.close();
|
||||
// close clients
|
||||
instances.forEach(i => i.destroy());
|
||||
// forcibly quit
|
||||
process.exit(0);
|
||||
});
|
@ -1,7 +1,7 @@
|
||||
|
||||
module.exports = function(cfg, events){
|
||||
events.on("message-in", function(event){
|
||||
// dispatch to other systems as message-out event
|
||||
events.on("message-in", function(event){
|
||||
// dispatch to other systems as message-out event
|
||||
|
||||
if (cfg.debug){
|
||||
console.log("message-in", event);
|
||||
@ -9,26 +9,26 @@ module.exports = function(cfg, events){
|
||||
|
||||
// dispatch to remotes
|
||||
events.emit("message-out", {
|
||||
// "discord", "irc"
|
||||
// "discord", "irc"
|
||||
type: event.type,
|
||||
// "Discord", "IRC"
|
||||
// "Discord", "IRC"
|
||||
name: event.name,
|
||||
// "somedude", "SomeDude@IRC", "SomeDude123@Discord"
|
||||
username: event.username,
|
||||
// null, "es", "main"
|
||||
// "somedude", "SomeDude@IRC", "SomeDude123@Discord"
|
||||
username: event.username,
|
||||
// null, "es", "main"
|
||||
channel: event.channel,
|
||||
// "give diamonds pls"
|
||||
// "give diamonds pls"
|
||||
message: event.message,
|
||||
|
||||
// "me", "message"
|
||||
message_type: event.message_type,
|
||||
// "me", "message"
|
||||
message_type: event.message_type,
|
||||
|
||||
// null, "SomeDude@IRC"
|
||||
target_username: event.target_username,
|
||||
// null, "minetest", "IRC", "Discord"
|
||||
target_name: event.target_name
|
||||
// null, "SomeDude@IRC"
|
||||
target_username: event.target_username,
|
||||
// null, "minetest", "IRC", "Discord"
|
||||
target_name: event.target_name
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user