comparison plugins.js @ 2:f31a75c3b6c8

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