Mercurial > eldonilo > lightstring
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:f31a75c3b6c8 | 3:029c12b8f048 |
---|---|
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 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 | 15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 NodeList.prototype.forEach = function(aCallback) { | |
20 for(var i = 0; i<this.length; i++) | |
21 aCallback(this[i]); | |
22 } | |
23 | |
24 | |
19 // | 25 // |
20 //Roster | 26 //Roster |
21 // | 27 // |
22 Lighstring.NS.roster = 'jabber:iq:roster'; | 28 Lightstring.NS.roster = 'jabber:iq:roster'; |
23 Lighstring.stanza.roster = { | 29 Lightstring.stanza.roster = { |
24 'get': function() { | 30 'get': function() { |
25 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>"; | 31 return "<iq type='get'><query xmlns='"+Lightstring.NS.roster+"'/></iq>"; |
26 }, | 32 }, |
27 add: function(aAddress, aGroups, aCustomName) { | 33 add: function(aAddress, aGroups, aCustomName) { |
28 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree(); | 34 var iq = $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress}).tree(); |
35 }, | 41 }, |
36 remove: function(aAddress) { | 42 remove: function(aAddress) { |
37 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree(); | 43 return $iq({type: 'set'}).c('query', {xmlns: Lightstring.NS.roster}).c('item', {jid: aAddress, subscription: 'remove'}).tree(); |
38 } | 44 } |
39 }; | 45 }; |
40 Lighstring.getRoster = function(connection, aCallback) { | 46 Lightstring.getRoster = function(connection, aCallback) { |
41 connection.send(this.stanza.roster.get(), function(answer){ | 47 connection.send(this.stanza.roster.get(), function(answer){ |
42 var contacts = []; | 48 var contacts = []; |
43 answer.querySelectorAll('item').forEach(function(item) { | 49 answer.querySelectorAll('item').forEach(function(item) { |
44 var jid = item.getAttribute('jid'); | 50 var jid = item.getAttribute('jid'); |
45 var name = item.getAttribute('name'); | 51 var name = item.getAttribute('name'); |
65 }); | 71 }); |
66 } | 72 } |
67 // | 73 // |
68 //vCard | 74 //vCard |
69 // | 75 // |
70 Lighstring.NS.vcard = 'vcard-temp'; | 76 Lightstring.NS.vcard = 'vcard-temp'; |
71 Lighstring.stanza.vcard = { | 77 Lightstring.stanza.vcard = { |
72 'get': function(aTo) { | 78 'get': function(aTo) { |
73 if(aTo) | 79 if(aTo) |
74 return "<iq type='get' to='"+aTo+"'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; | 80 return "<iq type='get' to='"+aTo+"'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; |
75 else | 81 else |
76 return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; | 82 return "<iq type='get'><vCard xmlns='"+Lightstring.NS.vcard+"'/></iq>"; |
77 } | 83 } |
78 }; | 84 }; |
79 //FIXME we should return a proper vcard, not an XMPP one | 85 //FIXME we should return a proper vcard, not an XMPP one |
80 Lighstring.getVcard = function(aConnection, aTo, aCallback) { | 86 Lightstring.getVcard = function(aConnection, aTo, aCallback) { |
81 aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){ | 87 aConnection.send(Lightstring.stanza.vcard.get(aTo), function(answer, err){ |
82 if(answer) { | 88 if(answer) { |
83 var vcard = answer.querySelector('vCard'); | 89 var vcard = answer.querySelector('vCard'); |
84 if(vcard) | 90 if(vcard) |
85 aCallback(vcard); | 91 aCallback(vcard); |
89 }); | 95 }); |
90 } | 96 } |
91 // | 97 // |
92 //Disco | 98 //Disco |
93 // | 99 // |
94 Lighstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info"; | 100 Lightstring.NS['disco#info'] = "http://jabber.org/protocol/disco#info"; |
95 Lighstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items"; | 101 Lightstring.NS['disco#items'] = "http://jabber.org/protocol/disco#items"; |
96 Lighstring.stanza.disco = { | 102 Lightstring.stanza.disco = { |
97 items: function(aTo, aNode) { | 103 items: function(aTo, aNode) { |
98 if(aTo) | 104 if(aTo) |
99 var iq = "<iq type='get' to='"+aTo+"'>"; | 105 var iq = "<iq type='get' to='"+aTo+"'>"; |
100 else | 106 else |
101 var iq = "<iq type='get'>"; | 107 var iq = "<iq type='get'>"; |
118 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>"; | 124 var query = "<query xmlns='"+Lightstring.NS['disco#info']+"'/>"; |
119 | 125 |
120 return iq+query+"</iq>"; | 126 return iq+query+"</iq>"; |
121 } | 127 } |
122 }; | 128 }; |
123 Lighstring.discoItems = function(aConnection, aTo, aCallback) { | 129 Lightstring.discoItems = function(aConnection, aTo, aCallback) { |
124 aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){ | 130 aConnection.send(Lightstring.stanza.disco.items(aTo), function(answer){ |
125 var items = []; | 131 var items = []; |
126 answer.querySelectorAll('item').forEach(function(node) { | 132 answer.querySelectorAll('item').forEach(function(node) { |
127 var item = { | 133 var item = { |
128 jid: node.getAttribute('jid'), | 134 jid: node.getAttribute('jid'), |
133 }); | 139 }); |
134 if(aCallback) | 140 if(aCallback) |
135 aCallback(items); | 141 aCallback(items); |
136 }); | 142 }); |
137 }; | 143 }; |
138 Lighstring.discoInfo = function(aConnection, aTo, aNode, aCallback) { | 144 Lightstring.discoInfo = function(aConnection, aTo, aNode, aCallback) { |
139 aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){ | 145 aConnection.send(Lightstring.stanza.disco.info(aTo, aNode), function(answer){ |
140 var field = answer.querySelector('field[var="pubsub#creator"] > value'); | 146 var field = answer.querySelector('field[var="pubsub#creator"] > value'); |
141 var creator = field ? field.textContent : ''; | 147 var creator = field ? field.textContent : ''; |
142 //FIXME callback the entire data | 148 //FIXME callback the entire data |
143 aCallback(creator); | 149 aCallback(creator); |
144 }); | 150 }); |
145 }; | 151 }; |
146 // | 152 // |
147 //PubSub | 153 //PubSub |
148 // | 154 // |
149 Lighstring.NS.x = "jabber:x:data"; | 155 Lightstring.NS.x = "jabber:x:data"; |
150 Lighstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; | 156 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; |
151 Lighstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; | 157 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; |
152 Lighstring.stanza.pubsub = { | 158 Lightstring.stanza.pubsub = { |
153 getConfig: function(aTo, aNode) { | 159 getConfig: function(aTo, aNode) { |
154 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; | 160 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; |
155 }, | 161 }, |
156 items: function(aTo, aNode) { | 162 items: function(aTo, aNode) { |
157 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; | 163 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; |
187 } | 193 } |
188 iq += "</affiliations></pubsub></iq>"; | 194 iq += "</affiliations></pubsub></iq>"; |
189 return iq; | 195 return iq; |
190 }, | 196 }, |
191 }; | 197 }; |
192 Lighstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { | 198 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { |
193 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ | 199 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ |
194 var items = []; | 200 var items = []; |
195 answer.querySelectorAll('item').forEach(function(node) { | 201 answer.querySelectorAll('item').forEach(function(node) { |
196 var item = { | 202 var item = { |
197 id: node.getAttribute('id'), | 203 id: node.getAttribute('id'), |
198 name: node.querySelector('title').textContent, | 204 name: node.querySelector('title').textContent, |
199 src: node.querySelector('content').getAttribute('src'), | 205 src: node.querySelector('content').getAttribute('src'), |
200 type: node.querySelector('content').getAttribute('type'), | 206 type: node.querySelector('content').getAttribute('type'), |
201 } | 207 } |
202 var thumbnail = node.querySelector('link'); | 208 var miniature = node.querySelector('link'); |
203 if(thumbnail) | 209 if(miniature) |
204 item.thumbnail = thumbnail.getAttribute('href'); | 210 item.miniature = miniature.getAttribute('href'); |
205 items.push(item); | 211 items.push(item); |
206 }) | 212 }) |
207 if(aCallback) | 213 if(aCallback) |
208 aCallback(items); | 214 aCallback(items); |
209 }); | 215 }); |
210 } | 216 } |
211 Lighstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { | 217 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { |
212 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { | 218 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { |
213 if(answer.getAttribute('type') === 'result') | 219 if(answer.getAttribute('type') === 'result') |
214 aCallback(null, answer); | 220 aCallback(null, answer); |
215 else | 221 else |
216 aCallback(answer, null); | 222 aCallback(answer, null); |
217 }); | 223 }); |
218 }; | 224 }; |
219 Lighstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { | 225 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { |
220 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ | 226 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ |
221 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; | 227 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; |
222 if(accessmodel) | 228 if(accessmodel) |
223 aCallback(accessmodel); | 229 aCallback(accessmodel); |
224 else | 230 else |
225 aCallback(null); | 231 aCallback(null); |
226 }); | 232 }); |
227 } | 233 } |
228 Lighstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { | 234 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { |
229 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ | 235 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ |
230 if(aCallback) | 236 if(aCallback) |
231 aCallback(answer); | 237 aCallback(answer); |
232 }); | 238 }); |
233 } | 239 } |
234 Lighstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { | 240 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { |
235 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ | 241 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ |
236 if(answer.getAttribute('type') === 'result') | 242 if(answer.getAttribute('type') === 'result') |
237 aCallback(null, answer); | 243 aCallback(null, answer); |
238 else | 244 else |
239 aCallback(answer, null); | 245 aCallback(answer, null); |
240 }); | 246 }); |
241 } | 247 } |
242 Lighstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { | 248 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { |
243 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ | 249 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ |
244 if(aCallback) | 250 if(aCallback) |
245 aCallback(answer); | 251 aCallback(answer); |
246 }); | 252 }); |
247 } | 253 } |
248 Lighstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { | 254 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { |
249 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { | 255 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { |
250 if((answer.getAttribute('type') === 'result') && aCallback) { | 256 if((answer.getAttribute('type') === 'result') && aCallback) { |
251 var affiliations = {}; | 257 var affiliations = {}; |
252 answer.querySelectorAll('affiliation').forEach(function(affiliation) { | 258 answer.querySelectorAll('affiliation').forEach(function(affiliation) { |
253 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); | 259 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); |
254 }) | 260 }) |
255 aCallback(affiliations); | 261 aCallback(affiliations); |
256 } | 262 } |
257 }); | 263 }); |
258 }; | 264 }; |
259 Lighstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { | 265 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { |
260 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); | 266 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); |
261 }; | 267 }; |
262 // | 268 // |
263 //IM | 269 //IM |
264 // | 270 // |
265 Lighstring.stanza.message = { | 271 Lightstring.stanza.message = { |
266 normal: function(aTo, aSubject, aText) { | 272 normal: function(aTo, aSubject, aText) { |
267 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>"; | 273 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>"; |
268 }, | 274 }, |
269 chat: function(aTo, aText) { | 275 chat: function(aTo, aText) { |
270 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>"; | 276 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>"; |