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 */
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
11 function randomString(string_length
) {
12 var chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",
16 for (i
= 0; i
< string_length
; i
++) {
17 rnum
= Math
.floor(Math
.random() * chars
.length
);
18 randomstring
+= chars
.substring(rnum
, rnum
+ 1);
26 if (typeof String
.prototype.trim
=== 'undefined') {
27 String
.prototype.trim = function () {
28 return this.replace(/^\s+|\s+$/g, "");
34 * @param {Number} length The length of padding
35 * @param {String} characher The character to pad with
36 * @returns {String} The padded string
38 if (typeof String
.prototype.lpad
=== 'undefined') {
39 String
.prototype.lpad = function (length
, character
) {
42 for (i
= 0; i
< length
; i
++) {
45 return (padding
+ this).slice(-length
);
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
55 function secondsToTime(secs
) {
56 var hours
, minutes
, seconds
, divisor_for_minutes
, divisor_for_seconds
, obj
;
57 hours
= Math
.floor(secs
/ (60 * 60));
59 divisor_for_minutes
= secs
% (60 * 60);
60 minutes
= Math
.floor(divisor_for_minutes
/ 60);
62 divisor_for_seconds
= divisor_for_minutes
% 60;
63 seconds
= Math
.ceil(divisor_for_seconds
);
74 /* Command input Alias + re-writing */
75 function InputPreProcessor () {
76 this.recursive_depth
= 3;
79 this.vars
= {version
: 1};
81 // Current recursive depth
85 // Takes an array of words to process!
86 this.processInput = function (input
) {
87 var words
= input
|| [],
88 alias
= this.aliases
[words
[0]],
90 current_alias_word
= '',
93 // If an alias wasn't found, return the original input
94 if (!alias
) return input
;
96 // Split the alias up into useable words
97 alias
= alias
.split(' ');
98 alias_len
= alias
.length
;
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
];
106 if (current_alias_word
[0] !== '$') {
107 compiled
.push(current_alias_word
);
111 // Refering to an input word ($N)
112 if (!isNaN(current_alias_word
[1])) {
113 var num
= current_alias_word
.match(/\$(\d+)(\+)?(\d+)?/);
115 // Did we find anything or does the word it refers to non-existant?
116 if (!num
|| !words
[num
[1]]) continue;
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)));
126 compiled
.push(words
[parseInt(num
[1], 10)]);
133 // Refering to a variable
134 if (typeof this.vars
[current_alias_word
.substr(1)] !== 'undefined') {
137 compiled
.push(this.vars
[current_alias_word
.substr(1)]);
148 this.process = function (input
) {
151 var words
= input
.split(' ');
154 if (depth
>= this.recursive_depth
) {
159 if (this.aliases
[words
[0]]) {
160 words
= this.processInput(words
);
162 if (this.aliases
[words
[0]]) {
163 words
= this.process(words
.join(' ')).split(' ');
169 return words
.join(' ');
175 * Convert HSL to RGB formatted colour
177 function hsl2rgb(h
, s
, l
) {
183 r
= g
= b
= (l
* 255);
185 function HueToRgb(m1
, m2
, hue
) {
193 v
= m1
+ (m2
- m1
) * hue
* 6;
194 else if (2 * hue
< 1)
196 else if (3 * hue
< 2)
197 v
= m1
+ (m2
- m1
) * (2/3 - hue
) * 6;
209 r
= HueToRgb(m1
, m2
, hue
+ 1/3);
210 g
= HueToRgb(m1
, m2
, hue
);
211 b
= HueToRgb(m1
, m2
, hue
- 1/3);
218 * Formats a message. Adds bold, underline and colouring
219 * @param {String} msg The message to format
220 * @returns {String} The HTML formatted message
222 function formatIRCMsg (msg
) {
232 spanFromOpen = function () {
235 if (!(openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
238 style
+= (openTags
.bold
) ? 'font-weight: bold; ' : '';
239 style
+= (openTags
.italic
) ? 'font-style: italic; ' : '';
240 style
+= (openTags
.underline
) ? 'text-decoration: underline; ' : '';
241 if (openTags
.colour
) {
242 colours
= openTags
.colour
.split(',');
243 style
+= 'color: ' + colours
[0] + ((colours
[1]) ? '; background-color: ' + colours
[1] + ';' : '');
245 return '<span class="format_span" style="' + style
+ '">';
248 colourMatch = function (str
) {
249 var re
= /^\x03(([0-9][0-9]?)(,([0-9][0-9]?))?)/;
252 hexFromNum = function (num
) {
253 switch (parseInt(num
, 10)) {
294 for (i
= 0; i
< msg
.length
; i
++) {
297 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
298 out
+= currentTag
+ '</span>';
300 openTags
.bold
= !openTags
.bold
;
301 currentTag
= spanFromOpen();
304 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
305 out
+= currentTag
+ '</span>';
307 openTags
.italic
= !openTags
.italic
;
308 currentTag
= spanFromOpen();
311 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
312 out
+= currentTag
+ '</span>';
314 openTags
.underline
= !openTags
.underline
;
315 currentTag
= spanFromOpen();
318 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
319 out
+= currentTag
+ '</span>';
321 match
= colourMatch(msg
.substr(i
, 6));
323 i
+= match
[1].length
;
325 colours
[0] = hexFromNum(match
[2]);
327 colours
[1] = hexFromNum(match
[4]);
329 openTags
.colour
= colours
.join(',');
331 openTags
.colour
= false;
333 currentTag
= spanFromOpen();
336 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
337 out
+= currentTag
+ '</span>';
339 openTags
.bold
= openTags
.italic
= openTags
.underline
= openTags
.colour
= false;
342 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
343 currentTag
+= msg
[i
];
350 if ((openTags
.bold
|| openTags
.italic
|| openTags
.underline
|| openTags
.colour
)) {
351 out
+= currentTag
+ '</span>';
357 function formatDate (d
) {
359 return d
.toLocaleDateString() + ', ' + d
.getHours().toString() + ':' + d
.getMinutes().toString() + ':' + d
.getSeconds().toString();
362 function escapeRegex (str
) {
363 return str
.replace(/[\[\\\^\$\.\|\?\*\+\(\)]/g, '\\$&');
366 function emoticonFromText(str
) {
367 var words_in
= str
.split(' '),
370 pushEmoticon = function (alt
, emote_name
) {
371 words_out
.push('<i class="emoticon ' + emote_name
+ '">' + alt
+ '</i>');
374 for (i
= 0; i
< words_in
.length
; i
++) {
375 switch(words_in
[i
]) {
377 pushEmoticon(':)', 'smile');
380 pushEmoticon(':(', 'sad');
383 pushEmoticon(':3', 'lion');
386 pushEmoticon(';3', 'winky_lion');
390 pushEmoticon(':s', 'confused');
394 pushEmoticon(';(', 'cry');
397 pushEmoticon(';)', 'wink');
400 pushEmoticon(';D"', 'wink_happy');
404 pushEmoticon(':P', 'tongue');
407 pushEmoticon('xP', 'cringe_tongue');
412 pushEmoticon(':o', 'shocked');
415 pushEmoticon(':D', 'happy');
419 pushEmoticon('^^,', 'eyebrows');
422 pushEmoticon('<3', 'heart');
426 pushEmoticon('>_<', 'doh');
430 pushEmoticon('xD', 'big_grin');
434 pushEmoticon('o.0', 'wide_eye_right');
438 pushEmoticon('0.o', 'wide_eye_left');
444 pushEmoticon(':\\', 'unsure');
447 words_out
.push(words_in
[i
]);
451 return words_out
.join(' ');
454 // Code based on http://anentropic.wordpress.com/2009/06/25/javascript-iso8601-parser-and-pretty-dates/#comment-154
455 function parseISO8601(str
) {
456 if (Date
.prototype.toISOString
) {
457 return new Date(str
);
459 var parts
= str
.split('T'),
460 dateParts
= parts
[0].split('-'),
461 timeParts
= parts
[1].split('Z'),
462 timeSubParts
= timeParts
[0].split(':'),
463 timeSecParts
= timeSubParts
[2].split('.'),
464 timeHours
= Number(timeSubParts
[0]),
467 _date
.setUTCFullYear(Number(dateParts
[0]));
469 _date
.setUTCMonth(Number(dateParts
[1])-1);
470 _date
.setUTCDate(Number(dateParts
[2]));
471 _date
.setUTCHours(Number(timeHours
));
472 _date
.setUTCMinutes(Number(timeSubParts
[1]));
473 _date
.setUTCSeconds(Number(timeSecParts
[0]));
474 if (timeSecParts
[1]) {
475 _date
.setUTCMilliseconds(Number(timeSecParts
[1]));