comparison plugins/roster.js @ 52:48e2bd6b1885

Fix roster plugin.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 01 Feb 2012 03:51:12 +0100
parents bdfbd58b4835
children 595be4a8584f
comparison
equal deleted inserted replaced
51:63c1c8bc8c19 52:48e2bd6b1885
17 */ 17 */
18 18
19 ////////// 19 //////////
20 //Roster// 20 //Roster//
21 ////////// 21 //////////
22 Lightstring.NS.roster = 'jabber:iq:roster'; 22 Lightstring.plugins['roster'] = {
23 Lightstring.stanza.roster = { 23 namespaces: {
24 'get': function() { 24 roster: 'jabber:iq:roster'
25 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>";
26 }, 25 },
27 add: function(aAddress, aGroups, aCustomName) { 26 stanzas: {
28 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree(); 27 get: function() {
29 if(aCustomName) iq.querySelector('item').setAttribute(aCustomName); 28 return "<iq type='get'>" +
30 for (var i=0; i<aGroups.length; i++) { 29 "<query xmlns='" + Lightstring.NS.roster + "'/>" +
31 if(i === 0) iq.querySelector('item').appendChild(document.createElement('group')); 30 "</iq>";
32 iq.querySelector('group').appendChild(document.createElement(aGroups[i])); 31 },
32 add: function(aAddress, aGroups) {
33 var iq = "<iq type='set'>" +
34 "<query xmlns='" + Lightstring.NS.roster + "'>" +
35 "<item jid='" + aAddress + "'/>" +
36 "</query>" +
37 "</iq>";
38 for (var i = 0; i < aGroups.length; i++) {
39 if (i === 0)
40 iq.querySelector('item').appendChild(document.createElement('group'));
41 iq.querySelector('group').appendChild(document.createElement(aGroups[i]));
42 }
43 return iq;
44 },
45 remove: function(aAddress) {
46 return "<iq type='set'>" +
47 "<query xmlns='" + Lightstring.NS.roster + "'>" +
48 "<item jid='" + aAddress + "' subscription='remove'/>" +
49 "</query>" +
50 "</iq>";
33 } 51 }
34 return iq;
35 }, 52 },
36 remove: function(aAddress) { 53 methods: {
37 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree(); 54 get: function(aResult, aError) {
55 this.send(this.stanza.roster.get(), function(stanza) {
56 var contacts = [];
57
58 var children = stanza.DOM.firstChild.childNodes;
59 var length = children.length;
60
61 for (var i = 0; i < length; i++) {
62 var item = children[i];
63 var jid = item.getAttributeNS(null, 'jid');
64 var name = item.getAttributeNS(null, 'name');
65 var subscription = item.getAttributeNS(null, 'subscription');
66 var groups = item.children;
67 var contact = {};
68 if (name)
69 contact.name = name;
70 if (jid)
71 contact.jid = jid;
72 if (subscription)
73 contact.subscription = subscription;
74 if (groups.length > 0) {
75 contact.groups = [];
76 groups.forEach(function(group) {
77 contact.groups.push(group.textContent);
78 });
79 }
80
81 contacts.push(contact);
82 }
83
84 if (aResult)
85 aResult(contacts);
86 }, aError);
87 }
38 } 88 }
39 }; 89 };
40 Lightstring.getRoster = function(connection, aCallback) {
41 connection.send(this.stanza.roster.get(), function(stanza){
42 var contacts = [];
43 var elems = stanza.DOM.querySelectorAll('item');
44 for(var i = 0; i<elms.length; i++) {
45 var item = elms[i];
46 var jid = item.getAttribute('jid');
47 var name = item.getAttribute('name');
48 var groups = item.querySelectorAll('group');
49 var subscription = item.getAttribute('subscription');
50 var contact = {};
51 if(name)
52 contact.name = name;
53 if(jid)
54 contact.jid = jid;
55 if(subscription)
56 contact.subscription = subscription;
57 if(groups.length > 0) {
58 contact.groups = [];
59 groups.forEach(function(group) {
60 contact.groups.push(group.textContent);
61 });
62 }
63
64 contacts.push(contact);
65 };
66 aCallback(contacts);
67 });
68 }