changeset 34:6ce66fba0242

Merging.
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 28 Jan 2012 04:39:03 +0100
parents 1e6d2ca2daae (current diff) 88d24231bf24 (diff)
children bdfbd58b4835
files lightstring.js plugins.js
diffstat 7 files changed, 308 insertions(+), 185 deletions(-) [+]
line wrap: on
line diff
--- a/lightstring.js
+++ b/lightstring.js
@@ -86,7 +86,20 @@ var Lightstring = {
     finally {
       return XML;
     };
-  }
+  },
+  /**
+   * @function Get an unique identifier.
+   * @param {String} [aString] Prefix to put before the identifier.
+   * @return {String} Identifier.
+   */
+  newId: (function() {
+    var id = 1024;
+    return function(prefix) {
+      if (typeof prefix === 'string')
+        return prefix + id++;
+      return '' + id++;
+    };
+  })()
 };
 
 /**
@@ -98,11 +111,6 @@ Lightstring.Connection = function(aServi
   if (aService)
     this.service = aService;
   this.handlers = {};
-  this.iqid = 1024;
-  this.getNewId = function() {
-    this.iqid++;
-    return 'sendiq:' + this.iqid;
-  };
   this.on('stream:features', function(stanza, that) {
     var nodes = stanza.DOM.querySelectorAll('mechanism');
     //SASL/Auth features
@@ -321,8 +329,7 @@ Lightstring.Connection.prototype = {
       var id = stanza.DOM.getAttribute('id');
       //TODO: This should be done by a plugin
       if (!id) {
-        alert(Lightstring.DOM2XML(stanza.DOM));
-        stanza.DOM.setAttribute('id', this.getNewId());
+        stanza.DOM.setAttribute('id', Lightstring.newId('sendiq:'));
       }
       if (aCallback)
         this.on(stanza.DOM.getAttribute('id'), aCallback);
new file mode 100644
--- /dev/null
+++ b/plugins/disco.js
@@ -0,0 +1,96 @@
+'use strict';
+
+/**
+  Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
+
+  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.
+*/
+
+/////////
+//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+"'>";
+    else
+      var iq = "<iq type='get'>";
+    
+    if(aNode)
+      var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>";
+    else
+      var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>";
+      
+    return iq+query+"</iq>";
+  },
+  info: function(aTo, aNode) {
+    if(aTo)
+      var iq = "<iq type='get' to='"+aTo+"'>";
+    else
+      var iq = "<iq type='get'>";
+    if(aNode)
+      var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>";
+    else
+      var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>";
+      
+    return iq+query+"</iq>";
+  }
+};
+Lightstring.discoItems = function(aConnection, aTo, aCallback) {
+  aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){
+    var items = [];
+    var elms = answer.querySelectorAll('item');
+    for(var i = 0; i < elms.length; i++) {
+      var node = elms[i];
+      var item = {
+        jid: node.getAttribute('jid'),
+        name: node.getAttribute('name'),
+        node: node.getAttribute('node')
+      }
+      items.push(item);
+    };
+    if(aCallback)
+      aCallback(items);
+  });
+};
+Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) {
+  aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){
+    var identities = [];
+    var features = [];
+
+    var children = answer.firstChild.children;
+    var length = children.length;
+
+    for (var i = 0; i < length; i++) {
+      var child = children[i];
+
+      if (child.localName === 'feature')
+        features.push(child.getAttributeNS(null, 'var'));
+
+      else if (child.localName === 'identity') {
+        var identity = {
+          category: child.getAttributeNS(null, 'category'),
+          type: child.getAttributeNS(null, 'type')
+        };
+        var name = child.getAttributeNS(null, 'name');
+        if (name)
+          identity.name = name;
+        identities.push(identity);
+      }
+    }
+
+    aCallback({identities: identities, features: features});
+  });
+};
new file mode 100644
--- /dev/null
+++ b/plugins/im.js
@@ -0,0 +1,30 @@
+'use strict';
+
+/**
+  Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
+
+  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.
+*/
+
+//////
+//IM//
+//////
+Lightstring.stanza.message = {
+  normal: function(aTo, aSubject, aText) {
+    return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>";
+  },
+  chat: function(aTo, aText) {
+    return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>";
+  }
+};
+
new file mode 100644
--- /dev/null
+++ b/plugins/presence.js
@@ -0,0 +1,57 @@
+'use strict';
+
+/**
+  Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
+  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.
+*/
+
+////////////
+//Presence// http://xmpp.org/rfcs/rfc6121.html#presence
+////////////
+(function() {
+  var legal_types = ['error', 'probe', 'subscribe', 'subscribed', 'unavailable', 'unsubscribe', 'unsubscribed'];
+
+  Lightstring.stanza.presence = function(object) {
+    if (object) {
+      var payloads = "";
+      var attributs = "";
+      if (object.type && legal_types.indexOf(object.type) !== -1)
+        attributs += " type='" + object.type + "'";
+
+      if (object.priority)
+        payloads += "<priority>" + object.priority + "</priority>";
+
+      if (object.show)
+        payloads += "<show>" + object.show + "</show>";
+
+      if (object.status)
+        payloads += "<status>" + object.status + "</status>";
+
+      if (object.payload)
+        payloads += object.payload;
+
+      if (payloads)
+        return "<presence" + attributs + ">" + payloads + "</presence>";
+      else
+        return "<presence" + attributs + "/>";
+
+    } else
+      return "<presence/>";
+  };
+
+  Lightstring.presence = function(aConnection, aObject) {
+    aConnection.send(Lightstring.stanza.presence(aObject));
+  };
+})();
rename from plugins.js
rename to plugins/pubsub.js
--- a/plugins.js
+++ b/plugins/pubsub.js
@@ -16,171 +16,6 @@
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
-////////////
-//Presence// http://xmpp.org/rfcs/rfc6121.html#presence
-////////////
-Lightstring.stanza.presence = function(aPriority) {
-  if(aPriority)
-    return "<presence><priority>"+aPriority+"</priority></presence>";
-  else
-    return "<presence/>";
-};
-Lightstring.presence = function(aConnection, aPriority) {
-  aConnection.send(Lightstring.stanza.presence(aPriority));
-};
-
-//////////
-//Roster//
-//////////
-Lightstring.NS.roster = 'jabber:iq:roster';
-Lightstring.stanza.roster = {
-  'get': function() {
-    return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>";
-  },
-  add: function(aAddress, aGroups, aCustomName) {
-    var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree();
-    if(aCustomName) iq.querySelector('item').setAttribute(aCustomName);
-    for (var i=0; i<aGroups.length; i++) {
-      if(i === 0) iq.querySelector('item').appendChild(document.createElement('group'));
-      iq.querySelector('group').appendChild(document.createElement(aGroups[i]));
-    }
-    return iq;
-  },
-  remove: function(aAddress) {
-    return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree();
-  }
-};
-Lightstring.getRoster = function(connection, aCallback) {
-  connection.send(this.stanza.roster.get(), function(answer){
-    var contacts = [];
-    var elems = answer.querySelectorAll('item');
-    for(var i = 0; i<elms.length; i++) {
-      var item = elms[i];
-      var jid = item.getAttribute('jid');
-      var name = item.getAttribute('name');
-      var groups = item.querySelectorAll('group');
-      var subscription = item.getAttribute('subscription');
-      var contact = {};
-      if(name)
-        contact.name = name;
-      if(jid)
-        contact.jid = jid;
-      if(subscription)
-        contact.subscription = subscription;
-      if(groups.length > 0) {
-        contact.groups = [];
-        groups.forEach(function(group) {
-          contact.groups.push(group.textContent);
-        });
-      }
-
-      contacts.push(contact);
-    };
-    aCallback(contacts);
-  });
-}
-/////////
-//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>";
-    else
-      return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>";
-  }
-};
-//FIXME we should return a proper vcard, not an XMPP one
-Lightstring.getVcard = function(aConnection, aTo, aCallback) {
-  aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){
-    if(answer) {
-      var vcard = answer.querySelector('vCard');
-      if(vcard)
-        aCallback(vcard);
-    }    
-    else
-      aCallback(null);
-  });
-}
-/////////
-//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+"'>";
-    else
-      var iq = "<iq type='get'>";
-    
-    if(aNode)
-      var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>";
-    else
-      var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>";
-      
-    return iq+query+"</iq>";
-  },
-  info: function(aTo, aNode) {
-    if(aTo)
-      var iq = "<iq type='get' to='"+aTo+"'>";
-    else
-      var iq = "<iq type='get'>";
-    if(aNode)
-      var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>";
-    else
-      var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>";
-      
-    return iq+query+"</iq>";
-  }
-};
-Lightstring.discoItems = function(aConnection, aTo, aCallback) {
-  aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){
-    var items = [];
-    var elms = answer.querySelectorAll('item');
-    for(var i = 0; i < elms.length; i++) {
-      var node = elms[i];
-      var item = {
-        jid: node.getAttribute('jid'),
-        name: node.getAttribute('name'),
-        node: node.getAttribute('node')
-      }
-      items.push(item);
-    };
-    if(aCallback)
-      aCallback(items);
-  });
-};
-Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) {
-  aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){
-    var identities = [];
-    var features = [];
-
-    var children = answer.firstChild.children;
-    var length = children.length;
-
-    for (var i = 0; i < length; i++) {
-      var child = children[i];
-
-      if (child.localName === 'feature')
-        features.push(child.getAttributeNS(null, 'var'));
-
-      else if (child.localName === 'identity') {
-        var identity = {
-          category: child.getAttributeNS(null, 'category'),
-          type: child.getAttributeNS(null, 'type')
-        };
-        var name = child.getAttributeNS(null, 'name');
-        if (name)
-          identity.name = name;
-        identities.push(identity);
-      }
-    }
-
-    aCallback({identities: identities, features: features});
-  });
-};
 //////////
 //PubSub//
 //////////
@@ -299,15 +134,3 @@ Lightstring.pubsubGetAffiliations = func
 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) {
   aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations));
 };
-//////
-//IM//
-//////
-Lightstring.stanza.message = {
-  normal: function(aTo, aSubject, aText) {
-    return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>";
-  },
-  chat: function(aTo, aText) {
-    return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>";
-  }
-};
-
new file mode 100644
--- /dev/null
+++ b/plugins/roster.js
@@ -0,0 +1,68 @@
+'use strict';
+
+/**
+  Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
+
+  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.
+*/
+
+//////////
+//Roster//
+//////////
+Lightstring.NS.roster = 'jabber:iq:roster';
+Lightstring.stanza.roster = {
+  'get': function() {
+    return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>";
+  },
+  add: function(aAddress, aGroups, aCustomName) {
+    var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree();
+    if(aCustomName) iq.querySelector('item').setAttribute(aCustomName);
+    for (var i=0; i<aGroups.length; i++) {
+      if(i === 0) iq.querySelector('item').appendChild(document.createElement('group'));
+      iq.querySelector('group').appendChild(document.createElement(aGroups[i]));
+    }
+    return iq;
+  },
+  remove: function(aAddress) {
+    return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree();
+  }
+};
+Lightstring.getRoster = function(connection, aCallback) {
+  connection.send(this.stanza.roster.get(), function(answer){
+    var contacts = [];
+    var elems = answer.querySelectorAll('item');
+    for(var i = 0; i<elms.length; i++) {
+      var item = elms[i];
+      var jid = item.getAttribute('jid');
+      var name = item.getAttribute('name');
+      var groups = item.querySelectorAll('group');
+      var subscription = item.getAttribute('subscription');
+      var contact = {};
+      if(name)
+        contact.name = name;
+      if(jid)
+        contact.jid = jid;
+      if(subscription)
+        contact.subscription = subscription;
+      if(groups.length > 0) {
+        contact.groups = [];
+        groups.forEach(function(group) {
+          contact.groups.push(group.textContent);
+        });
+      }
+
+      contacts.push(contact);
+    };
+    aCallback(contacts);
+  });
+}
new file mode 100644
--- /dev/null
+++ b/plugins/vcard.js
@@ -0,0 +1,42 @@
+'use strict';
+
+/**
+  Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
+
+  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.
+*/
+
+/////////
+//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>";
+    else
+      return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>";
+  }
+};
+//FIXME we should return a proper vcard, not an XMPP one
+Lightstring.getVcard = function(aConnection, aTo, aCallback) {
+  aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){
+    if(answer) {
+      var vcard = answer.querySelector('vCard');
+      if(vcard)
+        aCallback(vcard);
+    }    
+    else
+      aCallback(null);
+  });
+}