#include "Server.hpp"
#include "errcodes.hpp"
#include "split.hpp"
#include <algorithm>

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

    Client& client = msg.getClient();
    string_vector const& args = msg.getArgs();

    std::vector<Channel>::iterator itChannel;
    itChannel = std::find(_channels.begin(), _channels.end(), args[0]);
    if (itChannel == _channels.end()) {
        return client.sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0],
                "No such channel");
    }
    if (msg.getArgs().size() > 1) {
        return itChannel->setMode(*this, client, args);
    } else {
        return itChannel->sendMode(client);
    }
}
