diff lightstring.js @ 62:b1e75cdbb0ad

Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 01 Feb 2012 18:37:57 +0100
parents 0e86fca6a596
children 20da4fb67977
line wrap: on
line diff
--- a/lightstring.js
+++ b/lightstring.js
@@ -44,6 +44,18 @@ var Lightstring = {
       close: function() {
         return "</stream:stream>";
       }
+    },
+    errors: {
+      iq: function(type, error) {
+        return "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
+                  " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
+                  " type='error'>" +
+                 "<error type='" + type + "'>" +
+                   "<" + error + " xmlns='" + Lightstring.namespaces['xmpp_stanzas'] + "'/>" + //TODO: allow text content.
+                    //TODO: allow text and payload.
+                 "</error>" +
+               "</iq>";
+      }
     }
   },
   plugins: {},
@@ -429,8 +441,24 @@ Lightstring.Connection.prototype = {
     if (!handlers)
       return;
 
-    for (var i = 0; i < handlers.length; i++)
-      handlers[i].call(this, aData);
+    if (aData.localName !== 'iq') {
+      for (var i = 0; i < handlers.length; i++)
+        handlers[i].call(this, aData);
+
+      return;
+    }
+
+    var ret;
+    for (var i = 0; i < handlers.length; i++) {
+      ret = handlers[i].call(this, aData);
+      if (typeof ret !== 'boolean')
+        return; //TODO: emit a big error!
+
+      if (ret)
+        return;
+    }
+
+    conn.send(Lightstring.stanzas.errors.iq('cancel', 'service-unavailable'));
   },
   /**
    * @function Register an event handler.