# HG changeset patch # User Emmanuel Gil Peyrot # Date 1328040547 -3600 # Node ID 063e31247e71cff61d75796bb8a634ef42555c1f # Parent 3dfb596cf66901650300737b56d7caad53685816 Fix disco plugin. diff --git a/plugins/disco.js b/plugins/disco.js --- a/plugins/disco.js +++ b/plugins/disco.js @@ -19,105 +19,155 @@ ///////// //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 = ""; - else - var iq = ""; - - 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" + }, + stanzas: { + 'disco#info': function(aTo, aNode) { + if(aTo) + var iq = ""; + else + var iq = ""; + + if(aNode) + var query = ""; + else + var query = ""; + + return iq+query+""; + }, + 'disco#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; + + var query = stanza.DOM.firstChild; + if (query.getAttributeNS(null, 'node')) { + var response = ""; //TODO: precise the error. + conn.send(response); + return; + } + + var features = [Lightstring.NS.sxe, Lightstring.NS.jingle.transports.sxe]; //TODO: put that elsewhere. + + var response = "" + + "" + + ""; + features.forEach(function(f) { + response += ""; + }); + response += "" + + ""; + + conn.send(response); + });*/ }, - info: function(aTo, aNode) { - if(aTo) - var iq = ""; - else - var iq = ""; - if(aNode) - var query = ""; - else - var query = ""; - - return iq+query+""; + methods: { + discoItems: 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; + + 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++) { + + if (children[i].localName === 'feature') + features.push(children[i].getAttributeNS(null, 'var')); + + else if (children[i].localName === 'identity') { + var identity = { + category: children[i].getAttributeNS(null, 'category'), + type: children[i].getAttributeNS(null, 'type') + }; + var name = children[i].getAttributeNS(null, 'name'); + if (name) + identity.name = name; + identities.push(identity); + } + + else if (children[i].localName === 'x') { + for (var j = 0; j < children[i].childNodes.length; j++) { + var child = children[i].childNodes[j]; + var field = { + type: child.getAttribute('type') + }; + + var _var = child.getAttribute('var'); + + var label = child.getAttribute('label'); + if (label) + field.label = label; + + for (var y = 0; y < child.childNodes.length; y++) { + if(child.childNodes[y].localName === 'desc') + field.desc = child.childNodes[y].textContent; + else if(child.childNodes[y].localName === 'required') + field.required = true; + else if(child.childNodes[y].localName === 'value') + field.value = child.childNodes[y].textContent; + } + + fields[_var] = field; + } + } + } + + stanza.identities = identities; + stanza.features = features; + stanza.fields = fields; + + if (aResult) + aResult(stanza); + }, aError); + } } }; -Lightstring.discoItems = function(aConnection, aTo, aCallback) { - aConnection.send(Lightstring.stanza.disco.items(aTo), function(stanza){ - var items = []; - var elms = stanza.DOM.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(stanza){ - var identities = []; - var features = []; - var fields = {}; - - var children = stanza.DOM.firstChild.childNodes; - var length = children.length; - - for (var i = 0; i < length; i++) { - - if (children[i].localName === 'feature') - features.push(children[i].getAttributeNS(null, 'var')); - - else if (children[i].localName === 'identity') { - var identity = { - category: children[i].getAttributeNS(null, 'category'), - type: children[i].getAttributeNS(null, 'type') - }; - var name = children[i].getAttributeNS(null, 'name'); - if (name) - identity.name = name; - identities.push(identity); - } - - else if (children[i].localName === 'x') { - for (var j = 0; j < children[i].childNodes.length; j++) { - var child = children[i].childNodes[j]; - var field = { - type: child.getAttribute('type') - }; - - var _var = child.getAttribute('var'); - - var label = child.getAttribute('label'); - if(label) field.label = label; - - - for (var y = 0; y < child.childNodes.length; y++) { - if(child.childNodes[y].localName === 'desc') - field.desc = child.childNodes[y].textContent; - else if(child.childNodes[y].localName === 'required') - field.required = true; - else if(child.childNodes[y].localName === 'value') - field.value = child.childNodes[y].textContent; - } - - - fields[_var] = field; - } - } - } - - aCallback({'identities': identities, 'features': features, 'fields': fields}, stanza); - }); -};