UNPKG

2.01 kBJavaScriptView Raw
1'use strict';
2
3require("core-js/modules/es.array.index-of.js");
4require("core-js/modules/es.regexp.exec.js");
5require("core-js/modules/es.string.replace.js");
6var MessageTags = require('./messagetags');
7var IrcMessage = require('./ircmessage');
8var helpers = require('./helpers');
9module.exports = parseIrcLine;
10var newline_regex = /^[\r\n]+|[\r\n]+$/g;
11function parseIrcLine(input_) {
12 var input = input_.replace(newline_regex, '');
13 var cPos = 0;
14 var inParams = false;
15 var nextToken = function nextToken() {
16 // Fast forward to somewhere with actual data
17 while (input[cPos] === ' ' && cPos < input.length) {
18 cPos++;
19 }
20 if (cPos === input.length) {
21 // If reading the params then return null to indicate no more params available.
22 // The trailing parameter may be empty but should still be included as an empty string.
23 return inParams ? null : '';
24 }
25 var end = input.indexOf(' ', cPos);
26 if (end === -1) {
27 // No more spaces means were on the last token
28 end = input.length;
29 }
30 if (inParams && input[cPos] === ':' && input[cPos - 1] === ' ') {
31 // If a parameter start with : then we're in the last parameter which may incude spaces
32 cPos++;
33 end = input.length;
34 }
35 var token = input.substring(cPos, end);
36 cPos = end;
37
38 // Fast forward our current position so we can peek what's next via input[cPos]
39 while (input[cPos] === ' ' && cPos < input.length) {
40 cPos++;
41 }
42 return token;
43 };
44 var ret = new IrcMessage();
45 if (input[cPos] === '@') {
46 ret.tags = MessageTags.decode(nextToken().substr(1));
47 }
48 if (input[cPos] === ':') {
49 ret.prefix = nextToken().substr(1);
50 var mask = helpers.parseMask(ret.prefix);
51 ret.nick = mask.nick;
52 ret.ident = mask.user;
53 ret.hostname = mask.host;
54 }
55 ret.command = nextToken().toUpperCase();
56 inParams = true;
57 var token = nextToken();
58 while (token !== null) {
59 ret.params.push(token);
60 token = nextToken();
61 }
62 return ret;
63}
\No newline at end of file