changeset 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 bdfbd58b4835
children 6773e5bc2ca0
files plugins/feature-not-implemented.js
diffstat 1 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/plugins/feature-not-implemented.js
@@ -0,0 +1,51 @@
+'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>");
+  });
+};