Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / xmpp / xmppSession.js

History | View | Annotate | Download (14.9 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dojox.xmpp.xmppSession"]){
9
dojo._hasResource["dojox.xmpp.xmppSession"]=true;
10
dojo.provide("dojox.xmpp.xmppSession");
11
dojo.require("dojox.xmpp.TransportSession");
12
dojo.require("dojox.xmpp.RosterService");
13
dojo.require("dojox.xmpp.PresenceService");
14
dojo.require("dojox.xmpp.UserService");
15
dojo.require("dojox.xmpp.ChatService");
16
dojo.require("dojox.xmpp.sasl");
17
dojox.xmpp.xmpp={STREAM_NS:"http://etherx.jabber.org/streams",CLIENT_NS:"jabber:client",STANZA_NS:"urn:ietf:params:xml:ns:xmpp-stanzas",SASL_NS:"urn:ietf:params:xml:ns:xmpp-sasl",BIND_NS:"urn:ietf:params:xml:ns:xmpp-bind",SESSION_NS:"urn:ietf:params:xml:ns:xmpp-session",BODY_NS:"http://jabber.org/protocol/httpbind",XHTML_BODY_NS:"http://www.w3.org/1999/xhtml",XHTML_IM_NS:"http://jabber.org/protocol/xhtml-im",INACTIVE:"Inactive",CONNECTED:"Connected",ACTIVE:"Active",TERMINATE:"Terminate",LOGIN_FAILURE:"LoginFailure",INVALID_ID:-1,NO_ID:0,error:{BAD_REQUEST:"bad-request",CONFLICT:"conflict",FEATURE_NOT_IMPLEMENTED:"feature-not-implemented",FORBIDDEN:"forbidden",GONE:"gone",INTERNAL_SERVER_ERROR:"internal-server-error",ITEM_NOT_FOUND:"item-not-found",ID_MALFORMED:"jid-malformed",NOT_ACCEPTABLE:"not-acceptable",NOT_ALLOWED:"not-allowed",NOT_AUTHORIZED:"not-authorized",SERVICE_UNAVAILABLE:"service-unavailable",SUBSCRIPTION_REQUIRED:"subscription-required",UNEXPECTED_REQUEST:"unexpected-request"}};
18
dojox.xmpp.xmppSession=function(_1){
19
this.roster=[];
20
this.chatRegister=[];
21
this._iqId=Math.round(Math.random()*1000000000);
22
if(_1&&dojo.isObject(_1)){
23
dojo.mixin(this,_1);
24
}
25
this.session=new dojox.xmpp.TransportSession(_1);
26
dojo.connect(this.session,"onReady",this,"onTransportReady");
27
dojo.connect(this.session,"onTerminate",this,"onTransportTerminate");
28
dojo.connect(this.session,"onProcessProtocolResponse",this,"processProtocolResponse");
29
};
30
dojo.extend(dojox.xmpp.xmppSession,{roster:[],chatRegister:[],_iqId:0,open:function(_2,_3,_4){
31
if(!_2){
32
throw new Error("User id cannot be null");
33
}else{
34
this.jid=_2;
35
if(_2.indexOf("@")==-1){
36
this.jid=this.jid+"@"+this.domain;
37
}
38
}
39
if(_3){
40
this.password=_3;
41
}
42
if(_4){
43
this.resource=_4;
44
}
45
this.session.open();
46
},close:function(){
47
this.state=dojox.xmpp.xmpp.TERMINATE;
48
this.session.close(dojox.xmpp.util.createElement("presence",{type:"unavailable",xmlns:dojox.xmpp.xmpp.CLIENT_NS},true));
49
},processProtocolResponse:function(_5){
50
var _6=_5.nodeName;
51
var _7=_6.indexOf(":");
52
if(_7>0){
53
_6=_6.substring(_7+1);
54
}
55
switch(_6){
56
case "iq":
57
case "presence":
58
case "message":
59
case "features":
60
this[_6+"Handler"](_5);
61
break;
62
default:
63
if(_5.getAttribute("xmlns")==dojox.xmpp.xmpp.SASL_NS){
64
this.saslHandler(_5);
65
}
66
}
67
},messageHandler:function(_8){
68
switch(_8.getAttribute("type")){
69
case "chat":
70
this.chatHandler(_8);
71
break;
72
case "normal":
73
default:
74
this.simpleMessageHandler(_8);
75
}
76
},iqHandler:function(_9){
77
if(_9.getAttribute("type")=="set"){
78
this.iqSetHandler(_9);
79
return;
80
}else{
81
if(_9.getAttribute("type")=="get"){
82
return;
83
}
84
}
85
},presenceHandler:function(_a){
86
switch(_a.getAttribute("type")){
87
case "subscribe":
88
this.presenceSubscriptionRequest(_a.getAttribute("from"));
89
break;
90
case "subscribed":
91
case "unsubscribed":
92
break;
93
case "error":
94
this.processXmppError(_a);
95
break;
96
default:
97
this.presenceUpdate(_a);
98
break;
99
}
100
},featuresHandler:function(_b){
101
var _c=[];
102
var _d=false;
103
var _e=false;
104
if(_b.hasChildNodes()){
105
for(var i=0;i<_b.childNodes.length;i++){
106
var n=_b.childNodes[i];
107
switch(n.nodeName){
108
case "mechanisms":
109
for(var x=0;x<n.childNodes.length;x++){
110
_c.push(n.childNodes[x].firstChild.nodeValue);
111
}
112
break;
113
case "bind":
114
_d=true;
115
break;
116
case "session":
117
_e=true;
118
}
119
}
120
}
121
if(this.state==dojox.xmpp.xmpp.CONNECTED){
122
if(!this.auth){
123
for(var i=0;i<_c.length;i++){
124
try{
125
this.auth=dojox.xmpp.sasl.registry.match(_c[i],this);
126
break;
127
}
128
catch(e){
129
console.warn("No suitable auth mechanism found for: ",_c[i]);
130
}
131
}
132
}else{
133
if(_d){
134
this.bindResource(_e);
135
}
136
}
137
}
138
},saslHandler:function(_f){
139
if(_f.nodeName=="success"){
140
this.auth.onSuccess();
141
return;
142
}
143
if(_f.nodeName=="challenge"){
144
this.auth.onChallenge(_f);
145
return;
146
}
147
if(_f.hasChildNodes()){
148
this.onLoginFailure(_f.firstChild.nodeName);
149
this.session.setState("Terminate",_f.firstChild.nodeName);
150
}
151
},sendRestart:function(){
152
this.session._sendRestart();
153
},chatHandler:function(msg){
154
var _10={from:msg.getAttribute("from"),to:msg.getAttribute("to")};
155
var _11=null;
156
for(var i=0;i<msg.childNodes.length;i++){
157
var n=msg.childNodes[i];
158
if(n.hasChildNodes()){
159
switch(n.nodeName){
160
case "thread":
161
_10.chatid=n.firstChild.nodeValue;
162
break;
163
case "body":
164
if(!n.getAttribute("xmlns")||(n.getAttribute("xmlns")=="")){
165
_10.body=n.firstChild.nodeValue;
166
}
167
break;
168
case "subject":
169
_10.subject=n.firstChild.nodeValue;
170
case "html":
171
if(n.getAttribute("xmlns")==dojox.xmpp.xmpp.XHTML_IM_NS){
172
_10.xhtml=n.getElementsByTagName("body")[0];
173
}
174
break;
175
case "x":
176
break;
177
default:
178
}
179
}
180
}
181
var _12=-1;
182
if(_10.chatid){
183
for(var i=0;i<this.chatRegister.length;i++){
184
var ci=this.chatRegister[i];
185
if(ci&&ci.chatid==_10.chatid){
186
_12=i;
187
break;
188
}
189
}
190
}else{
191
for(var i=0;i<this.chatRegister.length;i++){
192
var ci=this.chatRegister[i];
193
if(ci){
194
if(ci.uid==this.getBareJid(_10.from)){
195
_12=i;
196
}
197
}
198
}
199
}
200
if(_12>-1&&_11){
201
var _13=this.chatRegister[_12];
202
_13.setState(_11);
203
if(_13.firstMessage){
204
if(_11==dojox.xmpp.chat.ACTIVE_STATE){
205
_13.useChatState=(_11!=null)?true:false;
206
_13.firstMessage=false;
207
}
208
}
209
}
210
if((!_10.body||_10.body=="")&&!_10.xhtml){
211
return;
212
}
213
if(_12>-1){
214
var _13=this.chatRegister[_12];
215
_13.recieveMessage(_10);
216
}else{
217
var _14=new dojox.xmpp.ChatService();
218
_14.uid=this.getBareJid(_10.from);
219
_14.chatid=_10.chatid;
220
_14.firstMessage=true;
221
if(!_11||_11!=dojox.xmpp.chat.ACTIVE_STATE){
222
this.useChatState=false;
223
}
224
this.registerChatInstance(_14,_10);
225
}
226
},simpleMessageHandler:function(msg){
227
},registerChatInstance:function(_15,_16){
228
_15.setSession(this);
229
this.chatRegister.push(_15);
230
this.onRegisterChatInstance(_15,_16);
231
_15.recieveMessage(_16,true);
232
},iqSetHandler:function(msg){
233
if(msg.hasChildNodes()){
234
var fn=msg.firstChild;
235
switch(fn.nodeName){
236
case "query":
237
if(fn.getAttribute("xmlns")=="jabber:iq:roster"){
238
this.rosterSetHandler(fn);
239
this.sendIqResult(msg.getAttribute("id"),msg.getAttribute("from"));
240
}
241
break;
242
default:
243
break;
244
}
245
}
246
},sendIqResult:function(_17,to){
247
var req={id:_17,to:to||this.domain,type:"result",from:this.jid+"/"+this.resource};
248
this.dispatchPacket(dojox.xmpp.util.createElement("iq",req,true));
249
},rosterSetHandler:function(_18){
250
for(var i=0;i<_18.childNodes.length;i++){
251
var n=_18.childNodes[i];
252
if(n.nodeName=="item"){
253
var _19=false;
254
var _1a=-1;
255
var _1b=null;
256
var _1c=null;
257
for(var x=0;x<this.roster.length;x++){
258
var r=this.roster[x];
259
if(n.getAttribute("jid")==r.jid){
260
_19=true;
261
if(n.getAttribute("subscription")=="remove"){
262
_1b={id:r.jid,name:r.name,groups:[]};
263
for(var y=0;y<r.groups.length;y++){
264
_1b.groups.push(r.groups[y]);
265
}
266
this.roster.splice(x,1);
267
_1a=dojox.xmpp.roster.REMOVED;
268
}else{
269
_1c=dojo.clone(r);
270
var _1d=n.getAttribute("name");
271
if(_1d){
272
this.roster[x].name=_1d;
273
}
274
r.groups=[];
275
if(n.getAttribute("subscription")){
276
r.status=n.getAttribute("subscription");
277
}
278
r.substatus=dojox.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE;
279
if(n.getAttribute("ask")=="subscribe"){
280
r.substatus=dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING;
281
}
282
for(var y=0;y<n.childNodes.length;y++){
283
var _1e=n.childNodes[y];
284
if((_1e.nodeName=="group")&&(_1e.hasChildNodes())){
285
var _1f=_1e.firstChild.nodeValue;
286
r.groups.push(_1f);
287
}
288
}
289
_1b=r;
290
_1a=dojox.xmpp.roster.CHANGED;
291
}
292
break;
293
}
294
}
295
if(!_19&&(n.getAttribute("subscription")!="remove")){
296
r=this.createRosterEntry(n);
297
_1b=r;
298
_1a=dojox.xmpp.roster.ADDED;
299
}
300
switch(_1a){
301
case dojox.xmpp.roster.ADDED:
302
this.onRosterAdded(_1b);
303
break;
304
case dojox.xmpp.roster.REMOVED:
305
this.onRosterRemoved(_1b);
306
break;
307
case dojox.xmpp.roster.CHANGED:
308
this.onRosterChanged(_1b,_1c);
309
break;
310
}
311
}
312
}
313
},presenceUpdate:function(msg){
314
if(msg.getAttribute("to")){
315
var jid=this.getBareJid(msg.getAttribute("to"));
316
if(jid!=this.jid){
317
return;
318
}
319
}
320
var _20=this.getResourceFromJid(msg.getAttribute("from"));
321
var p={from:this.getBareJid(msg.getAttribute("from")),resource:_20,show:dojox.xmpp.presence.STATUS_ONLINE,priority:5,hasAvatar:false};
322
if(msg.getAttribute("type")=="unavailable"){
323
p.show=dojox.xmpp.presence.STATUS_OFFLINE;
324
}
325
for(var i=0;i<msg.childNodes.length;i++){
326
var n=msg.childNodes[i];
327
if(n.hasChildNodes()){
328
switch(n.nodeName){
329
case "status":
330
case "show":
331
p[n.nodeName]=n.firstChild.nodeValue;
332
break;
333
case "status":
334
p.priority=parseInt(n.firstChild.nodeValue);
335
break;
336
case "x":
337
if(n.firstChild&&n.firstChild.firstChild&&n.firstChild.firstChild.nodeValue!=""){
338
p.avatarHash=n.firstChild.firstChild.nodeValue;
339
p.hasAvatar=true;
340
}
341
break;
342
}
343
}
344
}
345
this.onPresenceUpdate(p);
346
},retrieveRoster:function(){
347
var _21={id:this.getNextIqId(),from:this.jid+"/"+this.resource,type:"get"};
348
var req=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_21,false));
349
req.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:roster"},true));
350
req.append("</iq>");
351
var def=this.dispatchPacket(req,"iq",_21.id);
352
def.addCallback(this,"onRetrieveRoster");
353
},getRosterIndex:function(jid){
354
if(jid.indexOf("@")==-1){
355
jid+="@"+this.domain;
356
}
357
for(var i=0;i<this.roster.length;i++){
358
if(jid==this.roster[i].jid){
359
return i;
360
}
361
}
362
return -1;
363
},createRosterEntry:function(_22){
364
var re={name:_22.getAttribute("name"),jid:_22.getAttribute("jid"),groups:[],status:dojox.xmpp.presence.SUBSCRIPTION_NONE,substatus:dojox.xmpp.presence.SUBSCRIPTION_SUBSTATUS_NONE};
365
if(!re.name){
366
re.name=re.id;
367
}
368
for(var i=0;i<_22.childNodes.length;i++){
369
var n=_22.childNodes[i];
370
if(n.nodeName=="group"&&n.hasChildNodes()){
371
re.groups.push(n.firstChild.nodeValue);
372
}
373
}
374
if(_22.getAttribute("subscription")){
375
re.status=_22.getAttribute("subscription");
376
}
377
if(_22.getAttribute("ask")=="subscribe"){
378
re.substatus=dojox.xmpp.presence.SUBSCRIPTION_REQUEST_PENDING;
379
}
380
return re;
381
},bindResource:function(_23){
382
var _24={id:this.getNextIqId(),type:"set"};
383
var _25=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_24,false));
384
_25.append(dojox.xmpp.util.createElement("bind",{xmlns:dojox.xmpp.xmpp.BIND_NS},false));
385
if(this.resource){
386
_25.append(dojox.xmpp.util.createElement("resource"));
387
_25.append(this.resource);
388
_25.append("</resource>");
389
}
390
_25.append("</bind></iq>");
391
var def=this.dispatchPacket(_25,"iq",_24.id);
392
def.addCallback(this,function(msg){
393
this.onBindResource(msg,_23);
394
return msg;
395
});
396
},getNextIqId:function(){
397
return "im_"+this._iqId++;
398
},presenceSubscriptionRequest:function(msg){
399
this.onSubscriptionRequest(msg);
400
},dispatchPacket:function(msg,_26,_27){
401
if(this.state!="Terminate"){
402
return this.session.dispatchPacket(msg,_26,_27);
403
}else{
404
}
405
},setState:function(_28,_29){
406
if(this.state!=_28){
407
if(this["on"+_28]){
408
this["on"+_28](_28,this.state,_29);
409
}
410
this.state=_28;
411
}
412
},search:function(_2a,_2b,_2c){
413
var req={id:this.getNextIqId(),"xml:lang":this.lang,type:"set",from:this.jid+"/"+this.resource,to:_2b};
414
var _2d=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",req,false));
415
_2d.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:search"},false));
416
_2d.append(dojox.xmpp.util.createElement(_2c,{},false));
417
_2d.append(_2a);
418
_2d.append("</").append(_2c).append(">");
419
_2d.append("</query></iq>");
420
var def=this.dispatchPacket(_2d.toString,"iq",req.id);
421
def.addCallback(this,"_onSearchResults");
422
},_onSearchResults:function(msg){
423
if((msg.getAttribute("type")=="result")&&(msg.hasChildNodes())){
424
this.onSearchResults([]);
425
}
426
},onLogin:function(){
427
this.retrieveRoster();
428
},onLoginFailure:function(msg){
429
},onBindResource:function(msg,_2e){
430
if(msg.getAttribute("type")=="result"){
431
if((msg.hasChildNodes())&&(msg.firstChild.nodeName=="bind")){
432
var _2f=msg.firstChild;
433
if((_2f.hasChildNodes())&&(_2f.firstChild.nodeName=="jid")){
434
if(_2f.firstChild.hasChildNodes()){
435
var _30=_2f.firstChild.firstChild.nodeValue;
436
this.jid=this.getBareJid(_30);
437
this.resource=this.getResourceFromJid(_30);
438
}
439
}
440
if(_2e){
441
var _31={id:this.getNextIqId(),type:"set"};
442
var _32=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_31,false));
443
_32.append(dojox.xmpp.util.createElement("session",{xmlns:dojox.xmpp.xmpp.SESSION_NS},true));
444
_32.append("</iq>");
445
var def=this.dispatchPacket(_32,"iq",_31.id);
446
def.addCallback(this,"onBindSession");
447
return;
448
}
449
}else{
450
}
451
this.onLogin();
452
}else{
453
if(msg.getAttribute("type")=="error"){
454
var err=this.processXmppError(msg);
455
this.onLoginFailure(err);
456
}
457
}
458
},onBindSession:function(msg){
459
if(msg.getAttribute("type")=="error"){
460
var err=this.processXmppError(msg);
461
this.onLoginFailure(err);
462
}else{
463
this.onLogin();
464
}
465
},onSearchResults:function(_33){
466
},onRetrieveRoster:function(msg){
467
if((msg.getAttribute("type")=="result")&&msg.hasChildNodes()){
468
var _34=msg.getElementsByTagName("query")[0];
469
if(_34.getAttribute("xmlns")=="jabber:iq:roster"){
470
for(var i=0;i<_34.childNodes.length;i++){
471
if(_34.childNodes[i].nodeName=="item"){
472
this.roster[i]=this.createRosterEntry(_34.childNodes[i]);
473
}
474
}
475
}
476
}else{
477
if(msg.getAttribute("type")=="error"){
478
}
479
}
480
this.setState(dojox.xmpp.xmpp.ACTIVE);
481
this.onRosterUpdated();
482
return msg;
483
},onRosterUpdated:function(){
484
},onSubscriptionRequest:function(req){
485
},onPresenceUpdate:function(p){
486
},onTransportReady:function(){
487
this.setState(dojox.xmpp.xmpp.CONNECTED);
488
this.rosterService=new dojox.xmpp.RosterService(this);
489
this.presenceService=new dojox.xmpp.PresenceService(this);
490
this.userService=new dojox.xmpp.UserService(this);
491
},onTransportTerminate:function(_35,_36,_37){
492
this.setState(dojox.xmpp.xmpp.TERMINATE,_37);
493
},onConnected:function(){
494
},onTerminate:function(_38,_39,_3a){
495
},onActive:function(){
496
},onRegisterChatInstance:function(_3b,_3c){
497
},onRosterAdded:function(ri){
498
},onRosterRemoved:function(ri){
499
},onRosterChanged:function(ri,_3d){
500
},processXmppError:function(msg){
501
var err={stanzaType:msg.nodeName,id:msg.getAttribute("id")};
502
for(var i=0;i<msg.childNodes.length;i++){
503
var n=msg.childNodes[i];
504
switch(n.nodeName){
505
case "error":
506
err.errorType=n.getAttribute("type");
507
for(var x=0;x<n.childNodes.length;x++){
508
var cn=n.childNodes[x];
509
if((cn.nodeName=="text")&&(cn.getAttribute("xmlns")==dojox.xmpp.xmpp.STANZA_NS)&&cn.hasChildNodes()){
510
err.message=cn.firstChild.nodeValue;
511
}else{
512
if((cn.getAttribute("xmlns")==dojox.xmpp.xmpp.STANZA_NS)&&(!cn.hasChildNodes())){
513
err.condition=cn.nodeName;
514
}
515
}
516
}
517
break;
518
default:
519
break;
520
}
521
}
522
return err;
523
},sendStanzaError:function(_3e,to,id,_3f,_40,_41){
524
var req={type:"error"};
525
if(to){
526
req.to=to;
527
}
528
if(id){
529
req.id=id;
530
}
531
var _42=new dojox.string.Builder(dojox.xmpp.util.createElement(_3e,req,false));
532
_42.append(dojox.xmpp.util.createElement("error",{type:_3f},false));
533
_42.append(dojox.xmpp.util.createElement("condition",{xmlns:dojox.xmpp.xmpp.STANZA_NS},true));
534
if(_41){
535
var _43={xmlns:dojox.xmpp.xmpp.STANZA_NS,"xml:lang":this.lang};
536
_42.append(dojox.xmpp.util.createElement("text",_43,false));
537
_42.append(_41).append("</text>");
538
}
539
_42.append("</error></").append(_3e).append(">");
540
this.dispatchPacket(_42.toString());
541
},getBareJid:function(jid){
542
var i=jid.indexOf("/");
543
if(i!=-1){
544
return jid.substring(0,i);
545
}
546
return jid;
547
},getResourceFromJid:function(jid){
548
var i=jid.indexOf("/");
549
if(i!=-1){
550
return jid.substring((i+1),jid.length);
551
}
552
return "";
553
}});
554
}