caca
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/30 17:33:40 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/01/30 17:40:18 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -73,9 +73,9 @@ namespace irc
|
||||
|
||||
bool Channel::removeClient(unstd::SharedPtr<Client> client)
|
||||
{
|
||||
if (!_clients.erase(client))
|
||||
if(!_clients.erase(client))
|
||||
return (false);
|
||||
if (isOp(client))
|
||||
if(isOp(client))
|
||||
_operators.erase(client);
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "PART", _name);
|
||||
@@ -267,7 +267,7 @@ namespace irc
|
||||
}
|
||||
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
|
||||
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(op->getNickName(), "KICK " + _name + ' ' + target->getNickName(), reason);
|
||||
if (isOp(target))
|
||||
if(isOp(target))
|
||||
_operators.erase(target);
|
||||
_clients.erase(target);
|
||||
return true;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/30 17:15:37 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/01/30 18:08:41 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace irc
|
||||
_s_data.sin_addr.s_addr = INADDR_ANY;
|
||||
_s_data.sin_port = htons(_port);
|
||||
_main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP
|
||||
if (_main_socket < 0)
|
||||
if(_main_socket < 0)
|
||||
logs::report(log_fatal_error, "socket error");
|
||||
logs::report(log_message, "socket succesfully started");
|
||||
}
|
||||
@@ -50,14 +50,14 @@ namespace irc
|
||||
int opt = 1;
|
||||
|
||||
initSocketData();
|
||||
if (setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends
|
||||
if(setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends
|
||||
logs::report(log_fatal_error, "setsockopt() error (tout a pete)");
|
||||
|
||||
if (bind(_main_socket, reinterpret_cast<sockaddr*>(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost
|
||||
if(bind(_main_socket, reinterpret_cast<sockaddr*>(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost
|
||||
logs::report(log_fatal_error, "bind error");
|
||||
logs::report(log_message, "bind successful, starting listen loop");
|
||||
|
||||
if (listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
|
||||
if(listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
|
||||
logs::report(log_fatal_error, "listen error");
|
||||
logs::report(log_message, "listen queue created successfully");
|
||||
|
||||
@@ -68,11 +68,11 @@ namespace irc
|
||||
{
|
||||
char buffer[INPUT_SIZE] = { 0 };
|
||||
ssize_t recv_size;
|
||||
for (std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
|
||||
for(std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
|
||||
{
|
||||
if (!FD_ISSET((*it)->getFD(), &_fd_set))
|
||||
if(!FD_ISSET((*it)->getFD(), &_fd_set))
|
||||
continue;
|
||||
while ((recv_size = recv((*it)->getFD(), buffer, INPUT_SIZE, 0)) > 0) // read() but for socket fd
|
||||
while ((recv_size = recv((*it)->getFD(), buffer, INPUT_SIZE, 0)) > 0) // read() but forsocket fd
|
||||
{
|
||||
(*it)->newMsgInFlight(buffer);
|
||||
#ifdef DEBUG
|
||||
@@ -81,14 +81,14 @@ namespace irc
|
||||
while (handleMessage(*it));
|
||||
std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining
|
||||
}
|
||||
if (recv_size == 0 || (*it)->disconnectRequired()) // recv return 0 if an user disconnect
|
||||
if(recv_size == 0 || (*it)->disconnectRequired()) // recv return 0 ifan user disconnect
|
||||
{
|
||||
logs::report(log_message, "User %d disconnected", (*it)->getID());
|
||||
close((*it)->getFD());
|
||||
for (channel_it cit = _channels.begin(); cit != _channels.end();)
|
||||
for(channel_it cit = _channels.begin(); cit != _channels.end();)
|
||||
{
|
||||
cit->removeClient(*it);
|
||||
if (cit->getNumberOfClients() == 0)
|
||||
if(cit->getNumberOfClients() == 0)
|
||||
{
|
||||
logs::report(log_message, "channel '%s' has been destroyed", cit->getName().c_str());
|
||||
cit = _channels.erase(cit);
|
||||
@@ -113,20 +113,20 @@ namespace irc
|
||||
FD_ZERO(&_fd_set);
|
||||
FD_SET(_main_socket, &_fd_set);
|
||||
|
||||
for (std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
|
||||
for(std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
|
||||
FD_SET((*it)->getFD(), &_fd_set);
|
||||
|
||||
tmp = select(MAX_USERS, &_fd_set, NULL, NULL, NULL); // SELECT blocks till a connection or message is received, and let only those in _fd_set
|
||||
if (tmp < 0 && _main_socket != NULL_SOCKET)
|
||||
if(tmp < 0 && _main_socket != NULL_SOCKET)
|
||||
logs::report(log_fatal_error, "select fd error");
|
||||
|
||||
if (FD_ISSET(_main_socket, &_fd_set)) // if it's a new connection
|
||||
if(FD_ISSET(_main_socket, &_fd_set)) // ifit's a new connection
|
||||
{
|
||||
sockaddr_in cli_sock;
|
||||
fd = accept(_main_socket, reinterpret_cast<sockaddr*>(&cli_sock), &len); // adds the new connection
|
||||
if (fd < 0)
|
||||
if(fd < 0)
|
||||
logs::report(log_fatal_error, "accept() error");
|
||||
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
|
||||
if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
|
||||
logs::report(log_fatal_error, "fcntl() error");
|
||||
|
||||
unstd::SharedPtr<Client> new_client(new Client(fd, cli_sock, i++));
|
||||
@@ -140,50 +140,50 @@ namespace irc
|
||||
|
||||
bool Server::handleMessage(unstd::SharedPtr<Client> client)
|
||||
{
|
||||
if (client->getMsgInFlight().empty()) // if there are no commands just return
|
||||
if(client->getMsgInFlight().empty()) // ifthere are no commands just return
|
||||
return false;
|
||||
|
||||
const Message msg(client, client->getNextMsg());
|
||||
if (msg.getCmd() == "NICK")
|
||||
if(msg.getCmd() == "NICK")
|
||||
handleNick(client, msg);
|
||||
else if (msg.getCmd() == "USER")
|
||||
else if(msg.getCmd() == "USER")
|
||||
handleUser(client, msg);
|
||||
else if (msg.getCmd() == "PASS")
|
||||
else if(msg.getCmd() == "PASS")
|
||||
handlePass(client, msg);
|
||||
else if (!client->isLogged())
|
||||
else if(!client->isLogged())
|
||||
return true;
|
||||
else if (msg.getCmd() == "QUIT")
|
||||
else if(msg.getCmd() == "QUIT")
|
||||
handleQuit(client, msg);
|
||||
else if (msg.getCmd() == "WHO")
|
||||
else if(msg.getCmd() == "WHO")
|
||||
handleWho(client, msg);
|
||||
else if (msg.getCmd() == "PART")
|
||||
else if(msg.getCmd() == "PART")
|
||||
handlePart(client, msg);
|
||||
else if (msg.getCmd() == "JOIN")
|
||||
else if(msg.getCmd() == "JOIN")
|
||||
handleJoin(client, msg);
|
||||
else if (msg.getCmd() == "INVITE")
|
||||
else if(msg.getCmd() == "INVITE")
|
||||
handleInvite(client, msg);
|
||||
else if (msg.getCmd() == "PRIVMSG")
|
||||
else if(msg.getCmd() == "PRIVMSG")
|
||||
handlePrivMsg(client, msg);
|
||||
else if (msg.getCmd() == "NOTICE")
|
||||
else if(msg.getCmd() == "NOTICE")
|
||||
handleNotice(client, msg);
|
||||
else if (msg.getCmd() == "KICK")
|
||||
else if(msg.getCmd() == "KICK")
|
||||
handleKick(client, msg);
|
||||
else if (msg.getCmd() == "TOPIC")
|
||||
else if(msg.getCmd() == "TOPIC")
|
||||
handleTopic(client, msg);
|
||||
else if (msg.getCmd() == "PING")
|
||||
else if(msg.getCmd() == "PING")
|
||||
handlePing(client, msg);
|
||||
else if (msg.getCmd() == "MODE")
|
||||
else if(msg.getCmd() == "MODE")
|
||||
handleMode(client, msg);
|
||||
else if (msg.getCmd() == "imfeelinglucky")
|
||||
else if(msg.getCmd() == "imfeelinglucky")
|
||||
handleRussian(client);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Server::isUserKnown(const std::string& user) const
|
||||
{
|
||||
for (client_const_it it = _client.begin(); it < _client.end(); ++it)
|
||||
for(client_const_it it = _client.begin(); it < _client.end(); ++it)
|
||||
{
|
||||
if (const_cast<unstd::SharedPtr<Client>&>(*it)->getNickName() == user)
|
||||
if(const_cast<unstd::SharedPtr<Client>&>(*it)->getNickName() == user)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -191,9 +191,9 @@ namespace irc
|
||||
|
||||
bool Server::isChannelKnown(const std::string& channel) const
|
||||
{
|
||||
for (channel_const_it it = _channels.begin(); it < _channels.end(); ++it)
|
||||
for(channel_const_it it = _channels.begin(); it < _channels.end(); ++it)
|
||||
{
|
||||
if (it->getName() == channel)
|
||||
if(it->getName() == channel)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -201,9 +201,9 @@ namespace irc
|
||||
|
||||
Channel* Server::getChannelByName(const std::string& name)
|
||||
{
|
||||
for (channel_it it = _channels.begin(); it < _channels.end(); ++it)
|
||||
for(channel_it it = _channels.begin(); it < _channels.end(); ++it)
|
||||
{
|
||||
if (it->getName() == name)
|
||||
if(it->getName() == name)
|
||||
return &*it;
|
||||
}
|
||||
return NULL;
|
||||
@@ -211,9 +211,9 @@ namespace irc
|
||||
|
||||
unstd::SharedPtr<Client> Server::getClientByName(const std::string& name)
|
||||
{
|
||||
for (client_it it = _client.begin(); it < _client.end(); ++it)
|
||||
for(client_it it = _client.begin(); it < _client.end(); ++it)
|
||||
{
|
||||
if ((*it)->getNickName() == name)
|
||||
if((*it)->getNickName() == name)
|
||||
return *it;
|
||||
}
|
||||
return unstd::SharedPtr<Client>(NULL);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */
|
||||
/* Updated: 2024/01/30 17:35:15 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/01/30 18:09:30 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -36,20 +36,20 @@ namespace irc
|
||||
|
||||
void Server::handleNick(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getTokens().size() < 2)
|
||||
if(msg.getTokens().size() < 2)
|
||||
{
|
||||
client->sendCode(ERR_NONICKNAMEGIVEN, "No nickname given");
|
||||
return;
|
||||
}
|
||||
if (msg.getTokens().size() != 2 && msg.getTokens().size() != 3)
|
||||
if(msg.getTokens().size() != 2 && msg.getTokens().size() != 3)
|
||||
{
|
||||
logs::report(log_error, "NICK, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
const std::string& nickname = msg.getTokens()[1];
|
||||
for (client_it it = _client.begin(); it != _client.end(); ++it)
|
||||
for(client_it it = _client.begin(); it != _client.end(); ++it)
|
||||
{
|
||||
if ((*it)->getNickName() == nickname)
|
||||
if((*it)->getNickName() == nickname)
|
||||
{
|
||||
client->sendCode(ERR_NICKCOLLISION, nickname + " Nickname is used");
|
||||
client->kill("Nickname already used");
|
||||
@@ -63,12 +63,13 @@ namespace irc
|
||||
for(client_it it = _client.begin(); it != _client.end(); ++it)
|
||||
(*it)->sendMsg(oldNick, "NICK", msg.getTokens()[1]);
|
||||
client->welcome();
|
||||
client->printUserHeader();
|
||||
std::cout << "new nickname, " << client->getNickName() << std::endl;
|
||||
}
|
||||
|
||||
void Server::handleUser(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getTokens().size() < 5)
|
||||
if(msg.getTokens().size() < 5)
|
||||
{
|
||||
client->sendCode(ERR_NEEDMOREPARAMS, "Need more parameters");
|
||||
logs::report(log_error, "USER, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
@@ -102,9 +103,9 @@ namespace irc
|
||||
|
||||
void Server::handlePass(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (client->isLogged())
|
||||
if(client->isLogged())
|
||||
return;
|
||||
if (msg.getTokens()[1] == _password)
|
||||
if(msg.getTokens()[1] == _password)
|
||||
{
|
||||
client->login();
|
||||
client->welcome();
|
||||
@@ -119,7 +120,7 @@ namespace irc
|
||||
void Server::handleQuit(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
(void)msg;
|
||||
for (channel_it it = _channels.begin(); it != _channels.end(); ++it)
|
||||
for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
|
||||
{
|
||||
it->removeClient(client);
|
||||
}
|
||||
@@ -129,37 +130,37 @@ namespace irc
|
||||
|
||||
void Server::handlePart(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getTokens().size() < 2 && msg.getTokens().size() > 3)
|
||||
if(msg.getTokens().size() < 2 && msg.getTokens().size() > 3)
|
||||
{
|
||||
logs::report(log_error, "PART, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
if (msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
|
||||
if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
|
||||
{
|
||||
logs::report(log_error, "PART, invalid channel name '%s'", msg.getTokens()[1].c_str());
|
||||
return;
|
||||
}
|
||||
if (!isChannelKnown(msg.getArgs()[0]))
|
||||
if(!isChannelKnown(msg.getArgs()[0]))
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel");
|
||||
return;
|
||||
}
|
||||
Channel* channel = getChannelByName(msg.getArgs()[0]);
|
||||
if (channel == NULL)
|
||||
if(channel == NULL)
|
||||
logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->getName().c_str());
|
||||
if (!channel->removeClient(client))
|
||||
if(!channel->removeClient(client))
|
||||
{
|
||||
client->sendCode(ERR_NOTONCHANNEL, "Not on channel");
|
||||
return;
|
||||
}
|
||||
client->printUserHeader();
|
||||
std::cout << "leaving channel, " << msg.getArgs()[0] << std::endl;
|
||||
if (channel->getNumberOfClients() == 0)
|
||||
if(channel->getNumberOfClients() == 0)
|
||||
{
|
||||
channel_it it;
|
||||
for (it = _channels.begin(); it < _channels.end(); ++it)
|
||||
for(it = _channels.begin(); it < _channels.end(); ++it)
|
||||
{
|
||||
if (it->getName() == msg.getArgs()[0])
|
||||
if(it->getName() == msg.getArgs()[0])
|
||||
break;
|
||||
}
|
||||
_channels.erase(it);
|
||||
@@ -169,72 +170,72 @@ namespace irc
|
||||
|
||||
void Server::handleJoin(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getArgs().empty())
|
||||
if(msg.getArgs().empty())
|
||||
{
|
||||
client->sendCode(ERR_NEEDMOREPARAMS, "Need more params");
|
||||
logs::report(log_error, "JOIN, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
if (msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
|
||||
if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
|
||||
{
|
||||
logs::report(log_error, "JOIN, invalid channel name '%s'", msg.getTokens()[1].c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
channel_it it;
|
||||
for (it = _channels.begin(); it != _channels.end(); ++it)
|
||||
for(it = _channels.begin(); it != _channels.end(); ++it)
|
||||
{
|
||||
if (msg.getTokens()[1] == it->getName())
|
||||
if(msg.getTokens()[1] == it->getName())
|
||||
break;
|
||||
}
|
||||
if (it == _channels.end())
|
||||
if(it == _channels.end())
|
||||
{
|
||||
_channels.push_back(Channel(msg.getTokens()[1]));
|
||||
_channels.back().addClient(client, true);
|
||||
logs::report(log_message, "channel '%s' has been created", msg.getTokens()[1].c_str());
|
||||
return ;
|
||||
}
|
||||
if ((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0))
|
||||
if((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0))
|
||||
client->sendCode(ERR_BADCHANNELKEY, "Invalid password");
|
||||
else if (it->getChannelSize() != -1 && it->getChannelSize() >= static_cast<int>(it->getNumberOfClients()))
|
||||
else if(it->getChannelSize() != -1 && it->getChannelSize() >= static_cast<int>(it->getNumberOfClients()))
|
||||
client->sendCode(ERR_CHANNELISFULL, "Channel is full");
|
||||
else if (it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName()))
|
||||
else if(it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName()))
|
||||
client->sendCode(ERR_INVITEONLYCHAN, "channel is invite only and you have not been invited u looser");
|
||||
else if (it->getPassword().size() == 0)
|
||||
else if(it->getPassword().size() == 0)
|
||||
it->addClient(client);
|
||||
else if (msg.getTokens().size() == 3 && it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword())
|
||||
else if(msg.getTokens().size() == 3 && it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword())
|
||||
it->addClient(client);
|
||||
}
|
||||
|
||||
void Server::handlePrivMsg(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getArgs().empty())
|
||||
if(msg.getArgs().empty())
|
||||
{
|
||||
client->sendCode(ERR_NORECIPIENT, "No recipient given\n");
|
||||
return;
|
||||
}
|
||||
if (msg.getTokens().size() < 3)
|
||||
if(msg.getTokens().size() < 3)
|
||||
{
|
||||
client->sendCode(ERR_NOTEXTTOSEND, "No text to send\n");
|
||||
return;
|
||||
}
|
||||
if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
|
||||
if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
|
||||
{
|
||||
for (client_it itc = _client.begin(); itc != _client.end(); ++itc)
|
||||
for(client_it itc = _client.begin(); itc != _client.end(); ++itc)
|
||||
{
|
||||
if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
|
||||
if((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
|
||||
{
|
||||
std::string complete_msg;
|
||||
if (msg.getTokens().size() > 2)
|
||||
if(msg.getTokens().size() > 2)
|
||||
{
|
||||
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
{
|
||||
complete_msg.append(*tit);
|
||||
complete_msg.append(" ");
|
||||
}
|
||||
complete_msg.erase(complete_msg.begin());
|
||||
}
|
||||
if (complete_msg.empty())
|
||||
if(complete_msg.empty())
|
||||
client->sendCode(ERR_NOTEXTTOSEND, "No text to send");
|
||||
(*itc)->sendMsg(client->getNickName(), "PRIVMSG " + (*itc)->getNickName(), complete_msg);
|
||||
break;
|
||||
@@ -242,19 +243,19 @@ namespace irc
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (channel_it it = _channels.begin(); it != _channels.end(); ++it)
|
||||
for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
|
||||
{
|
||||
if (msg.getTokens()[1] == it->getName())
|
||||
if(msg.getTokens()[1] == it->getName())
|
||||
{
|
||||
if (!it->hasClient(client))
|
||||
if(!it->hasClient(client))
|
||||
{
|
||||
client->sendCode(ERR_CANNOTSENDTOCHAN, "You are not in the channel");
|
||||
return ;
|
||||
}
|
||||
std::string complete_msg;
|
||||
if (msg.getTokens().size() > 2)
|
||||
if(msg.getTokens().size() > 2)
|
||||
{
|
||||
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
{
|
||||
complete_msg.append(*tit);
|
||||
complete_msg.append(" ");
|
||||
@@ -269,21 +270,21 @@ namespace irc
|
||||
|
||||
void Server::handleNotice(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getArgs().empty())
|
||||
if(msg.getArgs().empty())
|
||||
{
|
||||
logs::report(log_error, "NOTICE, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
|
||||
if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
|
||||
{
|
||||
for (client_it itc = _client.begin(); itc != _client.end(); ++itc)
|
||||
for(client_it itc = _client.begin(); itc != _client.end(); ++itc)
|
||||
{
|
||||
if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
|
||||
if((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
|
||||
{
|
||||
std::string complete_msg;
|
||||
if (msg.getTokens().size() > 2)
|
||||
if(msg.getTokens().size() > 2)
|
||||
{
|
||||
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
{
|
||||
complete_msg.append(*tit);
|
||||
complete_msg.append(" ");
|
||||
@@ -296,14 +297,14 @@ namespace irc
|
||||
}
|
||||
return ;
|
||||
}
|
||||
for (channel_it it = _channels.begin(); it != _channels.end(); ++it)
|
||||
for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
|
||||
{
|
||||
if (msg.getTokens()[1] == it->getName())
|
||||
if(msg.getTokens()[1] == it->getName())
|
||||
{
|
||||
std::string complete_msg;
|
||||
if (msg.getTokens().size() > 2)
|
||||
if(msg.getTokens().size() > 2)
|
||||
{
|
||||
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
|
||||
{
|
||||
complete_msg.append(*tit);
|
||||
complete_msg.append(" ");
|
||||
@@ -318,44 +319,44 @@ namespace irc
|
||||
|
||||
void Server::handleInvite(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getArgs().empty() || msg.getArgs().size() != 2)
|
||||
if(msg.getArgs().empty() || msg.getArgs().size() != 2)
|
||||
{
|
||||
logs::report(log_error, "INVITE, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isUserKnown(msg.getArgs()[0]))
|
||||
if(!isUserKnown(msg.getArgs()[0]))
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(msg.getArgs()[0]) + " no such nick");
|
||||
return;
|
||||
}
|
||||
if (!isChannelKnown(msg.getArgs()[1]))
|
||||
if(!isChannelKnown(msg.getArgs()[1]))
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, const_cast<std::string&>(msg.getArgs()[1]) + " no such channel");
|
||||
return;
|
||||
}
|
||||
|
||||
Channel* channel_target = getChannelByName(msg.getArgs()[1]);
|
||||
if (channel_target == NULL)
|
||||
if(channel_target == NULL)
|
||||
logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
|
||||
|
||||
unstd::SharedPtr<Client> client_target = getClientByName(msg.getArgs()[0]);
|
||||
if (client_target.get() == NULL)
|
||||
if(client_target.get() == NULL)
|
||||
logs::report(log_fatal_error, "(INVITE), cannot get client '%s' by name; panic !", msg.getArgs()[0].c_str());
|
||||
|
||||
if (!channel_target->hasClient(client))
|
||||
if(!channel_target->hasClient(client))
|
||||
{
|
||||
logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (channel_target->hasClient(client_target->getNickName()))
|
||||
if(channel_target->hasClient(client_target->getNickName()))
|
||||
{
|
||||
client->sendCode(ERR_USERONCHANNEL, msg.getArgs()[0] + ' ' + msg.getArgs()[1], "is already on channel");
|
||||
return;
|
||||
}
|
||||
|
||||
if (channel_target->isInviteOnly() && !channel_target->isOp(client))
|
||||
if(channel_target->isInviteOnly() && !channel_target->isOp(client))
|
||||
{
|
||||
client->sendCode(ERR_CHANOPRIVSNEEDED, msg.getArgs()[1], "you're not channel operator");
|
||||
return;
|
||||
@@ -368,7 +369,7 @@ namespace irc
|
||||
|
||||
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getArgs().empty())
|
||||
if(msg.getArgs().empty())
|
||||
{
|
||||
logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
@@ -378,45 +379,45 @@ namespace irc
|
||||
std::vector<std::string> channels = unstd::split(msg.getArgs()[0], ',');
|
||||
std::vector<std::string> users = unstd::split(msg.getArgs()[1], ',');
|
||||
|
||||
if (users.size() != channels.size())
|
||||
if(users.size() != channels.size())
|
||||
{
|
||||
client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters");
|
||||
return;
|
||||
}
|
||||
|
||||
for (name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
|
||||
for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
|
||||
{
|
||||
if (!isUserKnown(*user))
|
||||
if(!isUserKnown(*user))
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*user) + " no such nick");
|
||||
continue;
|
||||
}
|
||||
if (!isChannelKnown(*channel))
|
||||
if(!isChannelKnown(*channel))
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, const_cast<std::string&>(*channel) + " no such channel");
|
||||
continue;
|
||||
}
|
||||
|
||||
Channel* channel_target = getChannelByName(*channel);
|
||||
if (channel_target == NULL)
|
||||
if(channel_target == NULL)
|
||||
logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->c_str());
|
||||
unstd::SharedPtr<Client> client_target = getClientByName(*user);
|
||||
if (client_target.get() == NULL)
|
||||
if(client_target.get() == NULL)
|
||||
logs::report(log_fatal_error, "(KICK), cannot get client '%s' by name; panic !", user->c_str());
|
||||
|
||||
if (!channel_target->kick(client, client_target, msg.getReason()))
|
||||
if(!channel_target->kick(client, client_target, msg.getReason()))
|
||||
{
|
||||
logs::report(log_error, "could not kick %s because why not", user->c_str());
|
||||
continue;
|
||||
}
|
||||
client->printUserHeader();
|
||||
std::cout << "kicked " << *user << " from " << *channel << std::endl;
|
||||
if (channel_target->getNumberOfClients() == 0)
|
||||
if(channel_target->getNumberOfClients() == 0)
|
||||
{
|
||||
channel_it it;
|
||||
for (it = _channels.begin(); it < _channels.end(); ++it)
|
||||
for(it = _channels.begin(); it < _channels.end(); ++it)
|
||||
{
|
||||
if (it->getName() == msg.getArgs()[0])
|
||||
if(it->getName() == msg.getArgs()[0])
|
||||
break;
|
||||
}
|
||||
_channels.erase(it);
|
||||
@@ -430,34 +431,34 @@ namespace irc
|
||||
irc::Channel *chan;
|
||||
|
||||
logs::report(log_message, "tokensize ok : %d", msg.getTokens().size());
|
||||
if (msg.getTokens().size() != 2)
|
||||
if(msg.getTokens().size() != 2)
|
||||
return;
|
||||
if ((chan = getChannelByName(msg.getTokens()[1])) == NULL)
|
||||
if((chan = getChannelByName(msg.getTokens()[1])) == NULL)
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, "No such channel");
|
||||
chan->sendWho(client);
|
||||
}
|
||||
|
||||
void Server::handleTopic(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getArgs().empty())
|
||||
if(msg.getArgs().empty())
|
||||
{
|
||||
logs::report(log_error, "TOPIC, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
if (!isChannelKnown(msg.getArgs()[0]))
|
||||
if(!isChannelKnown(msg.getArgs()[0]))
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel");
|
||||
return;
|
||||
}
|
||||
Channel* channel = getChannelByName(msg.getArgs()[0]);
|
||||
if (channel == NULL)
|
||||
if(channel == NULL)
|
||||
logs::report(log_fatal_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str());
|
||||
if (!channel->hasClient(client))
|
||||
if(!channel->hasClient(client))
|
||||
{
|
||||
client->sendCode(ERR_NOTONCHANNEL, msg.getArgs()[0] + " you're not on that channel");
|
||||
return;
|
||||
}
|
||||
if (!msg.getReason().empty())
|
||||
if(!msg.getReason().empty())
|
||||
{
|
||||
channel->setTopic(client, msg.getReason());
|
||||
client->printUserHeader();
|
||||
@@ -469,13 +470,13 @@ namespace irc
|
||||
|
||||
void Server::handlePing(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
if (msg.getTokens().size() == 1)
|
||||
if(msg.getTokens().size() == 1)
|
||||
{
|
||||
logs::report(log_error, "PING, invalid command '%s'", msg.getRawMsg().c_str());
|
||||
return;
|
||||
}
|
||||
std::string out = "PONG";
|
||||
for (std::vector<std::string>::const_iterator it = msg.getTokens().begin() + 1; it < msg.getTokens().end(); ++it)
|
||||
for(std::vector<std::string>::const_iterator it = msg.getTokens().begin() + 1; it < msg.getTokens().end(); ++it)
|
||||
out += ' ' + *it;
|
||||
out += "\r\n";
|
||||
client->sendPlainText(out);
|
||||
@@ -486,12 +487,12 @@ namespace irc
|
||||
void Server::handleMode(unstd::SharedPtr<class Client> client, const Message& msg)
|
||||
{
|
||||
irc::Channel *chan;
|
||||
if (msg.getTokens().size() < 2)
|
||||
if(msg.getTokens().size() < 2)
|
||||
return ;
|
||||
if (msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&'))
|
||||
if(msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&'))
|
||||
{
|
||||
chan = getChannelByName(msg.getTokens()[1]);
|
||||
if (chan == NULL)
|
||||
if(chan == NULL)
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, "No such channel");
|
||||
else
|
||||
chan->showModes(client);
|
||||
@@ -499,17 +500,17 @@ namespace irc
|
||||
}
|
||||
logs::report(log_message, "Mode parsing ok");
|
||||
chan = getChannelByName(msg.getTokens()[1]);
|
||||
if (chan == NULL)
|
||||
if(chan == NULL)
|
||||
{
|
||||
client->sendCode(ERR_NOSUCHCHANNEL, "No such channel");
|
||||
return ;
|
||||
}
|
||||
if (!chan->isOp(client))
|
||||
if(!chan->isOp(client))
|
||||
{
|
||||
client->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *chan, "You need operator privileges to execute this command");
|
||||
return ;
|
||||
}
|
||||
if (msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+')
|
||||
if(msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+')
|
||||
return ;
|
||||
chan->changeMode(client, msg);
|
||||
}
|
||||
@@ -518,7 +519,7 @@ namespace irc
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
if (rand() % 6 == 0)
|
||||
if(rand() % 6 == 0)
|
||||
{
|
||||
client->kill("Pew Pew");
|
||||
usleep(100);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */
|
||||
/* Updated: 2024/01/30 02:40:04 by vvaas ### ########.fr */
|
||||
/* Updated: 2024/01/30 17:56:37 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
int main()
|
||||
{
|
||||
bot greg;
|
||||
|
||||
greg.init();
|
||||
greg.connect_to_server();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user