diff --git a/includes/channel.hpp b/includes/channel.hpp index 7c73b59..16ff26f 100644 --- a/includes/channel.hpp +++ b/includes/channel.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ -/* Updated: 2024/01/25 19:38:50 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 21:21:19 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -54,7 +54,8 @@ namespace irc inline bool isInviteOnly() const { return _invite_only; } void setTopic(unstd::SharedPtr client, const std::string& new_topic); - void showModes(void) const; + void relayTopic(unstd::SharedPtr client); + void showModes() const; bool kick(unstd::SharedPtr op, unstd::SharedPtr target, const std::string& reason); diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 700f36d..1bb0de5 100644 --- a/srcs/channel.cpp +++ b/srcs/channel.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */ -/* Updated: 2024/01/25 21:15:06 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 21:21:12 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -35,6 +35,7 @@ namespace irc for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendMsg(client->getNickName(), "JOIN", _name); } + void Channel::ModOperator(unstd::SharedPtr client, const std::string &clientname, bool mode) { client_it it; @@ -56,6 +57,7 @@ namespace irc const_cast&>(*it)->sendMsg(client->getNickName(), "MODE", out); } } + bool Channel::removeClient(unstd::SharedPtr client) { if (!_clients.erase(client)) @@ -109,6 +111,7 @@ namespace irc for(client_it it = _clients.begin(); it != _clients.end(); ++it) const_cast&>(*it)->sendCode(RPL_CHANNELMODEIS, modes); } + void Channel::changeMode(unstd::SharedPtr client, const Message& msg) { bool modevalue = (msg.getTokens()[2][0] != '-'); @@ -169,9 +172,19 @@ namespace irc if(_topic_op_restrict && !isOp(client)) { client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges"); - return ; + return; } _topic = new_topic; + relayTopic(client); + } + + void Channel::relayTopic(unstd::SharedPtr client) + { + if(!hasClient(client)) + return; + if(_topic.empty()) + return client->sendCode(RPL_NOTOPIC " " + _name, " no topic is set"); + return client->sendCode(RPL_TOPIC " " + _name, _topic); } bool Channel::isOp(unstd::SharedPtr client) const @@ -193,7 +206,7 @@ namespace irc } if(!hasClient(target)) { - op->sendCode(ERR_USERNOTINCHANNEL, const_cast(target->getNickName()) + std::string(" " + _name) + " they aren't on that channel"); + op->sendCode(ERR_USERNOTINCHANNEL, const_cast(target->getNickName()) + ' ' + _name + " they aren't on that channel"); return false; } if(!isOp(op)) diff --git a/srcs/client.cpp b/srcs/client.cpp index 53013aa..8759adb 100644 --- a/srcs/client.cpp +++ b/srcs/client.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */ -/* Updated: 2024/01/25 20:24:16 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 21:20:41 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -27,15 +27,19 @@ namespace irc void Client::sendCode(const std::string& code, const std::string& msg) { - const std::string command = code + " :" + msg; - logs::report(log_message, "C<-%s", command.c_str()); + const std::string command = code + " :" + msg + "\r\n"; +#ifdef DEBUG + logs::report(log_message, "sending '%s'", command.c_str()); +#endif if(send(_fd, command.c_str(), command.size(), 0) != static_cast(command.length())) logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str()); } void Client::sendPlainText(const std::string& str) { - logs::report(log_message,"<- S %s", str.c_str()); +#ifdef DEBUG + logs::report(log_message,"sending '%s'", str.c_str()); +#endif if(send(_fd, str.c_str(), str.length(), 0) != static_cast(str.length())) logs::report(log_error, "server failed to send a message to '%s' (:sadge:)", _username.c_str()); } @@ -44,6 +48,9 @@ namespace irc { const std::string out = ':' + sender + ' ' + cmd + " :" + trailing + "\r\n"; logs::report(log_message,"<- S %s", out.c_str()); +#ifdef DEBUG + logs::report(log_message,"sending '%s'", out.c_str()); +#endif if(send(_fd, out.c_str(), out.length(), 0) != static_cast(out.length())) logs::report(log_error, "server failed to send a message from '%s' to '%s' (:sadge:)", sender.c_str(), _username.c_str()); } diff --git a/srcs/message.cpp b/srcs/message.cpp index 9abfe7d..d080c14 100644 --- a/srcs/message.cpp +++ b/srcs/message.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */ -/* Updated: 2024/01/25 18:06:49 by maldavid ### ########.fr */ +/* Updated: 2024/01/25 20:30:51 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,6 +64,8 @@ namespace irc _args.push_back(*it); else { + if((*it)[0] == ':') + continue; _reason.append(*it); _reason.push_back(' '); } diff --git a/srcs/server.cpp b/srcs/server.cpp index fba9c98..29eca2c 100644 --- a/srcs/server.cpp +++ b/srcs/server.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ -/* Updated: 2024/01/25 18:40:10 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 21:20:52 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -76,7 +76,9 @@ namespace irc while(recv((*it)->getFD(), buffer, INPUT_SIZE, 0) > 0) // read() but for socket fd { (*it)->newMsgInFlight(buffer); - std::cout << buffer << std::endl; + #ifdef DEBUG + logs::report(log_message,"processing '%s'", buffer); + #endif while(handleMessage(*it)); std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining } diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index b208fb7..93584d9 100644 --- a/srcs/server_functions.cpp +++ b/srcs/server_functions.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */ -/* Updated: 2024/01/25 20:28:19 by vvaas ### ########.fr */ +/* Updated: 2024/01/25 21:21:04 by maldavid ### ########.fr */ /* */ /******************************************************************************/ @@ -293,14 +293,12 @@ namespace irc void Server::handleKick(unstd::SharedPtr client, const Message& msg) { - std::cout << "kick" << std::endl; if(msg.getArgs().empty()) { logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str()); return; } - std::cout << "kick" << std::endl; - + typedef std::vector::iterator name_it; std::vector channels = unstd::split(msg.getArgs()[0], ','); std::vector users = unstd::split(msg.getArgs()[1], ','); @@ -310,22 +308,19 @@ namespace irc client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters"); return; } - std::cout << "kick" << std::endl; + for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel) { - std::cout << *user << " " << *channel << std::endl; if(!isUserKnown(*user)) { client->sendCode(ERR_NOSUCHNICK, const_cast(*user) + " no such nick"); continue; } - std::cout << *user << " " << *channel << std::endl; if(!isChannelKnown(*channel)) { client->sendCode(ERR_NOSUCHCHANNEL, const_cast(*channel) + " no such channel"); continue; } - std::cout << *user << " " << *channel << std::endl; Channel* channel_target = getChannelByName(*channel); if(channel_target == NULL) @@ -357,17 +352,32 @@ namespace irc } void Server::handleTopic(unstd::SharedPtr client, const Message& msg) { - (void)client; if(msg.getArgs().empty()) { logs::report(log_error, "TOPIC, invalid command '%s'", msg.getRawMsg().c_str()); return; } - if(msg.getArgs()[0][0] != '#' && msg.getArgs()[0][0] != '&') + if(!isChannelKnown(msg.getArgs()[0])) { - logs::report(log_error, "TOPIC, invalid channel name '%s'", msg.getArgs()[0].c_str()); + client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel"); return; } + Channel* channel = getChannelByName(msg.getArgs()[0]); + if(channel == NULL) + logs::report(log_fatal_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str()); + if(!channel->hasClient(client)) + { + client->sendCode(ERR_NOTONCHANNEL, msg.getArgs()[0] + " you're not on that channel"); + return; + } + if(!msg.getReason().empty()) + { + channel->setTopic(client, msg.getReason()); + client->printUserHeader(); + std::cout << "changed topic to " << msg.getReason() << std::endl; + } + else + channel->relayTopic(client); } void Server::handlePing(unstd::SharedPtr client, const Message& msg)