diff plugins/disco.js @ 70:fdd1ae375067

Fix disco plugin and add it an handler.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 02 Feb 2012 01:58:35 +0100
parents bcb5b7c2c3d3
children 5dbf93cef55d
line wrap: on
line diff
--- a/plugins/disco.js
+++ b/plugins/disco.js
@@ -19,138 +19,155 @@
 /////////
 //Disco//
 /////////
-Lightstring.plugins['disco'] = {
-  namespaces: {
-    'disco#info': "http://jabber.org/protocol/disco#info",
-    'disco#items': "http://jabber.org/protocol/disco#items"
-  },
-  stanzas: {
-    'disco#items': function(aTo, aNode) {
-      if(aTo)
-        var iq = "<iq type='get' to='"+aTo+"'>";
-      else
-        var iq = "<iq type='get'>";
+(function() {
+  var identities = [{category: 'client', type: 'browser'}];
+  var features = [];
 
-      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>";
+  Lightstring.plugins['disco'] = {
+    namespaces: {
+      'disco#info': "http://jabber.org/protocol/disco#info",
+      'disco#items': "http://jabber.org/protocol/disco#items"
     },
-    'disco#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']+"'/>";
+    stanzas: {
+      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>";
-    }
-  },
-  handlers: {
-    //TODO: fix that handler.
-    /*conn.on('iq/' + Lightstring.NS['disco#info'] + ':query', function(stanza) {
-      if (stanza.DOM.getAttributeNS(null, 'type') !== 'get')
-        return;
+        return iq + query + "</iq>";
+      }
+    },
+    methods: {
+      items: function(aTo, aResult, aError) {
+        this.send(Lightstring.stanzas.disco.items(aTo), function (stanza) {
+          var items = [];
+
+          var children = stanza.DOM.firstChild.childNodes;
+          var length = children.length;
+
+          for (var i = 0; i < length; i++) {
+            var node = children[i];
+            if (node.localName !== 'item')
+              continue;
+
+            var item = {
+              jid: node.getAttributeNS(null, 'jid'),
+              name: node.getAttributeNS(null, 'name'),
+              node: node.getAttributeNS(null, 'node')
+            };
+            items.push(item);
+          }
+
+          stanza.items = items;
 
-      var query = stanza.DOM.firstChild;
-      if (query.getAttributeNS(null, 'node')) {
-        var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
-                          " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
-                          " type='error'/>"; //TODO: precise the error.
-        conn.send(response);
-        return;
-      }
+          if (aResult)
+            aResult(stanza);
+        }, aError);
+      },
+      info: function(aTo, aNode, aResult, aError) {
+        this.send(Lightstring.stanzas.disco.info(aTo, aNode), function(stanza){
+          var identities = [];
+          var features = [];
+          var fields = {};
 
-      var features = [Lightstring.NS.sxe, Lightstring.NS.jingle.transports.sxe]; //TODO: put that elsewhere.
+          var children = stanza.DOM.firstChild.childNodes;
+          var length = children.length;
 
-      var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
-                        " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
-                        " type='result'>" +
-                       "<query xmlns='" + Lightstring.NS['disco#info'] + "'>" +
-                         "<identity category='client' type='browser'/>";
-      features.forEach(function(f) {
-        response += "<feature var='" + f + "'/>";
-      });
-      response += "</query>" +
-                "</iq>";
+          for (var i = 0; i < length; i++) {
+            var child = children[i];
+            var ns = child.namespaceURI;
+            var name = child.localName;
+
+            if (ns === Lightstring.namespaces['disco#info'] && name === 'feature')
+              features.push(child.getAttributeNS(null, 'var'));
+
+            else if (ns === Lightstring.namespaces['disco#info'] && name === '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);
+            }
 
-      conn.send(response);
-    });*/
-  },
-  methods: {
-    discoItems: function(aTo, aResult, aError) {
-      this.send(Lightstring.stanzas.disco.items(aTo), function (stanza) {
-        var items = [];
+            else if (ns === Lightstring.namespaces['dataforms'] && name === 'x')
+              this.disco.parse(child); //TODO: check if that plugin is enabled.
 
-        var children = stanza.DOM.firstChild.childNodes;
-        var length = children.length;
+            else
+              ; //TODO: emit a warning.
+          }
+
+          stanza.identities = identities;
+          stanza.features = features;
+          stanza.fields = fields;
 
-        for (var i = 0; i < length; i++) {
-          var node = children[i];
-          if (node.localName !== 'item')
-            continue;
+          if (aResult)
+            aResult(stanza);
+        }, aError);
+      },
+      addFeature: function(feature) {
+        features.push(feature);
+      }
+    },
+    init: function() {
+      conn.on('iq/' + Lightstring.ns['disco#info'] + ':query', function(stanza) {
+        if (stanza.DOM.getAttributeNS(null, 'type') !== 'get')
+          return false;
 
-          var item = {
-            jid: node.getAttributeNS(null, 'jid'),
-            name: node.getAttributeNS(null, 'name'),
-            node: node.getAttributeNS(null, 'node')
-          };
-          items.push(item);
+        var query = stanza.DOM.firstChild;
+        if (query.getAttributeNS(null, 'node')) {
+          var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
+                            " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
+                            " type='error'/>"; //TODO: precise the error.
+          conn.send(response);
+          return true;
         }
 
-        stanza.items = items;
-
-        if (aResult)
-          aResult(stanza);
-      }, aError);
-    },
-    discoInfo: function(aTo, aNode, aResult, aError) {
-      this.send(Lightstring.stanzas.disco.info(aTo, aNode), function(stanza){
-        var identities = [];
-        var features = [];
-        var fields = {};
-
-        var children = stanza.DOM.firstChild.childNodes;
-        var length = children.length;
-
-        for (var i = 0; i < length; i++) {
-          var child = children[i];
-          var ns = child.namespaceURI;
-          var name = child.localName;
-
-          if (ns === Lightstring.namespaces['disco#info'] && name === 'feature')
-            features.push(child.getAttributeNS(null, 'var'));
+        var res = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
+                     " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
+                     " type='result'>" +
+                    "<query xmlns='" + Lightstring.ns['disco#info'] + "'>";
 
-          else if (ns === Lightstring.namespaces['disco#info'] && name === '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);
-          }
-
-          else if (ns === Lightstring.namespaces['dataforms'] && name === 'x')
-            this.disco.parse(child); //TODO: check if that plugin is enabled.
+        identities.forEach(function(i) {
+          res += "<identity";
+          if (i.category)
+            res += " category='" + i.category + "'";
+          if (i.name)
+            res += " name='" + i.name + "'";
+          if (i.type)
+            res += " type='" + i.type + "'";
+          res += "/>";
+        });
 
-          else
-            ; //TODO: emit a warning.
-        }
+        features.forEach(function(f) {
+          res += "<feature var='" + f + "'/>";
+        });
 
-        stanza.identities = identities;
-        stanza.features = features;
-        stanza.fields = fields;
+        res += "</query>" +
+             "</iq>";
 
-        if (aResult)
-          aResult(stanza);
-      }, aError);
+        conn.send(res);
+      });
     }
-  }
-};
+  };
+})();