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

parseNodes.js (7365B)


      1 /**
      2  * NodeHelper Module
      3  *
      4  * @module Main
      5  * @class getNodes
      6  * @submodule getNodes
      7  * @author Marcel Radzio
      8  */
      9 //Load needed Features
     10 var request = require('request')
     11 //require('request-debug')(request);
     12 var fs = require('fs');
     13 var jsonfile = require('jsonfile')
     14 //var slack = require('./slack.js');
     15 var _ = require("lodash");
     16 /**
     17  * Get Community Config at start
     18  *
     19  * @property communities
     20  * @type String
     21  */
     22 module.exports = {
     23   saveNodes: function () {
     24     request.get('https://cdn.rawgit.com/MTRNord/gluon-web-remote/00a5511119dbb817528d5e1cde91b42640296143/configs/communities.json', function (err, res, body) {
     25       if (!err && res.statusCode === 200) {
     26         /**
     27           * Content of communities
     28           *
     29           * @property communities
     30           * @type String
     31           */
     32         communities = body;
     33         if (!fs.existsSync("handlers/tmp")){
     34           fs.mkdirSync("handlers/tmp");
     35         }
     36         fs.writeFile("handlers/tmp/communities.json", communities, { flag: 'w' }, function(err) {
     37           if(err) {
     38             console.log(err);
     39             return err
     40           }
     41         });
     42       }else{
     43         /**
     44           * Fired when an error occurs...
     45           *
     46           * @property error
     47           * @type String
     48           */
     49           //TODO Add real Error Handler
     50         console.log(err);
     51         return err
     52       }
     53     })
     54   },
     55   countNodes: function (ccode_func, handler, TfromId, Tmsg, channel, bot, botan, IRCto) {
     56     jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) {
     57       if (err) {
     58         return console.log(err);
     59       }
     60       var communities = obj
     61       _.forEach(communities, function (key, ccode) {
     62         if (ccode.toLowerCase() == ccode_func.toLowerCase()) {
     63           var nodesFile_link = key["nodesURL"];
     64           var name = key["name"];
     65           request.get(nodesFile_link, function (err, res, body) {
     66             if (!err && res.statusCode == 200) {
     67               /**
     68                 * Content of communities
     69                 *
     70                 * @property communities
     71                 * @type String
     72                 */
     73               var count = 0;
     74               nodes = JSON.parse(body)
     75               _.forEach(nodes.nodes, function (key) {
     76                 if (key.hasOwnProperty('flags')) {
     77                   if (key["flags"]["online"] == true) {
     78                     count++;
     79                   }
     80                 }
     81                 if (key.hasOwnProperty('status')) {
     82                   if (key["status"]["online"] == true) {
     83                     count++;
     84                   }
     85                 }
     86               });
     87               var output = "Current nodes in " + name + ": " + count
     88               if(handler == "telegram"){
     89                 bot.sendMessage(TfromId, output)
     90                 botan.track(Tmsg, 'nodes ' + ccode_func)
     91               }else {
     92                 if(handler == "irc"){
     93                   bot.say(IRCto, output);
     94                 }else {
     95                   //if(handler == "slack"){
     96                     //slack.slackSend(Schannel, output);
     97                   //}
     98                 }
     99               }
    100             }else{
    101               /**
    102                 * Fired when an error occurs...
    103                 *
    104                 * @property error
    105                 * @type String
    106                 */
    107               //TODO Add real Error Handler
    108               throw new Error(err);
    109             }
    110           })
    111         }
    112       });
    113     });
    114   },
    115   NodeInfo: function (ccode_func, askedNode, handler, TfromId, Tmsg, channel, bot, botan, IRCto) {
    116     jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) {
    117       if (err) {
    118         throw new Error(err);
    119       }
    120       var communities = obj
    121       _.forEach(communities, function (key, ccode) {
    122         if (ccode.toLowerCase() == ccode_func.toLowerCase()) {
    123           var nodesFile_link = key["nodesURL"];
    124           request.get(nodesFile_link, function (err, res, body) {
    125             if (!err && res.statusCode == 200) {
    126               /**
    127                 * Content of communities
    128                 *
    129                 * @property communities
    130                 * @type String
    131                 */
    132               var count = 0;
    133               nodes = JSON.parse(body)
    134               var output = "Some error"
    135               var name = ""
    136               var router = ""
    137               var version = ""
    138               var autoupdate = ""
    139               var autoupdateBranch = ""
    140               var clients = ""
    141               var status = ""
    142               var since = ""
    143               _.forEach(nodes.nodes, function (key) {
    144                 name = key["nodeinfo"]["hostname"]
    145                 if (name.toLowerCase() == askedNode.toLowerCase()){
    146                   router = key["nodeinfo"]["hardware"]["model"]
    147                   version = key["nodeinfo"]["software"]["firmware"]["release"]
    148                   autoupdate = key["nodeinfo"]["software"]["autoupdater"]["enabled"]
    149                   if (autoupdate === true) {
    150                     autoupdate = "active"
    151                   }else{
    152                     autoupdate = "unactive"
    153                   }
    154                   autoupdateBranch = key["nodeinfo"]["software"]["autoupdater"]["branch"]
    155                   clients = key["statistics"]["clients"]
    156                   status = key["flags"]["online"]
    157                   since = key["firstseen"]
    158                   if(handler == "telegram"){
    159                     output = "<b>Name: </b>" + name + "\n<b>Router Model: </b>" + router + "\n<b>Firmware Version: </b>" + version + "\n<b>Autoupdater Status: </b>" + autoupdate + "\n<b>Autoupdater Branch: </b>" + autoupdateBranch + "\n<b>Clients Connected: </b>" + clients + "\n<b>Online Status: </b>" + status + "\n<b>First Seen: </b>" + since
    160                   }
    161                   if(handler == "irc"){
    162                     output = "Name: " + name + "\nRouter Model: " + router + "\nFirmware Version: " + version + "\nAutoupdater Status: " + autoupdate + "\nAutoupdater Branch: " + autoupdateBranch + "\nClients Connected: " + clients + "\nOnline Status: " + status + "\nFirst Seen: " + since
    163                   }
    164                   //if(handler == "slack"){
    165                     //output = "<b>Name: </b>" + name + "\n<b>Router Model: </b>" + router + "\n<b>Firmware Version: </b>" + version + "\n<b>Autoupdater Status: </b>" + autoupdate + "\n<b>Autoupdater Branch: </b>" + autoupdateBranch + "\n<b>Clients Connected: </b>" + clients + "\n<b>Online Status: </b>" + status + "\n<b>First Seen: </b>" + since
    166                   //}
    167                 }
    168               });
    169               if (askedNode.toLowerCase() !== "noarg") {
    170                 if(handler == "telegram"){
    171                   bot.sendMessage(TfromId, output, {"parse_mode": "HTML"})
    172                   botan.track(Tmsg, 'node ' + ccode_func + " " + askedNode.toLowerCase())
    173                 }
    174                 if(handler == "irc"){
    175                   bot.say(IRCto, output);
    176                 }
    177                 //if(handler == "slack"){
    178                   //slack.slackSend(Schannel, output);
    179                 //}
    180               }
    181             }else{
    182               /**
    183                 * Fired when an error occurs...
    184                 *
    185                 * @property error
    186                 * @type String
    187                 */
    188               //TODO Add real Error Handler
    189               throw new Error(err);
    190             }
    191           })
    192         }
    193       });
    194     });
    195   }
    196 }