Mercurial > eldonilo > lightstring
comparison 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 |
comparison
equal
deleted
inserted
replaced
19:fc577e5b2f4a | 27:b6e4bc19ff5a |
---|---|
32 ////////// | 32 ////////// |
33 //Roster// | 33 //Roster// |
34 ////////// | 34 ////////// |
35 Lightstring.NS.roster = 'jabber:iq:roster'; | 35 Lightstring.NS.roster = 'jabber:iq:roster'; |
36 Lightstring.stanza.roster = { | 36 Lightstring.stanza.roster = { |
37 'get': function() { | 37 'get': function() { |
38 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>"; | 38 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>"; |
39 }, | 39 }, |
40 add: function(aAddress, aGroups, aCustomName) { | 40 add: function(aAddress, aGroups, aCustomName) { |
41 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree(); | 41 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree(); |
42 if(aCustomName) iq.querySelector('item').setAttribute(aCustomName); | 42 if(aCustomName) iq.querySelector('item').setAttribute(aCustomName); |
43 for (var i=0; i<aGroups.length; i++) { | 43 for (var i=0; i<aGroups.length; i++) { |
44 if(i === 0) iq.querySelector('item').appendChild(document.createElement('group')); | 44 if(i === 0) iq.querySelector('item').appendChild(document.createElement('group')); |
45 iq.querySelector('group').appendChild(document.createElement(aGroups[i])); | 45 iq.querySelector('group').appendChild(document.createElement(aGroups[i])); |
46 } | 46 } |
47 return iq; | 47 return iq; |
48 }, | 48 }, |
49 remove: function(aAddress) { | 49 remove: function(aAddress) { |
50 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree(); | 50 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree(); |
51 } | 51 } |
52 }; | 52 }; |
53 Lightstring.getRoster = function(connection, aCallback) { | 53 Lightstring.getRoster = function(connection, aCallback) { |
54 connection.send(this.stanza.roster.get(), function(answer){ | 54 connection.send(this.stanza.roster.get(), function(answer){ |
55 var contacts = []; | 55 var contacts = []; |
56 var elems = answer.querySelectorAll('item'); | 56 var elems = answer.querySelectorAll('item'); |
57 for(var i = 0; i<elms.length; i++) { | 57 for(var i = 0; i<elms.length; i++) { |
58 var item = elms[i]; | 58 var item = elms[i]; |
59 var jid = item.getAttribute('jid'); | 59 var jid = item.getAttribute('jid'); |
60 var name = item.getAttribute('name'); | 60 var name = item.getAttribute('name'); |
61 var groups = item.querySelectorAll('group'); | 61 var groups = item.querySelectorAll('group'); |
73 contact.groups.push(group.textContent); | 73 contact.groups.push(group.textContent); |
74 }); | 74 }); |
75 } | 75 } |
76 | 76 |
77 contacts.push(contact); | 77 contacts.push(contact); |
78 }; | 78 }; |
79 aCallback(contacts); | 79 aCallback(contacts); |
80 }); | 80 }); |
81 } | 81 } |
82 ///////// | 82 ///////// |
83 //vCard// | 83 //vCard// |
84 ///////// | 84 ///////// |
85 Lightstring.NS.vcard = 'vcard-temp'; | 85 Lightstring.NS.vcard = 'vcard-temp'; |
86 Lightstring.stanza.vcard = { | 86 Lightstring.stanza.vcard = { |
87 'get': function(aTo) { | 87 'get': function(aTo) { |
88 if(aTo) | 88 if(aTo) |
89 return "<iq type='get' to='"+aTo+"'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; | 89 return "<iq type='get' to='"+aTo+"'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; |
90 else | 90 else |
91 return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; | 91 return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; |
92 } | 92 } |
93 }; | 93 }; |
94 //FIXME we should return a proper vcard, not an XMPP one | 94 //FIXME we should return a proper vcard, not an XMPP one |
95 Lightstring.getVcard = function(aConnection, aTo, aCallback) { | 95 Lightstring.getVcard = function(aConnection, aTo, aCallback) { |
96 aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){ | 96 aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){ |
97 if(answer) { | 97 if(answer) { |
98 var vcard = answer.querySelector('vCard'); | 98 var vcard = answer.querySelector('vCard'); |
99 if(vcard) | 99 if(vcard) |
100 aCallback(vcard); | 100 aCallback(vcard); |
101 } | 101 } |
102 else | 102 else |
103 aCallback(null); | 103 aCallback(null); |
104 }); | 104 }); |
105 } | 105 } |
106 ///////// | 106 ///////// |
107 //Disco// | 107 //Disco// |
108 ///////// | 108 ///////// |
109 Lightstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info"; | 109 Lightstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info"; |
110 Lightstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items"; | 110 Lightstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items"; |
111 Lightstring.stanza.disco = { | 111 Lightstring.stanza.disco = { |
112 items: function(aTo, aNode) { | 112 items: function(aTo, aNode) { |
113 if(aTo) | 113 if(aTo) |
114 var iq = "<iq type='get' to='"+aTo+"'>"; | 114 var iq = "<iq type='get' to='"+aTo+"'>"; |
115 else | 115 else |
116 var iq = "<iq type='get'>"; | 116 var iq = "<iq type='get'>"; |
117 | 117 |
118 if(aNode) | 118 if(aNode) |
119 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>"; | 119 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"' node='"+aNode+"'/>"; |
120 else | 120 else |
121 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>"; | 121 var query = "<query xmlns='"+Lightstring.NS['disco#items']+"'/>"; |
122 | 122 |
123 return iq+query+"</iq>"; | 123 return iq+query+"</iq>"; |
124 }, | 124 }, |
125 info: function(aTo, aNode) { | 125 info: function(aTo, aNode) { |
126 if(aTo) | 126 if(aTo) |
127 var iq = "<iq type='get' to='"+aTo+"'>"; | 127 var iq = "<iq type='get' to='"+aTo+"'>"; |
128 else | 128 else |
129 var iq = "<iq type='get'>"; | 129 var iq = "<iq type='get'>"; |
130 if(aNode) | 130 if(aNode) |
131 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>"; | 131 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"' node='"+aNode+"'/>"; |
132 else | 132 else |
133 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>"; | 133 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>"; |
134 | 134 |
135 return iq+query+"</iq>"; | 135 return iq+query+"</iq>"; |
136 } | 136 } |
137 }; | 137 }; |
138 Lightstring.discoItems = function(aConnection, aTo, aCallback) { | 138 Lightstring.discoItems = function(aConnection, aTo, aCallback) { |
139 aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){ | 139 aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){ |
140 var items = []; | 140 var items = []; |
141 var elms = answer.querySelectorAll('item'); | 141 var elms = answer.querySelectorAll('item'); |
142 for(var i = 0; i < elms.length; i++) { | 142 for(var i = 0; i < elms.length; i++) { |
143 var node = elms[i]; | 143 var node = elms[i]; |
144 var item = { | 144 var item = { |
145 jid: node.getAttribute('jid'), | 145 jid: node.getAttribute('jid'), |
146 name: node.getAttribute('name'), | 146 name: node.getAttribute('name'), |
147 node: node.getAttribute('node') | 147 node: node.getAttribute('node') |
148 } | 148 } |
149 items.push(item); | 149 items.push(item); |
150 }; | 150 }; |
151 if(aCallback) | 151 if(aCallback) |
152 aCallback(items); | 152 aCallback(items); |
153 }); | 153 }); |
154 }; | 154 }; |
155 Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) { | 155 Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) { |
156 aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){ | 156 aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){ |
157 var field = answer.querySelector('field[var="pubsub#creator"] > value'); | 157 var identities = []; |
158 var creator = field ? field.textContent : ''; | 158 var features = []; |
159 //FIXME callback the entire data | 159 |
160 aCallback(creator); | 160 var children = answer.firstChild.children; |
161 }); | 161 var length = children.length; |
162 | |
163 for (var i = 0; i < length; i++) { | |
164 var child = children[i]; | |
165 | |
166 if (child.localName === 'feature') | |
167 features.push(child.getAttributeNS(null, 'var')); | |
168 | |
169 else if (child.localName === 'identity') { | |
170 var identity = { | |
171 category: child.getAttributeNS(null, 'category'), | |
172 type: child.getAttributeNS(null, 'type') | |
173 }; | |
174 var name = child.getAttributeNS(null, 'name'); | |
175 if (name) | |
176 identity.name = name; | |
177 identities.push(identity); | |
178 } | |
179 } | |
180 | |
181 aCallback({identities: identities, features: features}); | |
182 }); | |
162 }; | 183 }; |
163 ////////// | 184 ////////// |
164 //PubSub// | 185 //PubSub// |
165 ////////// | 186 ////////// |
166 Lightstring.NS.x = "jabber:x:data"; | 187 Lightstring.NS.x = "jabber:x:data"; |
167 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; | 188 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; |
168 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; | 189 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; |
169 Lightstring.stanza.pubsub = { | 190 Lightstring.stanza.pubsub = { |
191 <<<<<<< HEAD | |
170 getConfig: function(aTo, aNode) { | 192 getConfig: function(aTo, aNode) { |
171 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; | 193 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; |
172 }, | 194 }, |
173 items: function(aTo, aNode) { | 195 items: function(aTo, aNode) { |
174 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; | 196 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; |
213 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" | 235 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" |
214 } | 236 } |
215 iq += "</affiliations></pubsub></iq>"; | 237 iq += "</affiliations></pubsub></iq>"; |
216 return iq; | 238 return iq; |
217 }, | 239 }, |
240 ======= | |
241 getConfig: function(aTo, aNode) { | |
242 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; | |
243 }, | |
244 items: function(aTo, aNode) { | |
245 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; | |
246 }, | |
247 affiliations: function(aTo, aNode) { | |
248 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>"; | |
249 }, | |
250 publish: function(aTo, aNode, aItem, aId) { | |
251 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>"; | |
252 }, | |
253 retract: function(aTo, aNode, aItem) { | |
254 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>"; | |
255 }, | |
256 'delete': function(aTo, aNode, aURI) { | |
257 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>"; | |
258 }, | |
259 create: function(aTo, aNode, aFields) { | |
260 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>"; | |
261 if(aFields) { | |
262 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>" | |
263 aFields.forEach(function(field) { | |
264 iq += field; | |
265 }); | |
266 iq += "</x></configure>"; | |
267 } | |
268 iq += "</pubsub></iq>"; | |
269 return iq; | |
270 }, | |
271 setAffiliations: function(aTo, aNode, aAffiliations) { | |
272 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>"; | |
273 for(var i = 0; i < aAffiliations.length; i++) { | |
274 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" | |
275 } | |
276 iq += "</affiliations></pubsub></iq>"; | |
277 return iq; | |
278 }, | |
279 >>>>>>> f6a7c0f93d154c2cd34dfdda4ab8eec808b91b34 | |
218 }; | 280 }; |
219 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { | 281 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { |
220 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ | 282 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ |
221 var items = []; | 283 var items = []; |
222 var elms = answer.querySelectorAll('item'); | 284 var elms = answer.querySelectorAll('item'); |
223 for(var i = 0; i < elms.length; i++) { | 285 for(var i = 0; i < elms.length; i++) { |
224 var node = elms[i]; | 286 var node = elms[i]; |
225 var item = { | 287 var item = { |
226 id: node.getAttribute('id'), | 288 id: node.getAttribute('id'), |
227 name: node.querySelector('title').textContent, | 289 name: node.querySelector('title').textContent, |
228 src: node.querySelector('content').getAttribute('src'), | 290 src: node.querySelector('content').getAttribute('src'), |
229 type: node.querySelector('content').getAttribute('type'), | 291 type: node.querySelector('content').getAttribute('type'), |
230 } | 292 } |
231 var miniature = node.querySelector('link'); | 293 var miniature = node.querySelector('link'); |
232 if(miniature) | 294 if(miniature) |
233 item.miniature = miniature.getAttribute('href'); | 295 item.miniature = miniature.getAttribute('href'); |
234 items.push(item); | 296 items.push(item); |
235 }; | 297 }; |
236 if(aCallback) | 298 if(aCallback) |
237 aCallback(items); | 299 aCallback(items); |
238 }); | 300 }); |
239 } | 301 } |
240 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { | 302 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { |
241 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { | 303 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { |
242 if(answer.getAttribute('type') === 'result') | 304 if(answer.getAttribute('type') === 'result') |
243 aCallback(null, answer); | 305 aCallback(null, answer); |
244 else | 306 else |
245 aCallback(answer, null); | 307 aCallback(answer, null); |
246 }); | 308 }); |
247 }; | 309 }; |
248 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { | 310 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { |
249 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ | 311 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ |
250 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; | 312 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; |
251 if(accessmodel) | 313 if(accessmodel) |
252 aCallback(accessmodel); | 314 aCallback(accessmodel); |
253 else | 315 else |
254 aCallback(null); | 316 aCallback(null); |
255 }); | 317 }); |
256 } | 318 } |
257 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { | 319 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { |
258 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ | 320 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ |
259 if(aCallback) | 321 if(aCallback) |
260 aCallback(answer); | 322 aCallback(answer); |
261 }); | 323 }); |
262 } | 324 } |
263 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { | 325 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { |
264 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ | 326 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ |
265 if(answer.getAttribute('type') === 'result') | 327 if(answer.getAttribute('type') === 'result') |
266 aCallback(null, answer); | 328 aCallback(null, answer); |
267 else | 329 else |
268 aCallback(answer, null); | 330 aCallback(answer, null); |
269 }); | 331 }); |
270 } | 332 } |
271 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { | 333 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { |
272 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ | 334 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ |
273 if(aCallback) | 335 if(aCallback) |
274 aCallback(answer); | 336 aCallback(answer); |
275 }); | 337 }); |
276 } | 338 } |
277 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { | 339 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { |
278 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { | 340 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { |
279 if((answer.getAttribute('type') === 'result') && aCallback) { | 341 if((answer.getAttribute('type') === 'result') && aCallback) { |
280 var affiliations = {}; | 342 var affiliations = {}; |
281 answer.querySelectorAll('affiliation').forEach(function(affiliation) { | 343 answer.querySelectorAll('affiliation').forEach(function(affiliation) { |
282 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); | 344 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); |
283 }) | 345 }) |
284 aCallback(affiliations); | 346 aCallback(affiliations); |
285 } | 347 } |
286 }); | 348 }); |
287 }; | 349 }; |
288 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { | 350 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { |
289 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); | 351 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); |
290 }; | 352 }; |
291 ////// | 353 ////// |
292 //IM// | 354 //IM// |
293 ////// | 355 ////// |
294 Lightstring.stanza.message = { | 356 Lightstring.stanza.message = { |
295 normal: function(aTo, aSubject, aText) { | 357 normal: function(aTo, aSubject, aText) { |
296 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>"; | 358 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>"; |
297 }, | 359 }, |
298 chat: function(aTo, aText) { | 360 chat: function(aTo, aText) { |
299 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>"; | 361 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>"; |
300 } | 362 } |
301 }; | 363 }; |
302 | 364 |