# HG changeset patch # User Emmanuel Gil Peyrot # Date 1328144315 -3600 # Node ID fdd1ae375067fda37fab22d68922e2074e05f342 # Parent 03ccf507ecda30873e5c100e45dabffd5deff740 Fix disco plugin and add it an handler. diff --git a/plugins/disco.js b/plugins/disco.js --- 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 = ""; - else - var iq = ""; +(function() { + var identities = [{category: 'client', type: 'browser'}]; + var features = []; - if(aNode) - var query = ""; - else - var query = ""; - - return iq+query+""; + 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 = ""; - else - var iq = ""; - if(aNode) - var query = ""; - else - var query = ""; + stanzas: { + items: function(aTo, aNode) { + if(aTo) + var iq = ""; + else + var iq = ""; + + if(aNode) + var query = ""; + else + var query = ""; + + return iq + query + ""; + }, + info: function(aTo, aNode) { + if(aTo) + var iq = ""; + else + var iq = ""; + if(aNode) + var query = ""; + else + var query = ""; - return iq+query+""; - } - }, - 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 + ""; + } + }, + 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 = ""; //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 = "" + - "" + - ""; - features.forEach(function(f) { - response += ""; - }); - response += "" + - ""; + 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 = ""; //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 = "" + + ""; - 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 += ""; + }); - stanza.identities = identities; - stanza.features = features; - stanza.fields = fields; + res += "" + + ""; - if (aResult) - aResult(stanza); - }, aError); + conn.send(res); + }); } - } -}; + }; +})();