forked from lsongdev/node-irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
48 lines (40 loc) · 1.04 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
const split = require('split2');
const { debuglog } = require('util');
const through = require('through2');
const combine = require('stream-combiner');
const Message = require('./message');
const debug = debuglog('ircs:MessageParser');
function MessageParser() {
return combine(
split(),
through.obj(parse)
)
/**
* Parses an individual IRC command.
*
* @param {string} line IRC command string.
* @return {Message}
*/
function parse(line, enc, cb) {
debug('parsing', line)
let prefix
let command
let params
if (line[0] === ':') {
let prefixEnd = line.indexOf(' ')
prefix = line.slice(1, prefixEnd)
line = line.slice(prefixEnd + 1)
}
let colon = line.indexOf(' :')
if (colon !== -1) {
let append = line.slice(colon + 2)
line = line.slice(0, colon)
params = line.split(/ +/g).concat([append])
} else {
params = line.split(/ +/g)
}
command = params.shift()
cb(null, new Message(prefix, command, params))
}
}
module.exports = MessageParser;