Mercurial > eldonilo > lightstring
diff plugins.js @ 27:b6e4bc19ff5a
undoing
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Sat, 28 Jan 2012 01:27:00 +0100 |
parents | fc577e5b2f4a 82bffc4a07a9 |
children | 630b9579fe4a |
line wrap: on
line diff
--- a/plugins.js +++ b/plugins.js @@ -34,26 +34,26 @@ Lightstring.presence = function(aConnect ////////// 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(); - } + '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'); + 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'); @@ -75,33 +75,33 @@ Lightstring.getRoster = function(connect } contacts.push(contact); - }; - aCallback(contacts); - }); + }; + 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>"; - } + '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); - }); + 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// @@ -109,56 +109,77 @@ Lightstring.getVcard = function(aConnect 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>"; - } + 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 = []; + 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); - }); + 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 field = answer.querySelector('field[var="pubsub#creator"] > value'); - var creator = field ? field.textContent : ''; - //FIXME callback the entire data - aCallback(creator); - }); + 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// @@ -167,6 +188,7 @@ 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 = { +<<<<<<< HEAD getConfig: function(aTo, aNode) { return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; }, @@ -215,88 +237,128 @@ Lightstring.stanza.pubsub = { iq += "</affiliations></pubsub></iq>"; return iq; }, +======= + getConfig: function(aTo, aNode) { + return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; + }, + items: function(aTo, aNode) { + return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; + }, + affiliations: function(aTo, aNode) { + return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>"; + }, + publish: function(aTo, aNode, aItem, aId) { + return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>"; + }, + retract: function(aTo, aNode, aItem) { + return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>"; + }, + 'delete': function(aTo, aNode, aURI) { + return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>"; + }, + create: function(aTo, aNode, aFields) { + var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>"; + if(aFields) { + iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>" + aFields.forEach(function(field) { + iq += field; + }); + iq += "</x></configure>"; + } + iq += "</pubsub></iq>"; + return iq; + }, + setAffiliations: function(aTo, aNode, aAffiliations) { + var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>"; + for(var i = 0; i < aAffiliations.length; i++) { + iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" + } + iq += "</affiliations></pubsub></iq>"; + return iq; + }, +>>>>>>> f6a7c0f93d154c2cd34dfdda4ab8eec808b91b34 }; Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { - aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ - var items = []; + aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ + var items = []; var elms = answer.querySelectorAll('item'); for(var i = 0; i < elms.length; i++) { var node = elms[i]; - var item = { - id: node.getAttribute('id'), - name: node.querySelector('title').textContent, - src: node.querySelector('content').getAttribute('src'), - type: node.querySelector('content').getAttribute('type'), - } - var miniature = node.querySelector('link'); - if(miniature) - item.miniature = miniature.getAttribute('href'); - items.push(item); - }; - if(aCallback) - aCallback(items); - }); + var item = { + id: node.getAttribute('id'), + name: node.querySelector('title').textContent, + src: node.querySelector('content').getAttribute('src'), + type: node.querySelector('content').getAttribute('type'), + } + var miniature = node.querySelector('link'); + if(miniature) + item.miniature = miniature.getAttribute('href'); + items.push(item); + }; + if(aCallback) + aCallback(items); + }); } 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); - else - aCallback(answer, null); - }); + aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { + if(answer.getAttribute('type') === 'result') + aCallback(null, answer); + else + aCallback(answer, null); + }); }; 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) - aCallback(accessmodel); - else - aCallback(null); - }); + aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ + var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; + if(accessmodel) + aCallback(accessmodel); + else + aCallback(null); + }); } Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { - aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ - if(aCallback) - aCallback(answer); - }); + aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ + if(aCallback) + aCallback(answer); + }); } 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); - else - aCallback(answer, null); - }); + aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ + if(answer.getAttribute('type') === 'result') + aCallback(null, answer); + else + aCallback(answer, null); + }); } Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { - aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ - if(aCallback) - aCallback(answer); - }); + aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ + if(aCallback) + aCallback(answer); + }); } 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 = {}; - answer.querySelectorAll('affiliation').forEach(function(affiliation) { - affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); - }) - aCallback(affiliations); - } - }); + aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { + if((answer.getAttribute('type') === 'result') && aCallback) { + var affiliations = {}; + answer.querySelectorAll('affiliation').forEach(function(affiliation) { + affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); + }) + aCallback(affiliations); + } + }); }; Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { - aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); + 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>"; - } + 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>"; + } };