freifunk-bot

Modular parser for the hackerspace door status with handlers for multiple services
git clone git://archive.git.mtrnord.blog/MTRNord/freifunk-bot.git
Log | Files | Refs | README | LICENSE

commit 7486c7d97b5e6eef0e2044ef1c5b9946a951d450
parent e394d785952b9423baf4a193cad7d2cb2a5aed03
Author: MTRNord <mtrnord1@gmail.com>
Date:   Thu, 10 Mar 2016 21:20:24 +0100

Add multiServer support

Diffstat:
Mconfigs/commands.json | 12+++---------
Mconfigs/params.json | 12++++++++++--
Mhandlers/irc.js | 120+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
3 files changed, 91 insertions(+), 53 deletions(-)

diff --git a/configs/commands.json b/configs/commands.json @@ -2,13 +2,13 @@ "commands": [ { "keyword": "help", - "message": "Here is your Help!\nCommand List:\n- !help - Shows this page.\n- !DoorStatus - Shows the actual Door Status in an PM\n- !DoorStatus channel - Shows the actual Door Status in the channel from where it was run\n- !where - Shows the address of the Nordlab e.V.\n- !who - Shows who is allowed to come to the Nordlab e.V.\n- !when - Shows who the Nordlab e.V. Hackerspace usually is open\n- !source - Shows where the source of this bot is.\n- !license - What is the license of the bot?????????", + "message": "Here is your Help!\nCommand List:\n- !help - Shows this page.\n- !DoorStatus - Shows the actual Door Status in an PM\n- !DoorStatus channel - Shows the actual Door Status in the channel from where it was run\n- !where - Shows the address of the Nordlab e.V.\n- !who - Shows who is allowed to come to the Nordlab e.V.\n- !when - Shows who the Nordlab e.V. Hackerspace usually is open\n- !source - Shows where the source of this bot is.\n- !license - What is the license of the bot?\n- !bug - Tell me the Bugs you have found :)\n- !afk - Tell the chat that you are away\n- !freifunk - Freifunk Memes \\(^^)/\n- !todo - List of things that have to be done", "argument": "1", "target": "from" }, { "keyword": "hilfe", - "message": "Hier ist die Hilfe!\nBefehl-Liste:\n- !hilfe - Zeigt diese Seite an.\n- !DoorStatus - Sendet eine PM mit dem aktuellen Status\n- !DoorStatus channel - Sendet in den Channel wo der Befehl ausgeführt wurde den aktuellen Status\n- !where - Zeigt die Addresse vom Nordlab e.V.\n- !who - Zeigt wer alles in den Nordlab e.V. kommen darf\n- !when - Zeigt wann der Nordlab e.V. Hackerspace geöffnet hat\n- !source - Zeigt wo die Sourcen des Bot's sind.\n- !license - HILFE! Welche Lizenz hat der Bot?????????", + "message": "Hier ist die Hilfe!\nBefehl-Liste:\n- !hilfe - Zeigt diese Seite an.\n- !DoorStatus - Sendet eine PM mit dem aktuellen Status\n- !DoorStatus channel - Sendet in den Channel wo der Befehl ausgeführt wurde den aktuellen Status\n- !where - Zeigt die Addresse vom Nordlab e.V.\n- !who - Zeigt wer alles in den Nordlab e.V. kommen darf\n- !when - Zeigt wann der Nordlab e.V. Hackerspace geöffnet hat\n- !source - Zeigt wo die Sourcen des Bot's sind.\n- !license - HILFE! Welche Lizenz hat der Bot?\n- !bug - Nenne mir Fehler die du gefunden hast :)\n- !afk - Teile dem Chat deine Abwesendheit mit\n- !freifunk - Freifunk Memes \\(^^)/\n- !todo - Dinge die ich noch tun muss", "argument": "1", "target": "from" }, @@ -82,15 +82,9 @@ }, { "keyword": "todo", - "message": "- Add ability to do multiple server connections\n- Add SSH Admin Control Server for editing the bot easily", + "message": "- Stabelize ability to do multiple server connections\n- Add SSH Admin Control Server for editing the bot easily", "argument": "0", "target": "from" - }, - { - "keyword": "xmas", - "message": "Der NordlabBot wünscht euch allen eine frohe und besinliche Weihnachtszeit :)", - "argument": "0", - "target": "to" } ] } diff --git a/configs/params.json b/configs/params.json @@ -1,5 +1,5 @@ { - "botname": "NordlabBot", + "botname": "FreifunkBot", "autoupdate": 1, "disconnect_meassage": "Bot was stopped!", "servers": [ @@ -7,7 +7,15 @@ "servername": "Lugfl", "serveraddress": "irc.lugfl.de", "main_channel": "#hackerspace", - "nickserv_pass": "NordlabBotPass", + "nickserv_pass": "FreifunkBotPass", + "active": 1, + "debug": 0 + }, + { + "servername": "Hackint", + "serveraddress": "irc.hackint.org", + "main_channel": "#ffnord", + "nickserv_pass": "FreifunkBotPass", "active": 1, "debug": 0 } diff --git a/handlers/irc.js b/handlers/irc.js @@ -1,6 +1,6 @@ /** * IRC-ChatBot Module - * + * * @module Main * @class irc * @submodule irc @@ -53,7 +53,8 @@ for (var key in params_config["servers"]) { } } */ - +var clients = []; +var bot = []; for (var key in params_config["servers"]) { if (params_config["servers"].hasOwnProperty(key)) { var serveraddress = params_config["servers"][key]["serveraddress"]; @@ -70,17 +71,52 @@ for (var key in params_config["servers"]) { console.log = function(){}; } if (active == 1) { - var bot = new irc.Client(serveraddress, botname, { + bot[key] = new irc.Client(serveraddress, botname, { debug: debug, - channels: [main_channel, "#freifunk-flensburg"], + channels: [main_channel], autoRejoin: true, autoConnect: false, - messageSplit: 1000000 + messageSplit: 1000000, + floodProtection: true, + floodProtectionDelay: 1000, }); } + clients.push(bot[key]); } } +function addListener(event, callback) { + clients.forEach(function(client) { + client.addListener(event, callback); + }); +} +function join(event, callback) { + clients.forEach(function(client) { + client.join(event, callback); + }); +} +function disconnect(event, callback) { + clients.forEach(function(client) { + client.disconnect(event, callback); + }); +} +function connect(event, callback) { + clients.forEach(function(client) { + client.connect(event, callback); + }); +} + +function send(event, callback) { + clients.forEach(function(client) { + client.send(event, callback); + }); +} + +function say(event, callback) { + clients.forEach(function(client) { + client.say(event, callback); + }); +} module.exports = { /** * Stops Bot with custom meassage @@ -89,7 +125,7 @@ module.exports = { * @constructor */ ircEndCustom: function (meassage) { - bot.disconnect(meassage); + disconnect(meassage); }, /** * Connects to Server, logs in to NickServ, sets Name @@ -97,12 +133,12 @@ module.exports = { * @method ircPreload */ ircPreload: function () { - bot.addListener('error', function(message) { - setTimeout(function() { - console.log("wait 7sek"); + addListener('error', function(message) { + setTimeout(function() { + console.log("wait 7sek"); }, 7000); - bot.send('nick', botname); + send('nick', botname); for (var key in params_config["servers"]) { if (params_config["servers"].hasOwnProperty(key)) { /** @@ -112,15 +148,15 @@ module.exports = { * @type String */ var nickserv_pass = params_config["servers"][key]["nickserv_pass"]; - bot.say('NickServ', 'identify ' + nickserv_pass); + say('NickServ', 'identify ' + nickserv_pass); } } }); - setTimeout(function() { - console.log("wait 7sek"); + setTimeout(function() { + console.log("wait 7sek"); }, 7000); - bot.connect(10, function() { - bot.send('nick', botname); + connect(10, function() { + send('nick', botname); for (var key in params_config["servers"]) { if (params_config["servers"].hasOwnProperty(key)) { /** @@ -137,11 +173,11 @@ module.exports = { * @type String */ var main_channel = params_config["servers"][key]["main_channel"]; - bot.say('NickServ', 'identify ' + nickserv_pass); - bot.join(main_channel); + say('NickServ', 'identify ' + nickserv_pass); + join(main_channel); } } - bot.say(main_channel, 'Door Bot is starting to watch on the Door Status'); + say(main_channel, 'Door Bot is starting to watch on the Door Status'); }) }, /** @@ -160,11 +196,11 @@ module.exports = { * @type String */ var main_channel = params_config["servers"][key]["main_channel"]; - bot.say(main_channel, 'Door Status changed to: ' + door_status); - bot.say("#freifunk-flensburg", 'Door Status changed to: ' + door_status); + say(main_channel, 'Door Status changed to: ' + door_status); + say("#freifunk-flensburg", 'Door Status changed to: ' + door_status); } } - bot.send('topic','#hackerspace "Hackerspace Flensburg - Treffen jeden Montag 18:00 Uhr im Offenen Kanal Flensburg! - Tür Status"' + door_status); + send('topic','#hackerspace "Hackerspace Flensburg - Treffen jeden Montag 18:00 Uhr im Offenen Kanal Flensburg! - Tür Status"' + door_status); console.log("IRC Door Status Chnaged"); }, /** @@ -173,7 +209,7 @@ module.exports = { * @method ircStopp */ ircStopp: function() { - bot.disconnect(disconnect_meassage); + disconnect(disconnect_meassage); }, /** * Handels Commands from the Bot @@ -192,7 +228,7 @@ module.exports = { return newArray; } - bot.addListener('message', function (from, to, message) { + addListener('message', function (from, to, message) { /** * Recived meassage * @@ -206,22 +242,22 @@ module.exports = { if (message == ("!" + command_config["commands"][key]["keyword"])) { if (command_config["commands"][key]["before"] == "from") { if (command_config["commands"][key]["target"] == "from") { - bot.say(from, from + command_config["commands"][key]["message"]); + say(from, from + command_config["commands"][key]["message"]); } else { - bot.say(to, from + command_config["commands"][key]["message"]); + say(to, from + command_config["commands"][key]["message"]); } } else { if (!command_config["commands"][key]["before"]) { if (command_config["commands"][key]["target"] == "from") { - bot.say(from, command_config["commands"][key]["message"]); + say(from, command_config["commands"][key]["message"]); } else { - bot.say(to, command_config["commands"][key]["message"]); + say(to, command_config["commands"][key]["message"]); } } else { if (command_config["commands"][key]["target"] == "from") { - bot.say(from, to + command_config["commands"][key]["message"]); + say(from, to + command_config["commands"][key]["message"]); } else { - bot.say(to, to + command_config["commands"][key]["message"]); + say(to, to + command_config["commands"][key]["message"]); } } } @@ -241,8 +277,8 @@ module.exports = { if (channel[0] + " " + channel[1] == "!source this") { console.log("!source " + channel[1]); if (channel[1] == "this") { - bot.join(to); - bot.say(to, "You can find the Source of this bot at https://github.com/MTRNord/nordlab-hackerspace-door"); + join(to); + say(to, "You can find the Source of this bot at https://github.com/MTRNord/nordlab-hackerspace-door"); } } if (message == "!doorstatus") { @@ -271,11 +307,11 @@ module.exports = { door_status = "closed"; } else { door_status = "open"; - } + } console.log(from + ' => ' + to + ': ' + message); - bot.say(from, "DoorStatus is: " + door_status); + say(from, "DoorStatus is: " + door_status); }else{ - bot.say(from, "DoorStatus is not availible at this time"); + say(from, "DoorStatus is not availible at this time"); } }).setMaxListeners(0); } @@ -316,21 +352,21 @@ module.exports = { for (var key in channel_list) { if (channel_list.hasOwnProperty(key)) { if (channel_list[key]["name"] == channel[1]) { - bot.join(channel[1]); - bot.say(channel[1], "DoorStatus is: " + door_status); + join(channel[1]); + say(channel[1], "DoorStatus is: " + door_status); } } } }); } else { - bot.say(from, "DoorStatus is: " + door_status); + say(from, "DoorStatus is: " + door_status); } } else { - bot.join(to); - bot.say(to, "DoorStatus is: " + door_status); + join(to); + say(to, "DoorStatus is: " + door_status); } }else{ - bot.say(from, "DoorStatus is not availible at this time"); + say(from, "DoorStatus is not availible at this time"); } }).setMaxListeners(0); } @@ -342,7 +378,7 @@ module.exports = { input: process.stdin, output: process.stdout }); - bot.disconnect(disconnect_meassage); + disconnect(disconnect_meassage); process.exit(); } } @@ -355,6 +391,6 @@ module.exports = { * @method ircNick */ ircNick: function() { - bot.send('nick', botname); + send('nick', botname); } };