social_stream-presence 0.7.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,6 @@
3
3
  ////////////////////
4
4
  var domain = '<%=SocialStream::Presence.domain%>';
5
5
 
6
-
7
6
  ////////////////////
8
7
  //Hash tables
9
8
  ////////////////////
@@ -34,7 +33,32 @@ statusIcons['away'] = "away";
34
33
  statusIcons['xa'] = "away";
35
34
  statusIcons['dnd'] = "dnd";
36
35
 
36
+ //Contact information
37
+ var contactsInfo = new Array();
38
+ //contactsInfo['slug'] = "[Object chatContact]";
39
+
40
+ //Manage contact information
41
+ function chatContact(domain,resource,client,version) {
42
+ this.domain=domain
43
+ this.resource=resource
44
+ this.client=client;
45
+ this.version=version;
46
+ //Sender: disconnected, negotiating , connecting , waiting, establishing, connected
47
+ //Receiver: disconnected , pending , establishing, connected
48
+ this.videoChatStatus = "disconnected";
49
+ this.session_id=null;
50
+ this.user_token=null;
51
+ this.guest_token=null;
52
+ this.session=null;
53
+ this.publisher=null;
54
+ }
55
+
37
56
 
57
+ //IQsIDs
58
+ var iqStanzaID = new Array();
59
+ iqStanzaID['cinfo'] = "versionID";
60
+ iqStanzaID['videochatRequest'] = "videochatRequestID";
61
+ iqStanzaID['videochatRequestCancel'] = "videochatRequestCancelID";
38
62
 
39
63
  ////////////////////
40
64
  //Connect functions
@@ -42,35 +66,39 @@ statusIcons['dnd'] = "dnd";
42
66
 
43
67
  function connectToChat(user_jid,cookie,password){
44
68
 
45
- if (isStropheConnected()){
69
+ if (isUserConnected()){
70
+ log("USer already connected")
46
71
  return true;
47
72
  }
48
73
 
49
74
  if (authByCookie()){
50
75
  if (connectToServerWithCookie(user_jid, cookie)==false){
51
- refreshChatWindow();
76
+ updateChatWindow();
52
77
  }
53
78
  } else {
54
79
  if (connectToServerWithPassword(user_jid,password)==false){
55
- refreshChatWindow();
80
+ updateChatWindow();
56
81
  }
57
82
  }
58
83
  }
59
84
 
60
85
 
61
86
  function isStropheConnected(){
62
- if((connection!=null)&&(connection.connected)){
87
+ if((strophe_connection!=null)&&(strophe_connection.connected)){
63
88
  return true;
64
89
  } else {
65
90
  return false;
66
91
  }
67
92
  }
68
93
 
94
+ function isUserConnected(){
95
+ return ((isStropheConnected()) && (userStatus!="offline") && (!disconnectionFlag));
96
+ }
69
97
 
70
98
  function connectToServerWithCookie(user_jid, cookie){
71
99
  try {
72
- connection = new Strophe.Connection(BOSH_SERVICE);
73
- connection.connect(user_jid, cookie, onConnect);
100
+ strophe_connection = new Strophe.Connection(BOSH_SERVICE);
101
+ strophe_connection.connect(user_jid, cookie, onConnect);
74
102
  } catch (err) {
75
103
  //"Handle errors"
76
104
  return false;
@@ -93,8 +121,8 @@ function connectToServerWithPassword(user_jid, chatPassword){
93
121
 
94
122
  try {
95
123
  //Connect actual user to the chat
96
- connection = new Strophe.Connection(BOSH_SERVICE);
97
- connection.connect(user_jid, password, onConnect);
124
+ strophe_connection = new Strophe.Connection(BOSH_SERVICE);
125
+ strophe_connection.connect(user_jid, password, onConnect);
98
126
  } catch (err) {
99
127
  //"Handle errors"
100
128
  return false;
@@ -125,21 +153,15 @@ function authByPassword(){
125
153
  //Global variables
126
154
  var userStatus;
127
155
  var awayTimerPeriod = 60000;
128
- var timerPeriod = 5000;
129
- var refreshMinTime = 3*timerPeriod;
130
156
  var awayTime = 10*60000; //10 minutes
131
157
  var awayCounter = 0;
132
- var timerCounter = 0;
133
- var connection = null;
158
+ var strophe_connection = null;
134
159
  var reconnectAttempts = 3;
135
160
  var awayTimer;
136
- var timer;
137
161
  var reconnectTimer;
138
- var disconnectionFlag = true;
162
+ var disconnectionFlag = false;
139
163
  var afterNewConnectionFlag = false;
140
164
  var afterFirstConnectionFlag = true;
141
- var requestContacts=false;
142
- var cyclesToRefresh = (refreshMinTime/timerPeriod);
143
165
 
144
166
 
145
167
  function onConnect(status) {
@@ -156,7 +178,12 @@ function onConnect(status) {
156
178
 
157
179
  log('Strophe onConnect callback call with status ' + status);
158
180
 
159
- if (status == Strophe.Status.ATTACHED){
181
+ if (status == Strophe.Status.ERROR){
182
+ log('Strophe connection error');
183
+ return;
184
+ }
185
+
186
+ if (status == Strophe.Status.ATTACHED){
160
187
  log('Strophe connection attached');
161
188
  return;
162
189
  }
@@ -174,9 +201,11 @@ function onConnect(status) {
174
201
  if (status == Strophe.Status.DISCONNECTING) {
175
202
  log('Strophe is disconnecting.');
176
203
  return;
177
- }
204
+ }
178
205
 
179
- clearTimeout(initialTimer);
206
+ if(typeof initialTimer != 'undefined'){
207
+ clearTimeout(initialTimer);
208
+ }
180
209
 
181
210
  if (status == Strophe.Status.CONNFAIL) {
182
211
  log('Strophe failed to connect.');
@@ -195,7 +224,7 @@ function onConnect(status) {
195
224
  log('Strophe is disconnected.');
196
225
  disconnectionFlag = true;
197
226
  clearTimeout(awayTimer);
198
- notifyWhenUsersDisconnect();
227
+ updateInterfaceAfterUsersDisconnect();
199
228
  reconnectTimer = setTimeout ("onReconnect()", 5000);
200
229
  } else if (status == Strophe.Status.CONNECTED) {
201
230
  //AFTER CONNECT ACTIONS
@@ -205,15 +234,13 @@ function onConnect(status) {
205
234
  clearTimeout(reconnectTimer);
206
235
 
207
236
  //addHandler:(callback, namespace to match, stanza name, stanza type, stanza id , stanza from, options)
208
- connection.addHandler(onMessage, null, 'message', null, null, null);
209
- connection.addHandler(onPresence, null, 'presence', null, null, null);
210
- connection.addHandler(onIQStanza,null, "iq", null, null);
237
+ strophe_connection.addHandler(onMessage, null, 'message', null, null, null);
238
+ strophe_connection.addHandler(onPresence, null, 'presence', null, null, null);
239
+ strophe_connection.addHandler(onIQStanza,null, "iq", null, null);
211
240
 
212
-
213
241
  disconnectionFlag = false;
214
242
  afterNewConnectionFlag = true;
215
243
 
216
- log('Presenze stanza send for:' + connection.jid);
217
244
  userStatus = getRestoreUserChatStatus();
218
245
  if(userStatus=="offline"){
219
246
  userStatus="chat";
@@ -221,7 +248,6 @@ function onConnect(status) {
221
248
  sendStatus(userStatus);
222
249
 
223
250
  awayTimer = setInterval("awayTimerFunction()", awayTimerPeriod);
224
- timer = setInterval("timerFunction()", timerPeriod);
225
251
  }
226
252
 
227
253
  updateChatWindow();
@@ -254,8 +280,8 @@ function disconnectStrophe(){
254
280
  setStatusWidgetTitle("offline");
255
281
 
256
282
  if(isStropheConnected()){
257
- connection.send($pres({type: "unavailable"}).tree());
258
- connection.disconnect();
283
+ strophe_connection.send($pres({type: "unavailable"}).tree());
284
+ strophe_connection.disconnect();
259
285
  }
260
286
  }
261
287
 
@@ -287,9 +313,9 @@ function onMessage(msg) {
287
313
  //Manage Presence stanzas
288
314
  ///////
289
315
  function onPresence(presence) {
290
-
291
- log(presence)
292
-
316
+
317
+ log(presence)
318
+
293
319
  //Check presence stanza type
294
320
  var ptype = $(presence).attr('type');
295
321
 
@@ -317,13 +343,14 @@ function processAvailablePresenceStanza(presence){
317
343
  var slug = from.split("@")[0];
318
344
 
319
345
  if (slug != user_slug) {
346
+ storeContactInformation(presence);
320
347
  if (getConnectionBoxFromSlug(slug)!=null){
321
348
  var status = $(presence).find('show').text();
322
349
  setUserIconStatus(slug,status);
323
350
  updateInterfaceAfterPresenceStanza(slug,true);
324
351
  } else {
325
352
  if(! isAdminSlug(slug)){
326
- setTimeout("refreshChatWindow()", 3000);
353
+ updateChatWindow();
327
354
  }
328
355
  }
329
356
  }
@@ -340,76 +367,287 @@ function processUnavailablePresenceStanza(presence){
340
367
  }
341
368
  }
342
369
 
370
+ function storeContactInformation(stanza){
371
+ var from = $(stanza).attr('from');
372
+ var split = from.split("@");
373
+ var slug = split[0];
374
+ var domain = split[1].split("/")[0];
375
+ var resource = split[1].split("/")[1];
376
+
377
+ if (!(slug in contactsInfo)) {
378
+ var contact = new chatContact(domain,resource,null,null);
379
+ contactsInfo[slug]=contact;
380
+ } else {
381
+ contactsInfo[slug].domain = domain;
382
+ contactsInfo[slug].resource = resource;
383
+ }
384
+ }
385
+
386
+
343
387
 
344
388
  ////////
345
389
  //Manage IQ stanzas
346
390
  ///////
391
+
392
+ ///////
393
+ //RECEIVED STANZAS
394
+ ///////
347
395
  function onIQStanza(iq){
348
-
396
+ var type = iq.getAttribute("type");
349
397
  var from = iq.getAttribute("from");
350
398
  var slug = from.split("@")[0];
351
399
 
352
400
  if (slug == user_slug) {
353
- return;
401
+ return true;
354
402
  }
355
403
 
356
- var queryElements = iq.getElementsByTagName('query');
404
+ if(type=="get"){
405
+ return handleGetIQStanza(iq,from,slug);
406
+ } else if(type="result"){
407
+ return handleResultIQStanza(iq,from,slug)
408
+ }
409
+
410
+ return true;
411
+ }
412
+
413
+ ///////
414
+ //RECEIVED STANZAS: GET
415
+ ///////
416
+ function handleGetIQStanza(iq,jid,slug){
417
+
418
+ var iqID = iq.getAttribute("id");
419
+
420
+ //Case 1: Request client info
421
+ var queryElements = iq.getElementsByTagName('query');
357
422
 
358
423
  if (queryElements.length > 0) {
359
424
  var query = queryElements[0];
360
- var type = query.getAttribute("xmlns");
361
-
362
- if( type == "jabber:iq:version"){
363
- var iqID = iq.getAttribute("id");
364
-
365
- //Response to client info request.
366
- sendIQStanzaWithClientInfo(from,iqID);
367
- }
425
+ var xmlns_type = query.getAttribute("xmlns");
426
+
427
+ if( xmlns_type == "jabber:iq:version"){
428
+ sendIQStanzaWithClientInfo(jid,iqID);
429
+ return true;
430
+ }
431
+
432
+ if (xmlns_type == "jabber:iq:last") {
433
+ sendIQStanzaLast(jid,iqID);
434
+ return true;
435
+ }
436
+ }
437
+
438
+ //Case 2: Request videochat
439
+ if(iqID==iqStanzaID['videochatRequest']){
440
+ handleGetVideochatIQStanza(jid,iqID,iq,slug)
441
+ return true;
442
+ }
443
+
444
+ //Case: Default behaviour
445
+ sendIQEmpty(jid,iqID);
446
+
447
+ return true;
448
+ }
449
+
450
+
451
+
452
+ function handleGetVideochatIQStanza(jid,iqID,iq,slug){
453
+ storeContactInformation(iq);
454
+ var queryTag = iq.getElementsByTagName('query');
455
+
456
+ if (queryTag.length > 0) {
457
+ var queryElement = queryTag[0];
458
+ var session_id = queryElement.getElementsByTagName('session_id');
459
+ var token = queryElement.getElementsByTagName('token');
460
+
461
+ if((session_id.length == 1)&&(token.length == 1)){
462
+ contactsInfo[slug].session_id = $(session_id).text();
463
+ contactsInfo[slug].user_token = $(token).text();
464
+ updateInterfaceAfterVideoRequestReceived(slug);
465
+ return;
466
+ }
467
+ }
468
+
469
+ //Send error stanza
470
+ sendIQEmpty(jid,iqID);
471
+ }
472
+
473
+
474
+ ///////
475
+ //RECEIVED STANZAS: RESULT
476
+ ///////
477
+ function handleResultIQStanza(iq,jid,slug){
478
+ var iqID = iq.getAttribute("id");
479
+
480
+
481
+ if (iqID==iqStanzaID['cinfo']){
482
+ return handleIQResultWithClientInfo(iq,slug);
368
483
  }
484
+
485
+ if (iqID==iqStanzaID['videochatRequest']){
486
+ return handleIQResultFromVideochatRequest(iq,slug);
487
+ }
488
+
489
+ if(iqID==iqStanzaID['videochatRequestCancel']){
490
+ return handleIQResultFromVideochatRequestCancel(iq,slug);
491
+ }
492
+
493
+ //Case: Default behaviour
494
+ return true;
495
+ }
369
496
 
497
+
498
+ function handleIQResultWithClientInfo(iq,slug){
499
+ var queryTag = iq.getElementsByTagName('query');
500
+
501
+ if (queryTag.length > 0) {
502
+ var queryElement = queryTag[0];
503
+ var name = queryElement.getElementsByTagName('name');
504
+ var version = queryElement.getElementsByTagName('version');
505
+
506
+ if((name.length == 1)&&(version.length == 1)){
507
+ var clientValue = $(name).text();
508
+ var versionValue = $(version).text();
509
+
510
+ if (!(slug in contactsInfo)) {
511
+ var contact = new chatContact(null,null,clientValue,versionValue);
512
+ contactsInfo[slug]=contact;
513
+ } else {
514
+ contactsInfo[slug].client = clientValue;
515
+ contactsInfo[slug].version = versionValue;
516
+ }
517
+ clientInfoReceivedTrigger(slug);
518
+ }
519
+ }
520
+
370
521
  return true;
371
522
  }
372
523
 
373
524
 
525
+ function handleIQResultFromVideochatRequest(iq,slug){
526
+
527
+ var queryTag = iq.getElementsByTagName('query');
528
+
529
+ if (queryTag.length > 0) {
530
+ var queryElement = queryTag[0];
531
+ var response = queryElement.getElementsByTagName('response');
532
+
533
+ if(response.length == 1){
534
+ receiveVideoChatResponseFromUser(slug,$(response).text());
535
+ return true;
536
+ }
537
+ }
538
+
539
+ receiveVideoChatResponseFromUser(slug,"Bad response");
540
+ return true;
541
+ }
542
+
543
+
544
+ function handleIQResultFromVideochatRequestCancel(iq,slug){
545
+
546
+ var queryTag = iq.getElementsByTagName('query');
547
+
548
+ if (queryTag.length > 0) {
549
+ var queryElement = queryTag[0];
550
+ var response = queryElement.getElementsByTagName('response');
551
+
552
+ if(response.length == 1){
553
+ receiveVideoChatCancelationFromUser(slug,$(response).text());
554
+ return true;
555
+ }
556
+ }
557
+
558
+ receiveVideoChatCancelationFromUser(slug,"Bad response");
559
+ return true;
560
+ }
561
+
562
+
563
+ ///////
564
+ //SEND STANZAS: GET
565
+ ///////
566
+ function sendIQStanzaForRequestClientInfo(slug){
567
+ if (slug in contactsInfo) {
568
+ var domain = contactsInfo[slug].domain;
569
+ var resource = contactsInfo[slug].resource;
570
+ var jid=slug+"@"+domain+"/"+resource
571
+
572
+ var iq = $iq({to: jid, type: "get", id: iqStanzaID['cinfo'], xmlns: "jabber:iq:version"})
573
+ .c("query", {xmlns: "jabber:iq:version"});
574
+ strophe_connection.sendIQ(iq);
575
+ }
576
+ }
577
+
578
+ function sendIQStanzaToRequestVideochat(slug){
579
+ if (slug in contactsInfo) {
580
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
581
+
582
+ var iq = $iq({to: jid, type: "get", id: iqStanzaID['videochatRequest']})
583
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
584
+ .c("session_id").t(contactsInfo[slug].session_id).up()
585
+ .c("token").t(contactsInfo[slug].guest_token);
586
+ strophe_connection.sendIQ(iq);
587
+ }
588
+ }
589
+
590
+
591
+
592
+ ///////
593
+ //SEND STANZAS: RESULT
594
+ ///////
374
595
  function sendIQStanzaWithClientInfo(jid,iqID){
375
596
  var client = "Social Stream XMPP Client"
376
- var version = '<%=SocialStream::Presence::VERSION%>';
597
+ var version = getJavascriptXMPPClientName();
377
598
  var iq = $iq({to: jid, type: "result", id: iqID})
378
599
  .c("query", {xmlns: "jabber:iq:version"}).c("name").t(client).up().c("version").t(version);
379
- connection.sendIQ(iq);
600
+ strophe_connection.sendIQ(iq);
380
601
  }
381
602
 
603
+ function getJavascriptXMPPClientName(){
604
+ return '<%=SocialStream::Presence::VERSION%>';
605
+ }
382
606
 
383
- var iqIDNumber = 1000;
384
- function sendIQStanzaForRequestClientInfo(jid){
385
- //var client = "Social Stream XMPP Client"
386
- //var version = '<%=SocialStream::Presence::VERSION%>';
387
- //var iq = $iq({to: jid, type: "result", id: iqID})
388
- //.c("query", {xmlns: "jabber:iq:version"}).c("name").t(client).up().c("version").t(version);
389
- //connection.sendIQ(iq);
607
+ function sendIQStanzaLast(jid,iqID){
608
+ var iq = $iq({to: jid, type: "result", id: iqID})
609
+ .c("query", {xmlns: "jabber:iq:last", seconds: "0"});
610
+ strophe_connection.sendIQ(iq);
390
611
  }
391
612
 
613
+ function sendIQEmpty(jid,iqID){
614
+ var iq = $iq({to: jid, type: "result", id: iqID})
615
+ strophe_connection.sendIQ(iq);
616
+ }
392
617
 
393
- function iqSuccess(){
394
- log("iq sucess")
618
+ function sendIQStanzaToResponseVideochat(slug,result){
619
+ if (slug in contactsInfo) {
620
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
621
+ var iq = $iq({to: jid, type: "result", id: iqStanzaID['videochatRequest']})
622
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
623
+ .c("response").t(result);
624
+ strophe_connection.sendIQ(iq);
625
+ }
395
626
  }
396
627
 
397
- function iqFail(){
398
- log("iq fail")
628
+ function sendIQStanzaToCancelVideochat(slug){
629
+ if (slug in contactsInfo) {
630
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
631
+ var iq = $iq({to: jid, type: "result", id: iqStanzaID['videochatRequestCancel']})
632
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
633
+ .c("response").t("cancel");
634
+ strophe_connection.sendIQ(iq);
635
+ }
399
636
  }
400
637
 
401
638
 
639
+
402
640
  ////////
403
641
  //Send Message stanzas
404
642
  ///////
405
643
  function sendChatMessage(from,to,text){
406
- var type = "chat";
407
- var body= $build("body");
408
- body.t(text);
409
- var message = $msg({to: to, from: from, type: 'chat'}).cnode(body.tree());
410
- connection.send(message.tree());
411
- restartAwayTimer();
412
- return true;
644
+ var type = "chat";
645
+ var body= $build("body");
646
+ body.t(text);
647
+ var message = $msg({to: to, from: from, type: 'chat'}).cnode(body.tree());
648
+ strophe_connection.send(message.tree());
649
+ restartAwayTimer();
650
+ return true;
413
651
  }
414
652
 
415
653
 
@@ -429,7 +667,7 @@ function sendStatus(status){
429
667
  .t(statusMessage[status]).up() //Status message
430
668
  .c('show')
431
669
  .t(status);
432
- connection.send(pres.tree());
670
+ strophe_connection.send(pres.tree());
433
671
  setStatusWidgetTitle(status);
434
672
  }
435
673
  }