#include "Server.hpp"
#include "errcodes.hpp"

int
Server::_cmdUSER(Message const& msg)
{
    if (int rv = msg.expectArgs(4)) {
        return rv == -1 ? rv : 0;
    }

    Client& client = msg.getClient();

    if (client.getRegistered()) {
        return client.sendCode(ERR_ALREADYREGISTRED);
    }

    client.username = msg.getArgs()[0];
    client.realname = msg.getArgs()[3];
    client.setRegistered();
    if (!client.nick.empty()) {
        return _welcomeClient(client);
    }
    return 0;
}
