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