Ejemplo n.º 1
0
ConnectionRouter.prototype.send = function (stanza) {
  var sent = false
  try {
    // logger.debug('deliver:' + stanza.root().toString());
    var self = this;

    if (stanza.attrs && stanza.attrs.to) {
      var toJid = new JID(stanza.attrs.to);

      // send to all local clients, check if jid is there
      if (self.sessions.hasOwnProperty(toJid.bare().toString())) {
        // Now loop over all the sesssions and only send to the right jid(s)
        var resource;
        for (resource in self.sessions[toJid.bare().toString()]) {
          if (toJid.bare().toString() === toJid.toString() || toJid.resource === resource) {
            logger.debug('send message to resource: ' + resource);
            self.sessions[toJid.bare().toString()][resource].send(stanza);
            sent = true;
          }
        }

        // we couldn't send the stanza
        if (!sent) {
          logger.error(stanza.root().toString() + ' could not be delivered');
        }
      } else {
        logger.warn('could not deliver: ' + stanza.toString());
      }
    }
  } catch (err) {
    logger.error(err.stack);
  }

  return sent;
};
Ejemplo n.º 2
0
Xmpp.prototype._handlePresenceSubscription = function(stanza) {
    var to = new JID(stanza.attrs.from)
    var outgoing = new ltx.Element(
        'presence',
        { to: to.bare().toString(), type: 'subscribed' }
    )
    this.client.send(outgoing)
}    
Ejemplo n.º 3
0
ConnectionRouter.prototype.match = function (stanza) {
  var match = false;

  if (stanza.attrs && stanza.attrs.to) {
    var toJid = new JID(stanza.attrs.to);

    // send to all local clients, check if jid is there
    if (this.sessions.hasOwnProperty(toJid.bare().toString())) {
      match = true;
    }
  }

  return match;
};
Ejemplo n.º 4
0
XComponent.prototype.verifyDomain = function (stanza)  {

  var jid = new JID(stanza.attrs.to);
  var xmppdomain = this.getDomain();

  logger.debug('check for domain: ' + xmppdomain);

  // check that the domain fits
  if (jid.getDomain().toString().localeCompare(xmppdomain) !== 0) {
    logger.debug('stanza does not match domain');
    return false;
  } else {
    return true;
  }
};
Ejemplo n.º 5
0
User.prototype.sendMessageToStream = function( query ) {
    var item = query;

    if( query.getChild( "body" ) ) {
        var message = query.getChild( "body" ).getText();
        var from = new JID( query.attrs.from.toString() );

        item = new Element( 'message', { to: this.jid.getUser() + "@" + this.jid.getDomain(), // Strip out resource
                                              from: from.getUser() + "@" + from.getDomain(),
                                              type: "chat",
                                              id: "JabberServer" } )
                       .c("body").t( message );
    }
    this.onRecieveMessage( item );
    this.sendElementToStream( item );
};
Ejemplo n.º 6
0
Presence.prototype.handle = function (stanza) {
  var handled = false;
  var to = new JID(stanza.attrs.to);

  if (stanza.is('presence')) {
    // handle presence request for specific room
    if (to.getDomain().toString().localeCompare(this.getDomain) !== 0) {
      handled = this.handleOccupantPresence(stanza);
    }

    // TODO handle normal presence request
    // 1. check if user is already offline
    // 2. make user offline in all active rooms
  }

  return handled;
};
Ejemplo n.º 7
0
ConnectionRouter.prototype.connectedClientsForJid = function (jid) {
  try {
    jid = new JID(jid);
    if (!this.sessions.hasOwnProperty(jid.bare().toString())) {
      return [];
    } else {
      var jids = [];
      var resources = this.sessions[jid.bare().toString()];
      for (var resource in resources) {
        if (resources.hasOwnProperty(resource)) {
          jids.push(new JID(jid.bare().toString() + '/' + resource));
        }
      }
      return jids;
    }
  } catch (err) {
    logger.error(err.stack);
    return [];
  }
};
Ejemplo n.º 8
0
Xmpp.prototype._handleChat = function(stanza) {
    var self = this
      , from = new JID(stanza.attrs.from)
      , body = stanza.getChildText('body') || ''
      , type = 'chat'

    /* Probably a chat state notification */
    if (!stanza.getChild('body')) return

    if (true === this._isChatRoom(from)) {
        var regex = new RegExp(
            '^' + this.config.xmpp.muc.nick + ': (.*)',
            'i'
        )
        var match = body.match(regex)
        if (!match) return /* Message is not for us */
        body = match[1]
        if ('groupchat' === stanza.attrs.type) {
            type = stanza.attrs.type
            from = from.bare()
        }
        if (this.config.xmpp.muc.roles) {
            var userRole = this._roomMembers[stanza.attrs.from] || ''
            if (-1 === this.config.xmpp.muc.roles.indexOf(userRole))
                return
        }
    } else if (false === this._checkIsAllowed(from, stanza)) {
        return
    }
    this.commander.handle(body, function(message) {
        if (!message) return /* No response expected */
        var attrs = stanza.root().attrs
        attrs.type = type
        delete attrs.from
        attrs.to = from
        var response = new ltx.Element('message', attrs)
        response.c('body').t(message)
        self.client.send(response)
    })
}