<commit_before>var util = require('util');
var events = require('events');
var xmpp = require('node-xmpp');
var bind = require('underscore').bind;

var Bot;

module.exports.Bot = (function() {
  function Bot(configs) {
    events.EventEmitter.call(this);
    this.once('connect', function() {}); // listener bug
    this.setMaxListeners(0);

    this.jabber = null;
    this.keepalive = null;

    this.plugins = {};

    configs = configs || {};

    this.jid = configs.jid;
    this.password = <PASSWORD>.password;
    this.debug = configs.debug;
    this.name = configs.name;
  };

  util.inherits(Bot, events.EventEmitter);

  Bot.prototype.connect = function() {
    var self = this;

    this.jabber = new xmpp.Client({
      jid: this.jid,
      password: <PASSWORD>
    });

    if (this.debug === true) {
      this.jabber.on('data', function(buffer) {
        console.log('  IN > ' + buffer.toString());
      });

      var origSend = this.jabber.send;
      this.jabber.send = function(stanza) {
        console.log(' OUT > ' + stanza);
        return origSend.call(self.jabber, stanza);
      };
    }

    this.jabber.on('error', function(error) {
      if (error instanceof xmpp.Element) {
        var errorMessage = 'Unknown error';

        // Check of see-other-host error
        if (error.getChild('see-other-host')) {
          errorMessage = 'This account is signing in somewhere else. Maybe HipChat web version?';
        } else {
          // Handle XMPP text errors (when available)
          var textNode = error.getChild('text');

          if (textNode) {
            errorMessage = textNode.getText();
          }
        }

        self.emit('error', errorMessage, error);
      } else {
        self.emit('error', error);
      }

      self.disconnect();
    });

    this.jabber.on('online', function() {
      // Send online presence
      self.setAvailability('chat');

      // Server will disconnect us after 150s of inactivity
      self.keepalive = setInterval(function() {
        self.jabber.send(' ');
        self.emit('pong');
      }, 30000);

      self.emit('connect');
    });

    this.jabber.on('stanza', bind(this._onStanza, this));
  };

  Bot.prototype._onStanza = function(stanza) {
    this.emit('data', stanza);

    if (stanza.is('message') && stanza.attrs.type == 'groupchat') {
      var body = stanza.getChild('body');
      if (!body) return;

      if (stanza.getChild('delay')) return;

      var attrFrom = stanza.attrs.from;
      var offset = attrFrom.indexOf('/');

      var channel = attrFrom.substring(0, offset);
      var from = attrFrom.substring(offset + 1);

      this.emit('message', channel, from, body.getText());
    } else if (stanza.is('message') && stanza.attrs.type == 'chat') {
      var body = stanza.getChild('body');
      if (!body) return;

      var attrFrom = stanza.attrs.from;
      var offset = attrFrom.indexOf('/');

      var jid = attrFrom.substring(0, offset);

      this.emit('pm', jid, body.getText());
    }
  };

  Bot.prototype.setAvailability = function(availability, status) {<commit_msg>Do not emit message event when sent from self - closes #2<commit_after>var util = require('util');
var events = require('events');
var xmpp = require('node-xmpp');
var bind = require('underscore').bind;

var Bot;

module.exports.Bot = (function() {
  function Bot(configs) {
    events.EventEmitter.call(this);
    this.once('connect', function() {}); // listener bug
    this.setMaxListeners(0);

    this.jabber = null;
    this.keepalive = null;

    this.plugins = {};

    configs = configs || {};

    this.jid = configs.jid;
    this.password = <PASSWORD>;
    this.debug = configs.debug;
    this.name = configs.name;
  };

  util.inherits(Bot, events.EventEmitter);

  Bot.prototype.connect = function() {
    var self = this;

    this.jabber = new xmpp.Client({
      jid: this.jid,
      password: <PASSWORD>
    });

    if (this.debug === true) {
      this.jabber.on('data', function(buffer) {
        console.log('  IN > ' + buffer.toString());
      });

      var origSend = this.jabber.send;
      this.jabber.send = function(stanza) {
        console.log(' OUT > ' + stanza);
        return origSend.call(self.jabber, stanza);
      };
    }

    this.jabber.on('error', function(error) {
      if (error instanceof xmpp.Element) {
        var errorMessage = 'Unknown error';

        // Check of see-other-host error
        if (error.getChild('see-other-host')) {
          errorMessage = 'This account is signing in somewhere else. Maybe HipChat web version?';
        } else {
          // Handle XMPP text errors (when available)
          var textNode = error.getChild('text');

          if (textNode) {
            errorMessage = textNode.getText();
          }
        }

        self.emit('error', errorMessage, error);
      } else {
        self.emit('error', error);
      }

      self.disconnect();
    });

    this.jabber.on('online', function() {
      // Send online presence
      self.setAvailability('chat');

      // Server will disconnect us after 150s of inactivity
      self.keepalive = setInterval(function() {
        self.jabber.send(' ');
        self.emit('pong');
      }, 30000);

      self.emit('connect');
    });

    this.jabber.on('stanza', bind(this._onStanza, this));
  };

  Bot.prototype._onStanza = function(stanza) {
    this.emit('data', stanza);

    if (stanza.is('message') && stanza.attrs.type == 'groupchat') {
      var body = stanza.getChild('body');
      if (!body) return;

      if (stanza.getChild('delay')) return;

      var attrFrom = stanza.attrs.from;
      var offset = attrFrom.indexOf('/');

      var channel = attrFrom.substring(0, offset);
      var from = attrFrom.substring(offset + 1);

      // Do not trigger when message is from self.
      if (from == this.name) return;

      this.emit('message', channel, from, body.getText());
    } else if (stanza.is('message') && stanza.attrs.type == 'chat') {
      var body = stanza.getChild('body');
      if (!body) return;

      var attrFrom = stanza.attrs.from;
      var offset = attrFrom.indexOf('/');

      var jid = attrFrom.substring(0, offset);

      this.emit('pm', jid, body.getText());
    }
  };

  Bot.prototype.setAvailability = function(availability, status) {
{'fraction_non_alphanumeric': 0.1551614946168461, 'fraction_numerical': 0.003958201393286891, 'mean_word_length': 2.468973091707853, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1909358', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1934, 'n_words': 434}