changed project structure

This commit is contained in:
Bettercallous
2024-05-03 16:25:20 +01:00
parent 814a141980
commit 1702674d75
26 changed files with 1240 additions and 1207 deletions

67
Includes/Channel.hpp Normal file
View File

@@ -0,0 +1,67 @@
#ifndef CHANNEL_HPP
#define CHANNEL_HPP
#include <sstream>
#include <algorithm>
#include <cctype>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <csignal>
#include <arpa/inet.h>
#include <fcntl.h>
#include <vector>
#include <poll.h>
#include "Client.hpp"
#include <cstring>
#include <map>
#include <string>
class Channel {
private:
std::string Channelname;
std::string topic;
std::string key;
std::string pass;
int limit;
std::map<std::string, int> userFdMap;
std::map<std::string, int> invitedUsers;
std::map<std::string, int> operators;
public:
Channel();
Channel(const std::string& name);
~Channel();
void setTopic(const std::string& newTopic);
std::string getTopic() const;
void setlimitchannel(int value);
int getChannelLimit();
void addClient(const std::string& client, int fd);
void addClientinveted(const std::string& client, int fd);
void addOperator(const std::string& operatorName, int fd);
int getUserFd(const std::string& username) const;
bool isUserInChannel(const std::string& nickname) const;
std::vector<std::string> getClients() const;
std::string getNickname(int fd) const;
bool isOperator(int fd);
bool isInvited(std::string nickname);
int findUserFdForKickRegulars(const std::string& username);
void ejectUserfromusers(int fd);
void ejectUserfromivited(std::string nickname);
std::string getOperatorNickname(int fd) const;
void removeOperator(const std::string& operatorName );
void setPass(const std::string &Newpass);
std::string getPass();
std::map<std::string, int>& getUserFdMap();
std::map<std::string, int>& invitedUserss();
std::map<std::string, int>& getOperators();
};
#endif

44
Includes/Client.hpp Normal file
View File

@@ -0,0 +1,44 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP
#include <iostream>
class Client {
private:
int _fd;
std::string pass;
std::string name;
std::string nick;
std::string user;
std::string command;
int _authentication;
public:
Client();
Client(int fd);
~Client();
int getFd() const;
std::string getPassowrd() const;
void setPassword(const std::string& password);
std::string getName() const;
void setName(const std::string& newName);
std::string getNick() const;
void setNick(const std::string& newNick);
std::string getUser() const;
void setUser(const std::string& newUser);
int getAuthentication() const;
void setAuthentication(int auth);
const std::string& getCommand() const;
void clearCommand();
void appendCommand(std::string str);
void setCommand(std::string cmd);
};
#endif

80
Includes/Macros.hpp Normal file
View File

@@ -0,0 +1,80 @@
#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 TOPIC_MESSAGE2(nicknamesender, channelname, topic) (":" + nicknamesender + " TOPIC #" + channelname + " :" + topic + "\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 INVITE_MESSAGE(senderFd, recipient, channelName) (":" + nicknames[senderFd] + " INVITE " + recipient + " :#" + channelName + "\r\n")
#define KICK_MESSAGE(nicknamesender, channelname, usertokick, reason) (":" + nicknamesender + " KICK #" + channelname + " " + usertokick + " :" + reason + "\n")
#define KICK_MESSAGE2(channelName, fd, userToKick, reason) (":" + channels[channelName].getNickname(fd) + " KICK #" + channelName + " " + userToKick + " :" + reason + "\n")
#define MODE_CHANGE_MESSAGE(channelname, mode, sender, receiver) \
(mode == "+t" ? MODE_CHANGE_T(channelname, sender) : \
(mode == "-t" ? MODE_CHANGE_MINUS_T(channelname, sender) : \
(mode == "+o" ? MODE_CHANGE_O(channelname, sender, mode, receiver) : \
(mode == "-o" ? MODE_CHANGE_MINUS_O(channelname, sender, mode, receiver) : \
(mode == "+i" ? MODE_CHANGE_IK(channelname, sender, mode) : \
(mode == "-i" ? MODE_CHANGE_IK(channelname, sender, mode) : \
(mode == "+k" ? MODE_CHANGE_IK(channelname, sender, mode) : \
(mode == "-k" ? MODE_CHANGE_IK(channelname, sender, mode) : \
(mode == "+l" ? MODE_CHANGE_L(channelname, sender, mode) : \
(mode == "-l" ? MODE_CHANGE_L(channelname, sender, mode) : ""))))))))))
#define MODE_CHANGE_T(channelname, sender) (MODE_CHANGE_MINUS_T(channelname, sender))
#define MODE_CHANGE_MINUS_T(channelname, sender) (":server.host MODE #" + channelname + " -t by " + sender + "\n")
#define MODE_CHANGE_O(channelname, sender, mode, receiver) (":server.host MODE #" + channelname + " " + mode + " by " + sender + " and set " + receiver + "as operator\n")
#define MODE_CHANGE_MINUS_O(channelname, sender, mode, receiver) (":server.host MODE #" + channelname + " " + mode + " by " + sender + " and unset " + receiver + "as operator\n")
#define MODE_CHANGE_IK(channelname, sender, mode) (":server.host MODE #" + channelname + " " + mode + " by " + sender + "\n")
#define MODE_CHANGE_L(channelname, sender, mode) (":server.host MODE #" + channelname + " " + mode + " by " + sender + "\n")
#define ERROR_MESSAGE2(nick) (":server.host NOTICE " + nick + what)
#define ERROR_MESSAGE3(sender, channelName, userToKick) (":" + sender + " PRIVMSG #" + channelName + " :Error: the user : " + userToKick + " is not found or offline.\r\n")
#define ERROR_MESSAGE4(sender, channelName, userToKick) (":" + sender + " PRIVMSG #" + channelName + " :Error1: You are not authorized to execute this command " + userToKick + "\r\n")
#define ERROR_MESSAGE5() (":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error: You are not authorized to execute this command " + "\r\n")
#define ERROR_MESSAGE6(sender, channelName) (":" + sender + " PRIVMSG #" + channelName + " :Error2: You are not authorized to execute this command\r\n")
#define ERROR_MESSAGE7(channelName, fd) (":" + channels[channelName].getNickname(fd) + " PRIVMSG #" + channelName + " :Error: You are not authorized to execute this command\r\n")
#define CONGRATS_MSG(guessed) (":server.host PRIVMSG #:Congratulations ,you have guessed the number : " + guessed + " correctly\n")
#define GUESS_AGAIN(random) (":server.host PRIVMSG #:Sorry ,the correct one is : " + random + "\n")
#define GUESS_ERROR() (":server.host PRIVMSG # :Error: Invalid range or guess\n")
#define MODE_SET_MESSAGE(channelName, mode, fd, nick) (":server.host MODE #" + channelName + " " + mode + " by " + channels[channelName].getNickname(fd) + " and set " + nick + "as operator\n")
#define MODE_UNSET_MESSAGE(channelName, mode, fd, nick) (":server.host MODE #" + channelName + " " + mode + " by " + channels[channelName].getNickname(fd) + " and unset " + nick + "as operator\n")
#define TOPIC_CHANGE_MESSAGE(channelName, mode, fd) (":server.host MODE #" + channelName + " " + mode + " by " + channels[channelName].getNickname(fd) + "\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"
extern int opperatorfd;
extern int issettop;
extern int isinvited;
extern int itHasPass;
extern int channelLimit;
extern int limitchannelforincrement;
extern int abaaba;
#endif

113
Includes/Server.hpp Normal file
View File

@@ -0,0 +1,113 @@
#ifndef SERVER_HPP
#define SERVER_HPP
#include <sstream>
#include <algorithm>
#include <cctype>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <csignal>
#include <arpa/inet.h>
#include <fcntl.h>
#include <vector>
#include <poll.h>
#include "Client.hpp"
#include <cstring>
#include <map>
#include "Channel.hpp"
#include <ctime>
#include <ctime>
#include <iomanip>
#include "Macros.hpp"
class Server {
private:
int _port;
int _serverSocketFd;
static bool _signal;
std::string _password;
std::vector<struct pollfd> _fds;
std::vector<Client> _clients;
std::map<int, std::string> nicknames;
std::map<int, std::string> usernames;
std::map<std::string, Channel> channels;
public:
Server();
~Server();
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 setUsernameregular(int fd, const std::string& username);
std::string formatCreationTime();
std::string constructCreationTimeMessage(const std::string& channelName);
std::string constructJoinedTimeMessage(const std::string& channelName);
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 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 smallBroadcastMsgForKick(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 smallbroadcastMessageforTopic(std::string nicknamesender, const std::string& channelname, std::string topic);
void smallbroadcastMOOD(std::string nicknamesender, const std::string& channelname, std::string mode, std::string receiver);
int findUserFd1(const std::string& username);
bool dontputthesamenick(const std::string& nickname);
bool dontputthesameusername(const std::string& username);
std::string intToString(int number);
std::string trim(const std::string& str);
bool startsWith(const std::string& str, const std::string& prefix);
int stringToInt(const std::string& str);
bool isValidPassword(const std::string& passwordLine);
int randomInRange(int min, int max);
// Server
void init();
void run();
void parseArgs(int ac, char **av);
static void receiveSignal(int signum);
void createServerSocket();
void bindServerSocket();
void addPollfd(int fd);
void handleClientConnection();
void handleClientData(int fd);
void clientCleanup(int fd);
void cleanChannel(int fd);
void closeFds();
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);
};
#endif