1 | dojo.provide("dojox.xmpp.xmppSession"); |
---|
2 | |
---|
3 | dojo.require("dojox.xmpp.TransportSession"); |
---|
4 | dojo.require("dojox.xmpp.RosterService"); |
---|
5 | dojo.require("dojox.xmpp.PresenceService"); |
---|
6 | dojo.require("dojox.xmpp.UserService"); |
---|
7 | dojo.require("dojox.xmpp.ChatService"); |
---|
8 | dojo.require("dojox.xmpp.sasl"); |
---|
9 | |
---|
10 | dojox.xmpp.xmpp = { |
---|
11 | STREAM_NS: 'http://etherx.jabber.org/streams', |
---|
12 | CLIENT_NS: 'jabber:client', |
---|
13 | STANZA_NS: 'urn:ietf:params:xml:ns:xmpp-stanzas', |
---|
14 | SASL_NS: 'urn:ietf:params:xml:ns:xmpp-sasl', |
---|
15 | BIND_NS: 'urn:ietf:params:xml:ns:xmpp-bind', |
---|
16 | SESSION_NS: 'urn:ietf:params:xml:ns:xmpp-session', |
---|
17 | BODY_NS: "http://jabber.org/protocol/httpbind", |
---|
18 | |
---|
19 | XHTML_BODY_NS: "http://www.w3.org/1999/xhtml", |
---|
20 | XHTML_IM_NS: "http://jabber.org/protocol/xhtml-im", |
---|
21 | |
---|
22 | INACTIVE: "Inactive", |
---|
23 | CONNECTED: "Connected", |
---|
24 | ACTIVE: "Active", |
---|
25 | TERMINATE: "Terminate", |
---|
26 | LOGIN_FAILURE: "LoginFailure", |
---|
27 | |
---|
28 | INVALID_ID: -1, |
---|
29 | NO_ID: 0, |
---|
30 | |
---|
31 | error:{ |
---|
32 | BAD_REQUEST: 'bad-request', |
---|
33 | CONFLICT: 'conflict', |
---|
34 | FEATURE_NOT_IMPLEMENTED: 'feature-not-implemented', |
---|
35 | FORBIDDEN: 'forbidden', |
---|
36 | GONE: 'gone', |
---|
37 | INTERNAL_SERVER_ERROR: 'internal-server-error', |
---|
38 | ITEM_NOT_FOUND: 'item-not-found', |
---|
39 | ID_MALFORMED: 'jid-malformed', |
---|
40 | NOT_ACCEPTABLE: 'not-acceptable', |
---|
41 | NOT_ALLOWED: 'not-allowed', |
---|
42 | NOT_AUTHORIZED: 'not-authorized', |
---|
43 | SERVICE_UNAVAILABLE: 'service-unavailable', |
---|
44 | SUBSCRIPTION_REQUIRED: 'subscription-required', |
---|
45 | UNEXPECTED_REQUEST: 'unexpected-request' |
---|
46 | } |
---|
47 | }; |
---|
48 | |
---|
49 | dojox.xmpp.xmppSession = function(props){ |
---|
50 | this.roster = []; |
---|
51 | this.chatRegister = []; |
---|
52 | this._iqId = Math.round(Math.random() * 1000000000); |
---|
53 | |
---|
54 | //mixin any options that we want to provide to this service |
---|
55 | if (props && dojo.isObject(props)) { |
---|
56 | dojo.mixin(this, props); |
---|
57 | } |
---|
58 | |
---|
59 | this.session = new dojox.xmpp.TransportSession(props); |
---|
60 | dojo.connect(this.session, "onReady", this, "onTransportReady"); |
---|
61 | dojo.connect(this.session, "onTerminate", this, "onTransportTerminate"); |
---|
62 | dojo.connect(this.session, "onProcessProtocolResponse", this, "processProtocolResponse"); |
---|
63 | }; |
---|
64 | |
---|
65 | |
---|
66 | dojo.extend(dojox.xmpp.xmppSession, { |
---|
67 | |
---|
68 | roster: [], |
---|
69 | chatRegister: [], |
---|
70 | _iqId: 0, |
---|
71 | |
---|
72 | open: function(user, password, resource){ |
---|
73 | |
---|
74 | if (!user) { |
---|
75 | throw new Error("User id cannot be null"); |
---|
76 | } else { |
---|
77 | this.jid = user; |
---|
78 | if(user.indexOf('@') == -1) { |
---|
79 | this.jid = this.jid + '@' + this.domain; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | //allow null password here as its not needed in the SSO case |
---|
84 | if (password) { |
---|
85 | this.password = password; |
---|
86 | } |
---|
87 | |
---|
88 | //normally you should NOT supply a resource and let the server send you one |
---|
89 | //as part of your jid...see onBindResource() |
---|
90 | if (resource) { |
---|
91 | this.resource = resource; |
---|
92 | } |
---|
93 | |
---|
94 | this.session.open(); |
---|
95 | }, |
---|
96 | |
---|
97 | close: function(){ |
---|
98 | this.state = dojox.xmpp.xmpp.TERMINATE; |
---|
99 | this.session.close(dojox.xmpp.util.createElement("presence",{type:"unavailable",xmlns:dojox.xmpp.xmpp.CLIENT_NS},true)); |
---|
100 | }, |
---|
101 | |
---|
102 | processProtocolResponse: function(msg){ |
---|
103 | //console.log("xmppSession::processProtocolResponse() ", msg, msg.nodeName); |
---|
104 | var type = msg.nodeName; |
---|
105 | var nsIndex =type.indexOf(":"); |
---|
106 | if(nsIndex > 0) { |
---|
107 | type = type.substring(nsIndex+1); |
---|
108 | } |
---|
109 | switch(type){ |
---|
110 | case "iq": |
---|
111 | case "presence": |
---|
112 | case "message": |
---|
113 | case "features": |
---|
114 | this[type + "Handler"](msg); |
---|
115 | break; |
---|
116 | default: |
---|
117 | //console.log("default action?", msg.getAttribute('xmlns')); |
---|
118 | if(msg.getAttribute('xmlns')==dojox.xmpp.xmpp.SASL_NS){ |
---|
119 | this.saslHandler(msg); |
---|
120 | } |
---|
121 | } |
---|
122 | }, |
---|
123 | |
---|
124 | //HANDLERS |
---|
125 | |
---|
126 | messageHandler: function(msg){ |
---|
127 | //console.log("xmppSession::messageHandler() ",msg); |
---|
128 | switch(msg.getAttribute('type')){ |
---|
129 | case "chat": |
---|
130 | this.chatHandler(msg); |
---|
131 | break; |
---|
132 | case "normal": |
---|
133 | default: |
---|
134 | this.simpleMessageHandler(msg); |
---|
135 | } |
---|
136 | |
---|
137 | }, |
---|
138 | |
---|
139 | iqHandler: function(msg){ |
---|
140 | //console.log("xmppSession::iqHandler()", msg); |
---|
141 | if (msg.getAttribute('type')=="set"){ |
---|
142 | this.iqSetHandler(msg); |
---|
143 | return; |
---|
144 | } else if (msg.getAttribute('type')=='get'){ |
---|
145 | // this.sendStanzaError('iq', this.domain, msg.getAttribute('from'), 'cancel', 'service-unavailable', 'service not implemented'); |
---|
146 | return; |
---|
147 | } |
---|
148 | }, |
---|
149 | |
---|
150 | presenceHandler: function(msg){ |
---|
151 | //console.log("xmppSession::presenceHandler()"); |
---|
152 | switch(msg.getAttribute('type')){ |
---|
153 | case 'subscribe': |
---|
154 | //console.log("PresenceHandler: ", msg.getAttribute('from')); |
---|
155 | this.presenceSubscriptionRequest(msg.getAttribute('from')); |
---|
156 | break; |
---|
157 | case 'subscribed': |
---|
158 | case 'unsubscribed': |
---|
159 | break; |
---|
160 | case 'error': |
---|
161 | this.processXmppError(msg); |
---|
162 | //console.log("xmppService::presenceHandler() Error"); |
---|
163 | break; |
---|
164 | default: |
---|
165 | this.presenceUpdate(msg); |
---|
166 | break; |
---|
167 | } |
---|
168 | }, |
---|
169 | |
---|
170 | featuresHandler: function(msg){ |
---|
171 | //console.log("xmppSession::featuresHandler() ",msg); |
---|
172 | var authMechanisms = []; |
---|
173 | var hasBindFeature = false; |
---|
174 | var hasSessionFeature = false; |
---|
175 | |
---|
176 | if(msg.hasChildNodes()){ |
---|
177 | for(var i=0; i<msg.childNodes.length;i++){ |
---|
178 | var n = msg.childNodes[i]; |
---|
179 | //console.log("featuresHandler::node", n); |
---|
180 | switch(n.nodeName){ |
---|
181 | case 'mechanisms': |
---|
182 | for (var x=0; x<n.childNodes.length; x++){ |
---|
183 | //console.log("featuresHandler::node::mechanisms", n.childNodes[x].firstChild.nodeValue); |
---|
184 | authMechanisms.push(n.childNodes[x].firstChild.nodeValue); |
---|
185 | } |
---|
186 | break; |
---|
187 | case 'bind': |
---|
188 | //if (n.getAttribute('xmlns')==dojox.xmpp.xmpp.BIND_NS) { |
---|
189 | hasBindFeature = true; |
---|
190 | // } |
---|
191 | break; |
---|
192 | case 'session': |
---|
193 | hasSessionFeature = true; |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | //console.log("Has connected/bind?", this.state, hasBindFeature, authMechanisms); |
---|
198 | if(this.state == dojox.xmpp.xmpp.CONNECTED){ |
---|
199 | if(!this.auth){ |
---|
200 | // start the login |
---|
201 | for(var i=0; i<authMechanisms.length; i++){ |
---|
202 | try{ |
---|
203 | this.auth = dojox.xmpp.sasl.registry.match(authMechanisms[i], this); |
---|
204 | break; |
---|
205 | }catch(e){ |
---|
206 | console.warn("No suitable auth mechanism found for: ", authMechanisms[i]); |
---|
207 | } |
---|
208 | } |
---|
209 | }else if(hasBindFeature){ |
---|
210 | this.bindResource(hasSessionFeature); |
---|
211 | } |
---|
212 | } |
---|
213 | }, |
---|
214 | |
---|
215 | saslHandler: function(msg){ |
---|
216 | //console.log("xmppSession::saslHandler() ", msg); |
---|
217 | if(msg.nodeName=="success"){ |
---|
218 | this.auth.onSuccess(); |
---|
219 | return; |
---|
220 | } |
---|
221 | |
---|
222 | if(msg.nodeName=="challenge"){ |
---|
223 | this.auth.onChallenge(msg); |
---|
224 | return; |
---|
225 | } |
---|
226 | |
---|
227 | if(msg.hasChildNodes()){ |
---|
228 | this.onLoginFailure(msg.firstChild.nodeName); |
---|
229 | this.session.setState('Terminate', msg.firstChild.nodeName); |
---|
230 | } |
---|
231 | }, |
---|
232 | |
---|
233 | sendRestart: function(){ |
---|
234 | this.session._sendRestart(); |
---|
235 | }, |
---|
236 | |
---|
237 | |
---|
238 | //SUB HANDLERS |
---|
239 | |
---|
240 | chatHandler: function(msg){ |
---|
241 | //console.log("xmppSession::chatHandler() ", msg); |
---|
242 | var message = { |
---|
243 | from: msg.getAttribute('from'), |
---|
244 | to: msg.getAttribute('to') |
---|
245 | } |
---|
246 | |
---|
247 | var chatState = null; |
---|
248 | //console.log("chat child node ", msg.childNodes, msg.childNodes.length); |
---|
249 | for (var i=0; i<msg.childNodes.length; i++){ |
---|
250 | var n = msg.childNodes[i]; |
---|
251 | if (n.hasChildNodes()){ |
---|
252 | //console.log("chat child node ", n); |
---|
253 | switch(n.nodeName){ |
---|
254 | case 'thread': |
---|
255 | message.chatid = n.firstChild.nodeValue; |
---|
256 | break; |
---|
257 | case 'body': |
---|
258 | if (!n.getAttribute('xmlns') || (n.getAttribute('xmlns')=="")){ |
---|
259 | message.body = n.firstChild.nodeValue; |
---|
260 | } |
---|
261 | break; |
---|
262 | case 'subject': |
---|
263 | message.subject = n.firstChild.nodeValue; |
---|
264 | case 'html': |
---|
265 | if (n.getAttribute('xmlns')==dojox.xmpp.xmpp.XHTML_IM_NS){ |
---|
266 | message.xhtml = n.getElementsByTagName("body")[0]; |
---|
267 | } |
---|
268 | break; |
---|
269 | case 'x': |
---|
270 | break; |
---|
271 | default: |
---|
272 | //console.log("xmppSession::chatHandler() Unknown node type: ",n.nodeName); |
---|
273 | } |
---|
274 | } |
---|
275 | /*//console.log("Foo", n, n.nodeName); |
---|
276 | if(n.getAttribute('xmlns')==dojox.xmpp.chat.CHAT_STATE_NS){ |
---|
277 | chatState = n.nodeName; |
---|
278 | }*/ |
---|
279 | } |
---|
280 | |
---|
281 | var found = -1; |
---|
282 | if (message.chatid){ |
---|
283 | for (var i=0; i< this.chatRegister.length; i++){ |
---|
284 | var ci = this.chatRegister[i]; |
---|
285 | ////console.log("ci.chatid: ", ci.chatid, message.chatid); |
---|
286 | if (ci && ci.chatid == message.chatid) { |
---|
287 | found = i; |
---|
288 | break; |
---|
289 | } |
---|
290 | } |
---|
291 | } else { |
---|
292 | for (var i=0; i< this.chatRegister.length; i++){ |
---|
293 | var ci = this.chatRegister[i]; |
---|
294 | if(ci){ |
---|
295 | if (ci.uid==this.getBareJid(message.from)){ |
---|
296 | found = i; |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | if (found>-1 && chatState){ |
---|
303 | var chat = this.chatRegister[found]; |
---|
304 | chat.setState(chatState); |
---|
305 | |
---|
306 | if (chat.firstMessage){ |
---|
307 | if (chatState == dojox.xmpp.chat.ACTIVE_STATE) { |
---|
308 | chat.useChatState = (chatState != null) ? true : false; |
---|
309 | chat.firstMessage = false; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | if ((!message.body || message.body=="") && !message.xhtml) {return;} |
---|
315 | |
---|
316 | if (found>-1){ |
---|
317 | var chat = this.chatRegister[found]; |
---|
318 | chat.recieveMessage(message); |
---|
319 | }else{ |
---|
320 | var chatInstance = new dojox.xmpp.ChatService(); |
---|
321 | chatInstance.uid = this.getBareJid(message.from); |
---|
322 | chatInstance.chatid = message.chatid; |
---|
323 | chatInstance.firstMessage = true; |
---|
324 | if(!chatState || chatState != dojox.xmpp.chat.ACTIVE_STATE){ |
---|
325 | this.useChatState = false; |
---|
326 | } |
---|
327 | this.registerChatInstance(chatInstance, message); |
---|
328 | } |
---|
329 | }, |
---|
330 | |
---|
331 | simpleMessageHandler: function(msg){ |
---|
332 | //console.log("xmppSession::simpleMessageHandler() ", msg); |
---|
333 | }, |
---|
334 | |
---|
335 | registerChatInstance: function(chatInstance, message){ |
---|
336 | chatInstance.setSession(this); |
---|
337 | this.chatRegister.push(chatInstance); |
---|
338 | this.onRegisterChatInstance(chatInstance, message); |
---|
339 | chatInstance.recieveMessage(message,true); |
---|
340 | }, |
---|
341 | |
---|
342 | iqSetHandler: function(msg){ |
---|
343 | if (msg.hasChildNodes()){ |
---|
344 | var fn = msg.firstChild; |
---|
345 | switch(fn.nodeName){ |
---|
346 | case 'query': |
---|
347 | if(fn.getAttribute('xmlns') == "jabber:iq:roster"){ |
---|
348 | this.rosterSetHandler(fn); |
---|
349 | this.sendIqResult(msg.getAttribute('id'), msg.getAttribute('from')); |
---|
350 | } |
---|
351 | break; |
---|
352 | default: |
---|
353 | // this.sendStanzaError('iq', this.domain, msg.getAttribute('id'), 'cancel', 'service-unavailable', 'service not implemented'); |
---|
354 | break; |
---|
355 | } |
---|
356 | } |
---|
357 | }, |
---|
358 | |
---|
359 | sendIqResult: function(iqId, to){ |
---|
360 | var req = { |
---|
361 | id: iqId, |
---|
362 | to: to || this.domain, |
---|
363 | type: 'result', |
---|
364 | from: this.jid + "/" + this.resource |
---|
365 | } |
---|
366 | this.dispatchPacket(dojox.xmpp.util.createElement("iq",req,true)); |
---|
367 | }, |
---|
368 | |
---|
369 | rosterSetHandler: function(elem){ |
---|
370 | //console.log("xmppSession::rosterSetHandler()", arguments); |
---|
371 | for (var i=0; i<elem.childNodes.length;i++){ |
---|
372 | var n = elem.childNodes[i]; |
---|
373 | |
---|
374 | if (n.nodeName=="item"){ |
---|
375 | var found = false; |
---|
376 | var state = -1; |
---|
377 | var rosterItem = null; |
---|
378 | var previousCopy = null; |
---|
379 | for(var x=0; x<this.roster.length;x++){ |
---|
380 | var r = this.roster[x]; |
---|
381 | if(n.getAttribute('jid')==r.jid){ |
---|
382 | found = true; |
---|
383 | if(n.getAttribute('subscription')=='remove'){ |
---|
384 | //remove the item |
---|
385 | rosterItem = { |
---|
386 | id: r.jid, |
---|
387 | name: r.name, |
---|
388 | groups:[] |
---|
389 | } |
---|
390 | |
---|
391 | for (var y=0;y<r.groups.length;y++){ |
---|
392 | rosterItem.groups.push(r.groups[y]); |
---|
393 | } |
---|
394 | |
---|
395 | this.roster.splice(x,1); |
---|
396 | state = dojox.xmpp.roster.REMOVED; |
---|
397 | |
---|
398 | } else { //update |
---|
399 | previousCopy = dojo.clone(r); |
---|
400 | var itemName = n.getAttribute('name'); |
---|
401 | if (itemName){ |
---|
402 | this.roster[x].name = itemName; |
---|
403 | } |
---|
404 | |
---|
405 | r.groups = []; |
---|
406 | |
---|
407 | if (n.getAttribute('subscription')){ |
---|
408 | r.status = n.getAttribute('subscription'); |
---|
409 | } |
---|
410 | |
---|
411 | r.substatus = dojox.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE; |
---|
412 | if(n.getAttribute('ask')=='subscribe'){ |
---|
413 | r.substatus = dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING; |
---|
414 | } |
---|
415 | |
---|
416 | for(var y=0;y<n.childNodes.length;y++){ |
---|
417 | var groupNode = n.childNodes[y]; |
---|
418 | if ((groupNode.nodeName=='group')&&(groupNode.hasChildNodes())){ |
---|
419 | var gname = groupNode.firstChild.nodeValue; |
---|
420 | r.groups.push(gname); |
---|
421 | } |
---|
422 | } |
---|
423 | rosterItem = r; |
---|
424 | state = dojox.xmpp.roster.CHANGED; |
---|
425 | } |
---|
426 | break; |
---|
427 | } |
---|
428 | } |
---|
429 | if(!found && (n.getAttribute('subscription')!='remove')){ |
---|
430 | r = this.createRosterEntry(n); |
---|
431 | rosterItem = r; |
---|
432 | state = dojox.xmpp.roster.ADDED; |
---|
433 | } |
---|
434 | |
---|
435 | switch(state){ |
---|
436 | case dojox.xmpp.roster.ADDED: |
---|
437 | this.onRosterAdded(rosterItem); |
---|
438 | break; |
---|
439 | case dojox.xmpp.roster.REMOVED: |
---|
440 | this.onRosterRemoved(rosterItem); |
---|
441 | break; |
---|
442 | case dojox.xmpp.roster.CHANGED: |
---|
443 | this.onRosterChanged(rosterItem, previousCopy); |
---|
444 | break; |
---|
445 | } |
---|
446 | } |
---|
447 | } |
---|
448 | }, |
---|
449 | |
---|
450 | presenceUpdate: function(msg){ |
---|
451 | if(msg.getAttribute('to')){ |
---|
452 | var jid = this.getBareJid(msg.getAttribute('to')); |
---|
453 | if(jid != this.jid) { |
---|
454 | //console.log("xmppService::presenceUpdate Update Recieved with wrong address - ",jid); |
---|
455 | return; |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | var fromRes = this.getResourceFromJid(msg.getAttribute('from')); |
---|
460 | |
---|
461 | var p = { |
---|
462 | from: this.getBareJid(msg.getAttribute('from')), |
---|
463 | resource: fromRes, |
---|
464 | show: dojox.xmpp.presence.STATUS_ONLINE, |
---|
465 | priority: 5, |
---|
466 | hasAvatar: false |
---|
467 | } |
---|
468 | |
---|
469 | if(msg.getAttribute('type')=='unavailable'){ |
---|
470 | p.show=dojox.xmpp.presence.STATUS_OFFLINE |
---|
471 | } |
---|
472 | |
---|
473 | for (var i=0; i<msg.childNodes.length;i++){ |
---|
474 | var n=msg.childNodes[i]; |
---|
475 | if (n.hasChildNodes()){ |
---|
476 | switch(n.nodeName){ |
---|
477 | case 'status': |
---|
478 | case 'show': |
---|
479 | p[n.nodeName]=n.firstChild.nodeValue; |
---|
480 | break; |
---|
481 | case 'status': |
---|
482 | p.priority=parseInt(n.firstChild.nodeValue); |
---|
483 | break; |
---|
484 | case 'x': |
---|
485 | if(n.firstChild && n.firstChild.firstChild && n.firstChild.firstChild.nodeValue != "") { |
---|
486 | p.avatarHash= n.firstChild.firstChild.nodeValue; |
---|
487 | p.hasAvatar = true; |
---|
488 | } |
---|
489 | break; |
---|
490 | } |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | this.onPresenceUpdate(p); |
---|
495 | }, |
---|
496 | |
---|
497 | retrieveRoster: function(){ |
---|
498 | ////console.log("xmppService::retrieveRoster()"); |
---|
499 | var props={ |
---|
500 | id: this.getNextIqId(), |
---|
501 | from: this.jid + "/" + this.resource, |
---|
502 | type: "get" |
---|
503 | } |
---|
504 | var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
---|
505 | req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:roster"},true)); |
---|
506 | req.append("</iq>"); |
---|
507 | |
---|
508 | var def = this.dispatchPacket(req,"iq", props.id); |
---|
509 | def.addCallback(this, "onRetrieveRoster"); |
---|
510 | |
---|
511 | }, |
---|
512 | |
---|
513 | getRosterIndex: function(jid){ |
---|
514 | if(jid.indexOf('@')==-1){ |
---|
515 | jid += '@' + this.domain; |
---|
516 | } |
---|
517 | for (var i=0; i<this.roster.length;i++){ |
---|
518 | if(jid == this.roster[i].jid) { return i; } |
---|
519 | } |
---|
520 | return -1; |
---|
521 | }, |
---|
522 | |
---|
523 | createRosterEntry: function(elem){ |
---|
524 | ////console.log("xmppService::createRosterEntry()"); |
---|
525 | var re = { |
---|
526 | name: elem.getAttribute('name'), |
---|
527 | jid: elem.getAttribute('jid'), |
---|
528 | groups: [], |
---|
529 | status: dojox.xmpp.presence.SUBSCRIPTION_NONE, |
---|
530 | substatus: dojox.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE |
---|
531 | // displayToUser: false |
---|
532 | } |
---|
533 | |
---|
534 | if (!re.name){ |
---|
535 | re.name = re.id; |
---|
536 | } |
---|
537 | |
---|
538 | |
---|
539 | |
---|
540 | for(var i=0; i<elem.childNodes.length;i++){ |
---|
541 | var n = elem.childNodes[i]; |
---|
542 | if (n.nodeName=='group' && n.hasChildNodes()){ |
---|
543 | re.groups.push(n.firstChild.nodeValue); |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | if (elem.getAttribute('subscription')){ |
---|
548 | re.status = elem.getAttribute('subscription'); |
---|
549 | } |
---|
550 | |
---|
551 | if (elem.getAttribute('ask')=='subscribe'){ |
---|
552 | re.substatus = dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING; |
---|
553 | } |
---|
554 | //Display contact rules from http://www.xmpp.org/extensions/xep-0162.html#contacts |
---|
555 | /* if(re.status == dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING || |
---|
556 | re.status == dojox.xmpp.presence.SUBSCRIPTION_TO || |
---|
557 | re.status == dojox.xmpp.presence.SUBSCRIPTION_BOTH || |
---|
558 | re.groups.length > 0 || |
---|
559 | re.name |
---|
560 | ) { |
---|
561 | re.displayToUser = true; |
---|
562 | } |
---|
563 | */ |
---|
564 | return re; |
---|
565 | }, |
---|
566 | |
---|
567 | bindResource: function(hasSession){ |
---|
568 | var props = { |
---|
569 | id: this.getNextIqId(), |
---|
570 | type: "set" |
---|
571 | } |
---|
572 | var bindReq = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", props, false)); |
---|
573 | bindReq.append(dojox.xmpp.util.createElement("bind", {xmlns: dojox.xmpp.xmpp.BIND_NS}, false)); |
---|
574 | |
---|
575 | if (this.resource){ |
---|
576 | bindReq.append(dojox.xmpp.util.createElement("resource")); |
---|
577 | bindReq.append(this.resource); |
---|
578 | bindReq.append("</resource>"); |
---|
579 | } |
---|
580 | |
---|
581 | bindReq.append("</bind></iq>"); |
---|
582 | |
---|
583 | var def = this.dispatchPacket(bindReq, "iq", props.id); |
---|
584 | def.addCallback(this, function(msg){ |
---|
585 | this.onBindResource(msg, hasSession); |
---|
586 | return msg; |
---|
587 | }); |
---|
588 | }, |
---|
589 | |
---|
590 | getNextIqId: function(){ |
---|
591 | return "im_" + this._iqId++; |
---|
592 | }, |
---|
593 | |
---|
594 | presenceSubscriptionRequest: function(msg) { |
---|
595 | this.onSubscriptionRequest(msg); |
---|
596 | /* |
---|
597 | this.onSubscriptionRequest({ |
---|
598 | from: msg, |
---|
599 | resource:"", |
---|
600 | show:"", |
---|
601 | status:"", |
---|
602 | priority: 5 |
---|
603 | }); |
---|
604 | */ |
---|
605 | }, |
---|
606 | |
---|
607 | dispatchPacket: function(msg, type, matchId){ |
---|
608 | if (this.state != "Terminate") { |
---|
609 | return this.session.dispatchPacket(msg,type,matchId); |
---|
610 | }else{ |
---|
611 | //console.log("xmppSession::dispatchPacket - Session in Terminate state, dropping packet"); |
---|
612 | } |
---|
613 | }, |
---|
614 | |
---|
615 | setState: function(state, message){ |
---|
616 | if (this.state != state){ |
---|
617 | if (this["on"+state]){ |
---|
618 | this["on"+state](state, this.state, message); |
---|
619 | } |
---|
620 | this.state=state; |
---|
621 | } |
---|
622 | }, |
---|
623 | |
---|
624 | search: function(searchString, service, searchAttribute){ |
---|
625 | var req={ |
---|
626 | id: this.getNextIqId(), |
---|
627 | "xml:lang": this.lang, |
---|
628 | type: 'set', |
---|
629 | from: this.jid + '/' + this.resource, |
---|
630 | to: service |
---|
631 | } |
---|
632 | var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",req,false)); |
---|
633 | request.append(dojox.xmpp.util.createElement('query',{xmlns:'jabber:iq:search'},false)); |
---|
634 | request.append(dojox.xmpp.util.createElement(searchAttribute,{},false)); |
---|
635 | request.append(searchString); |
---|
636 | request.append("</").append(searchAttribute).append(">"); |
---|
637 | request.append("</query></iq>"); |
---|
638 | |
---|
639 | var def = this.dispatchPacket(request.toString,"iq",req.id); |
---|
640 | def.addCallback(this, "_onSearchResults"); |
---|
641 | }, |
---|
642 | |
---|
643 | _onSearchResults: function(msg){ |
---|
644 | if ((msg.getAttribute('type')=='result')&&(msg.hasChildNodes())){ |
---|
645 | //console.log("xmppSession::_onSearchResults(): ", msg.firstChild); |
---|
646 | |
---|
647 | //call the search results event with an array of results |
---|
648 | this.onSearchResults([]); |
---|
649 | } |
---|
650 | }, |
---|
651 | |
---|
652 | // EVENTS |
---|
653 | |
---|
654 | onLogin: function(){ |
---|
655 | ////console.log("xmppSession::onLogin()"); |
---|
656 | this.retrieveRoster(); |
---|
657 | }, |
---|
658 | |
---|
659 | onLoginFailure: function(msg){ |
---|
660 | //console.log("xmppSession::onLoginFailure ", msg); |
---|
661 | }, |
---|
662 | |
---|
663 | onBindResource: function(msg, hasSession){ |
---|
664 | //console.log("xmppSession::onBindResource() ", msg); |
---|
665 | |
---|
666 | if (msg.getAttribute('type')=='result'){ |
---|
667 | //console.log("xmppSession::onBindResource() Got Result Message"); |
---|
668 | if ((msg.hasChildNodes()) && (msg.firstChild.nodeName=="bind")){ |
---|
669 | var bindTag = msg.firstChild; |
---|
670 | if ((bindTag.hasChildNodes()) && (bindTag.firstChild.nodeName=="jid")){ |
---|
671 | if (bindTag.firstChild.hasChildNodes()){ |
---|
672 | var fulljid = bindTag.firstChild.firstChild.nodeValue; |
---|
673 | this.jid = this.getBareJid(fulljid); |
---|
674 | this.resource = this.getResourceFromJid(fulljid); |
---|
675 | } |
---|
676 | } |
---|
677 | if(hasSession){ |
---|
678 | var props = { |
---|
679 | id: this.getNextIqId(), |
---|
680 | type: "set" |
---|
681 | } |
---|
682 | var bindReq = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", props, false)); |
---|
683 | bindReq.append(dojox.xmpp.util.createElement("session", {xmlns: dojox.xmpp.xmpp.SESSION_NS}, true)); |
---|
684 | bindReq.append("</iq>"); |
---|
685 | |
---|
686 | var def = this.dispatchPacket(bindReq, "iq", props.id); |
---|
687 | def.addCallback(this, "onBindSession"); |
---|
688 | return; |
---|
689 | } |
---|
690 | }else{ |
---|
691 | //console.log("xmppService::onBindResource() No Bind Element Found"); |
---|
692 | } |
---|
693 | |
---|
694 | this.onLogin(); |
---|
695 | |
---|
696 | }else if(msg.getAttribute('type')=='error'){ |
---|
697 | //console.log("xmppSession::onBindResource() Bind Error ", msg); |
---|
698 | var err = this.processXmppError(msg); |
---|
699 | this.onLoginFailure(err); |
---|
700 | } |
---|
701 | }, |
---|
702 | |
---|
703 | onBindSession: function(msg){ |
---|
704 | if(msg.getAttribute('type')=='error'){ |
---|
705 | //console.log("xmppSession::onBindSession() Bind Error ", msg); |
---|
706 | var err = this.processXmppError(msg); |
---|
707 | this.onLoginFailure(err); |
---|
708 | }else{ |
---|
709 | this.onLogin(); |
---|
710 | } |
---|
711 | }, |
---|
712 | |
---|
713 | onSearchResults: function(results){ |
---|
714 | //console.log("xmppSession::onSearchResult() ", results); |
---|
715 | }, |
---|
716 | |
---|
717 | onRetrieveRoster: function(msg){ |
---|
718 | ////console.log("xmppService::onRetrieveRoster() ", arguments); |
---|
719 | |
---|
720 | if ((msg.getAttribute('type')=='result') && msg.hasChildNodes()){ |
---|
721 | var query = msg.getElementsByTagName('query')[0]; |
---|
722 | if (query.getAttribute('xmlns')=="jabber:iq:roster"){ |
---|
723 | for (var i=0;i<query.childNodes.length;i++){ |
---|
724 | if (query.childNodes[i].nodeName=="item"){ |
---|
725 | this.roster[i] = this.createRosterEntry(query.childNodes[i]); |
---|
726 | } |
---|
727 | } |
---|
728 | } |
---|
729 | }else if(msg.getAttribute('type')=="error"){ |
---|
730 | //console.log("xmppService::storeRoster() Error recieved on roster get"); |
---|
731 | } |
---|
732 | |
---|
733 | ////console.log("Roster: ", this.roster); |
---|
734 | this.setState(dojox.xmpp.xmpp.ACTIVE); |
---|
735 | this.onRosterUpdated(); |
---|
736 | |
---|
737 | return msg; |
---|
738 | }, |
---|
739 | |
---|
740 | onRosterUpdated: function() {}, |
---|
741 | |
---|
742 | onSubscriptionRequest: function(req){}, |
---|
743 | |
---|
744 | onPresenceUpdate: function(p){}, |
---|
745 | |
---|
746 | onTransportReady: function(){ |
---|
747 | this.setState(dojox.xmpp.xmpp.CONNECTED); |
---|
748 | this.rosterService = new dojox.xmpp.RosterService(this); |
---|
749 | this.presenceService= new dojox.xmpp.PresenceService(this); |
---|
750 | this.userService = new dojox.xmpp.UserService(this); |
---|
751 | |
---|
752 | ////console.log("xmppSession::onTransportReady()"); |
---|
753 | }, |
---|
754 | |
---|
755 | onTransportTerminate: function(newState, oldState, message){ |
---|
756 | this.setState(dojox.xmpp.xmpp.TERMINATE, message); |
---|
757 | }, |
---|
758 | |
---|
759 | onConnected: function(){ |
---|
760 | ////console.log("xmppSession::onConnected()"); |
---|
761 | }, |
---|
762 | |
---|
763 | onTerminate: function(newState, oldState, message){ |
---|
764 | //console.log("xmppSession::onTerminate()", newState, oldState, message); |
---|
765 | }, |
---|
766 | |
---|
767 | onActive: function(){ |
---|
768 | ////console.log("xmppSession::onActive()"); |
---|
769 | //this.presenceService.publish({show: dojox.xmpp.presence.STATUS_ONLINE}); |
---|
770 | }, |
---|
771 | |
---|
772 | onRegisterChatInstance: function(chatInstance, message){ |
---|
773 | ////console.log("xmppSession::onRegisterChatInstance()"); |
---|
774 | }, |
---|
775 | |
---|
776 | onRosterAdded: function(ri){}, |
---|
777 | onRosterRemoved: function(ri){}, |
---|
778 | onRosterChanged: function(ri, previousCopy){}, |
---|
779 | |
---|
780 | //Utilities |
---|
781 | |
---|
782 | processXmppError: function(msg){ |
---|
783 | ////console.log("xmppSession::processXmppError() ", msg); |
---|
784 | var err = { |
---|
785 | stanzaType: msg.nodeName, |
---|
786 | id: msg.getAttribute('id') |
---|
787 | } |
---|
788 | |
---|
789 | for (var i=0; i<msg.childNodes.length; i++){ |
---|
790 | var n = msg.childNodes[i]; |
---|
791 | switch(n.nodeName){ |
---|
792 | case 'error': |
---|
793 | err.errorType = n.getAttribute('type'); |
---|
794 | for (var x=0; x< n.childNodes.length; x++){ |
---|
795 | var cn = n.childNodes[x]; |
---|
796 | if ((cn.nodeName=="text") && (cn.getAttribute('xmlns') == dojox.xmpp.xmpp.STANZA_NS) && cn.hasChildNodes()) { |
---|
797 | err.message = cn.firstChild.nodeValue; |
---|
798 | } else if ((cn.getAttribute('xmlns') == dojox.xmpp.xmpp.STANZA_NS) &&(!cn.hasChildNodes())){ |
---|
799 | err.condition = cn.nodeName; |
---|
800 | } |
---|
801 | } |
---|
802 | break; |
---|
803 | default: |
---|
804 | break; |
---|
805 | } |
---|
806 | } |
---|
807 | return err; |
---|
808 | }, |
---|
809 | |
---|
810 | sendStanzaError: function(stanzaType,to,id,errorType,condition,text){ |
---|
811 | ////console.log("xmppSession: sendStanzaError() ", arguments); |
---|
812 | var req = {type:'error'}; |
---|
813 | if (to) { req.to=to; } |
---|
814 | if (id) { req.id=id; } |
---|
815 | |
---|
816 | var request = new dojox.string.Builder(dojox.xmpp.util.createElement(stanzaType,req,false)); |
---|
817 | request.append(dojox.xmpp.util.createElement('error',{type:errorType},false)); |
---|
818 | request.append(dojox.xmpp.util.createElement('condition',{xmlns:dojox.xmpp.xmpp.STANZA_NS},true)); |
---|
819 | |
---|
820 | if(text){ |
---|
821 | var textAttr={ |
---|
822 | xmlns: dojox.xmpp.xmpp.STANZA_NS, |
---|
823 | "xml:lang":this.lang |
---|
824 | } |
---|
825 | request.append(dojox.xmpp.util.createElement('text',textAttr,false)); |
---|
826 | request.append(text).append("</text>"); |
---|
827 | } |
---|
828 | request.append("</error></").append(stanzaType).append(">"); |
---|
829 | |
---|
830 | this.dispatchPacket(request.toString()); |
---|
831 | }, |
---|
832 | |
---|
833 | getBareJid: function(jid){ |
---|
834 | var i = jid.indexOf('/'); |
---|
835 | if (i != -1){ |
---|
836 | return jid.substring(0, i); |
---|
837 | } |
---|
838 | return jid; |
---|
839 | }, |
---|
840 | |
---|
841 | getResourceFromJid: function(jid){ |
---|
842 | var i = jid.indexOf('/'); |
---|
843 | if (i != -1){ |
---|
844 | return jid.substring((i + 1), jid.length); |
---|
845 | } |
---|
846 | return ""; |
---|
847 | } |
---|
848 | |
---|
849 | }); |
---|