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 8859f8465dadbf489537b32576aebcd24cfa2635
parent 2684f356bbee3d27af2836cb98645c98570386d3
Author: Marcel Radzio <marcel@radzio-sh.de>
Date:   Fri, 18 Dec 2015 23:27:11 +0100

add config

Diffstat:
Rcommands.json -> configs/commands.json | 0
Aconfigs/params.json | 16++++++++++++++++
Mhandlers/irc.js | 308+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mmain.js | 10+++++++---
4 files changed, 205 insertions(+), 129 deletions(-)

diff --git a/commands.json b/configs/commands.json diff --git a/configs/params.json b/configs/params.json @@ -0,0 +1,15 @@ +{ + "botname": "NordlabBot", + "autoupdate": 1, + "disconnect_meassage": "Bot was stopped!", + "servers": [ + { + "servername": "Lugfl", + "serveraddress": "irc.lugfl.de", + "main_channel": "#hackerspace", + "nickserv_pass": "NordlabBotPass", + "active": 1, + "debug": 0 + } + ] +} +\ No newline at end of file diff --git a/handlers/irc.js b/handlers/irc.js @@ -1,13 +1,52 @@ var irc = require('irc'); -var bot = new irc.Client('irc.lugfl.de', 'NordlabBot', { - debug: false, - channels: ['#hackerspace'], - autoRejoin: false, - autoConnect: false, - messageSplit: 1000000 -}); var request = require('request'); -var command_config = require("../commands.json"); +var command_config = require("../configs/commands.json"); +var params_config = require("../configs/params.json"); + +//Set Params +var botname = params_config["botname"]; +var autoupdate = params_config["autoupdate"]; +var disconnect_meassage = params_config["disconnect_meassage"]; +/* // TEMPLATE // +for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + var servername = params_config["servers"][key]["servername"]; + var serveraddress = params_config["servers"][key]["serveraddress"]; + var main_channel = params_config["servers"][key]["main_channel"]; + var nickserv_pass = params_config["servers"][key]["nickserv_pass"]; + var active = params_config["servers"][key]["active"]; + var debug = params_config["servers"][key]["debug"]; + if (debug == 1) { + var debug = true; + }else{ + var debug = false; + } + } +} +*/ + +for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + var serveraddress = params_config["servers"][key]["serveraddress"]; + var main_channel = params_config["servers"][key]["main_channel"]; + var active = params_config["servers"][key]["active"]; + var debug = params_config["servers"][key]["debug"]; + if (debug == 1) { + var debug = true; + }else{ + var debug = false; + } + if (active == 1) { + var bot = new irc.Client(serveraddress, botname, { + debug: false, + channels: [main_channel], + autoRejoin: false, + autoConnect: false, + messageSplit: 1000000 + }); + } + } +} module.exports = { ircEndCustom: function (meassage) { @@ -18,120 +57,137 @@ module.exports = { setTimeout(function() { // console.log("wait 7sek"); }, 7000); - bot.send('nick', 'NordlabBot'); - bot.say('NickServ', 'identify NordlabBotPass'); + + bot.send('nick', botname); + for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + var nickserv_pass = params_config["servers"][key]["nickserv_pass"]; + bot.say('NickServ', 'identify ' + nickserv_pass); + } + } }); setTimeout(function() { // console.log("wait 7sek"); }, 7000); bot.connect(10, function() { - bot.send('nick', 'NordlabBot'); - bot.say('NickServ', 'identify NordlabBotPass'); - bot.join('#hackerspace'); + bot.send('nick', botname); + for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + var nickserv_pass = params_config["servers"][key]["nickserv_pass"]; + var main_channel = params_config["servers"][key]["main_channel"]; + bot.say('NickServ', 'identify ' + nickserv_pass); + bot.join(main_channel); + } + } //bot.say('#hackerspace', 'Door Bot is starting to watch on the Door Status'); }) }, ircSend: function (door_status) { - bot.say('#hackerspace', '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); - // console.log("IRC Door Status Chnaged"); - }, - ircStopp: function() { - bot.disconnect('Bot was stopped!'); - }, - ircBotCommands: function() { - // Will remove all falsy values: undefined, null, 0, false, NaN and "" (empty string) - function cleanArray(actual) { - var newArray = new Array(); - for (var i = 0; i < actual.length; i++) { - if (actual[i]) { - newArray.push(actual[i]); - } - } - return newArray; + for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + var main_channel = params_config["servers"][key]["main_channel"]; + bot.say(main_channel, '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); + // console.log("IRC Door Status Chnaged"); + }, + ircStopp: function() { + bot.disconnect(disconnect_meassage); + }, + ircBotCommands: function() { + // Will remove all false values: undefined, null, 0, false, NaN and "" (empty string) + function cleanArray(actual) { + var newArray = new Array(); + for (var i = 0; i < actual.length; i++) { + if (actual[i]) { + newArray.push(actual[i]); } + } + return newArray; + } - bot.addListener('message', function (from, to, message) { - message = message.toLowerCase(); - for (var key in command_config["commands"]) { - if (command_config["commands"].hasOwnProperty(key)) { - // console.log("!" + JSON.stringify(command_config["commands"][key]["keyword"])); - if (message == ("!" + command_config["commands"][key]["keyword"])) { - if (command_config["commands"][key]["before"] == "from") { + bot.addListener('message', function (from, to, message) { + message = message.toLowerCase(); + for (var key in command_config["commands"]) { + if (command_config["commands"].hasOwnProperty(key)) { + // console.log("!" + JSON.stringify(command_config["commands"][key]["keyword"])); + 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"]); + } else { + bot.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, from + command_config["commands"][key]["message"]); + bot.say(from, command_config["commands"][key]["message"]); } else { - bot.say(to, from + command_config["commands"][key]["message"]); + bot.say(to, 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"]); - } else { - bot.say(to, command_config["commands"][key]["message"]); - } + if (command_config["commands"][key]["target"] == "from") { + bot.say(from, to + command_config["commands"][key]["message"]); } else { - if (command_config["commands"][key]["target"] == "from") { - bot.say(from, to + command_config["commands"][key]["message"]); - } else { - bot.say(to, to + command_config["commands"][key]["message"]); - } + bot.say(to, to + command_config["commands"][key]["message"]); } } } } } - var channel = message.split(" "); - channel = cleanArray(channel); - 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"); - } - } - if (message == "!doorstatus") { - request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) { - if (!error && response.statusCode == 200) { - door_status = body; - //console.log(body); - } else { - door_status = error; - //console.log(error); - } - if (door_status == "geschlossen") { - door_status = "closed"; - } else { - door_status = "open"; - } - // console.log(from + ' => ' + to + ': ' + message); - bot.say(from, "DoorStatus is: " + door_status); - }).setMaxListeners(0); + } + var channel = message.split(" "); + channel = cleanArray(channel); + 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"); } - if (channel[0] + " " + channel[1] == "!doorstatus this") { - request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) { - if (!error && response.statusCode == 200) { - door_status = body; - //console.log(body); - } else { - door_status = error; - //console.log(error); - } - if (door_status == "geschlossen") { - door_status = "closed"; - } else { - door_status = "open"; - } - // console.log(message); - bot.list(); - // console.log(channel[(channel.length-(channel.length-1))+1]); - // console.log(channel.length); - if (channel[1] !== "this") { - if (channel[1] !== " ") { - bot.addListener('channellist', function (channel_list) { - for (var key in channel_list) { - if (channel_list.hasOwnProperty(key)) { - if (channel_list[key]["name"] == channel[1]) { + } + if (message == "!doorstatus") { + request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) { + if (!error && response.statusCode == 200) { + door_status = body; + //console.log(body); + } else { + door_status = error; + //console.log(error); + } + if (door_status == "geschlossen") { + door_status = "closed"; + } else { + door_status = "open"; + } + // console.log(from + ' => ' + to + ': ' + message); + bot.say(from, "DoorStatus is: " + door_status); + }).setMaxListeners(0); + } + if (channel[0] + " " + channel[1] == "!doorstatus this") { + request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) { + if (!error && response.statusCode == 200) { + door_status = body; + //console.log(body); + } else { + door_status = error; + //console.log(error); + } + if (door_status == "geschlossen") { + door_status = "closed"; + } else { + door_status = "open"; + } + // console.log(message); + bot.list(); + // console.log(channel[(channel.length-(channel.length-1))+1]); + // console.log(channel.length); + if (channel[1] !== "this") { + if (channel[1] !== " ") { + bot.addListener('channellist', function (channel_list) { + for (var key in channel_list) { + if (channel_list.hasOwnProperty(key)) { + if (channel_list[key]["name"] == channel[1]) { // console.log("1"); bot.join(channel[1]); bot.say(channel[1], "DoorStatus is: " + door_status); @@ -139,31 +195,31 @@ module.exports = { } } }); - } else { - bot.say(from, "DoorStatus is: " + door_status); - } - } else { - bot.join(to); - bot.say(to, "DoorStatus is: " + door_status); - } - }).setMaxListeners(0); -} -if (message == "!kill") { - if ((from == "DasNordlicht") || (from == "MTRNord")) { - // console.log(from + ' => ' + to + ': ' + message); - if (process.platform === "win32") { - var rl = require("readline").createInterface({ - input: process.stdin, - output: process.stdout - }); - bot.disconnect('Bot was stopped!'); - process.exit(); - } - } + } else { + bot.say(from, "DoorStatus is: " + door_status); + } + } else { + bot.join(to); + bot.say(to, "DoorStatus is: " + door_status); + } + }).setMaxListeners(0); + } + if (message == "!kill") { + if ((from == "DasNordlicht") || (from == "MTRNord")) { + // console.log(from + ' => ' + to + ': ' + message); + if (process.platform === "win32") { + var rl = require("readline").createInterface({ + input: process.stdin, + output: process.stdout + }); + bot.disconnect(disconnect_meassage); + process.exit(); } - }) -}, -ircNick: function() { - bot.send('nick', 'NordlabBot'); -} + } + } + }) + }, + ircNick: function() { + bot.send('nick', botname); + } }; diff --git a/main.js b/main.js @@ -3,6 +3,9 @@ var git = require('simple-git'); var schedule = require('node-schedule'); var argv = require('yargs').argv; var fs = require('fs'); +var params_config = require("../configs/params.json"); +var autoupdate = params_config["autoupdate"]; + //LOAD MODULES DOWN HERE var pushbullet = require('./handlers/pushbullet.js'); var irc = require('./handlers/irc.js'); @@ -60,20 +63,21 @@ request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, re }).setMaxListeners(0); } irc.ircBotCommands(); -if (!argv.noupdate) { +if (!argv.noupdate && autoupdate == 1) { var j = schedule.scheduleJob('59 2 * * *', function(){ git.pull("origin", "master", function(err, update) { if(update && update.summary.changes) { console.log('Start Update!'); + + console.log('Daily restart!'); irc.ircEndCustom('Start update! Coming back in a few Seconds!'); require('child_process').exec('npm restart'); } }); }); } -if (argv.noupdate) { +if ((argv.noupdate) || (autoupdate == 0)) { var k = schedule.scheduleJob('59 2 * * *', function(){ - console.log(restartFile); console.log('Daily restart!'); irc.ircEndCustom('Daily restart! Coming back in a few Seconds!'); require('child_process').exec('npm restart');