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