Mercurial > eldonilo > lightstring
annotate plugins.js @ 3:029c12b8f048
various bug fixes and improvements
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Sat, 14 Jan 2012 17:36:10 +0100 |
parents | f31a75c3b6c8 |
children | 715726598b23 |
rev | line source |
---|---|
0 | 1 'use strict'; |
2 | |
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 | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
19 NodeList.prototype.forEach = function(aCallback) { |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
20 for(var i = 0; i<this.length; i++) |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
21 aCallback(this[i]); |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
22 } |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
23 |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
24 |
0 | 25 // |
26 //Roster | |
27 // | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
28 Lightstring.NS.roster = 'jabber:iq:roster'; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
29 Lightstring.stanza.roster = { |
0 | 30 'get': function() { |
2 | 31 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>"; |
0 | 32 }, |
33 add: function(aAddress, aGroups, aCustomName) { | |
2 | 34 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree(); |
0 | 35 if(aCustomName) iq.querySelector('item').setAttribute(aCustomName); |
36 for (var i=0; i<aGroups.length; i++) { | |
37 if(i === 0) iq.querySelector('item').appendChild(document.createElement('group')); | |
38 iq.querySelector('group').appendChild(document.createElement(aGroups[i])); | |
39 } | |
40 return iq; | |
41 }, | |
42 remove: function(aAddress) { | |
2 | 43 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree(); |
0 | 44 } |
45 }; | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
46 Lightstring.getRoster = function(connection, aCallback) { |
0 | 47 connection.send(this.stanza.roster.get(), function(answer){ |
48 var contacts = []; | |
49 answer.querySelectorAll('item').forEach(function(item) { | |
50 var jid = item.getAttribute('jid'); | |
51 var name = item.getAttribute('name'); | |
52 var groups = item.querySelectorAll('group'); | |
53 var subscription = item.getAttribute('subscription'); | |
54 var contact = {}; | |
55 if(name) | |
56 contact.name = name; | |
57 if(jid) | |
58 contact.jid = jid; | |
59 if(subscription) | |
60 contact.subscription = subscription; | |
61 if(groups.length > 0) { | |
62 contact.groups = []; | |
63 groups.forEach(function(group) { | |
64 contact.groups.push(group.textContent); | |
65 }); | |
66 } | |
67 | |
68 contacts.push(contact); | |
69 }); | |
70 aCallback(contacts); | |
71 }); | |
72 } | |
73 // | |
74 //vCard | |
75 // | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
76 Lightstring.NS.vcard = 'vcard-temp'; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
77 Lightstring.stanza.vcard = { |
0 | 78 'get': function(aTo) { |
79 if(aTo) | |
2 | 80 return "<iq type='get' to='"+aTo+"'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; |
0 | 81 else |
2 | 82 return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; |
0 | 83 } |
84 }; | |
85 //FIXME we should return a proper vcard, not an XMPP one | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
86 Lightstring.getVcard = function(aConnection, aTo, aCallback) { |
2 | 87 aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){ |
0 | 88 if(answer) { |
89 var vcard = answer.querySelector('vCard'); | |
90 if(vcard) | |
91 aCallback(vcard); | |
92 } | |
93 else | |
94 aCallback(null); | |
95 }); | |
96 } | |
97 // | |
98 //Disco | |
99 // | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
100 Lightstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
101 Lightstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
102 Lightstring.stanza.disco = { |
0 | 103 items: function(aTo, aNode) { |
104 if(aTo) | |
105 var iq = "<iq type='get' to='"+aTo+"'>"; | |
106 else | |
107 var iq = "<iq type='get'>"; | |
108 | |
109 if(aNode) | |
2 | 110 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>"; |
0 | 111 else |
2 | 112 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>"; |
0 | 113 |
114 return iq+query+"</iq>"; | |
115 }, | |
116 info: function(aTo, aNode) { | |
117 if(aTo) | |
118 var iq = "<iq type='get' to='"+aTo+"'>"; | |
119 else | |
120 var iq = "<iq type='get'>"; | |
121 if(aNode) | |
2 | 122 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>"; |
0 | 123 else |
2 | 124 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>"; |
0 | 125 |
126 return iq+query+"</iq>"; | |
127 } | |
128 }; | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
129 Lightstring.discoItems = function(aConnection, aTo, aCallback) { |
2 | 130 aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){ |
0 | 131 var items = []; |
132 answer.querySelectorAll('item').forEach(function(node) { | |
133 var item = { | |
134 jid: node.getAttribute('jid'), | |
135 name: node.getAttribute('name'), | |
136 node: node.getAttribute('node') | |
137 } | |
138 items.push(item); | |
139 }); | |
140 if(aCallback) | |
141 aCallback(items); | |
142 }); | |
143 }; | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
144 Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) { |
2 | 145 aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){ |
0 | 146 var field = answer.querySelector('field[var="pubsub#creator"] > value'); |
147 var creator = field ? field.textContent : ''; | |
148 //FIXME callback the entire data | |
149 aCallback(creator); | |
150 }); | |
151 }; | |
152 // | |
153 //PubSub | |
154 // | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
155 Lightstring.NS.x = "jabber:x:data"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
156 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
157 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
158 Lightstring.stanza.pubsub = { |
0 | 159 getConfig: function(aTo, aNode) { |
2 | 160 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; |
0 | 161 }, |
162 items: function(aTo, aNode) { | |
2 | 163 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; |
0 | 164 }, |
165 affiliations: function(aTo, aNode) { | |
2 | 166 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>"; |
0 | 167 }, |
168 publish: function(aTo, aNode, aItem, aId) { | |
2 | 169 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>"; |
0 | 170 }, |
171 retract: function(aTo, aNode, aItem) { | |
2 | 172 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>"; |
0 | 173 }, |
174 'delete': function(aTo, aNode, aURI) { | |
2 | 175 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>"; |
0 | 176 }, |
177 create: function(aTo, aNode, aFields) { | |
2 | 178 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>"; |
0 | 179 if(aFields) { |
2 | 180 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>" |
0 | 181 aFields.forEach(function(field) { |
182 iq += field; | |
183 }); | |
184 iq += "</x></configure>"; | |
185 } | |
186 iq += "</pubsub></iq>"; | |
187 return iq; | |
188 }, | |
189 setAffiliations: function(aTo, aNode, aAffiliations) { | |
2 | 190 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>"; |
0 | 191 for(var i = 0; i < aAffiliations.length; i++) { |
192 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" | |
193 } | |
194 iq += "</affiliations></pubsub></iq>"; | |
195 return iq; | |
196 }, | |
197 }; | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
198 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { |
2 | 199 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ |
0 | 200 var items = []; |
201 answer.querySelectorAll('item').forEach(function(node) { | |
202 var item = { | |
203 id: node.getAttribute('id'), | |
204 name: node.querySelector('title').textContent, | |
205 src: node.querySelector('content').getAttribute('src'), | |
206 type: node.querySelector('content').getAttribute('type'), | |
207 } | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
208 var miniature = node.querySelector('link'); |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
209 if(miniature) |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
210 item.miniature = miniature.getAttribute('href'); |
0 | 211 items.push(item); |
212 }) | |
213 if(aCallback) | |
214 aCallback(items); | |
215 }); | |
216 } | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
217 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { |
2 | 218 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { |
0 | 219 if(answer.getAttribute('type') === 'result') |
220 aCallback(null, answer); | |
221 else | |
222 aCallback(answer, null); | |
223 }); | |
224 }; | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
225 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { |
2 | 226 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ |
0 | 227 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; |
228 if(accessmodel) | |
229 aCallback(accessmodel); | |
230 else | |
231 aCallback(null); | |
232 }); | |
233 } | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
234 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { |
2 | 235 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ |
0 | 236 if(aCallback) |
237 aCallback(answer); | |
238 }); | |
239 } | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
240 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { |
2 | 241 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ |
0 | 242 if(answer.getAttribute('type') === 'result') |
243 aCallback(null, answer); | |
244 else | |
245 aCallback(answer, null); | |
246 }); | |
247 } | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
248 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { |
2 | 249 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ |
0 | 250 if(aCallback) |
251 aCallback(answer); | |
252 }); | |
253 } | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
254 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { |
2 | 255 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { |
0 | 256 if((answer.getAttribute('type') === 'result') && aCallback) { |
257 var affiliations = {}; | |
258 answer.querySelectorAll('affiliation').forEach(function(affiliation) { | |
259 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); | |
260 }) | |
261 aCallback(affiliations); | |
262 } | |
263 }); | |
264 }; | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
265 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { |
2 | 266 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); |
0 | 267 }; |
268 // | |
269 //IM | |
270 // | |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
271 Lightstring.stanza.message = { |
0 | 272 normal: function(aTo, aSubject, aText) { |
273 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>"; | |
274 }, | |
275 chat: function(aTo, aText) { | |
276 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>"; | |
277 } | |
278 }; | |
279 |