<commit_before>var redis = require('redis');
var log = require('logmagic').local('treslek.raxcm');
var sprintf = require('sprintf').sprintf;
var colors = require('irc-colors');

var config = require('./config.json');


var redisClient;

var STATE_COLORS = {
  OK: colors.green.bold,
  CRITICAL: colors.red,
  WARNING: colors.yellow
};

/**
 * Return a state string with irc colors.
 * @return {String} IRC-colored string.
 */
function getColoredState(state) {
  if (STATE_COLORS[state]) {
    state = STATE_COLORS[state](state);
  }

  return state;
}


/**
 * Rackspace Cloud Monitoring plugin
 */
var CloudMonitoring = function() {
  this.auto = ['listen'];
};

CloudMonitoring.prototype.listen = function(bot) {
  redisClient = redis.createClient(bot.redisConf.port, bot.redisConf.host);

  var pattern = [bot.getWebhookChannel(), 'raxcm/*'].join(':');

  redisClient.on("pmessage", function(pattern, channel, message) {
    var channelPath = channel.slice(bot.getWebhookChannel().length + 1).split('/')[1],
        ircChannels = config.paths[channelPath],
        body = {},
        headers = {},
        output = '';

    ircChannels = ircChannels instanceof Array ? ircChannels : [ircChannels];

    try {
      message = JSON.parse(message);
      headers = message.headers;
      body = JSON.parse(message.body);
    } catch(e) {
      log.error('Invalid JSON payload.', {payload: message});
      return;
    }

    if (headers['x-rackspace-webhook-token'] !== config.token) {
      log.error('Token mismatch. Ignoring message.', {token: token});
      return;
    }

    output = sprintf('%s %s %s - %s',
                     body.entity.label || body.entity.id,
                     getColoredState(body.details.state),
                     body.check.label || body.check.id,
                     body.details.status);

    ircChannels.forEach(function(ircChannel) {
      bot.say(ircChannel, output);
    });
  });
  redisClient.psubscribe(pattern);
};

exports.Plugin = CloudMonitoring;
<commit_msg>feature(templates): Add support for template strings for controlling the messages output. Also allow disabling IRC colors.<commit_after>var redis = require('redis');
var _ = require('underscore');
var log = require('logmagic').local('treslek.raxcm');
var sprintf = require('sprintf').sprintf;
var colors = require('irc-colors');

var config = require('./config.json');


var redisClient;

var STATE_COLORS = {
  OK: colors.green.bold,
  CRITICAL: colors.red,
  WARNING: colors.yellow
};

var templateString = '{{entity.label || entity.id}} - {{check.label || check.id}} - {{details.state}} - {{alarm.label || alarm.id}} - {{details.status}}';

if (config.templateString) {
  templateString = config.templateString;
}

var msgTemplate = _.template(templateString);

/**
 * Return a state string with irc colors.
 * @return {String} IRC-colored string.
 */
function getColoredState(state) {
  if (STATE_COLORS[state]) {
    state = STATE_COLORS[state](state);
  }

  return state;
}


/**
 * Rackspace Cloud Monitoring plugin
 */
var CloudMonitoring = function() {
  this.auto = ['listen'];
};

CloudMonitoring.prototype.listen = function(bot) {
  redisClient = redis.createClient(bot.redisConf.port, bot.redisConf.host);

  var pattern = [bot.getWebhookChannel(), 'raxcm/*'].join(':');

  redisClient.on("pmessage", function(pattern, channel, message) {
    var channelPath = channel.slice(bot.getWebhookChannel().length + 1).split('/')[1],
        ircChannels = config.paths[channelPath],
        body = {},
        headers = {},
        output = '';

    _.templateSettings = {
      interpolate: /\{\{(.+?)\}\}/g
    };

    ircChannels = ircChannels instanceof Array ? ircChannels : [ircChannels];

    try {
      message = JSON.parse(message);
      headers = message.headers;
      body = JSON.parse(message.body);
    } catch(e) {
      log.error('Invalid JSON payload.', {payload: message});
      return;
    }

    if (headers['x-rackspace-webhook-token'] !== config.token) {
      log.error('Token mismatch. Ignoring message.', {token: token});
      return;
    }

    if (!config.disableStateColors) {
      body.details.state = getColoredState(body.details.state);
    }

    output = msgTemplate(body);

    ircChannels.forEach(function(ircChannel) {
      bot.say(ircChannel, output);
    });
  });
  redisClient.psubscribe(pattern);
};

exports.Plugin = CloudMonitoring;

{'fraction_non_alphanumeric': 0.1572727272727273, 'fraction_numerical': 0.0009090909090909091, 'mean_word_length': 3.3062622309197653, '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': '3881274', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1348, 'n_words': 276}