changeset 3:029c12b8f048

various bug fixes and improvements
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 14 Jan 2012 17:36:10 +0100
parents f31a75c3b6c8
children 6b95d15e303c
files lightstring.js plugins.js
diffstat 2 files changed, 48 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/lightstring.js
+++ b/lightstring.js
@@ -38,6 +38,7 @@ Lightstring.Connection = function (aServ
     return serializer.serializeToString(elm);
   };
   this.connect = function(aJid, aPassword) {
+    this.emit('connecting');
     if(aJid)
       this.jid = aJid;
     if(this.jid) {
@@ -55,24 +56,23 @@ Lightstring.Connection = function (aServ
     if(!this.service)
       throw "Lightstring: Connection.service is undefined.";
 
-    //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635   
-    if(MozWebSocket)
+    //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635
+    try {
+      this.socket = new WebSocket(this.service);
+    }
+    catch(error) {
       this.socket = new MozWebSocket(this.service);
-    else if(WebSocket)
-      this.socket = new WebSocket(this.service);
-    else
-      this.emit('error', 'No WebSocket support.');
+    }
 
     var that = this;
     this.socket.addEventListener('open', function() {
-      that.emit('connecting');
-      //FIXME there shouldn't be an ending "/"
-      that.send(
+      var stream =
         "<stream:stream to='"+that.domain+"'\
                         xmlns='jabber:client'\
                         xmlns:stream='http://etherx.jabber.org/streams'\
-                        version='1.0'/>"
-      );
+                        version='1.0'/>";
+      that.socket.send(stream)
+      that.emit('XMLOutput', stream);
     });
     this.socket.addEventListener('error', function(e) {
       that.emit('error', e.data);
@@ -126,7 +126,6 @@ Lightstring.Connection = function (aServ
 		this.emit('disconnecting');
 		this.send('</stream:stream>');
 		this.socket.close();
-    this.emit('disconnected');
 	};
   this.emit = function(name, data) {
     var handlers = this.handlers[name];
@@ -213,6 +212,10 @@ Lightstring.Connection = function (aServ
     );
   });
   //Internal
+  this.on('failure', function(stanza, that) {
+    that.emit('conn-error', stanza.firstChild.tagName);
+  });
+  //Internal
   this.on('challenge', function(stanza, that) {
     //FIXME this is mostly Strophe code
     
--- a/plugins.js
+++ b/plugins.js
@@ -16,11 +16,17 @@
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
+NodeList.prototype.forEach = function(aCallback) {
+	for(var i = 0; i<this.length; i++)
+		aCallback(this[i]);
+}
+
+
 //
 //Roster
 //
-Lighstring.NS.roster = 'jabber:iq:roster';
-Lighstring.stanza.roster = {
+Lightstring.NS.roster = 'jabber:iq:roster';
+Lightstring.stanza.roster = {
 	'get': function() {
 		return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>";
 	},
@@ -37,7 +43,7 @@ Lighstring.stanza.roster = {
 		return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree();
 	}
 };
-Lighstring.getRoster = function(connection, aCallback) {
+Lightstring.getRoster = function(connection, aCallback) {
 	connection.send(this.stanza.roster.get(), function(answer){
 		var contacts = [];
 		answer.querySelectorAll('item').forEach(function(item) {
@@ -67,8 +73,8 @@ Lighstring.getRoster = function(connecti
 //
 //vCard
 //
-Lighstring.NS.vcard = 'vcard-temp';
-Lighstring.stanza.vcard = {
+Lightstring.NS.vcard = 'vcard-temp';
+Lightstring.stanza.vcard = {
 	'get': function(aTo) {
 		if(aTo)
 			return "<iq type='get' to='"+aTo+"'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>";
@@ -77,7 +83,7 @@ Lighstring.stanza.vcard = {
 	}
 };
 //FIXME we should return a proper vcard, not an XMPP one
-Lighstring.getVcard = function(aConnection, aTo, aCallback) {
+Lightstring.getVcard = function(aConnection, aTo, aCallback) {
 	aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){
 		if(answer) {
 			var vcard = answer.querySelector('vCard');
@@ -91,9 +97,9 @@ Lighstring.getVcard = function(aConnecti
 //
 //Disco
 //
-Lighstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info";
-Lighstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items";
-Lighstring.stanza.disco = {
+Lightstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info";
+Lightstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items";
+Lightstring.stanza.disco = {
 	items: function(aTo, aNode) {
 		if(aTo)
 			var iq = "<iq type='get' to='"+aTo+"'>";
@@ -120,7 +126,7 @@ Lighstring.stanza.disco = {
 		return iq+query+"</iq>";
 	}
 };
-Lighstring.discoItems = function(aConnection, aTo, aCallback) {
+Lightstring.discoItems = function(aConnection, aTo, aCallback) {
 	aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){
 		var items = [];
 		answer.querySelectorAll('item').forEach(function(node) {
@@ -135,7 +141,7 @@ Lighstring.discoItems = function(aConnec
 			aCallback(items);
 	});
 };
-Lighstring.discoInfo = function(aConnection, aTo, aNode, aCallback) {
+Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) {
 	aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){
 		var field = answer.querySelector('field[var="pubsub#creator"] > value');
 		var creator = field ? field.textContent : '';
@@ -146,10 +152,10 @@ Lighstring.discoInfo = function(aConnect
 //
 //PubSub
 //
-Lighstring.NS.x = "jabber:x:data";
-Lighstring.NS.pubsub = "http://jabber.org/protocol/pubsub";
-Lighstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
-Lighstring.stanza.pubsub = {
+Lightstring.NS.x = "jabber:x:data";
+Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub";
+Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
+Lightstring.stanza.pubsub = {
 	getConfig: function(aTo, aNode) {
 		return  "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>";
 	},
@@ -189,7 +195,7 @@ Lighstring.stanza.pubsub = {
 		return iq;
 	},
 };
-Lighstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) {
+Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){
 		var items = [];
 		answer.querySelectorAll('item').forEach(function(node) {
@@ -199,16 +205,16 @@ Lighstring.pubsubItems = function(aConne
 				src: node.querySelector('content').getAttribute('src'),
 				type: node.querySelector('content').getAttribute('type'),
 			}
-			var thumbnail = node.querySelector('link');
-			if(thumbnail)
-				item.thumbnail = thumbnail.getAttribute('href');
+			var miniature = node.querySelector('link');
+			if(miniature)
+				item.miniature = miniature.getAttribute('href');
 			items.push(item);
 		})
 		if(aCallback)
 			aCallback(items);
 	});
 }
-Lighstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) {
+Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) {
 		if(answer.getAttribute('type') === 'result')
 			aCallback(null, answer);
@@ -216,7 +222,7 @@ Lighstring.pubsubCreate = function(aConn
 			aCallback(answer, null);
 	});
 };
-Lighstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) {
+Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){
 		var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent;
 		if(accessmodel)
@@ -225,13 +231,13 @@ Lighstring.pubsubConfig = function(aConn
 			aCallback(null);
 	});
 }
-Lighstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) {
+Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){
 		if(aCallback)
 			aCallback(answer);
 	});
 }
-Lighstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) {
+Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){
 		if(answer.getAttribute('type') === 'result')
 			aCallback(null, answer);
@@ -239,13 +245,13 @@ Lighstring.pubsubPublish = function(aCon
 			aCallback(answer, null);
 	});
 }
-Lighstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) {
+Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){
 		if(aCallback)
 			aCallback(answer);
 	});
 }
-Lighstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) {
+Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) {
 		if((answer.getAttribute('type') === 'result') && aCallback) {
 			var affiliations = {};
@@ -256,13 +262,13 @@ Lighstring.pubsubGetAffiliations = funct
 		}
 	});
 };
-Lighstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) {
+Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) {
 	aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations));
 };
 //
 //IM
 //
-Lighstring.stanza.message = {
+Lightstring.stanza.message = {
 	normal: function(aTo, aSubject, aText) {
 		return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>";
 	},