1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
|
/*
Jappix - An open social platform
These are the common JS script for Jappix
-------------------------------------------------
License: dual-licensed under AGPL and MPLv2
Authors: Valérian Saliou, olivierm, regilero, Maranda
*/
// Bundle
var Common = (function () {
/**
* Alias of this
* @private
*/
var self = {};
/**
* Checks if an element exists in the DOM
* @public
* @param {string} path
* @return {boolean}
*/
self.exists = function(path) {
var exists = false;
try {
if(jQuery(path).size() > 0) {
exists = true;
}
} catch(e) {
Console.error('Common.exists', e);
} finally {
return exists;
}
};
/**
* Checks if Jappix is connected
* @public
* @return {boolean}
*/
self.isConnected = function() {
connected = false;
try {
if((typeof con != 'undefined') && con && con.connected()) {
connected = true;
}
} catch(e) {
Console.error('Common.isConnected', e);
} finally {
return connected;
}
};
/**
* Checks if Jappix is connected
* @public
* @return {boolean}
*/
self.hasWebSocket = function() {
has_websocket = false;
try {
if(HOST_WEBSOCKET && typeof window.WebSocket != 'undefined') {
has_websocket = true;
}
} catch(e) {
Console.error('Common.hasWebSocket', e);
} finally {
return has_websocket;
}
};
/**
* Checks if Jappix has focus
* @public
* @return {boolean}
*/
self.isFocused = function() {
has_focus = true;
try {
if(!document.hasFocus()) {
has_focus = false;
}
} catch(e) {
Console.error('Common.isFocused', e);
} finally {
return has_focus;
}
};
/**
* Generates the good XID
* @public
* @param {string} xid
* @param {string} type
* @return {string}
*/
self.generateXID = function(xid, type) {
try {
// XID needs to be transformed
// .. and made lowercase (uncertain though this is the right place...)
xid = xid.toLowerCase();
if(xid && (xid.indexOf('@') == -1)) {
// Groupchat
if(type == 'groupchat')
return xid + '@' + HOST_MUC;
// One-to-one chat
if(xid.indexOf('.') == -1)
return xid + '@' + HOST_MAIN;
// It might be a gateway?
return xid;
}
// Nothing special (yet bare XID)
return xid;
} catch(e) {
Console.error('Common.generateXID', e);
}
};
/**
* Gets the asked translated string
* @public
* @param {string} string
* @return {string}
*/
self._e = function(string) {
try {
return string;
} catch(e) {
Console.error('Common._e', e);
}
};
/**
* Replaces '%s' to a given value for a translated string
* @public
* @param {string} string
* @param {string} value
* @return {string}
*/
self.printf = function(string, value) {
try {
return string.replace('%s', value);
} catch(e) {
Console.error('Common.printf', e);
}
};
/**
* Returns the string after the last given char
* @public
* @param {string} given_char
* @param {string} str
* @return {string}
*/
self.strAfterLast = function(given_char, str) {
try {
if(!given_char || !str)
return '';
var char_index = str.lastIndexOf(given_char);
var str_return = str;
if(char_index >= 0)
str_return = str.substr(char_index + 1);
return str_return;
} catch(e) {
Console.error('Common.strAfterLast', e);
}
};
/**
* Properly explodes a string with a given character
* @public
* @param {string} toEx
* @param {string} toStr
* @param {number} i
* @return {string}
*/
self.explodeThis = function(toEx, toStr, i) {
try {
// Get the index of our char to explode
var index = toStr.indexOf(toEx);
// We split if necessary the string
if(index !== -1) {
if(i === 0)
toStr = toStr.substr(0, index);
else
toStr = toStr.substr(index + 1);
}
// We return the value
return toStr;
} catch(e) {
Console.error('Common.explodeThis', e);
}
};
/**
* Cuts the resource of a XID
* @public
* @param {string} aXID
* @return {string}
*/
self.cutResource = function(aXID) {
try {
return self.explodeThis('/', aXID, 0);
} catch(e) {
Console.error('Common.cutResource', e);
}
};
/**
* Gets the resource of a XID
* @public
* @param {string} aXID
* @return {string}
*/
self.thisResource = function(aXID) {
resource = '';
try {
// Any resource?
if(self.isFullXID(aXID)) {
resource = self.explodeThis('/', aXID, 1);
}
} catch(e) {
Console.error('Common.thisResource', e);
} finally {
return resource;
}
};
/**
* Returns whether this XID is full or not
* @public
* @param {string} xid
* @return {boolean}
*/
self.isFullXID = function(xid) {
try {
return xid.indexOf('/') !== -1;
} catch(e) {
Console.error('Common.isFullXID', e);
return false;
}
};
/**
* nodepreps an XMPP node
* @public
* @param {string} node
* @return {string}
*/
self.nodeprep = function(node) {
// Spec: http://tools.ietf.org/html/rfc6122#appendix-A
try {
if(!node)
return node;
// Remove prohibited chars
var prohibited_chars = ['"', '&', '\'', '/', ':', '<', '>', '@'];
for(var j in prohibited_chars) {
node = node.replace(prohibited_chars[j], '');
}
// Lower case
node = node.toLowerCase();
return node;
} catch(e) {
Console.error('Common.nodeprep', e);
}
};
/**
* Encodes quotes in a string
* @public
* @param {string} str
* @return {string}
*/
self.encodeQuotes = function(str) {
try {
return (str + '').htmlEnc();
} catch(e) {
Console.error('Common.encodeQuotes', e);
}
};
/**
* Gets the bare XID from a XID
* @public
* @param {string} xid
* @return {string}
*/
self.bareXID = function(xid) {
try {
// Cut the resource
xid = self.cutResource(xid);
// Launch nodeprep
if(xid.indexOf('@') != -1)
xid = self.nodeprep(self.getXIDNick(xid)) + '@' + self.getXIDHost(xid);
return xid;
} catch(e) {
Console.error('Common.bareXID', e);
}
};
/**
* Gets the full XID from a XID
* @public
* @param {string} xid
* @return {string}
*/
self.fullXID = function(xid) {
try {
// Normalizes the XID
var full = self.bareXID(xid);
var resource = self.thisResource(xid);
// Any resource?
if(resource)
full += '/' + resource;
return full;
} catch(e) {
Console.error('Common.fullXID', e);
}
};
/**
* Gets the nick from a XID
* @public
* @param {string} aXID
* @return {string}
*/
self.getXIDNick = function(aXID) {
try {
// Gateway nick?
if(aXID.match(/\\40/))
return self.explodeThis('\\40', aXID, 0);
return self.explodeThis('@', aXID, 0);
} catch(e) {
Console.error('Common.getXIDNick', e);
}
};
/**
* Gets the host from a XID
* @public
* @param {string} aXID
* @return {string}
*/
self.getXIDHost = function(aXID) {
try {
return self.explodeThis('@', aXID, 1);
} catch(e) {
Console.error('Common.getXIDHost', e);
}
};
/**
* Checks if we are RTL (Right-To-Left)
* @public
* @return {boolean}
*/
self.isRTL = function() {
try {
return (self._e("default:LTR") == 'default:RTL');
} catch(e) {
Console.error('Common.isRTL', e);
}
};
/**
* Checks if anonymous mode is allowed
* @public
* @return {boolean}
*/
self.allowedAnonymous = function() {
try {
return (ANONYMOUS == 'on');
} catch(e) {
Console.error('Common.allowedAnonymous', e);
}
};
/**
* Checks if host is locked
* @public
* @return {boolean}
*/
self.lockHost = function() {
try {
return (LOCK_HOST == 'on');
} catch(e) {
Console.error('Common.lockHost', e);
}
};
/**
* Gets the full XID of the user
* @public
* @return {string}
*/
self.getXID = function() {
try {
// Return the XID of the user
if(con.username && con.domain) {
return con.username + '@' + con.domain;
}
return '';
} catch(e) {
Console.error('Common.getXID', e);
}
};
/**
* Generates the colors for a given user XID
* @public
* @param {type} xid
* @return {string}
*/
self.generateColor = function(xid) {
try {
var colors = new Array(
'ac0000',
'a66200',
'007703',
'00705f',
'00236b',
'4e005c'
);
var number = 0;
for(var i = 0; i < xid.length; i++) {
number += xid.charCodeAt(i);
}
var color = '#' + colors[number % (colors.length)];
return color;
} catch(e) {
Console.error('Common.generateColor', e);
}
};
/**
* Checks if the XID is a gateway
* @public
* @param {string} xid
* @return {boolean}
*/
self.isGateway = function(xid) {
is_gateway = true;
try {
if(xid.indexOf('@') != -1) {
is_gateway = false;
}
} catch(e) {
Console.error('Common.isGateway', e);
} finally {
return is_gateway;
}
};
/**
* Gets the from attribute of a stanza (overrides some servers like Prosody missing from attributes)
* @public
* @param {object} stanza
* @return {string}
*/
self.getStanzaFrom = function(stanza) {
try {
var from = stanza.getFrom();
// No from, we assume this is our XID
if(!from)
from = self.getXID();
return from;
} catch(e) {
Console.error('Common.getStanzaFrom', e);
}
};
/**
* Adds a zero to a date when needed
* @public
* @param {number} i
* @return {string}
*/
self.padZero = function(i) {
try {
// Negative number (without first 0)
if(i > -10 && i < 0)
return '-0' + (i * -1);
// Positive number (without first 0)
if(i < 10 && i >= 0)
return '0' + i;
// All is okay
return i;
} catch(e) {
Console.error('Common.padZero', e);
}
};
/**
* Escapes a string for a regex usage
* @public
* @param {string} query
* @return {string}
*/
self.escapeRegex = function(query) {
try {
return query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
} catch(e) {
Console.error('Common.escapeRegex', e);
}
};
/**
* Returns a random array value
* @public
* @param {object} arr
* @return {object}
*/
self.randomArrayValue = function(arr) {
try {
return arr[Math.floor(Math.random() * arr.length)];
} catch(e) {
Console.error('Common.randomArrayValue', e);
}
};
/**
* Returns whether the browser is mobile or not
* @public
* @return {boolean}
*/
self.isMobile = function() {
is_mobile = false;
try {
is_mobile = /Android|iPhone|iPod|iPad|Windows Phone|BlackBerry|Bada|Maemo|Meego|webOS/i.test(navigator.userAgent);
} catch(e) {
Console.error('Common.isMobile', e);
} finally {
return is_mobile;
}
};
/**
* Converts a XML document to a string
* @public
* @param {object} xmlData
* @return {string}
*/
self.xmlToString = function(xmlData) {
xml_str = null;
try {
// For Mozilla, Firefox, Opera, etc.
if(window.XMLSerializer) {
xml_str = (new XMLSerializer()).serializeToString(xmlData);
}
// For Internet Explorer
if(window.ActiveXObject) {
xml_str = xmlData.xml;
}
} catch(e) {
Console.error('Common.xmlToString', e);
} finally {
return xml_str;
}
};
/**
* Converts a string to a XML document
* @public
* @param {string} sXML
* @return {object}
*/
self.XMLFromString = function(sXML) {
try {
// No data?
if(!sXML) {
return '';
}
// Add the XML tag
if(!sXML.match(/^<\?xml/i)) {
sXML = '<?xml version="1.0"?>' + sXML;
}
// Parse it!
if(window.DOMParser) {
return (new DOMParser()).parseFromString(sXML, 'text/xml');
}
if(window.ActiveXObject) {
var oXML = new ActiveXObject('Microsoft.XMLDOM');
oXML.loadXML(sXML);
return oXML;
}
} catch(e) {
Console.error('Common.XMLFromString', e);
return '';
}
};
/**
* Watches for input value change (delays callback)
* @public
* @param {function} cb
* @return {function}
*/
self.typewatch = function(cb) {
try {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
} catch(e) {
Console.error('Common.typewatch', e);
}
};
/**
* Return class scope
*/
return self;
})();
var JappixCommon = Common;
|