view plugins/feature-not-implemented.js @ 36:b43ca01b9f6f

Add a plugin that respond an error to each get/set iq not handled.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 28 Jan 2012 06:01:58 +0100
parents
children ea276b47c555
line wrap: on
line source

'use strict';

/**
  Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

Lightstring.NS.xmpp_stanzas = 'urn:ietf:params:xml:ns:xmpp-stanzas';

function register_feature_not_implemented() {
  var conn = this;
  conn.on('iq', function callback(stanza) {
    var type = stanza.DOM.getAttributeNS(null, 'type');
    if (type !== 'get' && type !== 'set')
      return;

    var handlers = conn.handlers;
    var events = [handlers['iq']];

    var payload = stanza.DOM.firstChild;
    if (payload)
      events.push('iq/' + payload.namespaceURI + ':' + payload.localName);

    var only = events.every(function(handler) {
      for (var i in handler)
        if (callback !== handler[i])
          return false;
      return true;
    });

    if (only)
      conn.send("<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
                   " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
                   " type='error'>" +
                  "<error type='cancel'>" +
                    "<feature-not-implemented xmlns='" + Lightstring.NS.xmpp_stanzas + "'/>" +
                  "</error>" +
                "</iq>");
  });
};