comparison plugins/roster.js @ 34:6ce66fba0242

Merging.
author Sonny Piers <sonny.piers@gmail.com>
date Sat, 28 Jan 2012 04:39:03 +0100
parents 1506992c33e2
children bdfbd58b4835
comparison
equal deleted inserted replaced
29:1e6d2ca2daae 34:6ce66fba0242
1 'use strict';
2
3 /**
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 //////////
20 //Roster//
21 //////////
22 Lightstring.NS.roster = 'jabber:iq:roster';
23 Lightstring.stanza.roster = {
24 'get': function() {
25 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>";
26 },
27 add: function(aAddress, aGroups, aCustomName) {
28 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree();
29 if(aCustomName) iq.querySelector('item').setAttribute(aCustomName);
30 for (var i=0; i<aGroups.length; i++) {
31 if(i === 0) iq.querySelector('item').appendChild(document.createElement('group'));
32 iq.querySelector('group').appendChild(document.createElement(aGroups[i]));
33 }
34 return iq;
35 },
36 remove: function(aAddress) {
37 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree();
38 }
39 };
40 Lightstring.getRoster = function(connection, aCallback) {
41 connection.send(this.stanza.roster.get(), function(answer){
42 var contacts = [];
43 var elems = answer.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 }