| 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 |
|
| 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 |
|
| 84 |
if (password) { |
| 85 |
this.password = password; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 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 |
|
| 118 |
if(msg.getAttribute('xmlns')==dojox.xmpp.xmpp.SASL_NS){ |
| 119 |
this.saslHandler(msg); |
| 120 |
} |
| 121 |
} |
| 122 |
}, |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
messageHandler: function(msg){ |
| 127 |
|
| 128 |
switch(msg.getAttribute('type')){ |
| 129 |
case "chat": |
| 130 |
this.chatHandler(msg); |
| 131 |
break; |
| 132 |
case "normal": |
| 133 |
break; |
| 134 |
default: |
| 135 |
this.simpleMessageHandler(msg); |
| 136 |
} |
| 137 |
|
| 138 |
}, |
| 139 |
|
| 140 |
iqHandler: function(msg){ |
| 141 |
|
| 142 |
if (msg.getAttribute('type')=="set"){ |
| 143 |
this.iqSetHandler(msg); |
| 144 |
return; |
| 145 |
} else if (msg.getAttribute('type')=='get'){ |
| 146 |
|
| 147 |
return; |
| 148 |
} |
| 149 |
}, |
| 150 |
|
| 151 |
presenceHandler: function(msg){ |
| 152 |
|
| 153 |
switch(msg.getAttribute('type')){ |
| 154 |
case 'subscribe': |
| 155 |
|
| 156 |
this.presenceSubscriptionRequest(msg.getAttribute('from')); |
| 157 |
break; |
| 158 |
case 'subscribed': |
| 159 |
case 'unsubscribed': |
| 160 |
break; |
| 161 |
case 'error': |
| 162 |
this.processXmppError(msg); |
| 163 |
|
| 164 |
break; |
| 165 |
default: |
| 166 |
this.presenceUpdate(msg); |
| 167 |
break; |
| 168 |
} |
| 169 |
}, |
| 170 |
|
| 171 |
featuresHandler: function(msg){ |
| 172 |
|
| 173 |
var authMechanisms = []; |
| 174 |
var hasBindFeature = false; |
| 175 |
var hasSessionFeature = false; |
| 176 |
|
| 177 |
if(msg.hasChildNodes()){ |
| 178 |
for(var i=0; i<msg.childNodes.length;i++){ |
| 179 |
var n = msg.childNodes[i]; |
| 180 |
|
| 181 |
switch(n.nodeName){ |
| 182 |
case 'mechanisms': |
| 183 |
for (var x=0; x<n.childNodes.length; x++){ |
| 184 |
|
| 185 |
authMechanisms.push(n.childNodes[x].firstChild.nodeValue); |
| 186 |
} |
| 187 |
break; |
| 188 |
case 'bind': |
| 189 |
|
| 190 |
hasBindFeature = true; |
| 191 |
|
| 192 |
break; |
| 193 |
case 'session': |
| 194 |
hasSessionFeature = true; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if(this.state == dojox.xmpp.xmpp.CONNECTED){ |
| 200 |
if(!this.auth){ |
| 201 |
|
| 202 |
for(var i=0; i<authMechanisms.length; i++){ |
| 203 |
try{ |
| 204 |
this.auth = dojox.xmpp.sasl.registry.match(authMechanisms[i], this); |
| 205 |
break; |
| 206 |
}catch(e){ |
| 207 |
console.warn("No suitable auth mechanism found for: ", authMechanisms[i]); |
| 208 |
} |
| 209 |
} |
| 210 |
}else if(hasBindFeature){ |
| 211 |
this.bindResource(hasSessionFeature); |
| 212 |
} |
| 213 |
} |
| 214 |
}, |
| 215 |
|
| 216 |
saslHandler: function(msg){ |
| 217 |
|
| 218 |
if(msg.nodeName=="success"){ |
| 219 |
this.auth.onSuccess(); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
if(msg.nodeName=="challenge"){ |
| 224 |
this.auth.onChallenge(msg); |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
if(msg.hasChildNodes()){ |
| 229 |
this.onLoginFailure(msg.firstChild.nodeName); |
| 230 |
this.session.setState('Terminate', msg.firstChild.nodeName); |
| 231 |
} |
| 232 |
}, |
| 233 |
|
| 234 |
sendRestart: function(){ |
| 235 |
this.session._sendRestart(); |
| 236 |
}, |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
chatHandler: function(msg){ |
| 242 |
|
| 243 |
var message = { |
| 244 |
from: msg.getAttribute('from'), |
| 245 |
to: msg.getAttribute('to') |
| 246 |
}; |
| 247 |
|
| 248 |
var chatState = null; |
| 249 |
|
| 250 |
for (var i=0; i<msg.childNodes.length; i++){ |
| 251 |
var n = msg.childNodes[i]; |
| 252 |
if (n.hasChildNodes()){ |
| 253 |
|
| 254 |
switch(n.nodeName){ |
| 255 |
case 'thread': |
| 256 |
message.chatid = n.firstChild.nodeValue; |
| 257 |
break; |
| 258 |
case 'body': |
| 259 |
if (!n.getAttribute('xmlns') || (n.getAttribute('xmlns')==="")){ |
| 260 |
message.body = n.firstChild.nodeValue; |
| 261 |
} |
| 262 |
break; |
| 263 |
case 'subject': |
| 264 |
message.subject = n.firstChild.nodeValue; |
| 265 |
break; |
| 266 |
case 'html': |
| 267 |
if (n.getAttribute('xmlns')==dojox.xmpp.xmpp.XHTML_IM_NS){ |
| 268 |
message.xhtml = n.getElementsByTagName("body")[0]; |
| 269 |
} |
| 270 |
break; |
| 271 |
case 'x': |
| 272 |
break; |
| 273 |
default: |
| 274 |
|
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
var found = -1; |
| 284 |
if (message.chatid){ |
| 285 |
for (var i=0; i< this.chatRegister.length; i++){ |
| 286 |
var ci = this.chatRegister[i]; |
| 287 |
|
| 288 |
if (ci && ci.chatid == message.chatid) { |
| 289 |
found = i; |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
} else { |
| 294 |
for (var i=0; i< this.chatRegister.length; i++){ |
| 295 |
var ci = this.chatRegister[i]; |
| 296 |
if(ci){ |
| 297 |
if (ci.uid==this.getBareJid(message.from)){ |
| 298 |
found = i; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if (found>-1 && chatState){ |
| 305 |
var chat = this.chatRegister[found]; |
| 306 |
chat.setState(chatState); |
| 307 |
|
| 308 |
if (chat.firstMessage){ |
| 309 |
if (chatState == dojox.xmpp.chat.ACTIVE_STATE) { |
| 310 |
chat.useChatState = (chatState !== null) ? true : false; |
| 311 |
chat.firstMessage = false; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if ((!message.body || message.body==="") && !message.xhtml) {return;} |
| 317 |
|
| 318 |
if (found>-1){ |
| 319 |
var chat = this.chatRegister[found]; |
| 320 |
chat.receiveMessage(message); |
| 321 |
}else{ |
| 322 |
var chatInstance = new dojox.xmpp.ChatService(); |
| 323 |
chatInstance.uid = this.getBareJid(message.from); |
| 324 |
chatInstance.chatid = message.chatid; |
| 325 |
chatInstance.firstMessage = true; |
| 326 |
if(!chatState || chatState != dojox.xmpp.chat.ACTIVE_STATE){ |
| 327 |
this.useChatState = false; |
| 328 |
} |
| 329 |
this.registerChatInstance(chatInstance, message); |
| 330 |
} |
| 331 |
}, |
| 332 |
|
| 333 |
simpleMessageHandler: function(msg){ |
| 334 |
|
| 335 |
}, |
| 336 |
|
| 337 |
registerChatInstance: function(chatInstance, message){ |
| 338 |
chatInstance.setSession(this); |
| 339 |
this.chatRegister.push(chatInstance); |
| 340 |
this.onRegisterChatInstance(chatInstance, message); |
| 341 |
chatInstance.receiveMessage(message,true); |
| 342 |
}, |
| 343 |
|
| 344 |
iqSetHandler: function(msg){ |
| 345 |
if (msg.hasChildNodes()){ |
| 346 |
var fn = msg.firstChild; |
| 347 |
switch(fn.nodeName){ |
| 348 |
case 'query': |
| 349 |
if(fn.getAttribute('xmlns') == "jabber:iq:roster"){ |
| 350 |
this.rosterSetHandler(fn); |
| 351 |
this.sendIqResult(msg.getAttribute('id'), msg.getAttribute('from')); |
| 352 |
} |
| 353 |
break; |
| 354 |
default: |
| 355 |
|
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
}, |
| 360 |
|
| 361 |
sendIqResult: function(iqId, to){ |
| 362 |
var req = { |
| 363 |
id: iqId, |
| 364 |
to: to || this.domain, |
| 365 |
type: 'result', |
| 366 |
from: this.jid + "/" + this.resource |
| 367 |
}; |
| 368 |
this.dispatchPacket(dojox.xmpp.util.createElement("iq",req,true)); |
| 369 |
}, |
| 370 |
|
| 371 |
rosterSetHandler: function(elem){ |
| 372 |
|
| 373 |
var r; |
| 374 |
for (var i=0; i<elem.childNodes.length;i++){ |
| 375 |
var n = elem.childNodes[i]; |
| 376 |
|
| 377 |
if (n.nodeName=="item"){ |
| 378 |
var found = false; |
| 379 |
var state = -1; |
| 380 |
var rosterItem = null; |
| 381 |
var previousCopy = null; |
| 382 |
for(var x=0; x<this.roster.length;x++){ |
| 383 |
r = this.roster[x]; |
| 384 |
if(n.getAttribute('jid')==r.jid){ |
| 385 |
found = true; |
| 386 |
if(n.getAttribute('subscription')=='remove'){ |
| 387 |
|
| 388 |
rosterItem = { |
| 389 |
id: r.jid, |
| 390 |
name: r.name, |
| 391 |
groups:[] |
| 392 |
}; |
| 393 |
|
| 394 |
for (var y=0;y<r.groups.length;y++){ |
| 395 |
rosterItem.groups.push(r.groups[y]); |
| 396 |
} |
| 397 |
|
| 398 |
this.roster.splice(x,1); |
| 399 |
state = dojox.xmpp.roster.REMOVED; |
| 400 |
|
| 401 |
} else { |
| 402 |
previousCopy = dojo.clone(r); |
| 403 |
var itemName = n.getAttribute('name'); |
| 404 |
if (itemName){ |
| 405 |
this.roster[x].name = itemName; |
| 406 |
} |
| 407 |
|
| 408 |
r.groups = []; |
| 409 |
|
| 410 |
if (n.getAttribute('subscription')){ |
| 411 |
r.status = n.getAttribute('subscription'); |
| 412 |
} |
| 413 |
|
| 414 |
r.substatus = dojox.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE; |
| 415 |
if(n.getAttribute('ask')=='subscribe'){ |
| 416 |
r.substatus = dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING; |
| 417 |
} |
| 418 |
|
| 419 |
for(var y=0;y<n.childNodes.length;y++){ |
| 420 |
var groupNode = n.childNodes[y]; |
| 421 |
if ((groupNode.nodeName=='group')&&(groupNode.hasChildNodes())){ |
| 422 |
var gname = groupNode.firstChild.nodeValue; |
| 423 |
r.groups.push(gname); |
| 424 |
} |
| 425 |
} |
| 426 |
rosterItem = r; |
| 427 |
state = dojox.xmpp.roster.CHANGED; |
| 428 |
} |
| 429 |
break; |
| 430 |
} |
| 431 |
} |
| 432 |
if(!found && (n.getAttribute('subscription')!='remove')){ |
| 433 |
r = this.createRosterEntry(n); |
| 434 |
rosterItem = r; |
| 435 |
state = dojox.xmpp.roster.ADDED; |
| 436 |
} |
| 437 |
|
| 438 |
switch(state){ |
| 439 |
case dojox.xmpp.roster.ADDED: |
| 440 |
this.onRosterAdded(rosterItem); |
| 441 |
break; |
| 442 |
case dojox.xmpp.roster.REMOVED: |
| 443 |
this.onRosterRemoved(rosterItem); |
| 444 |
break; |
| 445 |
case dojox.xmpp.roster.CHANGED: |
| 446 |
this.onRosterChanged(rosterItem, previousCopy); |
| 447 |
break; |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
}, |
| 452 |
|
| 453 |
presenceUpdate: function(msg){ |
| 454 |
if(msg.getAttribute('to')){ |
| 455 |
var jid = this.getBareJid(msg.getAttribute('to')); |
| 456 |
if(jid != this.jid) { |
| 457 |
|
| 458 |
return; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
var fromRes = this.getResourceFromJid(msg.getAttribute('from')); |
| 463 |
|
| 464 |
var p = { |
| 465 |
from: this.getBareJid(msg.getAttribute('from')), |
| 466 |
resource: fromRes, |
| 467 |
show: dojox.xmpp.presence.STATUS_ONLINE, |
| 468 |
priority: 5, |
| 469 |
hasAvatar: false |
| 470 |
}; |
| 471 |
|
| 472 |
if(msg.getAttribute('type')=='unavailable'){ |
| 473 |
p.show=dojox.xmpp.presence.STATUS_OFFLINE; |
| 474 |
} |
| 475 |
|
| 476 |
for (var i=0; i<msg.childNodes.length;i++){ |
| 477 |
var n=msg.childNodes[i]; |
| 478 |
if (n.hasChildNodes()){ |
| 479 |
switch(n.nodeName){ |
| 480 |
case 'status': |
| 481 |
case 'show': |
| 482 |
p[n.nodeName]=n.firstChild.nodeValue; |
| 483 |
break; |
| 484 |
case 'priority': |
| 485 |
p.priority=parseInt(n.firstChild.nodeValue, 10); |
| 486 |
break; |
| 487 |
case 'x': |
| 488 |
if(n.firstChild && n.firstChild.firstChild && n.firstChild.firstChild.nodeValue !== "") { |
| 489 |
p.avatarHash= n.firstChild.firstChild.nodeValue; |
| 490 |
p.hasAvatar = true; |
| 491 |
} |
| 492 |
break; |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
this.onPresenceUpdate(p); |
| 498 |
}, |
| 499 |
|
| 500 |
retrieveRoster: function(){ |
| 501 |
|
| 502 |
var props={ |
| 503 |
id: this.getNextIqId(), |
| 504 |
from: this.jid + "/" + this.resource, |
| 505 |
type: "get" |
| 506 |
}; |
| 507 |
var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false)); |
| 508 |
req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:roster"},true)); |
| 509 |
req.append("</iq>"); |
| 510 |
|
| 511 |
var def = this.dispatchPacket(req,"iq", props.id); |
| 512 |
def.addCallback(this, "onRetrieveRoster"); |
| 513 |
|
| 514 |
}, |
| 515 |
|
| 516 |
getRosterIndex: function(jid){ |
| 517 |
if(jid.indexOf('@')==-1){ |
| 518 |
jid += '@' + this.domain; |
| 519 |
} |
| 520 |
for (var i=0; i<this.roster.length;i++){ |
| 521 |
if(jid == this.roster[i].jid) { return i; } |
| 522 |
} |
| 523 |
return -1; |
| 524 |
}, |
| 525 |
|
| 526 |
createRosterEntry: function(elem){ |
| 527 |
|
| 528 |
var re = { |
| 529 |
name: elem.getAttribute('name'), |
| 530 |
jid: elem.getAttribute('jid'), |
| 531 |
groups: [], |
| 532 |
status: dojox.xmpp.presence.SUBSCRIPTION_NONE, |
| 533 |
substatus: dojox.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE |
| 534 |
|
| 535 |
}; |
| 536 |
|
| 537 |
if (!re.name){ |
| 538 |
re.name = re.id; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
for(var i=0; i<elem.childNodes.length;i++){ |
| 544 |
var n = elem.childNodes[i]; |
| 545 |
if (n.nodeName=='group' && n.hasChildNodes()){ |
| 546 |
re.groups.push(n.firstChild.nodeValue); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
if (elem.getAttribute('subscription')){ |
| 551 |
re.status = elem.getAttribute('subscription'); |
| 552 |
} |
| 553 |
|
| 554 |
if (elem.getAttribute('ask')=='subscribe'){ |
| 555 |
re.substatus = dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
return re; |
| 568 |
}, |
| 569 |
|
| 570 |
bindResource: function(hasSession){ |
| 571 |
var props = { |
| 572 |
id: this.getNextIqId(), |
| 573 |
type: "set" |
| 574 |
}; |
| 575 |
var bindReq = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", props, false)); |
| 576 |
bindReq.append(dojox.xmpp.util.createElement("bind", {xmlns: dojox.xmpp.xmpp.BIND_NS}, false)); |
| 577 |
|
| 578 |
if (this.resource){ |
| 579 |
bindReq.append(dojox.xmpp.util.createElement("resource")); |
| 580 |
bindReq.append(this.resource); |
| 581 |
bindReq.append("</resource>"); |
| 582 |
} |
| 583 |
|
| 584 |
bindReq.append("</bind></iq>"); |
| 585 |
|
| 586 |
var def = this.dispatchPacket(bindReq, "iq", props.id); |
| 587 |
def.addCallback(this, function(msg){ |
| 588 |
this.onBindResource(msg, hasSession); |
| 589 |
return msg; |
| 590 |
}); |
| 591 |
}, |
| 592 |
|
| 593 |
getNextIqId: function(){ |
| 594 |
return "im_" + this._iqId++; |
| 595 |
}, |
| 596 |
|
| 597 |
presenceSubscriptionRequest: function(msg) { |
| 598 |
this.onSubscriptionRequest(msg); |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
}, |
| 609 |
|
| 610 |
dispatchPacket: function(msg, type, matchId){ |
| 611 |
if (this.state != "Terminate") { |
| 612 |
return this.session.dispatchPacket(msg,type,matchId); |
| 613 |
}else{ |
| 614 |
|
| 615 |
|
| 616 |
} |
| 617 |
}, |
| 618 |
|
| 619 |
setState: function(state, message){ |
| 620 |
if (this.state != state){ |
| 621 |
if (this["on"+state]){ |
| 622 |
this["on"+state](state, this.state, message); |
| 623 |
} |
| 624 |
this.state=state; |
| 625 |
} |
| 626 |
}, |
| 627 |
|
| 628 |
search: function(searchString, service, searchAttribute){ |
| 629 |
var req={ |
| 630 |
id: this.getNextIqId(), |
| 631 |
"xml:lang": this.lang, |
| 632 |
type: 'set', |
| 633 |
from: this.jid + '/' + this.resource, |
| 634 |
to: service |
| 635 |
}; |
| 636 |
var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",req,false)); |
| 637 |
request.append(dojox.xmpp.util.createElement('query',{xmlns:'jabber:iq:search'},false)); |
| 638 |
request.append(dojox.xmpp.util.createElement(searchAttribute,{},false)); |
| 639 |
request.append(searchString); |
| 640 |
request.append("</").append(searchAttribute).append(">"); |
| 641 |
request.append("</query></iq>"); |
| 642 |
|
| 643 |
var def = this.dispatchPacket(request.toString,"iq",req.id); |
| 644 |
def.addCallback(this, "_onSearchResults"); |
| 645 |
}, |
| 646 |
|
| 647 |
_onSearchResults: function(msg){ |
| 648 |
if ((msg.getAttribute('type')=='result')&&(msg.hasChildNodes())){ |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
this.onSearchResults([]); |
| 653 |
} |
| 654 |
}, |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
onLogin: function(){ |
| 659 |
|
| 660 |
this.retrieveRoster(); |
| 661 |
}, |
| 662 |
|
| 663 |
onLoginFailure: function(msg){ |
| 664 |
|
| 665 |
}, |
| 666 |
|
| 667 |
onBindResource: function(msg, hasSession){ |
| 668 |
|
| 669 |
|
| 670 |
if (msg.getAttribute('type')=='result'){ |
| 671 |
|
| 672 |
if ((msg.hasChildNodes()) && (msg.firstChild.nodeName=="bind")){ |
| 673 |
var bindTag = msg.firstChild; |
| 674 |
if ((bindTag.hasChildNodes()) && (bindTag.firstChild.nodeName=="jid")){ |
| 675 |
if (bindTag.firstChild.hasChildNodes()){ |
| 676 |
var fulljid = bindTag.firstChild.firstChild.nodeValue; |
| 677 |
this.jid = this.getBareJid(fulljid); |
| 678 |
this.resource = this.getResourceFromJid(fulljid); |
| 679 |
} |
| 680 |
} |
| 681 |
if(hasSession){ |
| 682 |
var props = { |
| 683 |
id: this.getNextIqId(), |
| 684 |
type: "set" |
| 685 |
}; |
| 686 |
var bindReq = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", props, false)); |
| 687 |
bindReq.append(dojox.xmpp.util.createElement("session", {xmlns: dojox.xmpp.xmpp.SESSION_NS}, true)); |
| 688 |
bindReq.append("</iq>"); |
| 689 |
|
| 690 |
var def = this.dispatchPacket(bindReq, "iq", props.id); |
| 691 |
def.addCallback(this, "onBindSession"); |
| 692 |
return; |
| 693 |
} |
| 694 |
}else{ |
| 695 |
|
| 696 |
} |
| 697 |
|
| 698 |
this.onLogin(); |
| 699 |
|
| 700 |
}else if(msg.getAttribute('type')=='error'){ |
| 701 |
|
| 702 |
var err = this.processXmppError(msg); |
| 703 |
this.onLoginFailure(err); |
| 704 |
} |
| 705 |
}, |
| 706 |
|
| 707 |
onBindSession: function(msg){ |
| 708 |
if(msg.getAttribute('type')=='error'){ |
| 709 |
|
| 710 |
var err = this.processXmppError(msg); |
| 711 |
this.onLoginFailure(err); |
| 712 |
}else{ |
| 713 |
this.onLogin(); |
| 714 |
} |
| 715 |
}, |
| 716 |
|
| 717 |
onSearchResults: function(results){ |
| 718 |
|
| 719 |
}, |
| 720 |
|
| 721 |
onRetrieveRoster: function(msg){ |
| 722 |
|
| 723 |
|
| 724 |
if ((msg.getAttribute('type')=='result') && msg.hasChildNodes()){ |
| 725 |
var query = msg.getElementsByTagName('query')[0]; |
| 726 |
if (query.getAttribute('xmlns')=="jabber:iq:roster"){ |
| 727 |
for (var i=0;i<query.childNodes.length;i++){ |
| 728 |
if (query.childNodes[i].nodeName=="item"){ |
| 729 |
this.roster[i] = this.createRosterEntry(query.childNodes[i]); |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
}else if(msg.getAttribute('type')=="error"){ |
| 734 |
|
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
this.setState(dojox.xmpp.xmpp.ACTIVE); |
| 739 |
this.onRosterUpdated(); |
| 740 |
|
| 741 |
return msg; |
| 742 |
}, |
| 743 |
|
| 744 |
onRosterUpdated: function() {}, |
| 745 |
|
| 746 |
onSubscriptionRequest: function(req){}, |
| 747 |
|
| 748 |
onPresenceUpdate: function(p){}, |
| 749 |
|
| 750 |
onTransportReady: function(){ |
| 751 |
this.setState(dojox.xmpp.xmpp.CONNECTED); |
| 752 |
this.rosterService = new dojox.xmpp.RosterService(this); |
| 753 |
this.presenceService= new dojox.xmpp.PresenceService(this); |
| 754 |
this.userService = new dojox.xmpp.UserService(this); |
| 755 |
|
| 756 |
|
| 757 |
}, |
| 758 |
|
| 759 |
onTransportTerminate: function(newState, oldState, message){ |
| 760 |
this.setState(dojox.xmpp.xmpp.TERMINATE, message); |
| 761 |
}, |
| 762 |
|
| 763 |
onConnected: function(){ |
| 764 |
|
| 765 |
}, |
| 766 |
|
| 767 |
onTerminate: function(newState, oldState, message){ |
| 768 |
|
| 769 |
}, |
| 770 |
|
| 771 |
onActive: function(){ |
| 772 |
|
| 773 |
|
| 774 |
}, |
| 775 |
|
| 776 |
onRegisterChatInstance: function(chatInstance, message){ |
| 777 |
|
| 778 |
}, |
| 779 |
|
| 780 |
onRosterAdded: function(ri){}, |
| 781 |
onRosterRemoved: function(ri){}, |
| 782 |
onRosterChanged: function(ri, previousCopy){}, |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
processXmppError: function(msg){ |
| 787 |
|
| 788 |
var err = { |
| 789 |
stanzaType: msg.nodeName, |
| 790 |
id: msg.getAttribute('id') |
| 791 |
}; |
| 792 |
|
| 793 |
for (var i=0; i<msg.childNodes.length; i++){ |
| 794 |
var n = msg.childNodes[i]; |
| 795 |
switch(n.nodeName){ |
| 796 |
case 'error': |
| 797 |
err.errorType = n.getAttribute('type'); |
| 798 |
for (var x=0; x< n.childNodes.length; x++){ |
| 799 |
var cn = n.childNodes[x]; |
| 800 |
if ((cn.nodeName=="text") && (cn.getAttribute('xmlns') == dojox.xmpp.xmpp.STANZA_NS) && cn.hasChildNodes()) { |
| 801 |
err.message = cn.firstChild.nodeValue; |
| 802 |
} else if ((cn.getAttribute('xmlns') == dojox.xmpp.xmpp.STANZA_NS) &&(!cn.hasChildNodes())){ |
| 803 |
err.condition = cn.nodeName; |
| 804 |
} |
| 805 |
} |
| 806 |
break; |
| 807 |
default: |
| 808 |
break; |
| 809 |
} |
| 810 |
} |
| 811 |
return err; |
| 812 |
}, |
| 813 |
|
| 814 |
sendStanzaError: function(stanzaType,to,id,errorType,condition,text){ |
| 815 |
|
| 816 |
var req = {type:'error'}; |
| 817 |
if (to) { req.to=to; } |
| 818 |
if (id) { req.id=id; } |
| 819 |
|
| 820 |
var request = new dojox.string.Builder(dojox.xmpp.util.createElement(stanzaType,req,false)); |
| 821 |
request.append(dojox.xmpp.util.createElement('error',{type:errorType},false)); |
| 822 |
request.append(dojox.xmpp.util.createElement('condition',{xmlns:dojox.xmpp.xmpp.STANZA_NS},true)); |
| 823 |
|
| 824 |
if(text){ |
| 825 |
var textAttr={ |
| 826 |
xmlns: dojox.xmpp.xmpp.STANZA_NS, |
| 827 |
"xml:lang":this.lang |
| 828 |
}; |
| 829 |
request.append(dojox.xmpp.util.createElement('text',textAttr,false)); |
| 830 |
request.append(text).append("</text>"); |
| 831 |
} |
| 832 |
request.append("</error></").append(stanzaType).append(">"); |
| 833 |
|
| 834 |
this.dispatchPacket(request.toString()); |
| 835 |
}, |
| 836 |
|
| 837 |
getBareJid: function(jid){ |
| 838 |
var i = jid.indexOf('/'); |
| 839 |
if (i != -1){ |
| 840 |
return jid.substring(0, i); |
| 841 |
} |
| 842 |
return jid; |
| 843 |
}, |
| 844 |
|
| 845 |
getResourceFromJid: function(jid){ |
| 846 |
var i = jid.indexOf('/'); |
| 847 |
if (i != -1){ |
| 848 |
return jid.substring((i + 1), jid.length); |
| 849 |
} |
| 850 |
return ""; |
| 851 |
} |
| 852 |
|
| 853 |
}); |