reconstructing code

This commit is contained in:
Bettercallous
2024-05-03 13:31:36 +01:00
parent 4a50dced99
commit 814a141980
11 changed files with 808 additions and 893 deletions

0
Authentication.cpp Normal file
View File

View File

@@ -1,5 +1,4 @@
#include "channel.hpp" #include "Channel.hpp"
Channel::Channel(){} Channel::Channel(){}
@@ -7,38 +6,29 @@ Channel::Channel(const std::string& name) : Channelname(name) {}
Channel::~Channel() {} Channel::~Channel() {}
void Channel::setlimitchannel(int value) {limit = value;}
void Channel::setlimitchannel(int value) int Channel::getChannelLimit() {return limit;}
{
limite = value;
}
int Channel::getlimitechannel() void Channel::setTopic(const std::string& newTopic) {topic = newTopic;}
{
return limite;
}
std::string Channel::getTopic() const {return topic;}
void Channel::setTopic(const std::string& newTopic) { void Channel::addClient(const std::string& client, int fd) {userFdMap[client] = fd;}
topic = newTopic;
}
std::string Channel::getTopic() const { void Channel::addClientinveted(const std::string& client, int fd) {invitedUsers[client] = fd;}
return topic;
} void Channel::addOperator(const std::string& operatorName, int fd) {operators[operatorName] = fd;}
void Channel::addClient(const std::string& client, int fd) { std::map<std::string, int>& Channel::getUserFdMap() {return userFdMap;}
userFdMap[client] = fd;
}
void Channel::addClientinveted(const std::string& client, int fd) { std::map<std::string, int>& Channel::invitedUserss() {return invitedUsers;}
invitedUsers[client] = fd;
}
void Channel::addOperator(const std::string& operatorName, int fd) { std::map<std::string, int>& Channel::getOperators() {return operators;}
operators[operatorName] = fd;
} void Channel::setPass(const std::string &Newpass) {pass = Newpass;}
std::string Channel::getPass() {return pass;}
int Channel::getUserFd(const std::string& username) const { int Channel::getUserFd(const std::string& username) const {
std::map<std::string, int>::const_iterator it = userFdMap.find(username); std::map<std::string, int>::const_iterator it = userFdMap.find(username);
@@ -48,7 +38,6 @@ int Channel::getUserFd(const std::string& username) const {
return -1; return -1;
} }
bool Channel::isUserInChannel(const std::string& nickname) const { bool Channel::isUserInChannel(const std::string& nickname) const {
std::map<std::string, int>::const_iterator it = userFdMap.find(nickname); std::map<std::string, int>::const_iterator it = userFdMap.find(nickname);
if (it != userFdMap.end() && it->second) { if (it != userFdMap.end() && it->second) {
@@ -78,43 +67,38 @@ std::string Channel::getNickname(int fd) const {
bool Channel::isOperator(int fd) { bool Channel::isOperator(int fd) {
for (std::map<std::string, int>::iterator it = operators.begin(); it != operators.end(); ++it) {
for (std::map<std::string, int>::iterator it = operators.begin(); it != operators.end(); ++it) {
if (it->second == fd) { if (it->second == fd) {
return true; return true;
} }
} }
return false; return false;
} }
bool Channel::isInvited(std::string nickname) { bool Channel::isInvited(std::string nickname) {
for (std::map<std::string, int>::iterator it = invitedUsers.begin(); it != invitedUsers.end(); ++it) { for (std::map<std::string, int>::iterator it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
if (it->first == nickname) { if (it->first == nickname) {
return true; return true;
} }
}
return false;
} }
return false;
}
int Channel::findUserFdForKickRegulars(const std::string& username) { int Channel::findUserFdForKickRegulars(const std::string& username) {
std::map<std::string, int>::iterator it; std::map<std::string, int>::iterator it;
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) { for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
if (it->first == username) { if (it->first == username) {
return it->second; return it->second;
} }
}
return -1;
} }
return -1;
}
void Channel::ejectUserfromusers(int fd) { void Channel::ejectUserfromusers(int fd) {
std::map<std::string, int>::iterator it; std::map<std::string, int>::iterator it;
for (it = userFdMap.begin(); it != userFdMap.end(); ++it) { for (it = userFdMap.begin(); it != userFdMap.end(); ++it) {
if (it->second == fd) { if (it->second == fd) {
userFdMap.erase(it); userFdMap.erase(it);
std::cout << "the user earased " << std::endl;
return; return;
} }
} }
@@ -125,7 +109,6 @@ void Channel::ejectUserfromivited(std::string nickname) {
for (it = invitedUsers.begin(); it != invitedUsers.end(); ++it) { for (it = invitedUsers.begin(); it != invitedUsers.end(); ++it) {
if (it->first == nickname) { if (it->first == nickname) {
invitedUsers.erase(it); invitedUsers.erase(it);
std::cout << "the user earased " << std::endl;
return; return;
} }
} }
@@ -151,26 +134,3 @@ void Channel::removeOperator(const std::string& operatorName )
} }
} }
} }
std::map<std::string, int>& Channel::getUserFdMap() {
return userFdMap;
}
std::map<std::string, int>& Channel::invitedUserss() {
return invitedUsers;
}
std::map<std::string, int>& Channel::getOperators() {
return operators;
}
void Channel::setPass(const std::string &Newpass)
{
pass = Newpass;
}
std::string Channel::getPass()
{
return pass;
}

View File

@@ -1,12 +1,11 @@
#ifndef CHANNEL_HPP #ifndef CHANNEL_HPP
#define CHANNEL_HPP #define CHANNEL_HPP
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <unistd.h> // Include for the send function #include <unistd.h>
#include <cstring> // Include for the strlen function #include <cstring>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <csignal> #include <csignal>
@@ -25,11 +24,10 @@ private:
std::string topic; std::string topic;
std::string key; std::string key;
std::string pass; std::string pass;
int limite; int limit;
std::map<std::string, int> userFdMap; std::map<std::string, int> userFdMap;
std::map<std::string, int> invitedUsers; std::map<std::string, int> invitedUsers;
std::map<std::string, int> operators; std::map<std::string, int> operators;
bool channelconnect;
public: public:
@@ -39,7 +37,7 @@ public:
void setTopic(const std::string& newTopic); void setTopic(const std::string& newTopic);
std::string getTopic() const; std::string getTopic() const;
void setlimitchannel(int value); void setlimitchannel(int value);
int getlimitechannel(); int getChannelLimit();
void addClient(const std::string& client, int fd); void addClient(const std::string& client, int fd);
@@ -66,7 +64,4 @@ public:
std::map<std::string, int>& getOperators(); std::map<std::string, int>& getOperators();
}; };
#endif #endif

View File

@@ -8,60 +8,30 @@ Client::~Client() {}
int Client::getFd() const {return _fd;} int Client::getFd() const {return _fd;}
void Client::setPassword(const std::string& password) { void Client::setPassword(const std::string& password) {pass = password;}
pass = password;
}
std::string Client::getPassowrd() const { std::string Client::getPassowrd() const {return pass;}
return pass;
}
std::string Client::getName() const { std::string Client::getName() const {return name;}
return name;
}
void Client::setName(const std::string& newName) { void Client::setName(const std::string& newName) {name = newName;}
name = newName;
}
std::string Client::getNick() const { std::string Client::getNick() const {return nick;}
return nick;
}
void Client::setNick(const std::string& newNick) { void Client::setNick(const std::string& newNick) {nick = newNick;}
nick = newNick;
}
std::string Client::getUser() const { std::string Client::getUser() const {return user;}
return user;
}
void Client::setUser(const std::string& newUser) { void Client::setUser(const std::string& newUser) {user = newUser;}
user = newUser;
}
int Client::getAuthentication() const { int Client::getAuthentication() const {return _authentication;}
return _authentication;
}
void Client::setAuthentication(int auth) { void Client::setAuthentication(int auth) {_authentication = auth;}
_authentication = auth;
}
const std::string& Client::getCommand() const {return command;}
const std::string& Client::getCommand() const { void Client::clearCommand() {command = "";}
return command;
}
void Client::clearCommand() { void Client::appendCommand(std::string str) {command += str;}
command.clear();
command = "";
}
void Client::appendCommand(std::string str) { void Client::setCommand(std::string cmd) {command = cmd;}
command += str;
}
void Client::setCommand(std::string cmd) {
command = cmd;
}

0
Helpers.cpp Normal file
View File

37
Macros.hpp Normal file
View File

@@ -0,0 +1,37 @@
#ifndef MACROS_HPP
#define MACROS_HPP
#define OO1 ":irc.l9oroch 001 " + nickname + " :Welcome to the MiniChat Network, " + nickname + '\n'
#define OO2 ":irc.l9oroch 002 " + nickname + " :Your host is MiniChat, running version 4.5" + '\n'
#define OO3 ":irc.l9oroch 003 " + nickname + " :This server was created " + formatCreationTime() + '\n'
#define OO4 ":irc.l9oroch 004 " + nickname + " MiniChat MiniChat(enterprise)-2.3(12)-netty(5.4c)-proxy(0.9) MiniChat" + '\n'
#define JOIN_MESSAGE(nickname, channelName) (":" + nickname + " JOIN #" + channelName + "\n")
#define MODE_MESSAGE(channelName) (":irc.l9oroch MODE #" + channelName + " +nt\n")
#define NAMES_MESSAGE(nickname, channelName) (":irc.l9oroch 353 " + nickname + " = #" + channelName + " :@" + nickname + "\n")
#define NAMES_MESSAGE2(nickname, channelName) (":irc.l9oroch 353 " + nickname + " @ #" + channelName + " :")
#define END_OF_NAMES_MESSAGE(nickname, channelName) (":irc.l9oroch 366 " + nickname + " #" + channelName + " :End of /NAMES list.\n")
#define CHANNEL_MESSAGE(channelName, creationTimeMessage) (":irc.l9oroch 354 " + channelName + " " + creationTimeMessage + "\n")
#define TOPIC_MESSAGE(nickname, channelName, topic) (":irc.l9oroch 332 " + nickname + " #" + channelName + " :" + topic + " https://irc.com\n")
#define PRIVATE_MESSAGE(senderFd, recipient, message) (":" + nicknames[senderFd] + " PRIVMSG " + recipient + " :" + message + "\r\n")
#define ERROR_MESSAGE(senderFd, recipient) (":server.host NOTICE " + nicknames[senderFd] + " :Error: User '" + recipient + "' not found or offline\r\n")
#define BUFFER_SIZE 1024
#define RED "\033[31m"
#define GREEN "\033[32m"
#define CYAN "\033[36m"
#define RESET "\033[0m"
#define MINICHAT "\n$$\\ $$\\ $$\\ $$\\ $$$$$$\\ $$\\ $$\\ \n" \
"$$$\\ $$$ |\\__| \\__|$$ __$$\\ $$ | $$ | \n" \
"$$$$\\ $$$$ |$$\\ $$$$$$$\\ $$\\ $$ / \\__|$$$$$$$\\ $$$$$$\\ $$$$$$\\ \n" \
"$$\\$$\\$$ $$ |$$ |$$ __$$\\ $$ |$$ | $$ __$$\\ \\____$$\\_$$ _| \n" \
"$$ \\$$$ $$ |$$ |$$ | $$ |$$ |$$ | $$ | $$ | $$$$$$$ | $$ | \n" \
"$$ |\\$ /$$ |$$ |$$ | $$ |$$ |$$ | $$\\ $$ | $$ |$$ __$$ | $$ |$$\\ \n" \
"$$ | \\_/ $$ |$$ |$$ | $$ |$$ |\\$$$$$$ |$$ | $$ |\\$$$$$$$ | \\$$$$ |\n" \
"\\__| \\__|\\__|\\__| \\__|\\__| \\______/ \\__| \\__| \\_______| \\____/ \n\n"
#endif

View File

@@ -1,6 +1,6 @@
NAME = ircserv NAME = ircserv
SRC = main.cpp Server.cpp Client.cpp channel.cpp SRC = main.cpp Server.cpp Client.cpp Channel.cpp
OBJ = ${SRC:.cpp=.o} OBJ = ${SRC:.cpp=.o}

0
Modes.cpp Normal file
View File

File diff suppressed because it is too large Load Diff

View File

@@ -16,18 +16,10 @@
#include "Client.hpp" #include "Client.hpp"
#include <cstring> #include <cstring>
#include <map> #include <map>
#include "channel.hpp" #include "Channel.hpp"
#include <ctime> #include <ctime>
#include <ctime> #include <ctime>
#include <iomanip> #include <iomanip>
#define BUFFER_SIZE 1024
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define RESET "\033[0m"
class Server { class Server {
private: private:
@@ -41,21 +33,25 @@ class Server {
std::map<int, std::string> usernames; std::map<int, std::string> usernames;
std::map<std::string, Channel> channels; std::map<std::string, Channel> channels;
public: public:
Server(); Server();
~Server(); ~Server();
void setNickname(int fd, const std::string& nickname);
std::string getPassowrd() const; std::string getPassowrd() const;
Client& getClientByFd(int fd);
void setNickname(int fd, const std::string& nickname);
void setUsernames(int fd, const std::string& username); void setUsernames(int fd, const std::string& username);
void setUsernameregular(int fd, const std::string& username);
std::string formatCreationTime(); std::string formatCreationTime();
std::string constructCreationTimeMessage(const std::string& channelName); std::string constructCreationTimeMessage(const std::string& channelName);
std::string constructJoinedTimeMessage(const std::string& channelName); std::string constructJoinedTimeMessage(const std::string& channelName);
void setUsernameregular(int fd, const std::string& username);
void createChannel(const std::string& channel, const std::string& nickname, int fd); void createChannel(const std::string& channel, const std::string& nickname, int fd);
void handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message); void handlePrivateMessage(int senderFd, const std::string& recipient, const std::string& message);
void handleInvitation(int senderFd, const std::string& recipient, std::string channelName); void handleInvitation(int senderFd, const std::string& recipient, std::string channelName);
void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg); void broadcastMessage(const std::string& channel, const std::string& senderNickname, const std::string& msg);
void smallbroadcastMessagefortheckick(std::string nicknamesender , const std::string& channelname, const std::string& usertokick, const std::string& reason); void smallbroadcastMessagefortheckick(std::string nicknamesender , const std::string& channelname, const std::string& usertokick, const std::string& reason);
void smallbroadcastMessageforjoin(std::string nicknamesender , const std::string& channelname); void smallbroadcastMessageforjoin(std::string nicknamesender , const std::string& channelname);
@@ -64,11 +60,12 @@ class Server {
int findUserFd1(const std::string& username); int findUserFd1(const std::string& username);
bool dontputthesamenick(const std::string& nickname); bool dontputthesamenick(const std::string& nickname);
bool dontputthesameusername(const std::string& username); bool dontputthesameusername(const std::string& username);
void parseArgs(int ac, char **av);
static void receiveSignal(int signum); // Server
void init(); void init();
void run(); void run();
void parseArgs(int ac, char **av);
static void receiveSignal(int signum);
void createServerSocket(); void createServerSocket();
void bindServerSocket(); void bindServerSocket();
void addPollfd(int fd); void addPollfd(int fd);
@@ -78,8 +75,32 @@ class Server {
void cleanChannel(int fd); void cleanChannel(int fd);
void closeFds(); void closeFds();
Client& getClientByFd(int fd); void addUserToChannel(const std::string& nickname, const std::string& channelName, int fd);
void sendJoinMsg(const std::string& nickname, const std::string& channelName, int fd);
void ping(const std::string& command, int fd);
// User authentication
void processPassword(Client& client, const std::string& command, int fd);
void processNickCmd(Client& client, const std::string& command, int fd);
void processUserCmd(Client& client, const std::string& command, int fd);
void welcome(const std::string& nickname, int fd);
// Commands
void processJoinCmd(Client& client, const std::string& command, int fd);
void processPrivmsgCmd(Client& client, const std::string& command, int fd);
void processQuitCmd(int fd);
void processKickCmd(Client& client, const std::string& command, int fd);
void processTopicCmd(Client& client, const std::string& command, int fd);
void processInviteCmd(Client& client, const std::string& command, int fd);
void processModeCmd(Client& client, const std::string& command, int fd);
void processBotCmd(Client& client, const std::string& command, int fd);
// Channel modes
void handleOpPrivilege(const std::string& nick, const std::string& channelName, const std::string& mode, int fd);
void handleTopicRestriction(const std::string& nick, const std::string& channelName, const std::string& mode, int fd);
void handleInviteOnly(const std::string& nick, const std::string& channelName, const std::string& mode, int fd);
void handleChannelKey(std::string& nick, const std::string& channelName, const std::string& mode, int fd);
void handleChannelLimit(const std::string& nick, const std::string& channelName, const std::string& mode, int fd);
}; };
int randomInRange(int min, int max); int randomInRange(int min, int max);

View File

@@ -1,4 +1,5 @@
#include "Server.hpp" #include "Server.hpp"
#include "Macros.hpp"
int main(int ac, char **av) int main(int ac, char **av)
{ {