TextTheme view removed
[KiwiIRC.git] / client / src / helpers / utils.js
1 /*jslint devel: true, browser: true, continue: true, sloppy: true, forin: true, plusplus: true, maxerr: 50, indent: 4, nomen: true, regexp: true*/
2 /*globals $, front, gateway, Utilityview */
3
4
5
6 /**
7 * Generate a random string of given length
8 * @param {Number} string_length The length of the random string
9 * @returns {String} The random string
10 */
11 function randomString(string_length) {
12 var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",
13 randomstring = '',
14 i,
15 rnum;
16 for (i = 0; i < string_length; i++) {
17 rnum = Math.floor(Math.random() * chars.length);
18 randomstring += chars.substring(rnum, rnum + 1);
19 }
20 return randomstring;
21 }
22
23 /**
24 * String.trim shim
25 */
26 if (typeof String.prototype.trim === 'undefined') {
27 String.prototype.trim = function () {
28 return this.replace(/^\s+|\s+$/g, "");
29 };
30 }
31
32 /**
33 * String.lpad shim
34 * @param {Number} length The length of padding
35 * @param {String} characher The character to pad with
36 * @returns {String} The padded string
37 */
38 if (typeof String.prototype.lpad === 'undefined') {
39 String.prototype.lpad = function (length, character) {
40 var padding = "",
41 i;
42 for (i = 0; i < length; i++) {
43 padding += character;
44 }
45 return (padding + this).slice(-length);
46 };
47 }
48
49
50 /**
51 * Convert seconds into hours:minutes:seconds
52 * @param {Number} secs The number of seconds to converts
53 * @returns {Object} An object representing the hours/minutes/second conversion of secs
54 */
55 function secondsToTime(secs) {
56 var hours, minutes, seconds, divisor_for_minutes, divisor_for_seconds, obj;
57 hours = Math.floor(secs / (60 * 60));
58
59 divisor_for_minutes = secs % (60 * 60);
60 minutes = Math.floor(divisor_for_minutes / 60);
61
62 divisor_for_seconds = divisor_for_minutes % 60;
63 seconds = Math.ceil(divisor_for_seconds);
64
65 obj = {
66 "h": hours,
67 "m": minutes,
68 "s": seconds
69 };
70 return obj;
71 }
72
73
74 /* Command input Alias + re-writing */
75 function InputPreProcessor () {
76 this.recursive_depth = 3;
77
78 this.aliases = {};
79 this.vars = {version: 1};
80
81 // Current recursive depth
82 var depth = 0;
83
84
85 // Takes an array of words to process!
86 this.processInput = function (input) {
87 var words = input || [],
88 alias = this.aliases[words[0]],
89 alias_len,
90 current_alias_word = '',
91 compiled = [];
92
93 // If an alias wasn't found, return the original input
94 if (!alias) return input;
95
96 // Split the alias up into useable words
97 alias = alias.split(' ');
98 alias_len = alias.length;
99
100 // Iterate over each word and pop them into the final compiled array.
101 // Any $ words are processed with the result ending into the compiled array.
102 for (var i=0; i<alias_len; i++) {
103 current_alias_word = alias[i];
104
105 // Non $ word
106 if (current_alias_word[0] !== '$') {
107 compiled.push(current_alias_word);
108 continue;
109 }
110
111 // Refering to an input word ($N)
112 if (!isNaN(current_alias_word[1])) {
113 var num = current_alias_word.match(/\$(\d+)(\+)?(\d+)?/);
114
115 // Did we find anything or does the word it refers to non-existant?
116 if (!num || !words[num[1]]) continue;
117
118 if (num[2] === '+' && num[3]) {
119 // Add X number of words
120 compiled = compiled.concat(words.slice(parseInt(num[1], 10), parseInt(num[1], 10) + parseInt(num[3], 10)));
121 } else if (num[2] === '+') {
122 // Add the remaining of the words
123 compiled = compiled.concat(words.slice(parseInt(num[1], 10)));
124 } else {
125 // Add a single word
126 compiled.push(words[parseInt(num[1], 10)]);
127 }
128
129 continue;
130 }
131
132
133 // Refering to a variable
134 if (typeof this.vars[current_alias_word.substr(1)] !== 'undefined') {
135
136 // Get the variable
137 compiled.push(this.vars[current_alias_word.substr(1)]);
138
139 continue;
140 }
141
142 }
143
144 return compiled;
145 };
146
147
148 this.process = function (input) {
149 input = input || '';
150
151 var words = input.split(' ');
152
153 depth++;
154 if (depth >= this.recursive_depth) {
155 depth--;
156 return input;
157 }
158
159 if (this.aliases[words[0]]) {
160 words = this.processInput(words);
161
162 if (this.aliases[words[0]]) {
163 words = this.process(words.join(' ')).split(' ');
164 }
165
166 }
167
168 depth--;
169 return words.join(' ');
170 };
171 }
172
173
174 /**
175 * Convert HSL to RGB formatted colour
176 */
177 function hsl2rgb(h, s, l) {
178 var m1, m2, hue;
179 var r, g, b
180 s /=100;
181 l /= 100;
182 if (s == 0)
183 r = g = b = (l * 255);
184 else {
185 function HueToRgb(m1, m2, hue) {
186 var v;
187 if (hue < 0)
188 hue += 1;
189 else if (hue > 1)
190 hue -= 1;
191
192 if (6 * hue < 1)
193 v = m1 + (m2 - m1) * hue * 6;
194 else if (2 * hue < 1)
195 v = m2;
196 else if (3 * hue < 2)
197 v = m1 + (m2 - m1) * (2/3 - hue) * 6;
198 else
199 v = m1;
200
201 return 255 * v;
202 }
203 if (l <= 0.5)
204 m2 = l * (s + 1);
205 else
206 m2 = l + s - l * s;
207 m1 = l * 2 - m2;
208 hue = h / 360;
209 r = HueToRgb(m1, m2, hue + 1/3);
210 g = HueToRgb(m1, m2, hue);
211 b = HueToRgb(m1, m2, hue - 1/3);
212 }
213 return [r,g,b];
214 }
215
216
217 /**
218 * Formats a kiwi message to IRC format
219 */
220 function formatToIrcMsg(message) {
221 // Format any colour codes (eg. $c4)
222 message = message.replace(/%C(\d)/ig, function(match, colour_number) {
223 return String.fromCharCode(3) + colour_number.toString();
224 });
225
226 var formatters = {
227 B: '\x02', // Bold
228 I: '\x1D', // Italics
229 U: '\x1F', // Underline
230 O: '\x0F' // Out / Clear formatting
231 };
232 message = message.replace(/%([BIUO])/ig, function(match, format_code) {
233 if (typeof formatters[format_code.toUpperCase()] !== 'undefined')
234 return formatters[format_code.toUpperCase()];
235 });
236
237 return message;
238 }
239
240
241 /**
242 * Formats a message. Adds bold, underline and colouring
243 * @param {String} msg The message to format
244 * @returns {String} The HTML formatted message
245 */
246 function formatIRCMsg (msg) {
247 "use strict";
248 var out = '',
249 currentTag = '',
250 openTags = {
251 bold: false,
252 italic: false,
253 underline: false,
254 colour: false
255 },
256 spanFromOpen = function () {
257 var style = '',
258 colours;
259 if (!(openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
260 return '';
261 } else {
262 style += (openTags.bold) ? 'font-weight: bold; ' : '';
263 style += (openTags.italic) ? 'font-style: italic; ' : '';
264 style += (openTags.underline) ? 'text-decoration: underline; ' : '';
265 if (openTags.colour) {
266 colours = openTags.colour.split(',');
267 style += 'color: ' + colours[0] + ((colours[1]) ? '; background-color: ' + colours[1] + ';' : '');
268 }
269 return '<span class="format_span" style="' + style + '">';
270 }
271 },
272 colourMatch = function (str) {
273 var re = /^\x03(([0-9][0-9]?)(,([0-9][0-9]?))?)/;
274 return re.exec(str);
275 },
276 hexFromNum = function (num) {
277 switch (parseInt(num, 10)) {
278 case 0:
279 return '#FFFFFF';
280 case 1:
281 return '#000000';
282 case 2:
283 return '#000080';
284 case 3:
285 return '#008000';
286 case 4:
287 return '#FF0000';
288 case 5:
289 return '#800040';
290 case 6:
291 return '#800080';
292 case 7:
293 return '#FF8040';
294 case 8:
295 return '#FFFF00';
296 case 9:
297 return '#80FF00';
298 case 10:
299 return '#008080';
300 case 11:
301 return '#00FFFF';
302 case 12:
303 return '#0000FF';
304 case 13:
305 return '#FF55FF';
306 case 14:
307 return '#808080';
308 case 15:
309 return '#C0C0C0';
310 default:
311 return null;
312 }
313 },
314 i = 0,
315 colours = [],
316 match;
317
318 for (i = 0; i < msg.length; i++) {
319 switch (msg[i]) {
320 case '\x02':
321 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
322 out += currentTag + '</span>';
323 }
324 openTags.bold = !openTags.bold;
325 currentTag = spanFromOpen();
326 break;
327 case '\x1D':
328 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
329 out += currentTag + '</span>';
330 }
331 openTags.italic = !openTags.italic;
332 currentTag = spanFromOpen();
333 break;
334 case '\x1F':
335 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
336 out += currentTag + '</span>';
337 }
338 openTags.underline = !openTags.underline;
339 currentTag = spanFromOpen();
340 break;
341 case '\x03':
342 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
343 out += currentTag + '</span>';
344 }
345 match = colourMatch(msg.substr(i, 6));
346 if (match) {
347 i += match[1].length;
348 // 2 & 4
349 colours[0] = hexFromNum(match[2]);
350 if (match[4]) {
351 colours[1] = hexFromNum(match[4]);
352 }
353 openTags.colour = colours.join(',');
354 } else {
355 openTags.colour = false;
356 }
357 currentTag = spanFromOpen();
358 break;
359 case '\x0F':
360 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
361 out += currentTag + '</span>';
362 }
363 openTags.bold = openTags.italic = openTags.underline = openTags.colour = false;
364 break;
365 default:
366 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
367 currentTag += msg[i];
368 } else {
369 out += msg[i];
370 }
371 break;
372 }
373 }
374 if ((openTags.bold || openTags.italic || openTags.underline || openTags.colour)) {
375 out += currentTag + '</span>';
376 }
377 return out;
378 }
379
380
381 function formatDate (d) {
382 d = d || new Date();
383 return d.toLocaleDateString() + ', ' + d.getHours().toString() + ':' + d.getMinutes().toString() + ':' + d.getSeconds().toString();
384 }
385
386 function escapeRegex (str) {
387 return str.replace(/[\[\\\^\$\.\|\?\*\+\(\)]/g, '\\$&');
388 }
389
390 function emoticonFromText(str) {
391 var words_in = str.split(' '),
392 words_out = [],
393 i,
394 pushEmoticon = function (alt, emote_name) {
395 words_out.push('<i class="emoticon ' + emote_name + '">' + alt + '</i>');
396 };
397
398 for (i = 0; i < words_in.length; i++) {
399 switch(words_in[i]) {
400 case ':)':
401 pushEmoticon(':)', 'smile');
402 break;
403 case ':(':
404 pushEmoticon(':(', 'sad');
405 break;
406 case ':3':
407 pushEmoticon(':3', 'lion');
408 break;
409 case ';3':
410 pushEmoticon(';3', 'winky_lion');
411 break;
412 case ':s':
413 case ':S':
414 pushEmoticon(':s', 'confused');
415 break;
416 case ';(':
417 case ';_;':
418 pushEmoticon(';(', 'cry');
419 break;
420 case ';)':
421 pushEmoticon(';)', 'wink');
422 break;
423 case ';D':
424 pushEmoticon(';D"', 'wink_happy');
425 break;
426 case ':P':
427 case ':p':
428 pushEmoticon(':P', 'tongue');
429 break;
430 case 'xP':
431 pushEmoticon('xP', 'cringe_tongue');
432 break;
433 case ':o':
434 case ':O':
435 case ':0':
436 pushEmoticon(':o', 'shocked');
437 break;
438 case ':D':
439 pushEmoticon(':D', 'happy');
440 break;
441 case '^^':
442 case '^.^':
443 pushEmoticon('^^,', 'eyebrows');
444 break;
445 case '&lt;3':
446 pushEmoticon('<3', 'heart');
447 break;
448 case '&gt;_&lt;':
449 case '&gt;.&lt;':
450 pushEmoticon('>_<', 'doh');
451 break;
452 case 'XD':
453 case 'xD':
454 pushEmoticon('xD', 'big_grin');
455 break;
456 case 'o.0':
457 case 'o.O':
458 pushEmoticon('o.0', 'wide_eye_right');
459 break;
460 case '0.o':
461 case 'O.o':
462 pushEmoticon('0.o', 'wide_eye_left');
463 break;
464 case ':\\':
465 case '=\\':
466 case ':/':
467 case '=/':
468 pushEmoticon(':\\', 'unsure');
469 break;
470 default:
471 words_out.push(words_in[i]);
472 }
473 }
474
475 return words_out.join(' ');
476 }
477
478 // Code based on http://anentropic.wordpress.com/2009/06/25/javascript-iso8601-parser-and-pretty-dates/#comment-154
479 function parseISO8601(str) {
480 if (Date.prototype.toISOString) {
481 return new Date(str);
482 } else {
483 var parts = str.split('T'),
484 dateParts = parts[0].split('-'),
485 timeParts = parts[1].split('Z'),
486 timeSubParts = timeParts[0].split(':'),
487 timeSecParts = timeSubParts[2].split('.'),
488 timeHours = Number(timeSubParts[0]),
489 _date = new Date();
490
491 _date.setUTCFullYear(Number(dateParts[0]));
492 _date.setUTCDate(1);
493 _date.setUTCMonth(Number(dateParts[1])-1);
494 _date.setUTCDate(Number(dateParts[2]));
495 _date.setUTCHours(Number(timeHours));
496 _date.setUTCMinutes(Number(timeSubParts[1]));
497 _date.setUTCSeconds(Number(timeSecParts[0]));
498 if (timeSecParts[1]) {
499 _date.setUTCMilliseconds(Number(timeSecParts[1]));
500 }
501
502 return _date;
503 }
504 }
505
506 // Simplyfy the translation syntax
507 function translateText(string_id, params) {
508 params = params || '';
509
510 return _kiwi.global.i18n.translate(string_id).fetch(params);
511 }
512
513 /**
514 * Simplyfy the text styling syntax
515 *
516 * Syntax:
517 * %N: nickname
518 * %C: channel
519 * %J: ident
520 * %H: host
521 * %R: realname
522 * %C[digit]: color
523 * %B: bold
524 * %I: italic
525 * %U: underline
526 * %O: cancel styles
527 * %T: translated text
528 **/
529 function styleText(string_id, params) {
530 var style, text;
531
532 style = formatToIrcMsg(_kiwi.app.text_theme[string_id]);
533
534 // Bring member info back to first level of params
535 if (params['%M']) {
536 _.each(params['%M'], function(val, key) {
537 params[key] = val;
538 });
539 }
540
541 // Do the magic. Use the shorthand syntax to produce output.
542 text = style.replace(/%([TJHNCR])/g, function(match, key) {
543 key = '%' + key;
544
545 if (typeof params[key.toUpperCase()] !== 'undefined')
546 return params[key.toUpperCase()];
547 });
548
549 return text;
550 }