comparison plugins.js @ 20:7fcccf59e6ec

Always use two-spaces indent.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Jan 2012 15:04:53 +0100
parents 6707f450549e
children 82bffc4a07a9
comparison
equal deleted inserted replaced
17:b7bd814333eb 20:7fcccf59e6ec
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 field = answer.querySelector('field[var="pubsub#creator"] > value');
158 var creator = field ? field.textContent : ''; 158 var creator = field ? field.textContent : '';
159 //FIXME callback the entire data 159 //FIXME callback the entire data
160 aCallback(creator); 160 aCallback(creator);
161 }); 161 });
162 }; 162 };
163 ////////// 163 //////////
164 //PubSub// 164 //PubSub//
165 ////////// 165 //////////
166 Lightstring.NS.x = "jabber:x:data"; 166 Lightstring.NS.x = "jabber:x:data";
167 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub"; 167 Lightstring.NS.pubsub = "http://jabber.org/protocol/pubsub";
168 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; 168 Lightstring.NS.pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
169 Lightstring.stanza.pubsub = { 169 Lightstring.stanza.pubsub = {
170 getConfig: function(aTo, aNode) { 170 getConfig: function(aTo, aNode) {
171 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>"; 171 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><configure node='"+aNode+"'/></pubsub></iq>";
172 }, 172 },
173 items: function(aTo, aNode) { 173 items: function(aTo, aNode) {
174 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>"; 174 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><items node='"+aNode+"'/></pubsub></iq>";
175 }, 175 },
176 affiliations: function(aTo, aNode) { 176 affiliations: function(aTo, aNode) {
177 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>"; 177 return "<iq type='get' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'/></pubsub></iq>";
178 }, 178 },
179 publish: function(aTo, aNode, aItem, aId) { 179 publish: function(aTo, aNode, aItem, aId) {
180 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>"; 180 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><publish node='"+aNode+"'><item id='"+aId+"'>"+aItem+"</item></publish></pubsub></iq>";
181 }, 181 },
182 retract: function(aTo, aNode, aItem) { 182 retract: function(aTo, aNode, aItem) {
183 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>"; 183 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><retract node='"+aNode+"'><item id='"+aItem+"'/></retract></pubsub></iq>";
184 }, 184 },
185 'delete': function(aTo, aNode, aURI) { 185 'delete': function(aTo, aNode, aURI) {
186 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>"; 186 return "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><delete node='"+aNode+"'/></pubsub></iq>";
187 }, 187 },
188 create: function(aTo, aNode, aFields) { 188 create: function(aTo, aNode, aFields) {
189 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>"; 189 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub+"'><create node='"+aNode+"'/>";
190 if(aFields) { 190 if(aFields) {
191 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>" 191 iq += "<configure><x xmlns='"+Lightstring.NS.x+"' type='submit'>"
192 aFields.forEach(function(field) { 192 aFields.forEach(function(field) {
193 iq += field; 193 iq += field;
194 }); 194 });
195 iq += "</x></configure>"; 195 iq += "</x></configure>";
196 } 196 }
197 iq += "</pubsub></iq>"; 197 iq += "</pubsub></iq>";
198 return iq; 198 return iq;
199 }, 199 },
200 setAffiliations: function(aTo, aNode, aAffiliations) { 200 setAffiliations: function(aTo, aNode, aAffiliations) {
201 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>"; 201 var iq = "<iq type='set' to='"+aTo+"'><pubsub xmlns='"+Lightstring.NS.pubsub_owner+"'><affiliations node='"+aNode+"'>";
202 for(var i = 0; i < aAffiliations.length; i++) { 202 for(var i = 0; i < aAffiliations.length; i++) {
203 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>" 203 iq += "<affiliation jid='"+aAffiliations[i][0]+"' affiliation='"+aAffiliations[i][1]+"'/>"
204 } 204 }
205 iq += "</affiliations></pubsub></iq>"; 205 iq += "</affiliations></pubsub></iq>";
206 return iq; 206 return iq;
207 }, 207 },
208 }; 208 };
209 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) { 209 Lightstring.pubsubItems = function(aConnection, aTo, aNode, aCallback) {
210 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){ 210 aConnection.send(Lightstring.stanza.pubsub.items(aTo, aNode), function(answer){
211 var items = []; 211 var items = [];
212 var elms = answer.querySelectorAll('item'); 212 var elms = answer.querySelectorAll('item');
213 for(var i = 0; i < elms.length; i++) { 213 for(var i = 0; i < elms.length; i++) {
214 var node = elms[i]; 214 var node = elms[i];
215 var item = { 215 var item = {
216 id: node.getAttribute('id'), 216 id: node.getAttribute('id'),
217 name: node.querySelector('title').textContent, 217 name: node.querySelector('title').textContent,
218 src: node.querySelector('content').getAttribute('src'), 218 src: node.querySelector('content').getAttribute('src'),
219 type: node.querySelector('content').getAttribute('type'), 219 type: node.querySelector('content').getAttribute('type'),
220 } 220 }
221 var miniature = node.querySelector('link'); 221 var miniature = node.querySelector('link');
222 if(miniature) 222 if(miniature)
223 item.miniature = miniature.getAttribute('href'); 223 item.miniature = miniature.getAttribute('href');
224 items.push(item); 224 items.push(item);
225 }; 225 };
226 if(aCallback) 226 if(aCallback)
227 aCallback(items); 227 aCallback(items);
228 }); 228 });
229 } 229 }
230 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) { 230 Lightstring.pubsubCreate = function(aConnection, aTo, aNode, aFields, aCallback) {
231 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) { 231 aConnection.send(Lightstring.stanza.pubsub.create(aTo, aNode, aFields), function(answer) {
232 if(answer.getAttribute('type') === 'result') 232 if(answer.getAttribute('type') === 'result')
233 aCallback(null, answer); 233 aCallback(null, answer);
234 else 234 else
235 aCallback(answer, null); 235 aCallback(answer, null);
236 }); 236 });
237 }; 237 };
238 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) { 238 Lightstring.pubsubConfig = function(aConnection, aTo, aNode, aCallback) {
239 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){ 239 aConnection.send(Lightstring.stanza.pubsub.getConfig(aTo, aNode), function(answer){
240 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent; 240 var accessmodel = answer.querySelector('field[var="pubsub#access_model"]').lastChild.textContent;
241 if(accessmodel) 241 if(accessmodel)
242 aCallback(accessmodel); 242 aCallback(accessmodel);
243 else 243 else
244 aCallback(null); 244 aCallback(null);
245 }); 245 });
246 } 246 }
247 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) { 247 Lightstring.pubsubRetract = function(aConnection, aTo, aNode, aItem, aCallback) {
248 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){ 248 aConnection.send(Lightstring.stanza.pubsub.retract(aTo, aNode, aItem), function(answer){
249 if(aCallback) 249 if(aCallback)
250 aCallback(answer); 250 aCallback(answer);
251 }); 251 });
252 } 252 }
253 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) { 253 Lightstring.pubsubPublish = function(aConnection, aTo, aNode, aItem, aId, aCallback) {
254 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){ 254 aConnection.send(Lightstring.stanza.pubsub.publish(aTo, aNode, aItem, aId), function(answer){
255 if(answer.getAttribute('type') === 'result') 255 if(answer.getAttribute('type') === 'result')
256 aCallback(null, answer); 256 aCallback(null, answer);
257 else 257 else
258 aCallback(answer, null); 258 aCallback(answer, null);
259 }); 259 });
260 } 260 }
261 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) { 261 Lightstring.pubsubDelete = function(aConnection, aTo, aNode, aCallback) {
262 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){ 262 aConnection.send(Lightstring.stanza.pubsub.delete(aTo, aNode), function(answer){
263 if(aCallback) 263 if(aCallback)
264 aCallback(answer); 264 aCallback(answer);
265 }); 265 });
266 } 266 }
267 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) { 267 Lightstring.pubsubGetAffiliations = function(aConnection, aTo, aNode, aCallback) {
268 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) { 268 aConnection.send(Lightstring.stanza.pubsub.affiliations(aTo, aNode), function(answer) {
269 if((answer.getAttribute('type') === 'result') && aCallback) { 269 if((answer.getAttribute('type') === 'result') && aCallback) {
270 var affiliations = {}; 270 var affiliations = {};
271 answer.querySelectorAll('affiliation').forEach(function(affiliation) { 271 answer.querySelectorAll('affiliation').forEach(function(affiliation) {
272 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation"); 272 affiliations[affiliation.getAttribute("jid")] = affiliation.getAttribute("affiliation");
273 }) 273 })
274 aCallback(affiliations); 274 aCallback(affiliations);
275 } 275 }
276 }); 276 });
277 }; 277 };
278 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) { 278 Lightstring.pubsubSetAffiliations = function(aConnection, aTo, aNode, aAffiliations, aCallback) {
279 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations)); 279 aConnection.send(Lightstring.stanza.pubsub.setAffiliations(aTo, aNode, aAffiliations));
280 }; 280 };
281 ////// 281 //////
282 //IM// 282 //IM//
283 ////// 283 //////
284 Lightstring.stanza.message = { 284 Lightstring.stanza.message = {
285 normal: function(aTo, aSubject, aText) { 285 normal: function(aTo, aSubject, aText) {
286 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>"; 286 return "<message type='normal' to='"+aTo+"'><subject>"+aSubject+"</subject><body>"+aText+"</body></message>";
287 }, 287 },
288 chat: function(aTo, aText) { 288 chat: function(aTo, aText) {
289 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>"; 289 return "<message type='chat' to='"+aTo+"'><body>"+aText+"</body></message>";
290 } 290 }
291 }; 291 };
292 292