lib/bot.js
function(stanza) {
    this.emit('data', stanza);

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

      // Ignore chat history
      if (stanza.getChild('delay')) return;

      var fromJid = new xmpp.JID(stanza.attrs.from);
      var fromChannel = fromJid.bare().toString();
      var fromNick = fromJid.resource;

      // Ignore our own messages
      if (fromNick === this.name) return;

      this.emit('message', fromChannel, fromNick, body);
    } else if (stanza.is('message') && stanza.attrs.type === 'chat') {
      // Message without body is probably a typing notification
      var body = stanza.getChildText('body');
      if (!body) return;

      var fromJid = new xmpp.JID(stanza.attrs.from);

      this.emit('privateMessage', fromJid.bare().toString(), body);
    } else if (stanza.is('message') && !stanza.attrs.type) {
      // TODO: It'd be great if we could have some sort of xpath-based listener
      // so we could just watch for '/message/x/invite' stanzas instead of
      // doing all this manual getChild nonsense.
      var x = stanza.getChild('x', 'http://jabber.org/protocol/muc#user');
      if (!x) return;
      var invite = x.getChild('invite');
      if (!invite) return;
      var reason = invite.getChildText('reason');

      var inviteRoom = new xmpp.JID(stanza.attrs.from);
      var inviteSender = new xmpp.JID(invite.attrs.from);

      this.emit('invite', inviteRoom.bare(), inviteSender.bare(), reason);
    } else if (stanza.is('iq')) {
      // Handle a response to an IQ request
      var event_id = 'iq:' + stanza.attrs.id;
      if (stanza.attrs.type === 'result') {
        this.emit(event_id, null, stanza);
      } else {
        // IQ error response
        // ex: http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-result
        var condition = 'unknown';
        var error_elem = stanza.getChild('error');
        if (error_elem) {
          condition = error_elem.children[0].name;
        }
        this.emit(event_id, condition, stanza);
      }
    }
  }
function(stanza) {
    this.emit('data', stanza);

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

      // Ignore chat history
      if (stanza.getChild('delay')) return;

      var fromJid = new xmpp.JID(stanza.attrs.from);
      var fromChannel = fromJid.bare().toString();
      var fromNick = fromJid.resource;

      // Ignore our own messages
      if (fromNick === this.name) return;

      this.emit('message', fromChannel, fromNick, body);
    } else if (stanza.is('message') && stanza.attrs.type === 'chat') {
      // Message without body is probably a typing notification
      var body = stanza.getChildText('body');
      if (!body) return;

      var fromJid = new xmpp.JID(stanza.attrs.from);

      this.emit('privateMessage', fromJid.bare().toString(), body);
    } else if (stanza.is('message') && !stanza.attrs.type) {
      // TODO: It'd be great if we could have some sort of xpath-based listener
      // so we could just watch for '/message/x/invite' stanzas instead of
      // doing all this manual getChild nonsense.
      var x = stanza.getChild('x', 'http://jabber.org/protocol/muc#user');
      if (!x) return;
      var invite = x.getChild('invite');
      if (!invite) return;
      var reason = invite.getChildText('reason');

      var inviteRoom = new xmpp.JID(stanza.attrs.from);
      var inviteSender = new xmpp.JID(invite.attrs.from);

      this.emit('invite', inviteRoom.bare(), inviteSender.bare(), reason);
    } else if (stanza.is('iq')) {
      // Handle a response to an IQ request
      var event_id = 'iq:' + stanza.attrs.id;
      if (stanza.attrs.type === 'result') {
        this.emit(event_id, null, stanza);
      } else {
        // IQ error response
        // ex: http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-result
        var condition = 'unknown';
        var error_elem = stanza.getChild('error');
        if (error_elem) {
          condition = error_elem.children[0].name;
        }
        this.emit(event_id, condition, stanza);
      }
    }
  }
function ( stanza ) { this . emit ( 'data' , stanza ) ; if ( stanza . is ( 'message' ) && stanza . attrs . type === 'groupchat' ) { var body = stanza . getChildText ( 'body' ) ; if ( ! body ) return ; // Ignore chat history if ( stanza . getChild ( 'delay' ) ) return ; var fromJid = new xmpp . JID ( stanza . attrs . from ) ; var fromChannel = fromJid . bare ( ) . toString ( ) ; var fromNick = fromJid . resource ; // Ignore our own messages if ( fromNick === this . name ) return ; this . emit ( 'message' , fromChannel , fromNick , body ) ; } else if ( stanza . is ( 'message' ) && stanza . attrs . type === 'chat' ) { // Message without body is probably a typing notification var body = stanza . getChildText ( 'body' ) ; if ( ! body ) return ; var fromJid = new xmpp . JID ( stanza . attrs . from ) ; this . emit ( 'privateMessage' , fromJid . bare ( ) . toString ( ) , body ) ; } else if ( stanza . is ( 'message' ) && ! stanza . attrs . type ) { // TODO: It'd be great if we could have some sort of xpath-based listener // so we could just watch for '/message/x/invite' stanzas instead of // doing all this manual getChild nonsense. var x = stanza . getChild ( 'x' , 'http://jabber.org/protocol/muc#user' ) ; if ( ! x ) return ; var invite = x . getChild ( 'invite' ) ; if ( ! invite ) return ; var reason = invite . getChildText ( 'reason' ) ; var inviteRoom = new xmpp . JID ( stanza . attrs . from ) ; var inviteSender = new xmpp . JID ( invite . attrs . from ) ; this . emit ( 'invite' , inviteRoom . bare ( ) , inviteSender . bare ( ) , reason ) ; } else if ( stanza . is ( 'iq' ) ) { // Handle a response to an IQ request var event_id = 'iq:' + stanza . attrs . id ; if ( stanza . attrs . type === 'result' ) { this . emit ( event_id , null , stanza ) ; } else { // IQ error response // ex: http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-result var condition = 'unknown' ; var error_elem = stanza . getChild ( 'error' ) ; if ( error_elem ) { condition = error_elem . children [ 0 ] . name ; } this . emit ( event_id , condition , stanza ) ; } } }
This function is responsible for handling incoming XMPP messages. The `data` event will be triggered with the message for custom XMPP handling.  The bot will parse the message and trigger the `message` event when it is a group chat message or the `privateMessage` event when it is a private message.
This function is responsible for handling incoming XMPP messages . The data event will be triggered with the message for custom XMPP handling . The bot will parse the message and trigger the message event when it is a group chat message or the privateMessage event when it is a private message .