<commit_before>var irc = require('irc');
var fs = require('fs');
var path = require('path');
var async = require('async');
var redis = require('redis');
var sprintf = require('sprintf').sprintf;
var _ = require('underscore');

var Sandbox = require('./sandbox').Sandbox;
var AdminSandbox = require('./sandbox').AdminSandbox;
var WebhookServer = require('./webhook').WebhookServer;

var log = require('logmagic').local('treslek.lib.treslek');

/*
 * TreslekBot. Where the magic happens.
 * {Obj} Config object.
 */
var TreslekBot = function(conf) {
  this.conf = conf;
  this.plugins = {};
  this.pluginInstances = {};
  this.adminCommands = {};
  this.registeredCommands = {};
  this.registeredHooks = {};
  this.commandUsage = {};
  this.redis = redis.createClient(this.conf.redis.port, this.conf.redis.host);
};


/*
 * Start the bot. Connect to irc, connect listeners, and load plugins.
 */
TreslekBot.prototype.start = function() {
  var that = this;

  if (this.conf.webhook && this.conf.webhook.host && this.conf.webhook.port && this.conf.webhook.channelKey) {
    this.webhookServer = new WebhookServer(this.conf);
    this.webhookServer.start();
  }

  this.irc = new irc.Client(this.conf.host, this.conf.nick, this.conf.ircOptions);

  this.irc.treslekProcessMessage = function(from, to, text, msg) {
    log.debug('processing message!', {from: from, to: to});
    if (from !== that.conf.nick && that.conf.ignored.indexOf(from) === -1) {
      async.parallel({
        log: function(callback) {
          that.logMessage(from, to, text, msg);
          callback();
        },
        commands: function(callback) {
          that.executeCommands(from, to, text, msg);
          callback();
        },
        hooks: function(callback) {
          that.executeHooks(from, to, text, msg);
          callback();
        },
        adminCommands: function(callback) {
          that.executeAdminCommands(from, to, text, msg);
          callback();
        }
      });
    }
  };

  this.irc.connect(1, function() {
    log.info('Connected to irc server.', {host: that.conf.host, options: that.conf.ircOptions});
  });

  this.irc.addListener('error', function(err) {
    log.error('Error', {err: err});
  });

  this.irc.addListener('topic', function(channel, topic, nick, msg) {
    var topicStore = sprintf('%s:topic:%s', that.conf.redis.prefix, channel);
    that.redis.set(topicStore, topic);
  });

  this.irc.addListener('message#', this.irc.treslekProcessMessage);<commit_msg>fix(error handling): If we recieve an error from the IRC network, end the process and restart.<commit_after>var irc = require('irc');
var fs = require('fs');
var path = require('path');
var async = require('async');
var redis = require('redis');
var sprintf = require('sprintf').sprintf;
var _ = require('underscore');

var Sandbox = require('./sandbox').Sandbox;
var AdminSandbox = require('./sandbox').AdminSandbox;
var WebhookServer = require('./webhook').WebhookServer;

var log = require('logmagic').local('treslek.lib.treslek');

/*
 * TreslekBot. Where the magic happens.
 * {Obj} Config object.
 */
var TreslekBot = function(conf) {
  this.conf = conf;
  this.plugins = {};
  this.pluginInstances = {};
  this.adminCommands = {};
  this.registeredCommands = {};
  this.registeredHooks = {};
  this.commandUsage = {};
  this.redis = redis.createClient(this.conf.redis.port, this.conf.redis.host);
};


/*
 * Start the bot. Connect to irc, connect listeners, and load plugins.
 */
TreslekBot.prototype.start = function() {
  var that = this;

  if (this.conf.webhook && this.conf.webhook.host && this.conf.webhook.port && this.conf.webhook.channelKey) {
    this.webhookServer = new WebhookServer(this.conf);
    this.webhookServer.start();
  }

  this.irc = new irc.Client(this.conf.host, this.conf.nick, this.conf.ircOptions);

  this.irc.treslekProcessMessage = function(from, to, text, msg) {
    log.debug('processing message!', {from: from, to: to});
    if (from !== that.conf.nick && that.conf.ignored.indexOf(from) === -1) {
      async.parallel({
        log: function(callback) {
          that.logMessage(from, to, text, msg);
          callback();
        },
        commands: function(callback) {
          that.executeCommands(from, to, text, msg);
          callback();
        },
        hooks: function(callback) {
          that.executeHooks(from, to, text, msg);
          callback();
        },
        adminCommands: function(callback) {
          that.executeAdminCommands(from, to, text, msg);
          callback();
        }
      });
    }
  };

  this.irc.connect(1, function() {
    log.info('Connected to irc server.', {host: that.conf.host, options: that.conf.ircOptions});
  });

  this.irc.addListener('error', function(err) {
    log.error('Error', {err: err});
    process.exit(1);
  });

  this.irc.addListener('topic', function(channel, topic, nick, msg) {
    var topicStore = sprintf('%s:topic:%s', that.conf.redis.prefix, channel);
    that.redis.set(topicStore, topic);
  });

  this.irc.addListener('message#', this.irc.treslekProcessMessage);
{'fraction_non_alphanumeric': 0.16758566364710517, 'fraction_numerical': 0.0009846396218983853, 'mean_word_length': 3.322553191489362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '711751', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1630, 'n_words': 331}