
const CMD_REG = (/(\:\S+ )?(\w+)((?: [^:\ ][^\ ]*)*)(?: :(.*))?/)

function parseParams(str) {
    if (str && str.length)
        str = str.substr(1);
    return str.split(/\s/).filter(i => i.length);
}

function IrcCmd(regResult) {
    this.prefix = regResult[1];
    this.command = (regResult[2] || '').toUpperCase();
    this.args = parseParams(regResult[3]);
    this.trailing = regResult[4];
    if (this.trailing)
        this.args.push(this.trailing);
}

IrcCmd.parse = function(str) {
    var regResult = CMD_REG.exec(str);
    return regResult ? new IrcCmd(regResult) : null;
}

module.exports.parse = IrcCmd.parse;

