diff lightstring.js @ 27:b6e4bc19ff5a

undoing
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 28 Jan 2012 01:27:00 +0100
parents 119ceb121908
children 630b9579fe4a
line wrap: on
line diff
--- a/lightstring.js
+++ b/lightstring.js
@@ -35,13 +35,13 @@ var Lightstring = {
     stream: {
       open: function(aService) {
         //FIXME no ending "/" - node-xmpp-bosh bug
-        return "<stream:stream to='" + aService + "'\
-                  xmlns='" + Lightstring.NS.jabberClient + "'\
-                  xmlns:stream='" + Lightstring.NS.stream + "'\
-                  version='1.0'/>";
+        return "<stream:stream to='" + aService + "'" +
+                             " xmlns='" + Lightstring.NS.jabberClient + "'" +
+                             " xmlns:stream='" + Lightstring.NS.stream + "'" +
+                             " version='1.0'/>";
       },
       close: function() {
-        return '</stream:stream>';
+        return "</stream:stream>";
       }
     }
   },
@@ -98,8 +98,8 @@ Lightstring.Connection = function(aServi
       //FIXME support SCRAM-SHA1 && allow specify method preferences
       if ('DIGEST-MD5' in mechanisms)
         that.send(
-          "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
-                 mechanism='DIGEST-MD5'/>"
+          "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'" +
+               " mechanism='DIGEST-MD5'/>"
         );
       else if ('PLAIN' in mechanisms) {
         var token = btoa(
@@ -110,8 +110,8 @@ Lightstring.Connection = function(aServi
           that.password
         );
         that.send(
-          "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
-                 mechanism='PLAIN'>" + token + '</auth>'
+          "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'" +
+               " mechanism='PLAIN'>" + token + "</auth>"
         );
       }
     }
@@ -119,29 +119,35 @@ Lightstring.Connection = function(aServi
     else {
       that.emit('features', stanza);
       //Bind http://xmpp.org/rfcs/rfc3920.html#bind
+      var bind =
+        "<iq type='set' xmlns='jabber:client'>" +
+          "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" +
+            (that.jid.resource? "<resource>" + that.jid.resource + "</resource>": "") +
+          "</bind>" +
+        "</iq>";
       that.send(
-        "<iq type='set' xmlns='jabber:client'>\
-          <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>\
-         </iq>",
-        function() {
-        //Session http://xmpp.org/rfcs/rfc3921.html#session
-        that.send(
-          "<iq type='set' xmlns='jabber:client'>\
-            <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>\
-          </iq>",
-          function() {
-            that.emit('connected');
-          }
-        );
-      });
+        bind,
+        function(stanza) {
+          //Session http://xmpp.org/rfcs/rfc3921.html#session
+          that.jid = new Lightstring.JID(stanza.textContent);
+          that.send(
+            "<iq type='set' xmlns='jabber:client'>" +
+              "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" +
+            "</iq>",
+            function() {
+              that.emit('connected');
+            }
+          );
+        }
+      );
     }
   });
   this.on('success', function(stanza, that) {
     that.send(
-      "<stream:stream to='" + that.host + "'\
-                      xmlns='jabber:client'\
-                      xmlns:stream='http://etherx.jabber.org/streams'\
-                      version='1.0' />"
+      "<stream:stream to='" + that.jid.domain + "'" +
+                    " xmlns='jabber:client'" +
+                    " xmlns:stream='http://etherx.jabber.org/streams'" +
+                    " version='1.0'/>"
     );
   });
   this.on('failure', function(stanza, that) {
@@ -185,17 +191,16 @@ Lightstring.Connection = function(aServi
       }
     }
 
-    var digest_uri = 'xmpp/' + that.host;
-    if (host !== null) {
+    var digest_uri = 'xmpp/' + that.jid.domain;
+    if (host !== null)
         digest_uri = digest_uri + '/' + host;
-    }
-    var A1 = MD5.hash(that.node +
+    var A1 = MD5.hash(that.jid.node +
                       ':' + realm + ':' + that.password) +
-        ':' + nonce + ':' + cnonce;
+                      ':' + nonce + ':' + cnonce;
     var A2 = 'AUTHENTICATE:' + digest_uri;
 
     var responseText = '';
-    responseText += 'username=' + _quote(that.node) + ',';
+    responseText += 'username=' + _quote(that.jid.node) + ',';
     responseText += 'realm=' + _quote(realm) + ',';
     responseText += 'nonce=' + _quote(nonce) + ',';
     responseText += 'cnonce=' + _quote(cnonce) + ',';
@@ -209,9 +214,9 @@ Lightstring.Connection = function(aServi
                       MD5.hexdigest(A2))) + ',';
     responseText += 'charset="utf-8"';
     that.send(
-      "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
-        + btoa(responseText) +
-      '</response>');
+      "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" +
+        btoa(responseText) +
+      "</response>");
   });
 };
 Lightstring.Connection.prototype = {
@@ -222,17 +227,11 @@ Lightstring.Connection.prototype = {
    */
   connect: function(aJid, aPassword) {
     this.emit('connecting');
-    if (aJid)
-      this.jid = aJid;
-    if (this.jid) {
-      this.host = this.jid.split('@')[1];
-      this.node = this.jid.split('@')[0];
-      this.resource = this.jid.split('/')[1];
-    }
+    this.jid = new Lightstring.JID(aJid);
     if (aPassword)
       this.password = aPassword;
 
-    if (!this.jid)
+    if (!this.jid.bare)
       throw 'Lightstring: Connection.jid is undefined.';
     if (!this.password)
       throw 'Lightstring: Connection.password is undefined.';
@@ -252,7 +251,7 @@ Lightstring.Connection.prototype = {
       if (this.protocol !== 'xmpp')
         console.error('Lightstring: The server located at '+ that.service + ' doesn\'t seems to be XMPP aware.');
 
-      var stream = Lightstring.stanza.stream.open(that.host);
+      var stream = Lightstring.stanza.stream.open(that.jid.domain);
 
       that.socket.send(stream);
       that.emit('XMLOutput', stream);
@@ -270,8 +269,17 @@ Lightstring.Connection.prototype = {
       that.emit('DOMInput', elm);
       that.emit(elm.tagName, elm);
 
+<<<<<<< HEAD
       if (elm.tagName === 'iq')
         that.emit(elm.getAttribute('id'), elm);
+=======
+      if (elm.tagName === 'iq') {
+        var payload = elm.firstChild;
+        if (payload)
+          that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, elm);
+        that.emit(elm.getAttribute('id'), elm); //FIXME: possible attack vector.
+      }
+>>>>>>> f6a7c0f93d154c2cd34dfdda4ab8eec808b91b34
     });
   },
   /**
@@ -303,9 +311,8 @@ Lightstring.Connection.prototype = {
       if (aCallback)
         this.on(elm.getAttribute('id'), aCallback);
     }
-    else if (aCallback) {
+    else if (aCallback)
       this.emit('warning', 'Callback can\'t be called with non-iq stanza.');
-    }
 
 
     this.socket.send(str);