social_stream-presence 0.13.1 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. data/app/assets/javascripts/jquery.flexselect.sstreampresence.js +2 -2
  2. data/app/assets/javascripts/jquery.ui.chatbox.sstreampresence.js +5 -5
  3. data/app/assets/javascripts/presence.js.erb +6 -4
  4. data/app/assets/javascripts/presence_XmppClient.js.erb +1136 -996
  5. data/app/assets/javascripts/presence_audio.js.erb +74 -60
  6. data/app/assets/javascripts/presence_game.js.erb +35 -22
  7. data/app/assets/javascripts/presence_game_comunication.js.erb +22 -5
  8. data/app/assets/javascripts/presence_game_factory.js.erb +1 -1
  9. data/app/assets/javascripts/presence_game_interface.js.erb +33 -13
  10. data/app/assets/javascripts/presence_notifications.js +206 -183
  11. data/app/assets/javascripts/presence_parser.js +265 -247
  12. data/app/assets/javascripts/presence_persistence.js +199 -188
  13. data/app/assets/javascripts/presence_store.js +22 -11
  14. data/app/assets/javascripts/presence_uiManager.js.erb +553 -530
  15. data/app/assets/javascripts/presence_utilities.js +244 -219
  16. data/app/assets/javascripts/presence_videochat.js.erb +436 -409
  17. data/app/assets/javascripts/presence_windowManager.js +586 -532
  18. data/app/views/chat/_index.html.erb +7 -13
  19. data/config/locales/en.yml +2 -1
  20. data/config/locales/es.yml +2 -1
  21. data/ejabberd/ejabberd_files.zip +0 -0
  22. data/ejabberd/ejabberd_scripts/authentication_script +9 -2
  23. data/ejabberd/mod_sspresence/mod_sspresence.beam +0 -0
  24. data/lib/generators/social_stream/presence/templates/initializer.rb +4 -0
  25. data/lib/social_stream/presence/version.rb +1 -1
  26. data/lib/social_stream/presence/xmpp_server_order.rb +1 -0
  27. data/lib/social_stream-presence.rb +3 -0
  28. metadata +68 -63
@@ -1,1062 +1,1202 @@
1
1
  ////////////////////
2
- //Global constants
2
+ //XMPP Client Module
3
3
  ////////////////////
4
- var domain = '<%=SocialStream::Presence.domain%>';
5
4
 
6
- ////////////////////
7
- //Hash tables
8
- ////////////////////
9
-
10
- //Keys: Social Stream Chat Status
11
- //Value: Xmpp status
12
- var sstreamChatStatus = new Array();
13
- sstreamChatStatus['available'] = "chat";
14
- sstreamChatStatus['away'] = "away";
15
- sstreamChatStatus['autoaway'] = "away";
16
- sstreamChatStatus['dnd'] = "dnd";
17
-
18
- //Keys: Xmpp status
19
- //Value: Message associated with Xmpp Status
20
- var statusMessage = new Array();
21
- statusMessage[''] = "";
22
- statusMessage['chat'] = "";
23
- statusMessage['away'] = "Away";
24
- statusMessage['xa'] = "Away";
25
- statusMessage['dnd'] = "Busy";
26
5
 
27
- //Keys: Xmpp status
28
- //Value: Icon name (Same as Social Stream Status)
29
- var statusIcons = new Array();
30
- statusIcons[''] = "available";
31
- statusIcons['chat'] = "available";
32
- statusIcons['away'] = "away";
33
- statusIcons['xa'] = "away";
34
- statusIcons['dnd'] = "dnd";
35
-
36
- //Contacts information
6
+ //Global variable: Contacts information
37
7
  var contactsInfo = new Array();
38
8
  //contactsInfo['slug'] = "[Object chatContact]";
39
9
 
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
-
47
- //Videochat params
48
- //Sender: disconnected, negotiating , connecting , waiting, establishing, connected
49
- //Receiver: disconnected , pending , establishing, connected
50
- this.videoChatStatus = "disconnected";
51
- this.session_id=null;
52
- this.user_token=null;
53
- this.guest_token=null;
54
- this.session=null;
55
- this.publisher=null;
56
-
57
- //Game params
58
- //Sender: disconnected, waiting, playing
59
- //Receiver: disconnected , pending , playing
60
- this.gameStatus="disconnected";
61
- this.game=null;
62
- }
63
-
64
- //Rooms information
65
- var roomsInfo = new Array();
66
- //roomsInfo['groupSlug'] = "[Object chatRoom]";
67
-
68
- //Manage room information
69
- function chatRoom() {
70
- this.occupants=[];
71
- this.joined = false;
72
- this.affiliation = "none";
73
- this.role = "none";
74
- this.nick = getBaseNickFromUserName();
75
- }
76
10
 
11
+ PRESENCE.XMPPClient = (function(P,$,undefined){
77
12
 
78
- //IQsIDs
79
- var iqStanzaID = new Array();
80
- iqStanzaID['cinfo'] = "versionID";
81
- iqStanzaID['videochatRequest'] = "videochatRequestID";
82
- iqStanzaID['videochatRequestCancel'] = "videochatRequestCancelID";
83
-
84
-
85
-
86
- ////////////////////
87
- //Connect functions
88
- ////////////////////
13
+ //XMPP Client Variables
14
+ var domain = '<%=SocialStream::Presence.domain%>';
15
+
16
+ ////////////////////
17
+ //Hash tables
18
+ ////////////////////
19
+
20
+ //Keys: Social Stream Chat Status
21
+ //Value: Xmpp status
22
+ var sstreamChatStatus = new Array();
23
+ sstreamChatStatus['available'] = "chat";
24
+ sstreamChatStatus['away'] = "away";
25
+ sstreamChatStatus['autoaway'] = "away";
26
+ sstreamChatStatus['dnd'] = "dnd";
27
+
28
+ //Keys: Xmpp status
29
+ //Value: Message associated with Xmpp Status
30
+ var statusMessage = new Array();
31
+ statusMessage[''] = "";
32
+ statusMessage['chat'] = "";
33
+ statusMessage['away'] = "Away";
34
+ statusMessage['xa'] = "Away";
35
+ statusMessage['dnd'] = "Busy";
36
+
37
+ //Manage contact information
38
+ function chatContact(domain,resource,client,version) {
39
+ this.domain=domain
40
+ this.resource=resource
41
+ this.client=client;
42
+ this.version=version;
43
+
44
+ //Videochat params
45
+ //Sender: disconnected, negotiating , connecting , waiting, establishing, connected
46
+ //Receiver: disconnected , pending , establishing, connected
47
+ this.videoChatStatus = "disconnected";
48
+ this.session_id=null;
49
+ this.user_token=null;
50
+ this.guest_token=null;
51
+ this.session=null;
52
+ this.publisher=null;
53
+
54
+ //Game params
55
+ //Sender: disconnected, waiting, playing
56
+ //Receiver: disconnected , pending , playing
57
+ this.gameStatus="disconnected";
58
+ this.game=null;
59
+ }
60
+
61
+ //Rooms information
62
+ var roomsInfo = new Array();
63
+ //roomsInfo['groupSlug'] = "[Object chatRoom]";
64
+
65
+ //Manage room information
66
+ function chatRoom() {
67
+ this.occupants=[];
68
+ this.joined = false;
69
+ this.affiliation = "none";
70
+ this.role = "none";
71
+ this.nick = getBaseNickFromUserName();
72
+ }
73
+
89
74
 
90
- function connectToChat(user_jid,cookie,password){
75
+ //IQsIDs
76
+ var iqStanzaID = new Array();
77
+ iqStanzaID['cinfo'] = "versionID";
78
+ iqStanzaID['videochatRequest'] = "videochatRequestID";
79
+ iqStanzaID['videochatRequestCancel'] = "videochatRequestCancelID";
80
+
81
+
82
+ var init = function(){
83
+ getConnection();
84
+ }
85
+
86
+
87
+ ////////////////////
88
+ //Connect functions
89
+ ////////////////////
90
+
91
+ var connectToChat = function(user_jid,credential){
92
+
93
+ if (isUserConnected()){
94
+ return true;
95
+ }
96
+
97
+ if (authByCookie()){
98
+ if (connectToServerWithCookie(user_jid, credential)==false){
99
+ PRESENCE.UIMANAGER.updateChatWindow();
100
+ }
101
+ } else {
102
+ if (connectToServerWithPassword(user_jid,credential)==false){
103
+ PRESENCE.UIMANAGER.updateChatWindow();
104
+ }
105
+ }
106
+ }
107
+
108
+ var isStropheConnected = function(){
109
+ if((strophe_connection!=null)&&(strophe_connection.connected)){
110
+ return true;
111
+ } else {
112
+ return false;
113
+ }
114
+ }
115
+
116
+ var isUserConnected = function(){
117
+ return ((isStropheConnected()) && (userStatus!="offline") && (!disconnectionFlag));
118
+ }
119
+
120
+ var connectToServerWithCookie = function(user_jid, cookie){
121
+ try {
122
+ strophe_connection = getConnection();
123
+ strophe_connection.connect(user_jid, cookie, onConnect);
124
+ } catch (err) {
125
+ //"Handle errors"
126
+ return false;
127
+ }
128
+ return true;
129
+ }
130
+
131
+ //Password: Get from chatPassword param if exists, instead try to get from sessionStorage.
132
+ var connectToServerWithPassword = function(user_jid, chatPassword){
133
+
134
+ //Get Password
135
+ if ((chatPassword!=null)&&(chatPassword!="")){
136
+ var password = chatPassword;
137
+ } else if ((window.sessionStorage)&&(sessionStorage.getItem("ss_user_pass") != null)) {
138
+ var password = sessionStorage.getItem("ss_user_pass");
139
+ } else {
140
+ return false;
141
+ }
142
+
143
+ try {
144
+ //Connect actual user to the chat
145
+ strophe_connection = getConnection();
146
+ strophe_connection.connect(user_jid, password, onConnect);
147
+ } catch (err) {
148
+ //"Handle errors"
149
+ return false;
150
+ }
151
+
152
+ return true;
153
+ }
154
+
155
+
156
+ ////////
157
+ //Auth Methods
158
+ ///////
159
+ var authByCookie = function(){
160
+ var authMethod = '<%= SocialStream::Presence.auth_method %>';
161
+ return authMethod=="cookie";
162
+ }
163
+
164
+ var authByPassword = function(){
165
+ var authMethod = '<%= SocialStream::Presence.auth_method %>';
166
+ return authMethod=="password";
167
+ }
168
+
169
+
170
+ ////////////////////
171
+ //Stanza management using Strophe
172
+ ////////////////////
173
+
174
+ //Global variables
175
+ var userStatus;
176
+ var awayTimerPeriod = 60000;
177
+ var awayTime = 10*60000; //10 minutes
178
+ var awayCounter = 0;
179
+ var strophe_connection = null;
180
+ var reconnectAttempts = 3;
181
+ var awayTimer;
182
+ var reconnectTimer;
183
+ var disconnectionFlag = false;
184
+ var afterNewConnectionFlag = false;
185
+ var afterFirstConnectionFlag = true;
186
+
187
+
188
+ var onConnect = function(status) {
189
+
190
+ //Status.ERROR An error has occurred
191
+ //Status.CONNECTING The connection is currently being made
192
+ //Status.CONNFAIL The connection attempt failed
193
+ //Status.AUTHENTICATING The connection is authenticating
194
+ //Status.AUTHFAIL The authentication attempt failed
195
+ //Status.CONNECTED The connection has succeeded
196
+ //Status.DISCONNECTED The connection has been terminated
197
+ //Status.DISCONNECTING The connection is currently being terminated
198
+ //Status.ATTACHED The connection has been attached
199
+
200
+ PRESENCE.UTILITIES.log('Strophe onConnect callback call with status ' + status);
201
+
202
+ if (status == Strophe.Status.ERROR){
203
+ PRESENCE.UTILITIES.log('Strophe connection error');
204
+ return;
205
+ }
206
+
207
+ if (status == Strophe.Status.ATTACHED){
208
+ PRESENCE.UTILITIES.log('Strophe connection attached');
209
+ return;
210
+ }
211
+
212
+ if (status == Strophe.Status.AUTHENTICATING ){
213
+ PRESENCE.UTILITIES.log('Strophe connection AUTHENTICATING');
214
+ return;
215
+ }
216
+
217
+ if (status == Strophe.Status.CONNECTING) {
218
+ PRESENCE.UTILITIES.log('Strophe is connecting.');
219
+ return;
220
+ }
221
+
222
+ if (status == Strophe.Status.DISCONNECTING) {
223
+ PRESENCE.UTILITIES.log('Strophe is disconnecting.');
224
+ return;
225
+ }
226
+
227
+ if(typeof initialTimer != 'undefined'){
228
+ clearTimeout(initialTimer);
229
+ }
230
+
231
+ if (status == Strophe.Status.CONNFAIL) {
232
+ PRESENCE.UTILITIES.log('Strophe failed to connect.');
233
+ reconnectTimer = setTimeout ("PRESENCE.XMPPClient.onReconnect()", 5000);
234
+ disconnectionFlag = true;
235
+ } else if (status == Strophe.Status.AUTHFAIL) {
236
+ PRESENCE.UTILITIES.log('Strophe authentication fail.');
237
+ if ((window.sessionStorage)&&(sessionStorage.getItem("ss_user_pass") != null)){
238
+ sessionStorage.setItem("ss_user_pass",null);
239
+ }
240
+ disconnectionFlag = true;
241
+ } else if (status == Strophe.Status.ERROR) {
242
+ PRESENCE.UTILITIES.log('Strophe error.');
243
+ disconnectionFlag = true;
244
+ } else if (status == Strophe.Status.DISCONNECTED) {
245
+ PRESENCE.UTILITIES.log('Strophe is disconnected.');
246
+ disconnectionFlag = true;
247
+ clearTimeout(awayTimer);
248
+ PRESENCE.UIMANAGER.updateInterfaceAfterUserDisconnect();
249
+ updateRoomsInfoAfterUserDisconnect();
250
+ reconnectTimer = setTimeout ("PRESENCE.XMPPClient.onReconnect()", 5000);
251
+ } else if (status == Strophe.Status.CONNECTED) {
252
+ //AFTER CONNECT ACTIONS
253
+
254
+ PRESENCE.UTILITIES.log('Strophe is connected.');
255
+
256
+ clearTimeout(reconnectTimer);
257
+
258
+ //addHandler:(callback, namespace to match, stanza name, stanza type, stanza id , stanza from, options)
259
+ strophe_connection.addHandler(onMessage, null, 'message', 'chat', null, null);
260
+ strophe_connection.addHandler(onRoomMessage, null, 'message', 'groupchat', null, null);
261
+ strophe_connection.addHandler(onPresence, null, 'presence', null, null, null);
262
+ strophe_connection.addHandler(onIQStanza,null, "iq", null, null);
263
+
264
+ disconnectionFlag = false;
265
+ afterNewConnectionFlag = true;
266
+
267
+ if(! userStatus){
268
+ userStatus = PRESENCE.PERSISTENCE.getRestoreUserChatStatus();
269
+ }
270
+ if(userStatus=="offline"){
271
+ userStatus="chat";
272
+ }
273
+ sendStatus(userStatus);
274
+
275
+ awayTimer = setInterval("PRESENCE.XMPPClient.awayTimerFunction()", awayTimerPeriod);
276
+ }
277
+
278
+ PRESENCE.UIMANAGER.updateChatWindow();
279
+ }
91
280
 
92
- if (isUserConnected()){
93
- return true;
94
- }
281
+ var onReconnect = function(){
282
+
283
+ return
95
284
 
96
- if (authByCookie()){
97
- if (connectToServerWithCookie(user_jid, cookie)==false){
98
- updateChatWindow();
99
- }
100
- } else {
101
- if (connectToServerWithPassword(user_jid,password)==false){
102
- updateChatWindow();
103
- }
104
- }
105
- }
285
+ if ((!isStropheConnected())&&(userStatus!="offline")) {
286
+
287
+ if (reconnectAttempts>0) {
288
+ reconnectAttempts--;
289
+
290
+ PRESENCE.UIMANAGER.changeChatHeaderTitle(I18n.t('chat.reconnecting'))
291
+
292
+ connectToChat(user_jid,cookie,null);
293
+
294
+ reconnectTimer = setTimeout ("PRESENCE.XMPPClient.onReconnect()", 9000);
295
+
296
+ } else {
297
+ PRESENCE.UIMANAGER.changeChatHeaderTitle(I18n.t('chat.unableconnect'))
298
+ //Notify issue to Rails App Server?
299
+ }
300
+
301
+ }
302
+ }
303
+
304
+ var disconnect = function(){
305
+ userStatus = "offline";
306
+ PRESENCE.UIMANAGER.setStatusWidgetTitle("offline");
307
+
308
+ if(isStropheConnected()){
309
+
310
+ //Leave from rooms before disconnect
311
+ if(roomsInfo){
312
+ for(var roomName in roomsInfo){
313
+ if(roomsInfo[roomName].joined){
314
+ leaveRoom(roomName);
315
+ }
316
+ }
317
+ }
318
+
319
+ strophe_connection.send($pres({type: "unavailable"}).tree());
320
+ strophe_connection.disconnect();
321
+ }
322
+ }
323
+
324
+
325
+
326
+ ////////////////////
327
+ //Away Timer Management
328
+ ////////////////////
329
+
330
+ var awayTimerFunction = function(){
331
+ awayCounter++;
332
+ if (awayCounter > (awayTime/awayTimerPeriod)){
333
+ if ((userStatus != "dnd")&&(userStatus != "away")) {
334
+ userStatus = "autoaway";
335
+ sendStatus(userStatus);
336
+ }
337
+ clearTimeout(awayTimer);
338
+ }
339
+ }
340
+
341
+ var autochangeStatusIfAutoAway = function(){
342
+ if (userStatus == "autoaway"){
343
+ userStatus = "available";
344
+ sendStatus(userStatus);
345
+ }
346
+ }
347
+
348
+ var restartAwayTimer = function(){
349
+ if (awayCounter > (awayTime/awayTimerPeriod)){
350
+ awayTimer = setInterval("PRESENCE.XMPPClient.awayTimerFunction()", awayTimerPeriod);
351
+ autochangeStatusIfAutoAway();
352
+ }
353
+ awayCounter = 0;
354
+ }
355
+
356
+
357
+ ////////
358
+ //Manage Message stanzas
359
+ ///////
360
+
361
+ //Chat messages handler
362
+ var onMessage = function(msg) {
106
363
 
107
- function isStropheConnected(){
108
- if((strophe_connection!=null)&&(strophe_connection.connected)){
364
+ var from = msg.getAttribute('from');
365
+ var type = msg.getAttribute('type');
366
+ var elems = msg.getElementsByTagName('body');
367
+
368
+ if (type == "chat" && elems.length > 0) {
369
+ var body = elems[0];
370
+ var msg = Strophe.getText(body);
371
+ var from_slug = getSlugFromJid(from)
372
+
373
+ if(isChatRoomJid(from)){
374
+ return true
375
+ } else {
376
+ var from_slug = getSlugFromJid(from)
377
+ PRESENCE.UIMANAGER.afterReceivedChatMessage(from_slug,msg,null)
378
+ }
379
+ }
380
+
381
+ // we must return true to keep the handler alive.
382
+ // returning false would remove it after it finishes.
383
+ return true;
384
+ }
385
+
386
+
387
+ ////////
388
+ //Manage Presence stanzas
389
+ ///////
390
+ var onPresence = function(presence) {
391
+ if (isChatRoomJid($(presence).attr('from'))){
392
+ return onRoomPresence(presence);
393
+ } else {
394
+ return onBuddyPresence(presence);
395
+ }
396
+ }
397
+
398
+ var onBuddyPresence = function(presence){
399
+ var ptype = $(presence).attr('type');
400
+
401
+ switch (ptype){
402
+ case undefined:
403
+ processAvailablePresenceStanza(presence)
404
+ break;
405
+ case "available":
406
+ processAvailablePresenceStanza(presence)
407
+ break;
408
+ case "unavailable":
409
+ processUnavailablePresenceStanza(presence)
410
+ break;
411
+ default :
412
+ //Stanza type not recognize
413
+ processAvailablePresenceStanza(presence)
414
+ }
415
+
416
+ return true;
417
+ }
418
+
419
+ var processAvailablePresenceStanza = function(presence){
420
+ var from = $(presence).attr('from');
421
+ var slug = from.split("@")[0];
422
+
423
+ if (slug != user_slug) {
424
+ storeContactInformation(presence);
425
+ if (PRESENCE.UIMANAGER.getConnectionBoxFromSlug(slug)!=null){
426
+ var status = $(presence).find('show').text();
427
+ PRESENCE.UIMANAGER.setUserIconStatus(slug,status);
428
+ PRESENCE.UIMANAGER.updateInterfaceAfterPresenceStanza(slug,true);
429
+ } else {
430
+ if(! PRESENCE.UTILITIES.isAdminSlug(slug)){
431
+ PRESENCE.UIMANAGER.updateChatWindow();
432
+ }
433
+ }
434
+ }
435
+ }
436
+
437
+ var processUnavailablePresenceStanza = function(presence){
438
+ var from = $(presence).attr('from');
439
+ var slug = from.split("@")[0];
440
+
441
+ if (slug != user_slug) {
442
+ if (PRESENCE.UIMANAGER.getConnectionBoxFromSlug(slug)!=null){
443
+ PRESENCE.UIMANAGER.updateInterfaceAfterPresenceStanza(slug,false)
444
+ PRESENCE.GAME.userDisconnected(slug)
445
+ }
446
+ }
447
+ }
448
+
449
+ var storeContactInformation = function(stanza){
450
+ var from = $(stanza).attr('from');
451
+ var split = from.split("@");
452
+ var slug = split[0];
453
+ var domain = split[1].split("/")[0];
454
+ var resource = split[1].split("/")[1];
455
+
456
+ if (!(slug in contactsInfo)) {
457
+ var contact = new chatContact(domain,resource,null,null);
458
+ contactsInfo[slug]=contact;
459
+ } else {
460
+ contactsInfo[slug].domain = domain;
461
+ contactsInfo[slug].resource = resource;
462
+ }
463
+ }
464
+
465
+ var storeRoomInformation = function(roomName){
466
+ if (!(roomName in roomsInfo)) {
467
+ var room = new chatRoom();
468
+ roomsInfo[roomName]=room;
469
+ }
470
+ }
471
+
472
+
473
+ ////////
474
+ //Manage IQ stanzas
475
+ ///////
476
+
477
+ ///////
478
+ //RECEIVED STANZAS
479
+ ///////
480
+ var onIQStanza = function(iq){
481
+ var type = iq.getAttribute("type");
482
+ var from = iq.getAttribute("from");
483
+ var slug = from.split("@")[0];
484
+
485
+ if (slug == user_slug) {
486
+ return true;
487
+ }
488
+
489
+ if(type=="get"){
490
+ return handleGetIQStanza(iq,from,slug);
491
+ } else if(type="result"){
492
+ return handleResultIQStanza(iq,from,slug);
493
+ }
494
+
495
+ return true;
496
+ }
497
+
498
+ ///////
499
+ //RECEIVED STANZAS: GET
500
+ ///////
501
+ var handleGetIQStanza = function(iq,jid,slug){
502
+
503
+ var iqID = iq.getAttribute("id");
504
+
505
+ //Case 1: Request client info
506
+ var queryElements = iq.getElementsByTagName('query');
507
+
508
+ if (queryElements.length > 0) {
509
+ var query = queryElements[0];
510
+ var xmlns_type = query.getAttribute("xmlns");
511
+
512
+ if( xmlns_type == "jabber:iq:version"){
513
+ sendIQStanzaWithClientInfo(jid,iqID);
514
+ return true;
515
+ }
516
+
517
+ if (xmlns_type == "jabber:iq:last") {
518
+ sendIQStanzaLast(jid,iqID);
519
+ return true;
520
+ }
521
+ }
522
+
523
+ //Case 2: Request videochat
524
+ if(iqID==iqStanzaID['videochatRequest']){
525
+ handleGetVideochatIQStanza(jid,iqID,iq,slug);
526
+ return true;
527
+ }
528
+
529
+ //Case 3: Request game
530
+ if(iqID==PRESENCE.GAME.COMUNICATION.getIQStanzaID()['gameRequest']){
531
+ //Do nothing
109
532
  return true;
110
- } else {
111
- return false;
112
533
  }
113
- }
114
-
115
- function isUserConnected(){
116
- return ((isStropheConnected()) && (userStatus!="offline") && (!disconnectionFlag));
117
- }
118
-
119
- function connectToServerWithCookie(user_jid, cookie){
120
- try {
121
- strophe_connection = new Strophe.Connection(BOSH_SERVICE);
122
- strophe_connection.connect(user_jid, cookie, onConnect);
123
- } catch (err) {
124
- //"Handle errors"
125
- return false;
126
- }
127
- return true;
128
- }
129
-
130
- //Password: Get from chatPassword param if exists, instead try to get from sessionStorage.
131
- function connectToServerWithPassword(user_jid, chatPassword){
132
-
133
- //Get Password
134
- if ((chatPassword!=null)&&(chatPassword!="")){
135
- var password = chatPassword;
136
- } else if ((window.sessionStorage)&&(sessionStorage.getItem("ss_user_pass") != null)) {
137
- var password = sessionStorage.getItem("ss_user_pass");
138
- } else {
139
- return false;
140
- }
141
-
142
- try {
143
- //Connect actual user to the chat
144
- strophe_connection = new Strophe.Connection(BOSH_SERVICE);
145
- strophe_connection.connect(user_jid, password, onConnect);
146
- } catch (err) {
147
- //"Handle errors"
148
- return false;
149
- }
150
-
151
- return true;
152
- }
153
-
154
-
155
- ////////
156
- //Auth Methods
157
- ///////
158
- function authByCookie(){
159
- var authMethod = '<%= SocialStream::Presence.auth_method %>';
160
- return authMethod=="cookie";
161
- }
162
-
163
- function authByPassword(){
164
- var authMethod = '<%= SocialStream::Presence.auth_method %>';
165
- return authMethod=="password";
166
- }
167
-
168
-
169
- ////////////////////
170
- //Stanza management using Strophe
171
- ////////////////////
172
-
173
- //Global variables
174
- var userStatus;
175
- var awayTimerPeriod = 60000;
176
- var awayTime = 10*60000; //10 minutes
177
- var awayCounter = 0;
178
- var strophe_connection = null;
179
- var reconnectAttempts = 3;
180
- var awayTimer;
181
- var reconnectTimer;
182
- var disconnectionFlag = false;
183
- var afterNewConnectionFlag = false;
184
- var afterFirstConnectionFlag = true;
185
-
186
-
187
- function onConnect(status) {
188
-
189
- //Status.ERROR An error has occurred
190
- //Status.CONNECTING The connection is currently being made
191
- //Status.CONNFAIL The connection attempt failed
192
- //Status.AUTHENTICATING The connection is authenticating
193
- //Status.AUTHFAIL The authentication attempt failed
194
- //Status.CONNECTED The connection has succeeded
195
- //Status.DISCONNECTED The connection has been terminated
196
- //Status.DISCONNECTING The connection is currently being terminated
197
- //Status.ATTACHED The connection has been attached
198
-
199
- log('Strophe onConnect callback call with status ' + status);
200
-
201
- if (status == Strophe.Status.ERROR){
202
- log('Strophe connection error');
203
- return;
204
- }
205
-
206
- if (status == Strophe.Status.ATTACHED){
207
- log('Strophe connection attached');
208
- return;
209
- }
210
-
211
- if (status == Strophe.Status.AUTHENTICATING ){
212
- log('Strophe connection AUTHENTICATING');
213
- return;
214
- }
215
-
216
- if (status == Strophe.Status.CONNECTING) {
217
- log('Strophe is connecting.');
218
- return;
219
- }
220
-
221
- if (status == Strophe.Status.DISCONNECTING) {
222
- log('Strophe is disconnecting.');
223
- return;
224
- }
225
-
226
- if(typeof initialTimer != 'undefined'){
227
- clearTimeout(initialTimer);
228
- }
229
-
230
- if (status == Strophe.Status.CONNFAIL) {
231
- log('Strophe failed to connect.');
232
- reconnectTimer = setTimeout ("onReconnect()", 5000);
233
- disconnectionFlag = true;
234
- } else if (status == Strophe.Status.AUTHFAIL) {
235
- log('Strophe authentication fail.');
236
- if ((window.sessionStorage)&&(sessionStorage.getItem("ss_user_pass") != null)){
237
- sessionStorage.setItem("ss_user_pass",null);
238
- }
239
- disconnectionFlag = true;
240
- } else if (status == Strophe.Status.ERROR) {
241
- log('Strophe error.');
242
- disconnectionFlag = true;
243
- } else if (status == Strophe.Status.DISCONNECTED) {
244
- log('Strophe is disconnected.');
245
- disconnectionFlag = true;
246
- clearTimeout(awayTimer);
247
- updateInterfaceAfterUserDisconnect();
248
- updateRoomsInfoAfterUserDisconnect();
249
- reconnectTimer = setTimeout ("onReconnect()", 5000);
250
- } else if (status == Strophe.Status.CONNECTED) {
251
- //AFTER CONNECT ACTIONS
252
-
253
- log('Strophe is connected.');
254
-
255
- clearTimeout(reconnectTimer);
256
534
 
257
- //addHandler:(callback, namespace to match, stanza name, stanza type, stanza id , stanza from, options)
258
- strophe_connection.addHandler(onMessage, null, 'message', 'chat', null, null);
259
- strophe_connection.addHandler(onRoomMessage, null, 'message', 'groupchat', null, null);
260
- strophe_connection.addHandler(onPresence, null, 'presence', null, null, null);
261
- strophe_connection.addHandler(onIQStanza,null, "iq", null, null);
262
-
263
- disconnectionFlag = false;
264
- afterNewConnectionFlag = true;
265
-
266
- if(! userStatus){
267
- userStatus = getRestoreUserChatStatus();
268
- }
269
- if(userStatus=="offline"){
270
- userStatus="chat";
271
- }
272
- sendStatus(userStatus);
273
-
274
- awayTimer = setInterval("awayTimerFunction()", awayTimerPeriod);
275
- }
276
-
277
- updateChatWindow();
278
- }
279
-
280
- function onReconnect(){
281
-
282
- if ((!isStropheConnected())&&(userStatus!="offline")) {
283
-
284
- if (reconnectAttempts>0) {
285
- reconnectAttempts--;
286
-
287
- changeChatHeaderTitle(I18n.t('chat.reconnecting'))
288
-
289
- connectToChat(user_jid,cookie,null);
290
-
291
- reconnectTimer = setTimeout ("onReconnect()", 9000);
292
-
293
- } else {
294
- changeChatHeaderTitle(I18n.t('chat.unableconnect'))
295
- //Notify issue to Rails App Server?
296
- }
297
-
298
- }
299
- }
300
-
301
- function disconnectStrophe(){
302
- userStatus = "offline";
303
- setStatusWidgetTitle("offline");
304
-
305
- if(isStropheConnected()){
306
- strophe_connection.send($pres({type: "unavailable"}).tree());
307
- strophe_connection.disconnect();
308
- }
309
- }
310
-
311
-
312
- ////////
313
- //Manage Message stanzas
314
- ///////
315
-
316
- //Chat messages handler
317
- function onMessage(msg) {
318
- var from = msg.getAttribute('from');
319
- var type = msg.getAttribute('type');
320
- var elems = msg.getElementsByTagName('body');
321
-
322
- if (type == "chat" && elems.length > 0) {
323
- var body = elems[0];
324
- var msg = Strophe.getText(body);
325
- var from_slug = getSlugFromJid(from)
326
-
327
- if(isChatRoomJid(from)){
328
- return true
329
- } else {
330
- var from_slug = getSlugFromJid(from)
331
- afterReceivedChatMessage(from_slug,msg,null)
332
- }
333
- }
334
-
335
- // we must return true to keep the handler alive.
336
- // returning false would remove it after it finishes.
337
- return true;
338
- }
339
-
340
-
341
- ////////
342
- //Manage Presence stanzas
343
- ///////
344
- function onPresence(presence) {
345
- if (isChatRoomJid($(presence).attr('from'))){
346
- return onRoomPresence(presence);
347
- } else {
348
- return onBuddyPresence(presence);
349
- }
350
- }
351
-
352
- function onBuddyPresence(presence){
353
- var ptype = $(presence).attr('type');
354
-
355
- switch (ptype){
356
- case undefined:
357
- processAvailablePresenceStanza(presence)
358
- break;
359
- case "available":
360
- processAvailablePresenceStanza(presence)
361
- break;
362
- case "unavailable":
363
- processUnavailablePresenceStanza(presence)
364
- break;
365
- default :
366
- //Stanza type not recognize
367
- processAvailablePresenceStanza(presence)
368
- }
535
+ //Case 4: Default behaviour
536
+ sendIQEmpty(jid,iqID);
369
537
 
370
- return true;
371
- }
372
-
373
- function processAvailablePresenceStanza(presence){
374
- var from = $(presence).attr('from');
375
- var slug = from.split("@")[0];
376
-
377
- if (slug != user_slug) {
378
- storeContactInformation(presence);
379
- if (getConnectionBoxFromSlug(slug)!=null){
380
- var status = $(presence).find('show').text();
381
- setUserIconStatus(slug,status);
382
- updateInterfaceAfterPresenceStanza(slug,true);
383
- } else {
384
- if(! isAdminSlug(slug)){
385
- updateChatWindow();
386
- }
387
- }
388
- }
389
- }
390
-
391
- function processUnavailablePresenceStanza(presence){
392
- var from = $(presence).attr('from');
393
- var slug = from.split("@")[0];
394
-
395
- if (slug != user_slug) {
396
- if (getConnectionBoxFromSlug(slug)!=null){
397
- updateInterfaceAfterPresenceStanza(slug,false)
398
- PRESENCE.GAME.userDisconnected(slug)
399
- }
400
- }
401
- }
402
-
403
- function storeContactInformation(stanza){
404
- var from = $(stanza).attr('from');
405
- var split = from.split("@");
406
- var slug = split[0];
407
- var domain = split[1].split("/")[0];
408
- var resource = split[1].split("/")[1];
409
-
410
- if (!(slug in contactsInfo)) {
411
- var contact = new chatContact(domain,resource,null,null);
412
- contactsInfo[slug]=contact;
413
- } else {
414
- contactsInfo[slug].domain = domain;
415
- contactsInfo[slug].resource = resource;
416
- }
417
- }
418
-
419
- function storeRoomInformation(roomName){
420
- if (!(roomName in roomsInfo)) {
421
- var room = new chatRoom();
422
- roomsInfo[roomName]=room;
423
- }
424
- }
425
-
426
-
427
- ////////
428
- //Manage IQ stanzas
429
- ///////
430
-
431
- ///////
432
- //RECEIVED STANZAS
433
- ///////
434
- function onIQStanza(iq){
435
- var type = iq.getAttribute("type");
436
- var from = iq.getAttribute("from");
437
- var slug = from.split("@")[0];
438
-
439
- if (slug == user_slug) {
440
- return true;
441
- }
538
+ return true;
539
+ }
442
540
 
443
- if(type=="get"){
444
- return handleGetIQStanza(iq,from,slug);
445
- } else if(type="result"){
446
- return handleResultIQStanza(iq,from,slug);
541
+ var handleGetVideochatIQStanza = function(jid,iqID,iq,slug){
542
+ storeContactInformation(iq);
543
+ var queryTag = iq.getElementsByTagName('query');
544
+
545
+ if (queryTag.length > 0) {
546
+ var queryElement = queryTag[0];
547
+ var session_id = queryElement.getElementsByTagName('session_id');
548
+ var token = queryElement.getElementsByTagName('token');
549
+
550
+ if((session_id.length == 1)&&(token.length == 1)){
551
+ contactsInfo[slug].session_id = $(session_id).text();
552
+ contactsInfo[slug].user_token = $(token).text();
553
+ PRESENCE.VIDEOCHAT.updateInterfaceAfterVideoRequestReceived(slug);
554
+ return;
555
+ }
556
+ }
557
+
558
+ //Send empty stanza
559
+ sendIQEmpty(jid,iqID);
447
560
  }
448
561
 
449
- return true;
450
- }
451
-
452
- ///////
453
- //RECEIVED STANZAS: GET
454
- ///////
455
- function handleGetIQStanza(iq,jid,slug){
456
562
 
457
- var iqID = iq.getAttribute("id");
563
+ ///////
564
+ //RECEIVED STANZAS: RESULT
565
+ ///////
566
+ var handleResultIQStanza = function(iq,jid,slug){
567
+ var iqID = iq.getAttribute("id");
568
+
569
+ if (iqID==iqStanzaID['cinfo']){
570
+ return handleIQResultWithClientInfo(iq,slug);
571
+ }
572
+
573
+ if (iqID==iqStanzaID['videochatRequest']){
574
+ return handleIQResultFromVideochatRequest(iq,slug);
575
+ }
576
+
577
+ if(iqID==iqStanzaID['videochatRequestCancel']){
578
+ return handleIQResultFromVideochatRequestCancel(iq,slug);
579
+ }
580
+
581
+ //Case: Default behaviour
582
+ return true;
583
+ }
458
584
 
459
- //Case 1: Request client info
460
- var queryElements = iq.getElementsByTagName('query');
461
-
462
- if (queryElements.length > 0) {
463
- var query = queryElements[0];
464
- var xmlns_type = query.getAttribute("xmlns");
465
-
466
- if( xmlns_type == "jabber:iq:version"){
467
- sendIQStanzaWithClientInfo(jid,iqID);
468
- return true;
469
- }
470
-
471
- if (xmlns_type == "jabber:iq:last") {
472
- sendIQStanzaLast(jid,iqID);
473
- return true;
474
- }
475
- }
476
585
 
477
- //Case 2: Request videochat
478
- if(iqID==iqStanzaID['videochatRequest']){
479
- handleGetVideochatIQStanza(jid,iqID,iq,slug);
480
- return true;
586
+ var handleIQResultWithClientInfo = function(iq,slug){
587
+ var queryTag = iq.getElementsByTagName('query');
588
+
589
+ if (queryTag.length > 0) {
590
+ var queryElement = queryTag[0];
591
+ var name = queryElement.getElementsByTagName('name');
592
+ var version = queryElement.getElementsByTagName('version');
593
+
594
+ if((name.length == 1)&&(version.length == 1)){
595
+ var clientValue = $(name).text();
596
+ var versionValue = $(version).text();
597
+
598
+ if (!(slug in contactsInfo)) {
599
+ var contact = new chatContact(null,null,clientValue,versionValue);
600
+ contactsInfo[slug]=contact;
601
+ } else {
602
+ contactsInfo[slug].client = clientValue;
603
+ contactsInfo[slug].version = versionValue;
604
+ }
605
+ PRESENCE.VIDEOCHAT.clientInfoReceivedTrigger(slug);
606
+ }
607
+ }
608
+
609
+ return true;
481
610
  }
482
611
 
483
- //Case 3: Default behaviour
484
- sendIQEmpty(jid,iqID);
485
-
486
- return true;
487
- }
488
-
489
- function handleGetVideochatIQStanza(jid,iqID,iq,slug){
490
- storeContactInformation(iq);
491
- var queryTag = iq.getElementsByTagName('query');
492
-
493
- if (queryTag.length > 0) {
494
- var queryElement = queryTag[0];
495
- var session_id = queryElement.getElementsByTagName('session_id');
496
- var token = queryElement.getElementsByTagName('token');
497
-
498
- if((session_id.length == 1)&&(token.length == 1)){
499
- contactsInfo[slug].session_id = $(session_id).text();
500
- contactsInfo[slug].user_token = $(token).text();
501
- updateInterfaceAfterVideoRequestReceived(slug);
502
- return;
503
- }
504
- }
505
612
 
506
- //Send empty stanza
507
- sendIQEmpty(jid,iqID);
508
- }
509
-
510
-
511
- ///////
512
- //RECEIVED STANZAS: RESULT
513
- ///////
514
- function handleResultIQStanza(iq,jid,slug){
515
- var iqID = iq.getAttribute("id");
613
+ var handleIQResultFromVideochatRequest = function(iq,slug){
614
+
615
+ var queryTag = iq.getElementsByTagName('query');
616
+
617
+ if (queryTag.length > 0) {
618
+ var queryElement = queryTag[0];
619
+ var response = queryElement.getElementsByTagName('response');
620
+
621
+ if(response.length == 1){
622
+ PRESENCE.VIDEOCHAT.receiveVideoChatResponseFromUser(slug,$(response).text());
623
+ return true;
624
+ }
625
+ }
626
+
627
+ PRESENCE.VIDEOCHAT.receiveVideoChatResponseFromUser(slug,"Bad response");
628
+ return true;
629
+ }
516
630
 
517
- if (iqID==iqStanzaID['cinfo']){
518
- return handleIQResultWithClientInfo(iq,slug);
631
+
632
+ var handleIQResultFromVideochatRequestCancel = function(iq,slug){
633
+
634
+ var queryTag = iq.getElementsByTagName('query');
635
+
636
+ if (queryTag.length > 0) {
637
+ var queryElement = queryTag[0];
638
+ var response = queryElement.getElementsByTagName('response');
639
+
640
+ if(response.length == 1){
641
+ PRESENCE.VIDEOCHAT.receiveVideoChatCancelationFromUser(slug);
642
+ return true;
643
+ }
644
+ }
645
+
646
+ return true;
519
647
  }
520
648
 
521
- if (iqID==iqStanzaID['videochatRequest']){
522
- return handleIQResultFromVideochatRequest(iq,slug);
523
- }
524
649
 
525
- if(iqID==iqStanzaID['videochatRequestCancel']){
526
- return handleIQResultFromVideochatRequestCancel(iq,slug);
527
- }
650
+ ///////
651
+ //SEND STANZAS: GET
652
+ ///////
653
+ var sendIQStanzaForRequestClientInfo = function(slug){
654
+ if (slug in contactsInfo) {
655
+ var domain = contactsInfo[slug].domain;
656
+ var resource = contactsInfo[slug].resource;
657
+ var jid=slug+"@"+domain+"/"+resource
658
+
659
+ var iq = $iq({to: jid, type: "get", id: iqStanzaID['cinfo'], xmlns: "jabber:iq:version"})
660
+ .c("query", {xmlns: "jabber:iq:version"});
661
+ strophe_connection.sendIQ(iq);
662
+ }
663
+ }
528
664
 
529
- //Case: Default behaviour
530
- return true;
531
- }
532
-
533
-
534
- function handleIQResultWithClientInfo(iq,slug){
535
- var queryTag = iq.getElementsByTagName('query');
536
-
537
- if (queryTag.length > 0) {
538
- var queryElement = queryTag[0];
539
- var name = queryElement.getElementsByTagName('name');
540
- var version = queryElement.getElementsByTagName('version');
541
-
542
- if((name.length == 1)&&(version.length == 1)){
543
- var clientValue = $(name).text();
544
- var versionValue = $(version).text();
545
-
546
- if (!(slug in contactsInfo)) {
547
- var contact = new chatContact(null,null,clientValue,versionValue);
548
- contactsInfo[slug]=contact;
549
- } else {
550
- contactsInfo[slug].client = clientValue;
551
- contactsInfo[slug].version = versionValue;
552
- }
553
- clientInfoReceivedTrigger(slug);
554
- }
555
- }
665
+ var sendIQStanzaToRequestVideochat = function(slug){
666
+ if (slug in contactsInfo) {
667
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
668
+
669
+ var iq = $iq({to: jid, type: "get", id: iqStanzaID['videochatRequest']})
670
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
671
+ .c("session_id").t(contactsInfo[slug].session_id).up()
672
+ .c("token").t(contactsInfo[slug].guest_token);
673
+ strophe_connection.sendIQ(iq);
674
+ }
675
+ }
556
676
 
557
- return true;
558
- }
559
-
560
-
561
- function handleIQResultFromVideochatRequest(iq,slug){
562
677
 
563
- var queryTag = iq.getElementsByTagName('query');
564
-
565
- if (queryTag.length > 0) {
566
- var queryElement = queryTag[0];
567
- var response = queryElement.getElementsByTagName('response');
568
-
569
- if(response.length == 1){
570
- receiveVideoChatResponseFromUser(slug,$(response).text());
571
- return true;
572
- }
573
- }
678
+ ///////
679
+ //SEND STANZAS: RESULT
680
+ ///////
681
+ var sendIQStanzaWithClientInfo = function(jid,iqID){
682
+ var client = "Social Stream XMPP Client"
683
+ var version = getJavascriptXMPPClientName();
684
+ var iq = $iq({to: jid, type: "result", id: iqID})
685
+ .c("query", {xmlns: "jabber:iq:version"}).c("name").t(client).up().c("version").t(version);
686
+ strophe_connection.sendIQ(iq);
687
+ }
574
688
 
575
- receiveVideoChatResponseFromUser(slug,"Bad response");
576
- return true;
577
- }
578
-
579
-
580
- function handleIQResultFromVideochatRequestCancel(iq,slug){
581
-
582
- var queryTag = iq.getElementsByTagName('query');
583
-
584
- if (queryTag.length > 0) {
585
- var queryElement = queryTag[0];
586
- var response = queryElement.getElementsByTagName('response');
587
-
588
- if(response.length == 1){
589
- receiveVideoChatCancelationFromUser(slug);
590
- return true;
591
- }
592
- }
593
-
594
- return true;
595
- }
596
-
597
-
598
- ///////
599
- //SEND STANZAS: GET
600
- ///////
601
- function sendIQStanzaForRequestClientInfo(slug){
602
- if (slug in contactsInfo) {
603
- var domain = contactsInfo[slug].domain;
604
- var resource = contactsInfo[slug].resource;
605
- var jid=slug+"@"+domain+"/"+resource
606
-
607
- var iq = $iq({to: jid, type: "get", id: iqStanzaID['cinfo'], xmlns: "jabber:iq:version"})
608
- .c("query", {xmlns: "jabber:iq:version"});
609
- strophe_connection.sendIQ(iq);
610
- }
611
- }
612
-
613
- function sendIQStanzaToRequestVideochat(slug){
614
- if (slug in contactsInfo) {
615
- var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
616
-
617
- var iq = $iq({to: jid, type: "get", id: iqStanzaID['videochatRequest']})
618
- .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
619
- .c("session_id").t(contactsInfo[slug].session_id).up()
620
- .c("token").t(contactsInfo[slug].guest_token);
621
- strophe_connection.sendIQ(iq);
622
- }
623
- }
624
-
625
-
626
- ///////
627
- //SEND STANZAS: RESULT
628
- ///////
629
- function sendIQStanzaWithClientInfo(jid,iqID){
630
- var client = "Social Stream XMPP Client"
631
- var version = getJavascriptXMPPClientName();
632
- var iq = $iq({to: jid, type: "result", id: iqID})
633
- .c("query", {xmlns: "jabber:iq:version"}).c("name").t(client).up().c("version").t(version);
634
- strophe_connection.sendIQ(iq);
635
- }
636
-
637
- function getJavascriptXMPPClientName(){
638
- return '<%=SocialStream::Presence::VERSION%>';
639
- }
640
-
641
- function sendIQStanzaLast(jid,iqID){
642
- var iq = $iq({to: jid, type: "result", id: iqID})
643
- .c("query", {xmlns: "jabber:iq:last", seconds: "0"});
644
- strophe_connection.sendIQ(iq);
645
- }
646
-
647
- function sendIQEmpty(jid,iqID){
648
- var iq = $iq({to: jid, type: "result", id: iqID})
649
- strophe_connection.sendIQ(iq);
650
- }
651
-
652
- function sendIQStanzaToResponseVideochat(slug,result){
653
- if (slug in contactsInfo) {
654
- var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
655
- var iq = $iq({to: jid, type: "result", id: iqStanzaID['videochatRequest']})
656
- .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
657
- .c("response").t(result);
658
- strophe_connection.sendIQ(iq);
659
- }
660
- }
661
-
662
- function sendIQStanzaToCancelVideochat(slug){
663
- if (slug in contactsInfo) {
664
- var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
665
- var iq = $iq({to: jid, type: "result", id: iqStanzaID['videochatRequestCancel']})
666
- .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
667
- .c("response").t("cancel");
668
- strophe_connection.sendIQ(iq);
669
- }
670
- }
671
-
672
-
673
- ////////
674
- //Send Message stanzas
675
- ///////
676
- function sendChatMessage(guest_slug,msg){
677
- rotatePriority(guest_slug);
678
- if (isSlugGroup(guest_slug)){
679
- var guest_jid = getRoomJidFromRoomName(guest_slug)
680
- return sendGroupMessageToRoom(guest_jid,msg);
681
- } else {
682
- var headerMessage = getParsedName(user_name,true);
683
- getChatBoxForSlug(guest_slug).chatbox("option", "boxManager").addMsg(headerMessage, getParsedContent(msg,true));
684
- var guest_jid = getJidFromSlug(guest_slug)
685
- return sendChatMessageToBuddy(guest_jid,msg);
686
- }
687
- }
688
-
689
- function sendChatMessageToBuddy(to,text){
690
- var from = user_jid
691
- var type = "chat";
692
- var body= $build("body");
693
- body.t(text);
694
- var message = $msg({to: to, from: from, type: 'chat'}).cnode(body.tree());
695
- strophe_connection.send(message.tree());
696
- restartAwayTimer();
697
- return true;
698
- }
699
-
700
-
701
- ////////
702
- //Send Presence stanzas with status
703
- ///////
704
- function sendStatus(status){
689
+ var getJavascriptXMPPClientName = function(){
690
+ return '<%=SocialStream::Presence::VERSION%>';
691
+ }
705
692
 
706
- if((status in sstreamChatStatus)){
707
- status = sstreamChatStatus[status];
708
- }
693
+ var sendIQStanzaLast = function(jid,iqID){
694
+ var iq = $iq({to: jid, type: "result", id: iqID})
695
+ .c("query", {xmlns: "jabber:iq:last", seconds: "0"});
696
+ strophe_connection.sendIQ(iq);
697
+ }
709
698
 
710
- if (status in statusMessage){
711
- //Send status to the XMPP Server
712
- var pres = $pres()
713
- .c('status')
714
- .t(statusMessage[status]).up() //Status message
715
- .c('show')
716
- .t(status);
717
- strophe_connection.send(pres.tree());
718
- setStatusWidgetTitle(status);
719
- }
720
- }
721
-
722
-
723
- ////////
724
- //MUC management
725
- ///////
726
-
727
- var muc_host = "conference";
728
-
729
- function joinRoom(roomName){
730
- return strophe_connection.muc.join(getRoomJidFromRoomName(roomName), getRoomNickForRoom(roomName), null, null, null, null)
731
- }
732
-
733
- function leaveRoom(roomName){
734
- return strophe_connection.muc.leave(getRoomJidFromRoomName(roomName), getRoomNickForRoom(roomName), null, null)
735
- }
736
-
737
- function sendGroupMessageToRoomWithName(roomName,msg){
738
- return sendMessageToRoom(getRoomJidFromRoomName(roomName),msg)
739
- }
740
-
741
- function sendGroupMessageToRoom(roomJid,msg){
742
- return strophe_connection.muc.message(roomJid, null, msg, null, "groupchat")
743
- }
744
-
745
- function sendPrivateMessageToRoomUser(roomName,userNick,msg){
746
- return strophe_connection.muc.message(getRoomJidFromRoomName(roomName), userNick, msg, null, "chat")
747
- }
748
-
749
- function queryChatRoomOccupants(roomName){
750
- return strophe_connection.muc.queryOccupants(getRoomJidFromRoomName(roomName), queryChatRoomOccupantsSuccess, queryChatRoomOccupantsFail)
751
- }
752
-
753
- function queryChatRoomOccupantsSuccess(iqResponse){
754
- handleIQResultFromMucOccupantsRequest(iqResponse);
755
- return true;
756
- }
757
-
758
- function queryChatRoomOccupantsFail(){
759
- return true;
760
- }
761
-
762
- function handleIQResultFromMucOccupantsRequest(iq){
763
- var from = $(iq).attr('from');
764
- var room_slug = getSlugFromJid(from)
765
- var queryTag = iq.getElementsByTagName('query');
766
-
767
- if (queryTag.length > 0) {
768
- var queryElement = queryTag[0];
769
- var occupants = queryElement.getElementsByTagName('item');
770
- $.each(occupants, function(index, value) {
771
- //List all occupants
772
- var nick = $(value).attr("name");
773
- if(roomsInfo[room_slug].occupants.indexOf(nick)==-1){
774
- roomsInfo[room_slug].occupants.push(nick)
775
- addNickToNotificationInGroup(room_slug,nick)
776
- }
777
- });
778
- }
779
- }
780
-
781
- function accessRoom(roomName,open){
782
- storeRoomInformation(roomName)
783
- if(roomsInfo[roomName].joined==false){
784
- joinRoom(roomName)
785
- queryChatRoomOccupants(roomName)
699
+ var sendIQEmpty = function(jid,iqID){
700
+ var iq = $iq({to: jid, type: "result", id: iqID})
701
+ strophe_connection.sendIQ(iq);
786
702
  }
787
- if(!existsSlugChatBox(roomName)){
788
- createGroupChatBox(roomName,open)
703
+
704
+ var sendIQStanzaToResponseVideochat = function(slug,result){
705
+ if (slug in contactsInfo) {
706
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
707
+ var iq = $iq({to: jid, type: "result", id: iqStanzaID['videochatRequest']})
708
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
709
+ .c("response").t(result);
710
+ strophe_connection.sendIQ(iq);
711
+ }
789
712
  }
790
- return true
791
- }
792
-
793
- function resetRoomConfig(roomName){
794
- storeRoomInformation(roomName)
795
- var room = new chatRoom()
796
- roomsInfo[roomName]=room
797
- }
798
-
799
- function updateRoomsInfoAfterUserDisconnect(){
800
- $.each(getAllSlugsWithVisibleGroupBoxes(), function(index, value) {
801
- resetRoomConfig(value)
802
- initialNotificationInGroup(value,I18n.t('chat.muc.offline'))
803
- });
804
- }
805
-
806
- function getRoomJidFromRoomName(roomName){
807
- return roomName + "@" + muc_host + "." + domain;
808
- }
809
-
810
- function getRoomNickForRoom(roomName){
811
- if(roomName in roomsInfo){
812
- return roomsInfo[roomName].nick;
813
- } else {
814
- return getBaseNickFromUserName();
713
+
714
+ var sendIQStanzaToCancelVideochat = function(slug){
715
+ if (slug in contactsInfo) {
716
+ var jid=slug+"@"+contactsInfo[slug].domain+"/"+contactsInfo[slug].resource;
717
+ var iq = $iq({to: jid, type: "result", id: iqStanzaID['videochatRequestCancel']})
718
+ .c("query", {xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas"})
719
+ .c("response").t("cancel");
720
+ strophe_connection.sendIQ(iq);
721
+ }
815
722
  }
816
- }
817
-
818
- function getBaseNickFromUserName(){
819
- return user_name.split(" ")[0];
820
- }
821
-
822
- function changeRoomNickNameAndJoinAgain(roomName){
823
- var old_nick = getRoomNickForRoom(roomName)
824
723
 
825
- if(old_nick.split("_").length > 4){
826
- //Bucle prevention...
827
- return false;
724
+
725
+ ////////
726
+ //Send Message stanzas
727
+ ///////
728
+ var sendChatMessage = function(guest_slug,msg){
729
+ PRESENCE.WINDOW.rotatePriority(guest_slug);
730
+ if (PRESENCE.WINDOW.isSlugGroup(guest_slug)){
731
+ var guest_jid = getRoomJidFromRoomName(guest_slug)
732
+ return sendGroupMessageToRoom(guest_jid,msg);
733
+ } else {
734
+ var headerMessage = PRESENCE.PARSER.getParsedName(user_name,true);
735
+ PRESENCE.WINDOW.getChatBoxForSlug(guest_slug).chatbox("option", "boxManager").addMsg(headerMessage, PRESENCE.PARSER.getParsedContent(msg,true));
736
+ var guest_jid = getJidFromSlug(guest_slug)
737
+ return sendChatMessageToBuddy(guest_jid,msg);
738
+ }
828
739
  }
829
740
 
830
- storeRoomInformation(roomName)
831
- roomsInfo[roomName].nick = old_nick + "_"
832
- joinRoom(roomName)
833
- return true;
834
- }
835
-
836
-
837
- ////////
838
- //MUC Stanzas
839
- ///////
840
-
841
- //MUC Messages
842
- function onRoomMessage(msg) {
843
- //from=roomSlug@conference.domain/from_slug
844
- var from = msg.getAttribute('from');
845
- var type = msg.getAttribute('type');
846
- var elems = msg.getElementsByTagName('body');
847
-
848
- if (type == "groupchat" && elems.length > 0) {
849
- var body = elems[0];
850
- var user_nick = getNickFromChatRoomJid(from)
851
- var msg = Strophe.getText(body);
852
- afterReceivedGroupChatMessage(from,msg)
853
- }
854
- return true;
855
- }
856
-
857
-
858
- //MUC Presence stanzas
859
- function onRoomPresence(presence) {
860
- var from = $(presence).attr('from');
861
- var to = $(presence).attr('to')
862
- var room_slug = getSlugFromJid(from);
863
- var nick = getNickFromChatRoomJid(from)
864
- var ptype = $(presence).attr('type')
741
+ var sendChatMessageToBuddy = function(to,text){
742
+ var from = user_jid
743
+ var type = "chat";
744
+ var body= $build("body");
745
+ body.t(text);
746
+ var message = $msg({to: to, from: from, type: 'chat'}).cnode(body.tree());
747
+ strophe_connection.send(message.tree());
748
+ restartAwayTimer();
749
+ return true;
750
+ }
751
+
752
+
753
+ ////////
754
+ //Send Presence stanzas with status
755
+ ///////
756
+ var sendStatus = function(status){
865
757
 
866
- //Join or Leave room stanzas of self user
867
- if((nick == getRoomNickForRoom(room_slug))&&(getSlugFromJid(to) == user_slug)){
758
+ if(! status){
759
+ status = userStatus;
760
+ }
761
+
762
+ if((status in sstreamChatStatus)){
763
+ status = sstreamChatStatus[status];
764
+ }
765
+
766
+ if (status in statusMessage){
767
+ //Send status to the XMPP Server
768
+ var pres = $pres()
769
+ .c('status')
770
+ .t(statusMessage[status]).up() //Status message
771
+ .c('show')
772
+ .t(status);
773
+ strophe_connection.send(pres.tree());
774
+ PRESENCE.UIMANAGER.setStatusWidgetTitle(status);
775
+ }
776
+ }
777
+
778
+
779
+ ////////
780
+ //MUC management
781
+ ///////
782
+
783
+ var muc_host = "conference";
784
+
785
+ var joinRoom = function(roomName){
786
+ return strophe_connection.muc.join(getRoomJidFromRoomName(roomName), getRoomNickForRoom(roomName), null, null, null, null)
787
+ }
788
+
789
+ var leaveRoom = function(roomName){
790
+ return strophe_connection.muc.leave(getRoomJidFromRoomName(roomName), getRoomNickForRoom(roomName), null, null)
791
+ }
792
+
793
+ var sendGroupMessageToRoomWithName = function(roomName,msg){
794
+ return sendMessageToRoom(getRoomJidFromRoomName(roomName),msg)
795
+ }
796
+
797
+ var sendGroupMessageToRoom = function(roomJid,msg){
798
+ return strophe_connection.muc.message(roomJid, null, msg, null, "groupchat")
799
+ }
800
+
801
+ var sendPrivateMessageToRoomUser = function(roomName,userNick,msg){
802
+ return strophe_connection.muc.message(getRoomJidFromRoomName(roomName), userNick, msg, null, "chat")
803
+ }
804
+
805
+ var queryChatRoomOccupants = function(roomName){
806
+ return strophe_connection.muc.queryOccupants(getRoomJidFromRoomName(roomName), queryChatRoomOccupantsSuccess, queryChatRoomOccupantsFail)
807
+ }
808
+
809
+ var queryChatRoomOccupantsSuccess = function(iqResponse){
810
+ handleIQResultFromMucOccupantsRequest(iqResponse);
811
+ return true;
812
+ }
813
+
814
+ var queryChatRoomOccupantsFail = function(){
815
+ return true;
816
+ }
817
+
818
+ var handleIQResultFromMucOccupantsRequest = function(iq){
819
+ var from = $(iq).attr('from');
820
+ var room_slug = getSlugFromJid(from)
821
+ var queryTag = iq.getElementsByTagName('query');
822
+
823
+ if (queryTag.length > 0) {
824
+ var queryElement = queryTag[0];
825
+ var occupants = queryElement.getElementsByTagName('item');
826
+ $.each(occupants, function(index, value) {
827
+ //List all occupants
828
+ var nick = $(value).attr("name");
829
+ if(roomsInfo[room_slug].occupants.indexOf(nick)==-1){
830
+ roomsInfo[room_slug].occupants.push(nick)
831
+ PRESENCE.NOTIFICATIONS.addNickToNotificationInGroup(room_slug,nick)
832
+ }
833
+ });
834
+ }
835
+ }
836
+
837
+ var accessRoom = function(roomName,open){
838
+ storeRoomInformation(roomName)
839
+ if(roomsInfo[roomName].joined==false){
840
+ joinRoom(roomName)
841
+ queryChatRoomOccupants(roomName)
842
+ }
843
+ if(!PRESENCE.UIMANAGER.existsSlugChatBox(roomName)){
844
+ PRESENCE.WINDOW.createGroupChatBox(roomName,open)
845
+ }
846
+ return true
847
+ }
848
+
849
+ var resetRoomConfig = function(roomName){
850
+ storeRoomInformation(roomName)
851
+ var room = new chatRoom()
852
+ roomsInfo[roomName]=room
853
+ }
854
+
855
+ var updateRoomsInfoAfterUserDisconnect = function(){
856
+ $.each(PRESENCE.WINDOW.getAllSlugsWithVisibleGroupBoxes(), function(index, value) {
857
+ resetRoomConfig(value)
858
+ PRESENCE.NOTIFICATIONS.initialNotificationInGroup(value,I18n.t('chat.muc.offline'))
859
+ });
860
+ }
861
+
862
+ var getRoomJidFromRoomName = function(roomName){
863
+ return roomName + "@" + muc_host + "." + domain;
864
+ }
865
+
866
+ var getRoomNickForRoom = function(roomName){
867
+ if(roomName in roomsInfo){
868
+ return roomsInfo[roomName].nick;
869
+ } else {
870
+ return getBaseNickFromUserName();
871
+ }
872
+ }
873
+
874
+ var getBaseNickFromUserName = function(){
875
+ return user_name.split(" ")[0];
876
+ }
877
+
878
+ var changeRoomNickNameAndJoinAgain = function(roomName){
879
+ var old_nick = getRoomNickForRoom(roomName)
880
+
881
+ if(old_nick.split("_").length > 4){
882
+ //Bucle prevention...
883
+ return false;
884
+ }
885
+
886
+ storeRoomInformation(roomName)
887
+ roomsInfo[roomName].nick = old_nick + "_"
888
+ joinRoom(roomName)
889
+ return true;
890
+ }
891
+
892
+
893
+ ////////
894
+ //MUC Stanzas
895
+ ///////
896
+
897
+ //MUC Messages
898
+ var onRoomMessage = function(msg) {
899
+ //from=roomSlug@conference.domain/from_slug
900
+ var from = msg.getAttribute('from');
901
+ var type = msg.getAttribute('type');
902
+ var elems = msg.getElementsByTagName('body');
903
+
904
+ if (type == "groupchat" && elems.length > 0) {
905
+ var body = elems[0];
906
+ var user_nick = getNickFromChatRoomJid(from)
907
+ var msg = Strophe.getText(body);
908
+ PRESENCE.UIMANAGER.afterReceivedGroupChatMessage(from,msg)
909
+ }
910
+ return true;
911
+ }
912
+
913
+
914
+ //MUC Presence stanzas
915
+ var onRoomPresence = function(presence) {
916
+ var from = $(presence).attr('from');
917
+ var to = $(presence).attr('to')
918
+ var room_slug = getSlugFromJid(from);
919
+ var nick = getNickFromChatRoomJid(from)
920
+ var ptype = $(presence).attr('type')
921
+
922
+ //Join or Leave room stanzas of self user
923
+ if((nick == getRoomNickForRoom(room_slug))&&(getSlugFromJid(to) == user_slug)){
924
+ switch (ptype){
925
+ case undefined:
926
+ processJoinRoomPresenceStanza(presence);
927
+ break;
928
+ case "available":
929
+ processJoinRoomPresenceStanza(presence);
930
+ break;
931
+ case "unavailable":
932
+ processLeaveRoomPresenceStanza(presence);
933
+ break;
934
+ case "error":
935
+ processJoinErrorRoomPresenceStanza(presence);
936
+ break;
937
+ }
938
+ return true;
939
+ }
940
+
941
+
942
+ //Rest of stanzas
868
943
  switch (ptype){
869
944
  case undefined:
870
- processJoinRoomPresenceStanza(presence);
945
+ processAvailableRoomPresenceStanza(presence)
871
946
  break;
872
947
  case "available":
873
- processJoinRoomPresenceStanza(presence);
948
+ processAvailableRoomPresenceStanza(presence)
874
949
  break;
875
950
  case "unavailable":
876
- processLeaveRoomPresenceStanza(presence);
951
+ processUnavailableRoomPresenceStanza(presence)
877
952
  break;
878
- case "error":
879
- processJoinErrorRoomPresenceStanza(presence);
880
- break;
881
- }
882
- return true;
883
- }
953
+ default :
954
+ //Stanza type not recognize
955
+ return true
956
+ }
957
+
958
+ return true
959
+ }
884
960
 
885
961
 
886
- //Rest of stanzas
887
- switch (ptype){
888
- case undefined:
889
- processAvailableRoomPresenceStanza(presence)
890
- break;
891
- case "available":
892
- processAvailableRoomPresenceStanza(presence)
893
- break;
894
- case "unavailable":
895
- processUnavailableRoomPresenceStanza(presence)
896
- break;
897
- default :
898
- //Stanza type not recognize
899
- return true
900
- }
962
+ var processJoinRoomPresenceStanza = function(presence){
963
+ var from = $(presence).attr('from');
964
+ var room_slug = getSlugFromJid(from);
965
+ storeRoomInformation(room_slug);
966
+
967
+ var xElements = presence.getElementsByTagName('x');
901
968
 
902
- return true
903
- }
904
-
905
-
906
- function processJoinRoomPresenceStanza(presence){
907
- var from = $(presence).attr('from');
908
- var room_slug = getSlugFromJid(from);
909
- storeRoomInformation(room_slug);
969
+ if (xElements.length == 1) {
970
+ var xElement = xElements[0];
971
+ var items = xElement.getElementsByTagName('item');
972
+
973
+ $.each(items, function(index, value) {
974
+ var role = $(value).attr("role")
975
+ var affiliation = $(value).attr("affiliation")
976
+
977
+ roomsInfo[room_slug].affiliation = affiliation
978
+ roomsInfo[room_slug].role = role
979
+ if(roomsInfo[room_slug].joined == false){
980
+ roomsInfo[room_slug].joined = true
981
+ PRESENCE.NOTIFICATIONS.changeInitialNotificationInGroup(room_slug,I18n.t('chat.muc.occupants'))
982
+ }
983
+ });
984
+ }
985
+ }
910
986
 
911
- var xElements = presence.getElementsByTagName('x');
912
-
913
- if (xElements.length == 1) {
914
- var xElement = xElements[0];
915
- var items = xElement.getElementsByTagName('item');
916
-
917
- $.each(items, function(index, value) {
918
- var role = $(value).attr("role")
919
- var affiliation = $(value).attr("affiliation")
920
-
921
- roomsInfo[room_slug].affiliation = affiliation
922
- roomsInfo[room_slug].role = role
923
- if(roomsInfo[room_slug].joined == false){
924
- roomsInfo[room_slug].joined = true
925
- changeInitialNotificationInGroup(room_slug,I18n.t('chat.muc.occupants'))
926
- }
927
- });
928
- }
929
- }
930
-
931
- function processLeaveRoomPresenceStanza(presence){
932
- var from = $(presence).attr('from');
933
- var room_slug = getSlugFromJid(from);
934
- resetRoomConfig(room_slug)
935
- changeInitialNotificationInGroup(room_slug,"")
936
- }
937
-
938
- function processJoinErrorRoomPresenceStanza(presence){
939
- var from = $(presence).attr('from')
940
- var room_slug = getSlugFromJid(from)
941
- var errorMsg = "Error";
942
-
943
- var eElements = presence.getElementsByTagName('error')
944
- if (eElements.length == 1) {
945
- var error = eElements[0];
946
- var code = $(error).attr("code")
947
- var type = $(error).attr("type")
948
- errorMsg = errorMsg + " with code:" + code + " and type:" + type
949
- var textElements = error.getElementsByTagName('text')
950
- if (textElements.length == 1) {
951
- var text = $(textElements[0]).text()
952
- errorMsg = errorMsg + "\n" + text
987
+ var processLeaveRoomPresenceStanza = function(presence){
988
+ var from = $(presence).attr('from');
989
+ var room_slug = getSlugFromJid(from);
990
+ resetRoomConfig(room_slug)
991
+ PRESENCE.NOTIFICATIONS.changeInitialNotificationInGroup(room_slug,"")
992
+ }
993
+
994
+ var processJoinErrorRoomPresenceStanza = function(presence){
995
+ var from = $(presence).attr('from')
996
+ var room_slug = getSlugFromJid(from)
997
+ var errorMsg = "Error";
998
+
999
+ var eElements = presence.getElementsByTagName('error')
1000
+ if (eElements.length == 1) {
1001
+ var error = eElements[0];
1002
+ var code = $(error).attr("code")
1003
+ var type = $(error).attr("type")
1004
+ errorMsg = errorMsg + " with code:" + code + " and type:" + type
1005
+ var textElements = error.getElementsByTagName('text')
1006
+ if (textElements.length == 1) {
1007
+ var text = $(textElements[0]).text()
1008
+ errorMsg = errorMsg + "\n" + text
1009
+ }
1010
+
1011
+ //Try to fix errors!
1012
+ if(code==409){
1013
+ //Case: Nickname already in use
1014
+ if (changeRoomNickNameAndJoinAgain(room_slug)){
1015
+ return;
1016
+ }
1017
+ }
1018
+ //if(code==403){
1019
+ //Case: Auth Denied
1020
+ //}
1021
+
1022
+ //[...]
1023
+
953
1024
  }
954
-
955
- //Try to fix errors!
956
- if(code==409){
957
- //Case: Nickname already in use
958
- if (changeRoomNickNameAndJoinAgain(room_slug)){
959
- return;
960
- }
1025
+
1026
+ resetRoomConfig(room_slug)
1027
+ PRESENCE.NOTIFICATIONS.initialNotificationInGroup(room_slug,I18n.t("chat.muc.joinError", {errorMsg: errorMsg}))
1028
+ }
1029
+
1030
+ var processAvailableRoomPresenceStanza = function(presence){
1031
+ var from = $(presence).attr('from');
1032
+ var nick = getNickFromChatRoomJid(from)
1033
+ var room_slug = getSlugFromJid(from)
1034
+ storeRoomInformation(room_slug);
1035
+
1036
+ if (roomsInfo[room_slug].occupants.indexOf(nick)==-1){
1037
+ roomsInfo[room_slug].occupants.push(nick)
1038
+ PRESENCE.NOTIFICATIONS.addNickToNotificationInGroup(room_slug,nick)
1039
+ PRESENCE.UIMANAGER.writeReceivedMessageOnChatWindow("",room_slug, I18n.t("chat.muc.join", {nick: nick}))
1040
+ }
1041
+ }
1042
+
1043
+ var processUnavailableRoomPresenceStanza = function(presence){
1044
+ var from = $(presence).attr('from');
1045
+ var nick = getNickFromChatRoomJid(from)
1046
+ var room_slug = getSlugFromJid(from)
1047
+ storeRoomInformation(room_slug);
1048
+
1049
+ if (roomsInfo[room_slug].occupants.indexOf(nick)!=-1){
1050
+ roomsInfo[room_slug].occupants.splice(roomsInfo[room_slug].occupants.indexOf(nick),1)
1051
+ PRESENCE.NOTIFICATIONS.removeNickFromNotificationInGroup(room_slug,nick)
1052
+ PRESENCE.UIMANAGER.writeReceivedMessageOnChatWindow("",room_slug,I18n.t("chat.muc.leave", {nick: nick}))
1053
+ }
1054
+ }
1055
+
1056
+
1057
+
1058
+ //////////////////
1059
+ // Getters
1060
+ //////////////////
1061
+ var getSlugFromJid = function(jid){
1062
+ return jid.split("@")[0];
1063
+ }
1064
+
1065
+ var getDomainFromJid = function(jid){
1066
+ return jid.split("@")[1].split("/")[0];
1067
+ }
1068
+
1069
+ var isChatRoomJid = function(jid){
1070
+ if ((getDomainFromJid(jid).split(".")[0])==muc_host){
1071
+ return true;
1072
+ }
1073
+ return false;
1074
+ }
1075
+
1076
+ var getNameFromSlug = function(slug){
1077
+ var connectionBox = PRESENCE.UIMANAGER.getConnectionBoxFromSlug(slug);
1078
+
1079
+ if(connectionBox!=null){
1080
+ return $(connectionBox).attr("name");
1081
+ }
1082
+
1083
+ var chatBox = PRESENCE.WINDOW.getChatBoxForSlug(slug)
1084
+ if(chatBox!=null){
1085
+ return chatBox.attr("name");
1086
+ }
1087
+
1088
+ //return rebuildNameFromSlug(slug)
1089
+ return slug;
1090
+ }
1091
+
1092
+ var rebuildNameFromSlug = function(slug){
1093
+ var cname = slug.split("-");
1094
+ var name = "";
1095
+ for(i=0; i<cname.length; i++){
1096
+ if (i!=0){
1097
+ name = name + " ";
1098
+ }
1099
+ name = name + cname[i][0].toUpperCase() + cname[i].substring(1,cname[i].length);
1100
+ }
1101
+ return name;
1102
+ }
1103
+
1104
+ var getNameFromJid = function(){
1105
+ return getNameFromSlug(getSlugFromJid(jid));
1106
+ }
1107
+
1108
+ var getJidFromSlug = function(slug){
1109
+ return slug + "@" + domain;
1110
+ }
1111
+
1112
+ var getNickFromChatRoomJid = function(jid){
1113
+ return jid.split("/")[1];
1114
+ }
1115
+
1116
+ var purgeNickFromChatRoomJid = function(jid){
1117
+ return jid.split("/")[0];
1118
+ }
1119
+
1120
+ var getSStreamChatStatus = function(){
1121
+ return sstreamChatStatus;
1122
+ }
1123
+
1124
+ var getConnection = function(){
1125
+ if(! strophe_connection){
1126
+ strophe_connection = new Strophe.Connection(BOSH_SERVICE);
961
1127
  }
962
- //if(code==403){
963
- //Case: Auth Denied
964
- //}
965
-
966
- //[...]
967
-
1128
+ return strophe_connection;
1129
+ }
1130
+
1131
+ var isNewConnection = function(){
1132
+ return afterNewConnectionFlag
1133
+ }
1134
+
1135
+ var setNewConnection = function(bol){
1136
+ afterNewConnectionFlag = bol;
968
1137
  }
969
1138
 
970
- resetRoomConfig(room_slug)
971
- initialNotificationInGroup(room_slug,I18n.t("chat.muc.joinError", {errorMsg: errorMsg}))
972
- }
973
-
974
- function processAvailableRoomPresenceStanza(presence){
975
- var from = $(presence).attr('from');
976
- var nick = getNickFromChatRoomJid(from)
977
- var room_slug = getSlugFromJid(from)
978
- storeRoomInformation(room_slug);
979
-
980
- if (roomsInfo[room_slug].occupants.indexOf(nick)==-1){
981
- roomsInfo[room_slug].occupants.push(nick)
982
- addNickToNotificationInGroup(room_slug,nick)
983
- writeReceivedMessageOnChatWindow("",room_slug, I18n.t("chat.muc.join", {nick: nick}))
984
- }
985
- }
986
-
987
- function processUnavailableRoomPresenceStanza(presence){
988
- var from = $(presence).attr('from');
989
- var nick = getNickFromChatRoomJid(from)
990
- var room_slug = getSlugFromJid(from)
991
- storeRoomInformation(room_slug);
992
-
993
- if (roomsInfo[room_slug].occupants.indexOf(nick)!=-1){
994
- roomsInfo[room_slug].occupants.splice(roomsInfo[room_slug].occupants.indexOf(nick),1)
995
- removeNickFromNotificationInGroup(room_slug,nick)
996
- writeReceivedMessageOnChatWindow("",room_slug,I18n.t("chat.muc.leave", {nick: nick}))
1139
+ var isFirstConnection = function(){
1140
+ return afterFirstConnectionFlag;
997
1141
  }
998
- }
999
-
1000
-
1001
-
1002
- //////////////////
1003
- // Getters
1004
- //////////////////
1005
- function getSlugFromJid(jid){
1006
- return jid.split("@")[0];
1007
- }
1008
-
1009
- function getDomainFromJid(jid){
1010
- return jid.split("@")[1].split("/")[0];
1011
- }
1012
-
1013
- function isChatRoomJid(jid){
1014
- if ((getDomainFromJid(jid).split(".")[0])==muc_host){
1015
- return true;
1142
+
1143
+ var setFirstConnection = function(bol){
1144
+ afterFirstConnectionFlag = bol;
1016
1145
  }
1017
- return false;
1018
- }
1019
-
1020
- function getNameFromSlug(slug){
1021
- var connectionBox = getConnectionBoxFromSlug(slug);
1022
1146
 
1023
- if(connectionBox!=null){
1024
- return $(connectionBox).attr("name");
1147
+ var getUserStatus = function(){
1148
+ return userStatus;
1025
1149
  }
1026
1150
 
1027
- var chatBox = getChatBoxForSlug(slug)
1028
- if(chatBox!=null){
1029
- return chatBox.attr("name");
1030
- }
1151
+ var setUserStatus = function(status){
1152
+ userStatus = status;
1153
+ }
1031
1154
 
1032
- //return rebuildNameFromSlug(slug)
1033
- return slug;
1034
- }
1035
-
1036
- function rebuildNameFromSlug(slug){
1037
- var cname = slug.split("-");
1038
- var name = "";
1039
- for(i=0; i<cname.length; i++){
1040
- if (i!=0){
1041
- name = name + " ";
1042
- }
1043
- name = name + cname[i][0].toUpperCase() + cname[i].substring(1,cname[i].length);
1044
- }
1045
- return name;
1046
- }
1047
-
1048
- function getNameFromJid(){
1049
- return getNameFromSlug(getSlugFromJid(jid));
1050
- }
1051
-
1052
- function getJidFromSlug(slug){
1053
- return slug + "@" + domain;
1054
- }
1155
+ var getDisconnectionFlag = function(){
1156
+ return disconnectionFlag;
1157
+ }
1158
+
1159
+ var getRoomsInfo = function(){
1160
+ return roomsInfo;
1161
+ }
1162
+
1163
+ return {
1164
+ init: init,
1165
+ connectToChat : connectToChat,
1166
+ onReconnect : onReconnect,
1167
+ isStropheConnected : isStropheConnected,
1168
+ isUserConnected : isUserConnected,
1169
+ authByCookie : authByCookie,
1170
+ authByPassword : authByPassword,
1171
+ disconnect : disconnect,
1172
+ awayTimerFunction : awayTimerFunction,
1173
+ restartAwayTimer : restartAwayTimer,
1174
+ sendIQStanzaForRequestClientInfo : sendIQStanzaForRequestClientInfo,
1175
+ sendIQStanzaToRequestVideochat : sendIQStanzaToRequestVideochat,
1176
+ sendIQStanzaToResponseVideochat : sendIQStanzaToResponseVideochat,
1177
+ sendIQStanzaToCancelVideochat : sendIQStanzaToCancelVideochat,
1178
+ getJavascriptXMPPClientName : getJavascriptXMPPClientName,
1179
+ sendChatMessage : sendChatMessage,
1180
+ sendStatus : sendStatus,
1181
+ accessRoom : accessRoom,
1182
+ joinRoom : joinRoom,
1183
+ leaveRoom : leaveRoom,
1184
+ getSlugFromJid : getSlugFromJid,
1185
+ getJidFromSlug : getJidFromSlug,
1186
+ getNameFromSlug : getNameFromSlug,
1187
+ getNickFromChatRoomJid : getNickFromChatRoomJid,
1188
+ getConnection : getConnection,
1189
+ isNewConnection : isNewConnection,
1190
+ setNewConnection : setNewConnection,
1191
+ isFirstConnection : isFirstConnection,
1192
+ setFirstConnection : setFirstConnection,
1193
+ getUserStatus : getUserStatus,
1194
+ setUserStatus : setUserStatus,
1195
+ getDisconnectionFlag : getDisconnectionFlag,
1196
+ getRoomsInfo : getRoomsInfo,
1197
+ getSStreamChatStatus: getSStreamChatStatus
1198
+ };
1199
+
1200
+ }) (PRESENCE, jQuery);
1055
1201
 
1056
- function getNickFromChatRoomJid(jid){
1057
- return jid.split("/")[1];
1058
- }
1059
1202
 
1060
- function purgeNickFromChatRoomJid(jid){
1061
- return jid.split("/")[0];
1062
- }