Mercurial > eldonilo > lightstring
annotate plugins/roster.js @ 91:c5240333ea4c
pubsub item node
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Sat, 17 Mar 2012 01:33:50 +0100 |
parents | 595be4a8584f |
children | c06ec02217ee |
rev | line source |
---|---|
30
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 'use strict'; |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 /** |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net> |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 Permission to use, copy, modify, and/or distribute this software for any |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 purpose with or without fee is hereby granted, provided that the above |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 copyright notice and this permission notice appear in all copies. |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 */ |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 ////////// |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 //Roster// |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 ////////// |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
22 Lightstring.plugins['roster'] = { |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
23 namespaces: { |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
24 roster: 'jabber:iq:roster' |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
25 }, |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
26 stanzas: { |
88 | 27 'get': function() { |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
28 return "<iq type='get'>" + |
88 | 29 "<query xmlns='" + Lightstring.ns.roster + "'/>" + |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
30 "</iq>"; |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
31 }, |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
32 add: function(aAddress, aGroups) { |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
33 var iq = "<iq type='set'>" + |
88 | 34 "<query xmlns='" + Lightstring.ns.roster + "'>" + |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
35 "<item jid='" + aAddress + "'/>" + |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
36 "</query>" + |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
37 "</iq>"; |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
38 for (var i = 0; i < aGroups.length; i++) { |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
39 if (i === 0) |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
40 iq.querySelector('item').appendChild(document.createElement('group')); |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
41 iq.querySelector('group').appendChild(document.createElement(aGroups[i])); |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
42 } |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
43 return iq; |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
44 }, |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
45 remove: function(aAddress) { |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
46 return "<iq type='set'>" + |
88 | 47 "<query xmlns='" + Lightstring.ns.roster + "'>" + |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
48 "<item jid='" + aAddress + "' subscription='remove'/>" + |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
49 "</query>" + |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
50 "</iq>"; |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
51 } |
30
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 }, |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
53 methods: { |
88 | 54 'get': function(aSuccess, aError) { |
55 this.send(Lightstring.stanzas.roster.get(), function(stanza) { | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
56 var contacts = []; |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
57 |
88 | 58 var items = stanza.DOM.getElementsByTagName('item'); |
59 | |
60 for (var i = 0; i < items.length; i++) { | |
61 var item = items[i]; | |
62 var contact = {} | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
63 |
88 | 64 var jid = item.getAttribute('jid'); |
65 if (jid) | |
66 contact.jid = jid; | |
67 | |
68 var name = item.getAttribute('name'); | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
69 if (name) |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
70 contact.name = name; |
88 | 71 |
72 var subscription = item.getAttribute('subscription'); | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
73 if (subscription) |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
74 contact.subscription = subscription; |
88 | 75 |
76 var groups = item.getElementsByTagName('group'); | |
77 if(groups) { | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
78 contact.groups = []; |
88 | 79 for (var y = 0; y < groups.length; y++) |
80 contact.groups.push(groups[y].textContent); | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
81 } |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
82 |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
83 contacts.push(contact); |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
84 } |
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
85 |
88 | 86 stanza.roster = { |
87 contacts: contacts | |
88 }; | |
89 | |
90 if (aSuccess) | |
91 aSuccess(stanza); | |
52
48e2bd6b1885
Fix roster plugin.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
92 }, aError); |
30
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 } |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 } |
1506992c33e2
Split plugins.js into multiple plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 }; |