-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.js
233 lines (189 loc) · 7.21 KB
/
common.js
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
/*
This file contains common functions and functions written by people unrelated to this project.
If the piece of code is licensed differently from the rest of the project, it will be separated with a line followed by a comment informing you of the license.
*/
function replaceFromTo(str, replacement, from, to)
{
return str.substring(0, from) + replacement + str.substring(to + 1, str.length);
}
/*
* ============================================================================
*/
/*
https://github.com/expr/irc-message
Copyright (c) 2013-2015, Fionn Kelleher
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var parseMessage = function (data)
{
var message = {
raw: data,
tags: {},
prefix: null,
command: null,
params: []
}
// position and nextspace are used by the parser as a reference.
var position = 0
var nextspace = 0
// The first thing we check for is IRCv3.2 message tags.
// http://ircv3.atheme.org/specification/message-tags-3.2
if (data.charCodeAt(0) === 64)
{
var nextspace = data.indexOf(' ')
if (nextspace === -1)
{
// Malformed IRC message.
return null
}
// Tags are split by a semi colon.
var rawTags = data.slice(1, nextspace).split(';')
for (var i = 0; i < rawTags.length; i++)
{
// Tags delimited by an equals sign are key=value tags.
// If there's no equals, we assign the tag a value of true.
var tag = rawTags[i]
var pair = tag.split('=')
message.tags[pair[0]] = pair[1] || true
}
position = nextspace + 1
}
// Skip any trailing whitespace.
while (data.charCodeAt(position) === 32)
{
position++
}
// Extract the message's prefix if present. Prefixes are prepended
// with a colon.
if (data.charCodeAt(position) === 58)
{
nextspace = data.indexOf(' ', position)
// If there's nothing after the prefix, deem this message to be
// malformed.
if (nextspace === -1)
{
// Malformed IRC message.
return null
}
message.prefix = data.slice(position + 1, nextspace)
position = nextspace + 1
// Skip any trailing whitespace.
while (data.charCodeAt(position) === 32)
{
position++
}
}
nextspace = data.indexOf(' ', position)
// If there's no more whitespace left, extract everything from the
// current position to the end of the string as the command.
if (nextspace === -1)
{
if (data.length > position)
{
message.command = data.slice(position)
return message
}
return null
}
// Else, the command is the current position up to the next space. After
// that, we expect some parameters.
message.command = data.slice(position, nextspace)
position = nextspace + 1
// Skip any trailing whitespace.
while (data.charCodeAt(position) === 32)
{
position++
}
while (position < data.length)
{
nextspace = data.indexOf(' ', position)
// If the character is a colon, we've got a trailing parameter.
// At this point, there are no extra params, so we push everything
// from after the colon to the end of the string, to the params array
// and break out of the loop.
if (data.charCodeAt(position) === 58)
{
message.params.push(data.slice(position + 1))
break
}
// If we still have some whitespace...
if (nextspace !== -1)
{
// Push whatever's between the current position and the next
// space to the params array.
message.params.push(data.slice(position, nextspace))
position = nextspace + 1
// Skip any trailing whitespace and continue looping.
while (data.charCodeAt(position) === 32)
{
position++
}
continue
}
// If we don't have any more whitespace and the param isn't trailing,
// push everything remaining to the params array.
if (nextspace === -1)
{
message.params.push(data.slice(position))
break
}
}
return message
}
/*
* ============================================================================
*/
// This script is licensed CC-ShareAlike w/ attribution
// From http://stackoverflow.com/a/979995/1780502 by Quentin (http://stackoverflow.com/users/19068/quentin)
// Edited to NOT be immediately executed
var QueryString = function ()
{
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++)
{
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined")
{
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string")
{
var arr = [query_string[pair[0]], pair[1]];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else
{
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
};
/*
* ============================================================================
*/
// The 3 functions below CC-by-SA http://stackoverflow.com/a/5624139/1780502 by http://stackoverflow.com/users/96100/tim-down
function hexToRgb(hex)
{
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function componentToHex(c)
{
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b)
{
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}