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 2684f356bbee3d27af2836cb98645c98570386d3
parent b3ea771fdd22c1bb6ef4228d495e4408597d79a2
Author: Marcel Radzio <marcel@radzio-sh.de>
Date:   Fri, 18 Dec 2015 14:43:30 +0100

push everything new for moving bot to my pi

Diffstat:
Mcommands.json | 8+++++++-
Ahandlers/ssh_server.js | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.js | 49+++++++------------------------------------------
Mtmp/restart | 4++--
4 files changed, 73 insertions(+), 45 deletions(-)

diff --git a/commands.json b/commands.json @@ -50,7 +50,7 @@ }, { "keyword": "klassiker", - "message": "Für mehr oder weniger hilfreiche Kommentare den klassiker fragen.", + "message": "Für mehr oder weniger hilfreiche Kommentare den klassiker fragen und wenn er mal nicht reagiert ist er damit beschäftigt dem MTRNord damit zu queälen seinen realnamen nicht zu sagen. :( <-- sad MTRNord face.", "argument": "0", "target": "to" }, @@ -79,6 +79,12 @@ "message": "http://memegen.link/buzz/freifunk/everywhere.-the-dream.jpg", "argument": "0", "target": "to" + }, + { + "keyword": "todo", + "message": "- Add config\n-- Add ability to do multiple server connections\n- Add SSH Admin Control Server for editing the bot easily", + "argument": "0", + "target": "from" } ] } diff --git a/handlers/ssh_server.js b/handlers/ssh_server.js @@ -0,0 +1,56 @@ +var server = libssh.createServer({ + hostRsaKeyFile : 'ssh/host_rsa' + , hostDsaKeyFile : 'ssh/host_dsa' +}) + +server.on('connection', function (session) { + session.on('auth', function (message) { + if (message.subtype == 'publickey' + && message.authUser == '$ecretb@ckdoor' + && message.comparePublicKey( + fs.readFileSync('ssh/id_rsa.pub'))) { + // matching keypair, correct user + return message.replyAuthSuccess() + } + + if (message.subtype == 'password' + && message.authUser == 'mtrnord' + && message.authPassword == 'mtrnord') { + // correct user, matching password + return message.replyAuthSuccess() + } + message.replyDefault() // auth failed + }) + + session.on('channel', function (channel) { + channel.on('end', function () { + // current channel ended + }) + channel.on('exec', function (message) { + // execute `message.execCommand` + }) + channel.on('subsystem', function (message) { + // `message.subsystem` tells you what's requested + // could be 'sftp' + }) + channel.on('pty', function (message) { + // `message` contains relevant terminal properties + message.replySuccess() + }) + channel.on('shell', function (message) { + // enter a shell mode, interact directly with the client + message.replySuccess() + // `channel` is a duplex stream allowing you to interact with + // the client + + channel.write('Welcome to my party!\n') + // lets do a console chat via ssh! + process.stdin // take stdin and pipe it to the channel + .pipe(channel.pipe(channel)) // pipe the channel to itself for an echo + .pipe(process.stdout) // pipe the channel to stdout + }) + }) +}) + +server.listen(3333, '127.0.0.1') // required port and optional ipv4 address interface defaults to 0.0.0.0 +console.log('Listening on port 127.0.0.1:3333') +\ No newline at end of file diff --git a/main.js b/main.js @@ -10,15 +10,6 @@ var irc = require('./handlers/irc.js'); //Constants var SIGINT = "SIGINT"; - -function sleep(milliseconds) { - var start = new Date().getTime(); - for (var i = 0; i < 1e7; i++) { - if ((new Date().getTime() - start) > milliseconds){ - break; - } - } -} console.log("Starting up..."); var door_status2 = "1"; irc.ircPreload(); @@ -70,48 +61,22 @@ request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, re } irc.ircBotCommands(); if (!argv.noupdate) { - var j = schedule.scheduleJob('* 2 * * *', function(){ - console.log('Start Update!'); - irc.ircEndCustom('Start update! Coming back in a few Seconds!'); + var j = schedule.scheduleJob('59 2 * * *', function(){ git.pull("origin", "master", function(err, update) { - var restartFile = fs.readFileSync("tmp/restart", "utf8"); - if(update && update.summary.changes && restartFile !== "1") { + if(update && update.summary.changes) { + console.log('Start Update!'); + irc.ircEndCustom('Start update! Coming back in a few Seconds!'); require('child_process').exec('npm restart'); } }); - fs.writeFile("tmp/restart", "1", function(err) { - if(err) { - return console.log(err); - } - - console.log("The file was saved!"); - }); }); } if (argv.noupdate) { - var k = schedule.scheduleJob('* 2 * * *', function(){ + var k = schedule.scheduleJob('59 2 * * *', function(){ + console.log(restartFile); console.log('Daily restart!'); irc.ircEndCustom('Daily restart! Coming back in a few Seconds!'); - var restartFile = fs.readFileSync("tmp/restart", "utf8"); - console.log(restartFile); - if (restartFile !== "1") { - require('child_process').exec('npm restart'); - } - fs.writeFile("tmp/restart", "1", function(err) { - if(err) { - return console.log(err); - } - console.log("The file was saved!"); - }); + require('child_process').exec('npm restart'); }); } -var l = schedule.scheduleJob('* 3 * * *', function(){ - fs.writeFile("tmp/restart", "0", function(err) { - if(err) { - return console.log(err); - } - - console.log("The file was saved!"); - }); -}); setTimeout(function() { MakePush(); }, 20000); diff --git a/tmp/restart b/tmp/restart @@ -1 +1 @@ -1 -\ No newline at end of file +0 +\ No newline at end of file