From f221c92fec6e454a3503b6502f22694f29125596 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 May 2026 09:03:36 +0000 Subject: [PATCH] push_Channel_class --- Makefile | 1 + includes/Channel.hpp | 55 +++++++++++++ includes/Server.hpp | 23 ++++-- srcs/Channel.cpp | 189 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 262 insertions(+), 6 deletions(-) create mode 100644 includes/Channel.hpp create mode 100644 srcs/Channel.cpp diff --git a/Makefile b/Makefile index 702328c..6b7c32c 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ INCLUDES = -I includes SRCS = main.cpp \ srcs/Server.cpp \ + srcs/Channel.cpp \ srcs/ParseBuffer.cpp \ srcs/IrcParser.cpp \ srcs/IrcMessage.cpp \ diff --git a/includes/Channel.hpp b/includes/Channel.hpp new file mode 100644 index 0000000..2c7ec02 --- /dev/null +++ b/includes/Channel.hpp @@ -0,0 +1,55 @@ +#ifndef CHANNEL_HPP +# define CHANNEL_HPP + +# include +# include + +class Channel +{ + private: + std::string _name; + std::string _topic; + std::string _key; + std::vector _members; + std::vector _operators; + std::vector _invited; + bool _inviteOnly; + bool _topicRestricted; + size_t _userLimit; + + public: + Channel(void); + explicit Channel(const std::string &name); + Channel(const Channel &other); + Channel &operator=(const Channel &other); + ~Channel(void); + + const std::string &getName(void) const; + const std::string &getTopic(void) const; + const std::string &getKey(void) const; + size_t getUserLimit(void) const; + bool isInviteOnly(void) const; + bool isTopicRestricted(void) const; + + void setTopic(const std::string &topic); + void setKey(const std::string &key); + void setUserLimit(size_t limit); + void setInviteOnly(bool val); + void setTopicRestricted(bool val); + + bool hasMember(int fd) const; + bool isOperator(int fd) const; + bool isInvited(int fd) const; + + void addMember(int fd); + void removeMember(int fd); + void addOperator(int fd); + void removeOperator(int fd); + void addInvited(int fd); + void removeInvited(int fd); + + const std::vector &getMembers(void) const; + size_t memberCount(void) const; +}; + +#endif diff --git a/includes/Server.hpp b/includes/Server.hpp index e809446..bab68b8 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -15,6 +15,7 @@ # include # include # include "CommandValidator.hpp" +# include "Channel.hpp" struct ConnectedUser { @@ -39,18 +40,28 @@ class Server int _serverFd; int _port; std::string _password; - std::vector _fds; + std::vector _fds; std::map _users; + std::map _channels; void acceptNew(void); void handleRecv(int fd); void removeUser(int fd); void dispatch(int fd, const IrcMessage &msg); - void handlePass(int fd, const IrcMessage &msg); - void handleNick(int fd, const IrcMessage &msg); - void handleUser(int fd, const IrcMessage &msg); - void handlePing(int fd, const IrcMessage &msg); - void handleQuit(int fd, const IrcMessage &msg); + void broadcastToChannel(const std::string &chanName, const std::string &msg, int exceptFd); + + void handlePass(int fd, const IrcMessage &msg); + void handleNick(int fd, const IrcMessage &msg); + void handleUser(int fd, const IrcMessage &msg); + void handlePing(int fd, const IrcMessage &msg); + void handleQuit(int fd, const IrcMessage &msg); + void handleJoin(int fd, const IrcMessage &msg); + void handlePart(int fd, const IrcMessage &msg); + void handlePrivmsg(int fd, const IrcMessage &msg); + void handleKick(int fd, const IrcMessage &msg); + void handleInvite(int fd, const IrcMessage &msg); + void handleTopic(int fd, const IrcMessage &msg); + void handleMode(int fd, const IrcMessage &msg); public: Server(int port, const std::string &password); diff --git a/srcs/Channel.cpp b/srcs/Channel.cpp new file mode 100644 index 0000000..9f6c5bd --- /dev/null +++ b/srcs/Channel.cpp @@ -0,0 +1,189 @@ +#include "Channel.hpp" + + +Channel::Channel(void) +{ + _inviteOnly = false; + _topicRestricted = false; + _userLimit = 0; +} + +Channel::Channel(const std::string &name) +{ + _inviteOnly = false; + _topicRestricted = false; + _userLimit = 0; + _name = name; +} + +Channel::Channel(const Channel &other) +{ + *this = other; +} + +Channel &Channel::operator=(const Channel &other) +{ + if (this != &other) + { + _name = other._name; + _topic = other._topic; + _key = other._key; + _members = other._members; + _operators = other._operators; + _invited = other._invited; + _inviteOnly = other._inviteOnly; + _topicRestricted = other._topicRestricted; + _userLimit = other._userLimit; + } + return *this; +} + + +Channel::~Channel(void) +{} + +const std::string &Channel::getName(void) const +{ + return _name; +} + +const std::string &Channel::getTopic(void) const +{ + return _topic; +} + +const std::string &Channel::getKey(void) const +{ + return _key; +} + +size_t Channel::getUserLimit(void) const +{ + return _userLimit; +} + +bool Channel::isInviteOnly(void) const +{ + return _inviteOnly; +} + +bool Channel::isTopicRestricted(void) const +{ + return _topicRestricted; +} + +const std::vector &Channel::getMembers(void) const +{ + return _members; +} + +size_t Channel::memberCount(void) const +{ + return _members.size(); +} + +void Channel::setTopic(const std::string &topic) +{ + _topic = topic; +} + +void Channel::setKey(const std::string &key) +{ + _key = key; +} + +void Channel::setUserLimit(size_t limit) +{ + _userLimit = limit; +} + +void Channel::setInviteOnly(bool val) +{ + _inviteOnly = val; +} + +void Channel::setTopicRestricted(bool val) +{ + _topicRestricted = val; +} + +bool Channel::hasMember(int fd) const +{ + for (std::vector::const_iterator it = _members.begin(); it != _members.end(); ++it) + { + if(*it == fd) + return true; + } + return false; +} + +bool Channel::isOperator(int fd) const +{ + for (std::vector::const_iterator it = _operators.begin(); it != _operators.end(); ++it) + { + if(*it == fd) + return true; + } + return false; +} + +bool Channel::isInvited(int fd) const +{ + for (std::vector::const_iterator it = _invited.begin(); it != _invited.end(); ++it) + { + if(*it == fd) + return true; + } + return false; +} + +void Channel::addMember(int fd) +{ + _members.push_back(fd); +} + +void Channel::addOperator(int fd) +{ + _operators.push_back(fd); +} + +void Channel::addInvited(int fd) +{ + _invited.push_back(fd); +} + +void Channel::removeMember(int fd) +{ + for (std::vector::iterator it = _members.begin(); it != _members.end(); ++it) + { + if(*it == fd) + { + _members.erase(it); + break ; + } + } +} + +void Channel::removeOperator(int fd) +{ + for (std::vector::iterator it = _operators.begin(); it != _operators.end(); ++it) + { + if(*it == fd) + { + _operators.erase(it); + break ; + } + } +} + +void Channel::removeInvited(int fd) +{ + for (std::vector::iterator it = _invited.begin(); it != _invited.end(); ++it) + { + if(*it == fd) + { + _invited.erase(it); + break ; + } + } +} \ No newline at end of file