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