-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathirc_util.js
More file actions
212 lines (189 loc) · 5.91 KB
/
Copy pathirc_util.js
File metadata and controls
212 lines (189 loc) · 5.91 KB
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
// Generated by CoffeeScript 1.4.0
(function() {
"use strict";
var arrayBuffer2String, concatArrayBuffers, exports, normaliseNick, string2ArrayBuffer, _base, _ref, _ref1,
__slice = [].slice;
var exports = (_ref = (_base = ((_ref1 = window.irc) != null ? _ref1 : window.irc = {})).util) != null ? _ref : _base.util = {};
exports.parseCommand = function(data) {
var parts, str;
str = $.trim(data.toString('utf8'));
parts = /^(?::([^\x20]+?)\x20)?([^\x20]+?)((?:\x20[^\x20:][^\x20]*)+)?(?:\x20:(.*))?$/.exec(str);
if (!parts) {
throw new Error("invalid IRC message: " + data);
}
/*
* could do more validation here...
* prefix = servername | nickname((!user)?@host)?
* command = letter+ | digit{3}
* params has weird stuff going on when there are 14 arguments
*/
/*
* trim whitespace
*/
if (parts[3] != null) {
parts[3] = parts[3].slice(1).split(/\x20/);
} else {
parts[3] = [];
}
if (parts[4] != null) {
parts[3].push(parts[4]);
}
return {
prefix: parts[1],
command: parts[2],
params: parts[3]
};
};
exports.parsePrefix = function(prefix) {
var p;
p = /^([^!]+?)(?:!(.+?)(?:@(.+?))?)?$/.exec(prefix);
return {
nick: p[1],
user: p[2],
host: p[3]
};
};
exports.makeCommand = function() {
var cmd, opt_lastArgIsMsg, params, _i, _params;
cmd = arguments[0], params = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), opt_lastArgIsMsg = arguments[_i++];
if (opt_lastArgIsMsg !== true) {
params.push(opt_lastArgIsMsg);
}
_params = (function() {
if (params && params.length > 0) {
if (!params.slice(0, params.length - 1).every(function(a) {
return !/^:|\x20/.test(a);
})) {
throw new Error("some non-final arguments had spaces or initial colons in them");
}
if (/^:|\x20/.test(params[params.length - 1]) || opt_lastArgIsMsg === true) {
params[params.length - 1] = ':' + params[params.length - 1];
}
return ' ' + params.join(' ');
} else {
return '';
}
})();
return cmd + _params + "\x0d\x0a";
};
exports.randomName = function(length) {
var chars, x;
if (length == null) {
length = 10;
}
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return ((function() {
var _i, _results;
_results = [];
for (x = _i = 0; 0 <= length ? _i < length : _i > length; x = 0 <= length ? ++_i : --_i) {
_results.push(chars[Math.floor(Math.random() * chars.length)]);
}
return _results;
})()).join('');
};
exports.normaliseNick = normaliseNick = function(nick) {
return nick.toLowerCase().replace(/[\[\]\\]/g, function(x) {
return {
'[': '{',
']': '}',
'|': '\\'
}[x];
});
};
exports.nicksEqual = function(a, b) {
var _ref2;
if (!((typeof a === (_ref2 = typeof b) && _ref2 === 'string'))) {
return false;
}
return (a != null) && (b != null) && normaliseNick(a) === normaliseNick(b);
};
exports.toSocketData = function(str, cb) {
return string2ArrayBuffer(str, function(ab) {
return cb(ab);
});
};
exports.fromSocketData = function(ab, cb) {
return arrayBuffer2String(ab, cb);
};
exports.emptySocketData = function() {
return new ArrayBuffer(0);
};
exports.concatSocketData = function(a, b) {
return concatArrayBuffers(a, b);
};
exports.arrayBufferConversionCount = 0;
exports.isConvertingArrayBuffers = function() {
return exports.arrayBufferConversionCount > 0;
};
/**
* Converts an array containing uint8 values to an ArrayBuffer.
* @param {Array.<number>} array An array of values in the range [0, 255].
* @return {ArrayBuffer} An array buffer containing the byte representation of
* the passed in array.
*/
exports.arrayToArrayBuffer = function(array) {
var arrayBuffer = new ArrayBuffer(array.length);
var arrayView = new Uint8Array(arrayBuffer);
arrayView.set(array);
return arrayBuffer;
};
exports.dumpBuffer = function(buffer) {
try {
var view, arr, hexes;
view = new Uint8Array(buffer);
arr = view.subarray();
arr = Array.prototype.slice.call(arr);
hexes = arr.map(function(n) {
return (n < 16 ? '0' : '') + Number(n).toString(16).toUpperCase();
});
console.log(hexes.join(' '));
} catch(e) {
console.err(e);
}
};
function createBlob(src) {
var BB = window.BlobBuilder || window.WebKitBlobBuilder;
if (BB) {
var bb = new BB();
bb.append(src);
return bb.getBlob();
}
return new Blob([src]);
}
concatArrayBuffers = function(a, b) {
var result, resultView;
result = new ArrayBuffer(a.byteLength + b.byteLength);
resultView = new Uint8Array(result);
resultView.set(new Uint8Array(a));
resultView.set(new Uint8Array(b), a.byteLength);
return result;
};
string2ArrayBuffer = function(string, callback) {
var blob, f;
exports.arrayBufferConversionCount++;
blob = createBlob(string);
f = new FileReader();
f.onload = function(e) {
exports.arrayBufferConversionCount--;
return callback(e.target.result);
};
return f.readAsArrayBuffer(blob);
};
arrayBuffer2String = function(buf, callback) {
var blob, f, hadReplacements;
exports.arrayBufferConversionCount++;
blob = createBlob(buf);
f = new FileReader();
hadReplacements = false;
f.onload = function(e) {
if(e.target.result.match(/\ufffd/) && !hadReplacements) {
hadReplacements = true;
return f.readAsText(blob, 'ISO-8859-1');
} else {
exports.arrayBufferConversionCount--;
return callback(e.target.result, hadReplacements ? 'ISO-8859-1' : 'UTF-8');
}
};
return f.readAsText(blob);
};
}).call(this);