diff --git a/includes/channel.hpp b/includes/channel.hpp index a7054ac..b068b3e 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 23:04:45 by maldavid ### ########.fr */ +/* Updated: 2024/01/26 02:08:19 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -55,8 +55,8 @@ namespace irc void setTopic(unstd::SharedPtr client, const std::string& new_topic); void relayTopic(unstd::SharedPtr client); - void showModes() const; - + void showModesModify(unstd::SharedPtr client, const Message& msg) const; + void showModes(unstd::SharedPtr client); bool kick(unstd::SharedPtr op, unstd::SharedPtr target, const std::string& reason); ~Channel(); diff --git a/includes/client.hpp b/includes/client.hpp index 68a8b5c..62257ee 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 10:33:17 by maldavid #+# #+# */ -/* Updated: 2024/01/24 18:34:36 by vvaas ### ########.fr */ +/* Updated: 2024/01/26 00:58:20 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -52,6 +52,7 @@ namespace irc void sendPlainText(const std::string& str); void sendCode(const std::string& code, const std::string& msg); + void sendCode(const std::string& code, const std::string& msg0, const std::string& msg1); void sendMsg(const std::string& sender, const std::string& cmd, const std::string& trailing); void sendModular(std::string message, ...); diff --git a/includes/server.hpp b/includes/server.hpp index 579342c..05377e7 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */ -/* Updated: 2024/01/25 18:44:01 by vvaas ### ########.fr */ +/* Updated: 2024/01/26 00:42:56 by vvaas ### ########.fr */ /* */ /******************************************************************************/ diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 0c629f3..a186722 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 22:45:17 by vvaas ### ########.fr */ +/* Updated: 2024/01/26 02:10:11 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -21,7 +21,7 @@ namespace irc typedef std::set >::iterator client_it; typedef std::set >::const_iterator client_const_it; - Channel::Channel(const std::string& name) : _name(name), _channel_size(-1), _topic_op_restrict(false) {} + Channel::Channel(const std::string& name) : _name(name), _channel_size(-1), _invite_only(false), _topic_op_restrict(false) {} void Channel::addClient(unstd::SharedPtr client, bool op) { @@ -98,7 +98,22 @@ namespace irc } } - void Channel::showModes(void) const + void Channel::showModesModify(unstd::SharedPtr client, const Message& msg) const + { + std::vector mode = msg.getArgs(); + std::string modes; + std::string out = "MODE "; + + for (std::vector::iterator it = mode.begin() + 1; it != mode.end(); ++it) + { + modes += *it; + modes += " "; + } + out += getName(); + for(client_it it = _clients.begin(); it != _clients.end(); ++it) + const_cast&>(*it)->sendMsg(client->getNickName(), out, modes); + } + void Channel::showModes(unstd::SharedPtr client) { std::string modes = "+"; @@ -109,14 +124,9 @@ namespace irc if (_password.size() > 0) modes += 'k'; if (_channel_size != -1) - { modes += 'l'; - modes += " " + unstd::toString(_channel_size); - } - for(client_it it = _clients.begin(); it != _clients.end(); ++it) - const_cast&>(*it)->sendCode(RPL_CHANNELMODEIS, modes); + client->sendCode(RPL_CHANNELMODEIS, modes); } - void Channel::changeMode(unstd::SharedPtr client, const Message& msg) { bool modevalue = (msg.getTokens()[2][0] != '-'); @@ -159,7 +169,7 @@ namespace irc _channel_size = -1; } logs::report(log_message, "%s MODES : i:%d t:%d k:%s l:%d", getName().c_str(), _invite_only, _topic_op_restrict, _password.c_str(), _channel_size); - showModes(); + showModesModify(client, msg); } bool Channel::hasClient(std::string client) const diff --git a/srcs/client.cpp b/srcs/client.cpp index 299a16b..099e9cf 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 21:55:52 by maldavid ### ########.fr */ +/* Updated: 2024/01/26 02:26:04 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -28,7 +28,17 @@ namespace irc void Client::sendCode(const std::string& code, const std::string& msg) { - const std::string command = code + " : " + msg + "\r"; + const std::string command = ":yipirc " + code + " " + getNickName() + " :" + 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::sendCode(const std::string& code, const std::string& msg0, const std::string& msg1) + { + const std::string command = code + " " + msg0 + " :" + msg1 + "\r\n"; #ifdef DEBUG logs::report(log_message, "sending '%s'", command.c_str()); #endif diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index 9176fd9..4110896 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 23:01:42 by maldavid ### ########.fr */ +/* Updated: 2024/01/26 02:29:06 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -52,6 +52,8 @@ namespace irc client->printUserHeader(); client->setNewNickName(msg.getTokens()[1]); client->sendMsg(oldNick, "NICK", msg.getTokens()[1]); + if (client->isLogged()) + client->sendCode(RPL_WELCOME, "Welcome to yipiirc :)\n"); std::cout << "new nickname, " << client->getNickName() << std::endl; } @@ -78,7 +80,8 @@ namespace irc if(msg.getTokens()[1] == _password) { client->login(); - client->sendCode("Welcome to", "yipiirc :)\n"); + if (client->getNickName().size() != 0) + client->sendCode(RPL_WELCOME, "Welcome to yipiirc :)\n"); } else { @@ -395,14 +398,24 @@ namespace irc void Server::handleMode(unstd::SharedPtr client, const Message& msg) { logs::report(log_message, "Mode requested"); - if (msg.getTokens().size() < 3) + irc::Channel *chan; + if (msg.getTokens().size() < 2) return ; + if (msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&')) + { + chan = getChannelByName(msg.getTokens()[1]); + if (chan == NULL) + client->sendCode(ERR_NOSUCHCHANNEL, "No such channel"); + else + chan->showModes(client); + return ; + } logs::report(log_message, "Mode parsing ok"); channel_it it; for (it = _channels.begin(); it != _channels.end() && it->getName() != msg.getTokens()[1]; ++it); if (it == _channels.end()) client->sendCode(ERR_NOSUCHCHANNEL, "No such channel"); - if (!it->isOp(client)) + if (getChannelByName(msg.getTokens()[1]) && !it->isOp(client)) client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges to execute this command"); if (msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+') return ;