-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
parser.js
243 lines (197 loc) · 6.81 KB
/
parser.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
234
235
236
237
238
239
240
241
242
243
/*
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:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
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 _ = require("./utils");
var nonspaceRegex = /\S+/g;
function parseComplexTag(tags, tagKey, splA = ",", splB = "/", splC) {
var raw = tags[tagKey];
if(raw === undefined) {
return tags;
}
var tagIsString = _.isString(raw);
tags[tagKey + "-raw"] = tagIsString ? raw : null;
if(raw === true) {
tags[tagKey] = null;
return tags;
}
tags[tagKey] = {};
if(tagIsString) {
var spl = raw.split(splA);
for (var i = 0; i < spl.length; i++) {
var parts = spl[i].split(splB);
var val = parts[1];
if(splC !== undefined && val) {
val = val.split(splC);
}
tags[tagKey][parts[0]] = val || null;
}
}
return tags;
}
module.exports = {
// Parse Twitch badges..
badges: function badges(tags) {
return parseComplexTag(tags, "badges");
},
// Parse Twitch badge-info..
badgeInfo: function badgeInfo(tags) {
return parseComplexTag(tags, "badge-info");
},
// Parse Twitch emotes..
emotes: function emotes(tags) {
return parseComplexTag(tags, "emotes", "/", ":", ",");
},
// Parse regex emotes..
emoteRegex: function emoteRegex(msg, code, id, obj) {
nonspaceRegex.lastIndex = 0;
var regex = new RegExp("(\\b|^|\s)" + _.unescapeHtml(code) + "(\\b|$|\s)");
var match;
// Check if emote code matches using RegExp and push it to the object..
while ((match = nonspaceRegex.exec(msg)) !== null) {
if(regex.test(match[0])) {
obj[id] = obj[id] || [];
obj[id].push([match.index, nonspaceRegex.lastIndex - 1])
}
}
},
// Parse string emotes..
emoteString: function emoteString(msg, code, id, obj) {
nonspaceRegex.lastIndex = 0;
var match;
// Check if emote code matches and push it to the object..
while ((match = nonspaceRegex.exec(msg)) !== null) {
if(match[0] === _.unescapeHtml(code)) {
obj[id] = obj[id] || [];
obj[id].push([match.index, nonspaceRegex.lastIndex - 1]);
}
}
},
// Transform the emotes object to a string with the following format..
// emote_id:first_index-last_index,another_first-another_last/another_emote_id:first_index-last_index
transformEmotes: function transformEmotes(emotes) {
var transformed = "";
Object.keys(emotes).forEach(id => {
transformed = `${transformed+id}:`;
emotes[id].forEach(
index => transformed = `${transformed+index.join("-")},`
);
transformed = `${transformed.slice(0,-1)}/`;
});
return transformed.slice(0,-1);
},
// Parse Twitch messages..
msg: function msg(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(" ");
// Malformed IRC message..
if(nextspace === -1) {
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]] = tag.substring(tag.indexOf("=") + 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) {
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;
}
}