From bed4006e907612e049fbc870dddc9266f24dd4e8 Mon Sep 17 00:00:00 2001 From: iherman- <200969603+iherman-p@users.noreply.github.com> Date: Sat, 23 May 2026 21:38:02 +0200 Subject: [PATCH] Added working INVITE command --- Channel/Channel.cpp | 5 ++++ Channel/Channel.hpp | 3 +- Makefile | 2 +- Server/Server.cpp | 3 +- Server/Server.hpp | 4 +-- cmds/invite.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++ cmds/mode.cpp | 8 ++--- 7 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 cmds/invite.cpp diff --git a/Channel/Channel.cpp b/Channel/Channel.cpp index 9c4c850..1f5fbd0 100644 --- a/Channel/Channel.cpp +++ b/Channel/Channel.cpp @@ -94,5 +94,10 @@ void Channel::setMode(std::string& mode, std::string& args) (void) args; } +void Channel::inviteMember(const User& client) +{ + invitedMembers_.insert(client.getFd()); +} + bool Channel::isInviteOnly() const { return isInviteOnly_; } bool Channel::isInvited(int fd) const { return invitedMembers_.count(fd) > 0; } diff --git a/Channel/Channel.hpp b/Channel/Channel.hpp index 6272cda..d021b69 100644 --- a/Channel/Channel.hpp +++ b/Channel/Channel.hpp @@ -53,13 +53,12 @@ class Channel void removeOperator(int fd); bool hasOperator(int fd) const; - void inviteMember(User& client); // not implemented - void broadcast(const std::string &msg, const std::map &clients, int excludedFd); // modes void setMode(std::string& mode, std::string& args); + void inviteMember(const User& client); // not implemented bool isInviteOnly() const; bool isInvited(int fd) const; diff --git a/Makefile b/Makefile index 26dcc6c..e0fc1df 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ SRC = main.cpp Server/Server.cpp User/User.cpp \ Channel/Channel.cpp \ cmds/pass.cpp cmds/nick.cpp cmds/user.cpp \ cmds/join.cpp cmds/privmsg.cpp cmds/quit.cpp \ - cmds/mode.cpp + cmds/mode.cpp cmds/invite.cpp HEADERS = Server/Server.hpp User/User.hpp diff --git a/Server/Server.cpp b/Server/Server.cpp index e17a54c..2732f20 100644 --- a/Server/Server.cpp +++ b/Server/Server.cpp @@ -6,7 +6,7 @@ /* By: iherman- > target >> channel; + + if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n")); + + if (target.empty() || channel.empty()) + { + return client.send(":" SERVER_NAME " 461 * INVITE :Not enough parameters\r\n"); + } + + // verify target exists + std::map::iterator client_it = clients_.begin(); + for (; client_it != clients_.end(); ++client_it) + { + if (client_it->second.getNick() == target) + break; + } + if (client_it == clients_.end()) + { + client.send(":" SERVER_NAME " 401 " + target + " :No such nick\r\n"); + return ; + } + + // verify channel exist & user is on channel + std::map::iterator channel_it = channels_.find(channel); + if (channel_it == channels_.end()) + { + client.send(":" SERVER_NAME " 401 " + target + " :No such channel\r\n"); + return ; + } + + if (!channel_it->second.hasMember(client.getFd())) + { + client.send(":" SERVER_NAME " 442 " + channel + " :You are not on that channel\r\n"); + return ; + } + + if (channel_it->second.hasMember(client_it->second.getFd())) + { + client.send(":" SERVER_NAME " 443 " + channel + " :is already on channel\r\n"); + return ; + } + + if (channel_it->second.isInviteOnly() && !channel_it->second.hasOperator(client.getFd())) + { + client.send(":" SERVER_NAME " 482 " + channel + ":You're not channel operator\r\n"); + return ; + } + + client_it->second.send("YOUVE BEEN INVITED YAAAAAAAYYYYY!!!!!!"); + channel_it->second.inviteMember(client_it->second); +} + diff --git a/cmds/mode.cpp b/cmds/mode.cpp index 0755477..dbb368a 100644 --- a/cmds/mode.cpp +++ b/cmds/mode.cpp @@ -6,7 +6,7 @@ /* By: iherman- > target >> mode; - std::getline(ss, args); + std::getline(ss, args); // might include space in channel name, need to fix if (!client.isRegistered()) return (client.send(":" SERVER_NAME " 451 * :You have not registered\r\n")); if (target.empty() || mode.empty()) return (client.send(":" SERVER_NAME " 461 " + client.getNick() + " MODE :Not enough parameters\r\n")); @@ -27,7 +27,7 @@ void Server::mode_cmd(User &client, std::istringstream &ss) std::map::iterator channel = channels_.find(target); if (channel == channels_.end()) { - client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel"); + client.send(":" SERVER_NAME " 403 " + client.getNick() + target + ":No such channel\r\n"); return ; } @@ -37,5 +37,5 @@ void Server::mode_cmd(User &client, std::istringstream &ss) return ; } - channel->second.setMode(mode, args); + channel->second.setMode(mode, args); // args should prob be a stringstream :( }