From 47ac8e2c670f928a46ec1e55dee11407321a06d1 Mon Sep 17 00:00:00 2001 From: Namonay Date: Tue, 6 Feb 2024 10:51:31 +0100 Subject: [PATCH] oh sa mere --- includes/client.hpp | 4 +- includes/errorscode.hpp | 6 +-- includes/server.hpp | 14 +++++-- srcs/channel.cpp | 10 ++--- srcs/client.cpp | 29 +++++++++++++- srcs/server.cpp | 17 +++++--- srcs/server_functions.cpp | 82 +++++++++++++++++++++------------------ 7 files changed, 103 insertions(+), 59 deletions(-) diff --git a/includes/client.hpp b/includes/client.hpp index f328f72..d4d9897 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/30 21:12:10 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 10:08:14 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -41,7 +41,6 @@ namespace irc inline void login() { _logged = true; } inline void register_user() { _registered = true; } - inline void welcome() { if (!isLogged() || !isRegistered() || isWelcomed() || _nickname.empty()) return ; _welcomed = true; sendCode(RPL_WELCOME, "Welcome to yipirc :), " + _nickname); } inline void requireDisconnect() { _disconnect_required = true; } inline bool isLogged() const { return _logged; } @@ -63,6 +62,7 @@ namespace irc void sendMsg(const std::string& sender, const std::string& cmd, const std::string& trailing); void sendModular(std::string message, ...); void sendCodeInChannel(const std::string& code, const class Channel &chan, const std::string& msg); + void welcome(const class Server& server); void kill(const std::string& reason); inline void inviteToChannel(const std::string& channel) { _invites.insert(channel); } inline bool hasBeenInvitedTo(const std::string& channel) { return _invites.erase(channel); } diff --git a/includes/errorscode.hpp b/includes/errorscode.hpp index be95a18..b112294 100644 --- a/includes/errorscode.hpp +++ b/includes/errorscode.hpp @@ -28,11 +28,11 @@ #define ERR_NOLOGIN "444" #define ERR_SUMMONDISABLED "445" #define ERR_USERSDISABLED "446" -#define ERR_NOTREGISTERED "451", "You have not registered" +#define ERR_NOTREGISTERED "451" #define ERR_NEEDMOREPARAMS "461" -#define ERR_ALREADYREGISTRED "462", "Unauthorized command (already registered)" +#define ERR_ALREADYREGISTRED "462" #define ERR_NOPERMFORHOST "463" -#define ERR_PASSWDMISMATCH "464", "Password incorrect\n" +#define ERR_PASSWDMISMATCH "464" #define ERR_YOUREBANNEDCREEP "465" #define ERR_YOUWILLBEBANNED "466" #define ERR_KEYSET "467" diff --git a/includes/server.hpp b/includes/server.hpp index ebbaf2d..e62f655 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/30 00:24:49 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 10:51:18 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -18,6 +18,7 @@ #include #include #include +#include namespace irc { @@ -36,6 +37,10 @@ namespace irc void wait(); + inline const std::string &getRunDate(void) const { return _run_date; }; + inline size_t getClientCount(void) const { return _client.size(); }; + inline size_t getChannelCount(void) const { return _channels.size(); }; + ~Server(); private: @@ -48,9 +53,9 @@ namespace irc void handleInput(); // ugly as f*ck - void handleNick(unstd::SharedPtr client, const class Message& msg); - void handleUser(unstd::SharedPtr client, const class Message& msg); - void handlePass(unstd::SharedPtr client, const class Message& msg); + void handleNick(unstd::SharedPtr client, const class Message& msg, const class Server& server); + void handleUser(unstd::SharedPtr client, const class Message& msg, const class Server& server); + void handlePass(unstd::SharedPtr client, const class Message& msg, const class Server& server); void handleQuit(unstd::SharedPtr client, const class Message& msg); void handlePart(unstd::SharedPtr client, const class Message& msg); void handleJoin(unstd::SharedPtr client, const class Message& msg); @@ -76,6 +81,7 @@ namespace irc sockaddr_in _s_data; socklen_t _s_len; fd_set _fd_set; + std::string _run_date; const std::string _password; const std::string _ip; int _port; diff --git a/srcs/channel.cpp b/srcs/channel.cpp index 30aae2c..582fa93 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/02/05 16:26:14 by maldavid ### ########.fr */ +/* Updated: 2024/02/06 10:33:44 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -56,7 +56,7 @@ namespace irc } } if(it == _clients.end()) - client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); + client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel"); return; } @@ -70,7 +70,7 @@ namespace irc } } if(it == _operators.end()) - client->sendCode(ERR_USERNOTINCHANNEL, "User not in channel"); + client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel"); } bool Channel::removeClient(unstd::SharedPtr client, const std::string& reason, bool quit) @@ -101,9 +101,9 @@ namespace irc clientlist += '@'; clientlist += const_cast&>(*it)->getNickName() + ' '; } - client->sendModular("%s\r\n", clientlist.c_str()); + client->sendModular("%s", clientlist.c_str()); clientlist = ":yipirc " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :"; - client->sendModular(":yipirc %s %s %s : End of names list\r\n", RPL_ENDOFNAMES, client->getNickName().c_str(), getName().c_str()); + client->sendModular(":yipirc %s %s %s : End of names list", RPL_ENDOFNAMES, client->getNickName().c_str(), getName().c_str()); } void Channel::handleMessage(const std::string& msg, unstd::SharedPtr client, bool notice) const diff --git a/srcs/client.cpp b/srcs/client.cpp index 288bef4..cd87a65 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/30 17:48:31 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 10:48:54 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -22,6 +22,8 @@ #include #include #include +#include +#include namespace irc { @@ -93,7 +95,7 @@ namespace irc std::vector tmp(len + 1); vsnprintf(&tmp[0], tmp.size(), message.c_str(), al_copy); buffer = std::string(&tmp[0], len); - buffer += "\r"; + buffer += "\r\n"; } va_end(al_copy); va_end(al); @@ -137,6 +139,29 @@ namespace irc return (std::string()); } + void Client::welcome(const Server& server) + { + char hostname[1024]; + int protocol; + socklen_t size = sizeof(protocol); + std::string tosend = "Your host is "; + getsockopt(_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &size); + protoent *proto = getprotobynumber(protocol); + if (!isLogged() || !isRegistered() || isWelcomed() || _nickname.empty()) + return ; + _welcomed = true; + gethostname(hostname, 1023); + tosend += hostname; + sendCode(RPL_YOURHOST, tosend); + tosend = "This server is running since "; + tosend += server.getRunDate(); + sendCode(RPL_CREATED, tosend); + sendModular("%s %s %s %s %s %s %s %s", ":yipirc", RPL_MYINFO, getNickName().c_str(), "YipIRC", "1.0", "o", "tikl", "kl"); + sendModular("There are %u users on %u channels", server.getClientCount(), server.getChannelCount()); + sendModular("You are connected using %s", proto->p_name); + sendCode(RPL_WELCOME, "Welcome to yipirc :), your nickname is : " + _nickname); + } + void Client::kill(const std::string& reason) { std::string cmd = "KILL " + getNickName(); diff --git a/srcs/server.cpp b/srcs/server.cpp index 44b71dc..a898f9b 100644 --- a/srcs/server.cpp +++ b/srcs/server.cpp @@ -6,7 +6,7 @@ /* By: vvaas +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ -/* Updated: 2024/02/05 16:14:13 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 10:22:58 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -19,6 +19,7 @@ #include #include #include +#include namespace irc { @@ -30,6 +31,12 @@ namespace irc Server::Server(int port, const std::string& password) : _s_len(sizeof(_s_data)), _password(password), _port(port), _main_socket(NULL_SOCKET), _active(true) { + time_t ltime = time(NULL); + struct tm tstruct = *localtime(<ime); + char buf[100]; + + strftime(buf, 100, "%a %b %d %Y %T CET", &tstruct); + _run_date = buf; if (password.empty() || password.find_first_of(" \t\r\v") != std::string::npos) { logs::report(log_error, "Password is invalid !"); @@ -153,12 +160,12 @@ namespace irc const Message msg(client, client->getNextMsg()); if(msg.getCmd() == "NICK") - handleNick(client, msg); + handleNick(client, msg, *this); else if(msg.getCmd() == "USER") - handleUser(client, msg); + handleUser(client, msg, *this); else if(msg.getCmd() == "PASS") - handlePass(client, msg); - else if(!client->isLogged()) + handlePass(client, msg, *this); + else if(!client->isWelcomed()) return true; else if(msg.getCmd() == "QUIT") handleQuit(client, msg); diff --git a/srcs/server_functions.cpp b/srcs/server_functions.cpp index ad86374..5abdbaa 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/02/05 12:24:42 by vvaas ### ########.fr */ +/* Updated: 2024/02/06 10:33:11 by vvaas ### ########.fr */ /* */ /******************************************************************************/ @@ -34,11 +34,11 @@ namespace irc typedef std::vector::iterator channel_it; typedef std::vector::const_iterator channel_const_it; - void Server::handleNick(unstd::SharedPtr client, const Message& msg) + void Server::handleNick(unstd::SharedPtr client, const Message& msg, const Server& server) { if(msg.getTokens().size() < 2) { - client->sendCode(ERR_NONICKNAMEGIVEN, "No nickname given"); + client->sendCode(ERR_NONICKNAMEGIVEN, " NICK : No nickname given"); return; } if(msg.getTokens().size() >= 3) @@ -48,10 +48,14 @@ namespace irc { if((*it)->getNickName() == nickname) { - client->setNewNickName(nickname); - client->sendCode(ERR_NICKCOLLISION, "Nickname is used"); - client->kill("Nickname already used"); - client->requireDisconnect(); + if (client->getNickName().empty()) + { + client->sendCode(ERR_NICKCOLLISION, " NICK : Nickname is used"); + client->kill(" NICK : Nickname already used"); + client->requireDisconnect(); + return ; + } + client->sendCode(ERR_NICKCOLLISION, "NICK : Nickname is used"); return; } } @@ -60,21 +64,21 @@ namespace irc client->setNewNickName(msg.getTokens()[1]); for(client_it it = _client.begin(); it != _client.end(); ++it) (*it)->sendMsg(oldNick, "NICK", msg.getTokens()[1]); - client->welcome(); + client->welcome(server); client->printUserHeader(); std::cout << "new nickname, " << client->getNickName() << std::endl; } - void Server::handleUser(unstd::SharedPtr client, const Message& msg) + void Server::handleUser(unstd::SharedPtr client, const Message& msg, const Server& server) { if(msg.getTokens().size() < 5) { - client->sendCode(ERR_NEEDMOREPARAMS, "Need more parameters"); + client->sendCode(ERR_NEEDMOREPARAMS, "USER : Need more parameters"); return; } if(client->isRegistered()) { - client->sendCode(ERR_ALREADYREGISTRED, "You are already registered"); + client->sendCode(ERR_ALREADYREGISTRED, "USER : You are already registered"); return ; } if(msg.getTokens()[4][0] != ':') @@ -96,21 +100,24 @@ namespace irc client->setNewRealName(realname); std::cout << "new realname, " << client->getRealName() << std::endl; client->register_user(); - client->welcome(); + client->welcome(server); } - void Server::handlePass(unstd::SharedPtr client, const Message& msg) + void Server::handlePass(unstd::SharedPtr client, const Message& msg, const Server& server) { if(client->isLogged()) + { + client->sendCode(ERR_ALREADYREGISTRED, "PASS : You are already registered"); return; + } if(msg.getTokens()[1] == _password) { client->login(); - client->welcome(); + client->welcome(server); } else { - client->sendCode(ERR_PASSWDMISMATCH); + client->sendCode(ERR_PASSWDMISMATCH, " PASS : Invalid password"); client->requireDisconnect(); } } @@ -127,7 +134,7 @@ namespace irc { if(msg.getTokens().size() < 2 && msg.getTokens().size() > 3) { - client->sendCode(ERR_NEEDMOREPARAMS, "Parameters amount invalid"); + client->sendCode(ERR_NEEDMOREPARAMS, "PART : Parameters amount invalid"); return; } if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&') @@ -142,7 +149,7 @@ namespace irc logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->getName().c_str()); if(!channel->removeClient(client, (msg.getReason().empty() ? "" : msg.getReason()))) { - client->sendCode(ERR_NOTONCHANNEL, "Not on channel"); + client->sendCode(ERR_NOTONCHANNEL, "PART : Not on channel"); return; } client->printUserHeader(); @@ -164,7 +171,7 @@ namespace irc { if(msg.getArgs().empty()) { - client->sendCode(ERR_NEEDMOREPARAMS, "Need more params"); + client->sendCode(ERR_NEEDMOREPARAMS, "JOIN : Need more params"); return; } if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&') @@ -186,9 +193,9 @@ namespace irc return ; } if((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0)) - client->sendCode(ERR_BADCHANNELKEY, "Invalid password"); + client->sendCode(ERR_BADCHANNELKEY, "JOIN : Invalid password"); else if(it->getChannelSize() != -1 && it->getChannelSize() <= static_cast(it->getNumberOfClients())) - client->sendCode(ERR_CHANNELISFULL, "Channel is full"); + client->sendCode(ERR_CHANNELISFULL, "JOIN : Channel is full"); else if(it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName())) client->sendCode(ERR_INVITEONLYCHAN, it->getName()); else if(it->getPassword().size() == 0) @@ -201,17 +208,17 @@ namespace irc { if(msg.getArgs().empty()) { - client->sendCode(ERR_NORECIPIENT, "No recipient given"); + client->sendCode(ERR_NORECIPIENT, "PRIVMSG : No recipient given"); return; } if(msg.getTokens().size() < 3) { - client->sendCode(ERR_NOTEXTTOSEND, "No text to send"); + client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send"); return; } if(msg.getReason().empty()) { - client->sendCode(ERR_NOTEXTTOSEND, "No text to send"); + client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send"); return ; } if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#') @@ -231,7 +238,7 @@ namespace irc complete_msg.erase(complete_msg.begin()); } if(complete_msg.empty()) - client->sendCode(ERR_NOTEXTTOSEND, "No text to send"); + client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send"); (*itc)->sendMsg(client->getNickName(), "PRIVMSG " + (*itc)->getNickName(), complete_msg); break; } @@ -244,7 +251,7 @@ namespace irc { if(!it->hasClient(client)) { - client->sendCode(ERR_CANNOTSENDTOCHAN, "You are not in the channel"); + client->sendCode(ERR_CANNOTSENDTOCHAN, "PRIVMSG : You are not in the channel"); return ; } std::string complete_msg; @@ -313,7 +320,7 @@ namespace irc { if(msg.getArgs().empty() || msg.getArgs().size() != 2) { - client->sendCode(ERR_NEEDMOREPARAMS, "Invalid parameters"); + client->sendCode(ERR_NEEDMOREPARAMS, "INVITE : Invalid parameters"); return; } @@ -350,7 +357,7 @@ namespace irc if(channel_target->isInviteOnly() && !channel_target->isOp(client)) { - client->sendCode(ERR_CHANOPRIVSNEEDED, msg.getArgs()[1], "you're not channel operator"); + client->sendCode(ERR_CHANOPRIVSNEEDED, msg.getArgs()[1], "INVITE : you're not channel operator"); return; } @@ -363,7 +370,7 @@ namespace irc { if(msg.getArgs().empty()) { - client->sendCode(ERR_NONICKNAMEGIVEN, "No nickname given"); + client->sendCode(ERR_NONICKNAMEGIVEN, "KICK : No nickname given"); return; } @@ -381,12 +388,12 @@ namespace irc { if(!isUserKnown(*user)) { - client->sendCode(ERR_NOSUCHNICK, const_cast(*user) + " no such nick"); + client->sendCode(ERR_NOSUCHNICK, const_cast(*user) + " : no such nick"); continue; } if(!isChannelKnown(*channel)) { - client->sendCode(ERR_NOSUCHCHANNEL, const_cast(*channel) + " no such channel"); + client->sendCode(ERR_NOSUCHCHANNEL, const_cast(*channel) + " : no such channel"); continue; } @@ -422,7 +429,7 @@ namespace irc if(msg.getTokens().size() != 2) return; if((chan = getChannelByName(msg.getTokens()[1])) == NULL) - client->sendCode(ERR_NOSUCHCHANNEL, "No such channel"); + client->sendCode(ERR_NOSUCHCHANNEL, "WHO : No such channel"); chan->sendWho(client); } @@ -430,7 +437,7 @@ namespace irc { if(msg.getArgs().empty()) { - client->sendCode(ERR_NEEDMOREPARAMS, "Need more parameters"); + client->sendCode(ERR_NEEDMOREPARAMS, "TOPIC : Need more parameters"); return; } if(!isChannelKnown(msg.getArgs()[0])) @@ -474,14 +481,14 @@ namespace irc irc::Channel *chan; if(msg.getTokens().size() < 2) { - client->sendCode(ERR_NEEDMOREPARAMS, "Need more parameters"); + client->sendCode(ERR_NEEDMOREPARAMS, "MODE : Need more parameters"); 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"); + client->sendCode(ERR_NOSUCHCHANNEL, "MODE : No such channel"); else chan->showModes(client); return ; @@ -489,12 +496,12 @@ namespace irc chan = getChannelByName(msg.getTokens()[1]); if(chan == NULL) { - client->sendCode(ERR_NOSUCHCHANNEL, "No such channel"); + client->sendCode(ERR_NOSUCHCHANNEL, "MODE : No such channel"); return ; } if(!chan->isOp(client)) { - client->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *chan, "You need operator privileges to execute this command"); + client->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *chan, "MODE : You need operator privileges to execute this command"); return ; } if(msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+') @@ -504,7 +511,6 @@ namespace irc } chan->changeMode(client, msg); } - void Server::handleRussian(unstd::SharedPtr client) { srand(time(NULL)); @@ -516,7 +522,7 @@ namespace irc client->requireDisconnect(); } else - client->sendModular("%s\n", "Click.."); + client->sendModular("%s", "Click.."); } }