fixed david

This commit is contained in:
2024-02-06 12:36:27 +01:00
parent e46bc4e602
commit 160c02fc75
23 changed files with 360 additions and 359 deletions

View File

@@ -6,7 +6,7 @@
# By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/08/09 15:08:49 by vavaas #+# #+# #
# Updated: 2024/01/30 02:40:31 by vvaas ### ########.fr #
# Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr #
# #
#******************************************************************************#

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* ansi.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 19:05:23 by maldavid #+# #+# */
/* Updated: 2024/01/20 19:09:18 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __ANSI__
#define __ANSI__

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */
/* Updated: 2024/02/06 11:37:58 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:33:17 by maldavid #+# #+# */
/* Updated: 2024/02/06 11:38:02 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 09:45:10 by maldavid #+# #+# */
/* Updated: 2024/02/06 11:38:01 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* irc.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vavaas <vavaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/29 13:24:01 by vavaas #+# #+# */
/* Updated: 2024/01/23 10:14:40 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* logs.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:15:24 by maldavid #+# #+# */
/* Updated: 2024/01/21 09:30:07 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __LOGS__
#define __LOGS__

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* message.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 11:35:21 by maldavid #+# #+# */
/* Updated: 2024/01/25 13:09:57 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __MESSAGE__
#define __MESSAGE__

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */
/* Updated: 2024/02/06 10:51:18 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:48 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -29,7 +29,7 @@ namespace irc
inline void closeMainSocket()
{
if(_main_socket > NULL_SOCKET)
if (_main_socket > NULL_SOCKET)
close(_main_socket);
_main_socket = NULL_SOCKET;
_active = false;

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* array.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 09:31:30 by maldavid #+# #+# */
/* Updated: 2024/01/20 19:12:36 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __UNSTD_ARRAY__
#define __UNSTD_ARRAY__
@@ -25,13 +25,13 @@ namespace unstd
TArray() {}
TArray(const TArray<T, N>& rhs)
{
for(std::size_t i = 0; i < N; i++)
for (std::size_t i = 0; i < N; i++)
_data[i] = rhs._data[i];
}
inline TArray& operator=(const TArray<T, N>& rhs)
{
for(std::size_t i = 0; i < N; i++)
for (std::size_t i = 0; i < N; i++)
_data[i] = rhs._data[i];
return *this;
}

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* ref_counter.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 12:54:09 by maldavid #+# #+# */
/* Updated: 2024/01/20 19:12:33 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __UNSTD_BITS_REFERENCE_COUNTER__
#define __UNSTD_BITS_REFERENCE_COUNTER__

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* shared_ptr.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 10:09:02 by maldavid #+# #+# */
/* Updated: 2024/02/06 11:59:45 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __UNSTD_SHARED_PTR__
#define __UNSTD_SHARED_PTR__

View File

@@ -21,14 +21,14 @@ namespace unstd
template <typename T>
SharedPtr<T>::SharedPtr(const SharedPtr<T>& rhs) : _ptr(rhs._ptr), _ref(rhs._ref)
{
if(_ptr && _ref)
if (_ptr && _ref)
++_ref->shared;
}
template <typename T>
SharedPtr<T>& SharedPtr<T>::operator=(const SharedPtr<T>& rhs)
{
if(_ptr == rhs._ptr)
if (_ptr == rhs._ptr)
return *this;
safeRelease();
@@ -96,10 +96,10 @@ namespace unstd
template <typename T>
void SharedPtr<T>::safeRelease()
{
if(_ref == NULL)
return;
if (_ref == NULL)
return ;
_ref->shared--;
if(_ref->shared <= 0)
if (_ref->shared <= 0)
{
delete _ptr;
_ptr = NULL;

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* string.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 19:13:26 by maldavid #+# #+# */
/* Updated: 2024/01/25 15:20:00 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#ifndef __UNSTD_STRING__
#define __UNSTD_STRING__

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
/* Updated: 2024/02/06 12:31:15 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -26,14 +26,14 @@ namespace irc
void Channel::addClient(unstd::SharedPtr<Client> client, bool op)
{
if(!_clients.insert(client).second)
if (!_clients.insert(client).second)
{
client->sendCode(ERR_USERONCHANNEL, "You are already in the channel");
return;
return ;
}
if(op)
if (op)
_operators.insert(client);
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "JOIN", _name);
sendWho(client);
}
@@ -41,52 +41,52 @@ namespace irc
void Channel::modOperator(unstd::SharedPtr<class Client> client, const std::string& clientname, bool mode)
{
client_it it;
if(mode)
if (mode)
{
for(it = _clients.begin(); it != _clients.end(); ++it)
for (it = _clients.begin(); it != _clients.end(); ++it)
{
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
{
if(isOp(*it))
return;
if (isOp(*it))
return ;
showModesModify(client, mode, 'o', clientname);
_operators.insert(const_cast<unstd::SharedPtr<irc::Client>&>(*it));
break;
}
}
if(it == _clients.end())
if (it == _clients.end())
client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel");
return;
return ;
}
for(it = _operators.begin(); it != _operators.end(); ++it)
for (it = _operators.begin(); it != _operators.end(); ++it)
{
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == clientname)
{
showModesModify(client, mode, 'o', clientname);
_operators.erase(it);
break;
}
}
if(it == _operators.end())
if (it == _operators.end())
client->sendCode(ERR_USERNOTINCHANNEL, "MODE : User not in channel");
}
bool Channel::removeClient(unstd::SharedPtr<Client> client, const std::string& reason, bool quit)
{
if(!_clients.erase(client))
if (!_clients.erase(client))
return (false);
if(isOp(client))
if (isOp(client))
_operators.erase(client);
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
{
if(!quit)
if (!quit)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "PART", _name + ' ' + reason);
else
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "QUIT", reason);
}
if(!quit)
if (!quit)
client->sendMsg(client->getNickName(), "PART", _name + ' ' + reason);
return true;
}
@@ -95,9 +95,9 @@ namespace irc
{
std::string clientlist(":YipIRC " RPL_NAMREPLY " " + client->getNickName() + " @ " + getName() + " :");
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
{
if(isOp(const_cast<unstd::SharedPtr<irc::Client>&>(*it)))
if (isOp(const_cast<unstd::SharedPtr<irc::Client>&>(*it)))
clientlist += '@';
clientlist += const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() + ' ';
}
@@ -109,9 +109,9 @@ namespace irc
void Channel::handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice) const
{
const std::string sender = client ? client->getNickName() : "";
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
{
if(client == *it)
if (client == *it)
continue;
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(sender, (notice ? "NOTICE " : "PRIVMSG ") + _name, msg);
}
@@ -125,14 +125,14 @@ namespace irc
modes += (modevalue) ? '+' : '-';
modes += flag;
if(flag == 'k')
if (flag == 'k')
modes += " " + _password;
if(flag == 'l')
if (flag == 'l')
modes += " " + unstd::toString(_channel_size);;
if(flag == 'o')
if (flag == 'o')
modes += " " + op;
out += getName();
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), out, modes);
}
@@ -140,18 +140,18 @@ namespace irc
{
std::string modes = _name + " +";
if(_invite_only)
if (_invite_only)
modes += 'i';
if(_topic_op_restrict)
if (_topic_op_restrict)
modes += 't';
if(_password.size() > 0)
if (_password.size() > 0)
modes += 'k';
if(_channel_size != -1)
if (_channel_size != -1)
modes += 'l';
if(modes.size() <= 2)
return;
if(modes.size() == _name.size() + 2)
return;
if (modes.size() <= 2)
return ;
if (modes.size() == _name.size() + 2)
return ;
client->sendCode(RPL_CHANNELMODEIS, modes);
}
@@ -162,48 +162,48 @@ namespace irc
unsigned long arg_nb = 3;
int arg_index = 2;
if(flags.find_first_not_of("itkol+-") != std::string::npos || flags.find_last_of("+-") != 0)
if (flags.find_first_not_of("itkol+-") != std::string::npos || flags.find_last_of("+-") != 0)
{
client->sendCode(ERR_UNKNOWNMODE, "MODE : Unknown mode");
return;
return ;
}
for(std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
for (std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
{
if(std::distance(flags.begin(), it) != static_cast<long>(flags.find_first_of(*it)))
if (std::distance(flags.begin(), it) != static_cast<long>(flags.find_first_of(*it)))
it = flags.erase(it) - 1;
}
if(flags.find('o') != std::string::npos)
if (flags.find('o') != std::string::npos)
arg_nb++;
if(flags.find('k') != std::string::npos && modevalue)
if (flags.find('k') != std::string::npos && modevalue)
arg_nb++;
if(flags.find('l') != std::string::npos && modevalue)
if (flags.find('l') != std::string::npos && modevalue)
arg_nb++;
if(msg.getTokens().size() < arg_nb)
if (msg.getTokens().size() < arg_nb)
{
client->sendCode(ERR_NEEDMOREPARAMS, "MODE : Need more params");
return;
return ;
}
for(std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
for (std::string::iterator it = flags.begin() + 1; it != flags.end(); ++it)
{
switch(*it)
{
case 'i':
if(_invite_only == modevalue)
if (_invite_only == modevalue)
break;
_invite_only = modevalue;
showModesModify(client, modevalue, 'i');
break;
case 't':
if(_topic_op_restrict == modevalue)
if (_topic_op_restrict == modevalue)
break;
_topic_op_restrict = modevalue;
showModesModify(client, modevalue, 't');
break;
case 'k':
if(modevalue)
if (modevalue)
{
if(msg.getArgs()[arg_index] == _password)
if (msg.getArgs()[arg_index] == _password)
{
arg_index++;
break;
@@ -214,8 +214,8 @@ namespace irc
}
else
{
if(_password.empty())
return;
if (_password.empty())
return ;
_password.clear();
logs::report(log_message, "password removed on %s", _name.c_str());
showModesModify(client, modevalue, 'k');
@@ -225,29 +225,29 @@ namespace irc
modOperator(client, msg.getArgs()[arg_index++], modevalue);
break;
case 'l':
if(!modevalue && _channel_size == -1)
return;
if(!modevalue)
if (!modevalue && _channel_size == -1)
return ;
if (!modevalue)
{
_channel_size = -1;
showModesModify(client, modevalue, 'l');
break;
}
if(modevalue)
if (modevalue)
{
char* end;
long tmp = std::strtol(msg.getArgs()[arg_index++].c_str(), &end, 10);
if(errno == ERANGE || *end != 0 || tmp < 0)
if (errno == ERANGE || *end != 0 || tmp < 0)
{
client->sendCode(ERR_UNKNOWNMODE, "MODE : Invalid channel size");
logs::report(log_error, "invalid channel size");
return;
return ;
}
if(tmp == _channel_size)
return;
if (tmp == _channel_size)
return ;
else
{
if(tmp == _channel_size)
if (tmp == _channel_size)
break;
_channel_size = tmp;
showModesModify(client, modevalue, 'l');
@@ -260,9 +260,9 @@ namespace irc
bool Channel::hasClient(std::string client) const
{
for(client_const_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_const_it it = _clients.begin(); it != _clients.end(); ++it)
{
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client)
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client)
return true;
}
return false;
@@ -270,10 +270,10 @@ namespace irc
void Channel::setTopic(unstd::SharedPtr<Client> client, const std::string& new_topic)
{
if(_topic_op_restrict && !isOp(client))
if (_topic_op_restrict && !isOp(client))
{
client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges");
return;
return ;
}
_topic = new_topic;
relayTopic(client, true);
@@ -281,26 +281,26 @@ namespace irc
void Channel::relayTopic(unstd::SharedPtr<Client> client, bool broadcast)
{
if(!hasClient(client))
return;
if(_topic.empty())
if (!hasClient(client))
return ;
if (_topic.empty())
{
if(!broadcast)
if (!broadcast)
return client->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set");
for(client_it it = _clients.begin(); it != _clients.end(); ++ it)
for (client_it it = _clients.begin(); it != _clients.end(); ++ it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_NOTOPIC, client->getNickName() + " " + _name, "no topic is set");
}
if(!broadcast)
if (!broadcast)
return client->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic);
for(client_it it = _clients.begin(); it != _clients.end(); ++ it)
for (client_it it = _clients.begin(); it != _clients.end(); ++ it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_TOPIC, client->getNickName() + " " + _name, _topic);
}
bool Channel::isOp(unstd::SharedPtr<Client> client) const
{
for(client_const_it it = _operators.begin(); it != _operators.end(); ++it)
for (client_const_it it = _operators.begin(); it != _operators.end(); ++it)
{
if(const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client->getNickName())
if (const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() == client->getNickName())
return true;
}
return false;
@@ -308,24 +308,24 @@ namespace irc
bool Channel::kick(unstd::SharedPtr<Client> op, unstd::SharedPtr<Client> target, const std::string& reason)
{
if(!hasClient(op))
if (!hasClient(op))
{
op->sendCode(ERR_NOTONCHANNEL, _name + " you're not on that channel");
return false;
}
if(!hasClient(target))
if (!hasClient(target))
{
op->sendCodeInChannel(ERR_USERNOTINCHANNEL, *this, "they aren't on that channel");
return false;
}
if(!isOp(op))
if (!isOp(op))
{
op->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *this, "you're not channel operator");
return false;
}
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(op->getNickName(), "KICK " + _name + ' ' + target->getNickName(), reason);
if(isOp(target))
if (isOp(target))
_operators.erase(target);
_clients.erase(target);
return true;

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */
/* Updated: 2024/02/06 12:31:53 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:00 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -35,7 +35,7 @@ namespace irc
#ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str());
#endif
if(send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
if (send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str());
}
@@ -45,7 +45,7 @@ namespace irc
#ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str());
#endif
if(send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
if (send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str());
}
@@ -55,7 +55,7 @@ namespace irc
#ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str());
#endif
if(send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
if (send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str());
}
@@ -64,7 +64,7 @@ namespace irc
#ifdef DEBUG
logs::report(log_message,"sending '%s'", str.c_str());
#endif
if(send(_fd, str.c_str(), str.length(), 0) != static_cast<ssize_t>(str.length()))
if (send(_fd, str.c_str(), str.length(), 0) != static_cast<ssize_t>(str.length()))
logs::report(log_error, "server failed to send a message to '%s' (:sadge:)", _username.c_str());
}
@@ -74,7 +74,7 @@ namespace irc
#ifdef DEBUG
logs::report(log_message,"sending '%s'", out.c_str());
#endif
if(send(_fd, out.c_str(), out.length(), 0) != static_cast<ssize_t>(out.length()))
if (send(_fd, out.c_str(), out.length(), 0) != static_cast<ssize_t>(out.length()))
logs::report(log_error, "server failed to send a message from '%s' to '%s' (:sadge:)", sender.c_str(), _username.c_str());
}
@@ -90,7 +90,7 @@ namespace irc
int len = vsnprintf(NULL, 0, message.c_str(), al);
if(len > 0)
if (len > 0)
{
std::vector<char> tmp(len + 1);
vsnprintf(&tmp[0], tmp.size(), message.c_str(), al_copy);
@@ -103,17 +103,17 @@ namespace irc
#ifdef DEBUG
logs::report(log_message,"sending '%s'", buffer.c_str());
#endif
if(send(_fd, buffer.c_str(), buffer.length(), 0) < static_cast<ssize_t>(buffer.length()))
if (send(_fd, buffer.c_str(), buffer.length(), 0) < static_cast<ssize_t>(buffer.length()))
logs::report(log_error, "server failed to send a message to '%s'", _nickname.c_str());
}
void Client::printUserHeader() const
{
std::cout << AnsiColor::green << "[User " << _id;
if(!_realname.empty())
if (!_realname.empty())
std::cout << " {realname " << _realname << '}';
if(!_username.empty())
if (!_username.empty())
std::cout << " {username " << _username << "}";
if(!_nickname.empty())
if (!_nickname.empty())
std::cout << " {nickname " << _nickname << "}";
std::cout << "] : " << AnsiColor::reset;
}
@@ -121,14 +121,14 @@ namespace irc
std::string Client::getNextMsg()
{
std::size_t finder = _msg_in_flight.find("\r\n");
if(finder != std::string::npos)
if (finder != std::string::npos)
{
std::string msg = _msg_in_flight.substr(0, finder);
_msg_in_flight = _msg_in_flight.substr(finder + 2);
return msg;
}
finder = _msg_in_flight.find("\n");
if(finder != std::string::npos)
if (finder != std::string::npos)
{
std::string msg = _msg_in_flight.substr(0, finder);
_msg_in_flight = _msg_in_flight.substr(finder + 1);
@@ -147,8 +147,8 @@ namespace irc
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;
if (!isLogged() || !isRegistered() || isWelcomed() || _nickname.empty())
return ;
_welcomed = true;
gethostname(hostname, 1023);
tosend += hostname;

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */
/* Updated: 2024/02/06 11:57:04 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */
/* Updated: 2024/02/05 16:23:31 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -22,13 +22,13 @@ namespace irc
void split(const std::string& s, std::vector<std::string>& elems)
{
std::string token;
for(std::string::const_iterator it = s.begin(); it != s.end();)
for (std::string::const_iterator it = s.begin(); it != s.end();)
{
if(std::isspace(*it))
if (std::isspace(*it))
{
elems.push_back(token);
token.clear();
while(std::isspace(*it) && it != s.end())
while (std::isspace(*it) && it != s.end())
it++;
}
else
@@ -38,18 +38,18 @@ namespace irc
}
}
elems.push_back(token);
for(std::vector<std::string>::iterator vit = elems.begin(); vit != elems.end();)
for (std::vector<std::string>::iterator vit = elems.begin(); vit != elems.end();)
{
bool isempty = true;
for(std::string::const_iterator it = vit->begin(); it != vit->end(); ++it)
for (std::string::const_iterator it = vit->begin(); it != vit->end(); ++it)
{
if(!std::isspace(*it))
if (!std::isspace(*it))
{
isempty = false;
break;
}
}
if(isempty)
if (isempty)
vit = elems.erase(vit);
else
vit++;
@@ -67,23 +67,23 @@ namespace irc
Message::Message(unstd::SharedPtr<class Client> client, const std::string& msg) : _raw_msg(msg), _client(client)
{
std::vector<std::string> tokens = details::split(msg);
if(tokens.empty())
return;
if (tokens.empty())
return ;
_command = tokens[0];
_tokens = tokens;
bool reason = false;
for(std::vector<std::string>::iterator it = tokens.begin() + 1; it < tokens.end(); ++it)
for (std::vector<std::string>::iterator it = tokens.begin() + 1; it < tokens.end(); ++it)
{
if((*it)[0] == ':')
if ((*it)[0] == ':')
reason = true;
if(!reason)
if (!reason)
_args.push_back(*it);
else
{
if((*it)[0] == ':')
if ((*it)[0] == ':')
it->erase(it->begin());
_reason.append(*it);
if(it != tokens.end() - 1)
if (it != tokens.end() - 1)
_reason.push_back(' ');
}
}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */
/* Updated: 2024/02/06 12:31:42 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -35,10 +35,10 @@ namespace irc
struct tm tstruct = *localtime(&ltime);
char buf[100];
if(password.empty() || password.find_first_of(" \t\r\v") != std::string::npos)
if (password.empty() || password.find_first_of(" \t\r\v") != std::string::npos)
{
logs::report(log_error, "Password is invalid !");
return;
return ;
}
std::memset(&_s_data, 0, sizeof(sockaddr));
initSocket();
@@ -52,7 +52,7 @@ namespace irc
_s_data.sin_addr.s_addr = INADDR_ANY;
_s_data.sin_port = htons(_port);
_main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP
if(_main_socket < 0)
if (_main_socket < 0)
logs::report(log_fatal_error, "socket error");
logs::report(log_message, "socket succesfully started");
}
@@ -62,14 +62,14 @@ namespace irc
int opt = 1;
initSocketData();
if(setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends
if (setsockopt(_main_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) // SOL_SOCKET : modify socket only, SO_REUSEADDR : Reusable after program ends
logs::report(log_fatal_error, "setsockopt() error (tout a pete)");
if(bind(_main_socket, reinterpret_cast<sockaddr*>(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost
if (bind(_main_socket, reinterpret_cast<sockaddr*>(&_s_data), sizeof(_s_data)) != 0) // Bind _main_socket to localhost
logs::report(log_fatal_error, "bind error");
logs::report(log_message, "bind successful, starting listen loop");
if(listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
if (listen(_main_socket, MAX_USERS) != 0) // init the listen with MAX_USERS
logs::report(log_fatal_error, "listen error");
logs::report(log_message, "listen queue created successfully");
@@ -80,27 +80,27 @@ namespace irc
{
char buffer[INPUT_SIZE] = { 0 };
ssize_t recv_size;
for(std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
for (std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
{
if(!FD_ISSET((*it)->getFD(), &_fd_set))
if (!FD_ISSET((*it)->getFD(), &_fd_set))
continue;
while((recv_size = recv((*it)->getFD(), buffer, INPUT_SIZE, 0)) > 0) // read() but forsocket fd
while ((recv_size = recv((*it)->getFD(), buffer, INPUT_SIZE, 0)) > 0) // read() but forsocket fd
{
(*it)->newMsgInFlight(buffer);
#ifdef DEBUG
logs::report(log_message,"processing '%s'", buffer);
#endif
while(handleMessage(*it));
while (handleMessage(*it));
std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining
}
if(recv_size == 0 || (*it)->disconnectRequired()) // recv return 0 ifan user disconnect
if (recv_size == 0 || (*it)->disconnectRequired()) // recv return 0 ifan user disconnect
{
logs::report(log_message, "User %d disconnected", (*it)->getID());
close((*it)->getFD());
for(channel_it cit = _channels.begin(); cit != _channels.end();)
for (channel_it cit = _channels.begin(); cit != _channels.end();)
{
cit->removeClient(*it, "", true);
if(cit->getNumberOfClients() == 0)
if (cit->getNumberOfClients() == 0)
{
logs::report(log_message, "channel '%s' has been destroyed", cit->getName().c_str());
cit = _channels.erase(cit);
@@ -121,27 +121,27 @@ namespace irc
int i = 0;
socklen_t len = sizeof(sockaddr_in);
if(_main_socket == NULL_SOCKET)
return;
while(_active)
if (_main_socket == NULL_SOCKET)
return ;
while (_active)
{
FD_ZERO(&_fd_set);
FD_SET(_main_socket, &_fd_set);
for(std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
for (std::vector<unstd::SharedPtr<Client> >::iterator it = _client.begin(); it != _client.end(); ++it)
FD_SET((*it)->getFD(), &_fd_set);
tmp = select(MAX_USERS, &_fd_set, NULL, NULL, NULL); // SELECT blocks till a connection or message is received, and let only those in _fd_set
if(tmp < 0 && _main_socket != NULL_SOCKET)
if (tmp < 0 && _main_socket != NULL_SOCKET)
logs::report(log_fatal_error, "select fd error");
if(FD_ISSET(_main_socket, &_fd_set)) // ifit's a new connection
if (FD_ISSET(_main_socket, &_fd_set)) // ifit's a new connection
{
sockaddr_in cli_sock;
fd = accept(_main_socket, reinterpret_cast<sockaddr*>(&cli_sock), &len); // adds the new connection
if(fd < 0)
if (fd < 0)
logs::report(log_fatal_error, "accept() error");
if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
logs::report(log_fatal_error, "fcntl() error");
unstd::SharedPtr<Client> new_client(new Client(fd, cli_sock, i++));
@@ -155,41 +155,41 @@ namespace irc
bool Server::handleMessage(unstd::SharedPtr<Client> client)
{
if(client->getMsgInFlight().empty()) // ifthere are no commands just return
if (client->getMsgInFlight().empty()) // ifthere are no commands just return
return false;
const Message msg(client, client->getNextMsg());
if(msg.getCmd() == "NICK")
if (msg.getCmd() == "NICK")
handleNick(client, msg, *this);
else if(msg.getCmd() == "USER")
else if (msg.getCmd() == "USER")
handleUser(client, msg, *this);
else if(msg.getCmd() == "PASS")
else if (msg.getCmd() == "PASS")
handlePass(client, msg, *this);
else if(!client->isWelcomed())
else if (!client->isWelcomed())
return true;
else if(msg.getCmd() == "QUIT")
else if (msg.getCmd() == "QUIT")
handleQuit(client, msg);
else if(msg.getCmd() == "WHO")
else if (msg.getCmd() == "WHO")
handleWho(client, msg);
else if(msg.getCmd() == "PART")
else if (msg.getCmd() == "PART")
handlePart(client, msg);
else if(msg.getCmd() == "JOIN")
else if (msg.getCmd() == "JOIN")
handleJoin(client, msg);
else if(msg.getCmd() == "INVITE")
else if (msg.getCmd() == "INVITE")
handleInvite(client, msg);
else if(msg.getCmd() == "PRIVMSG")
else if (msg.getCmd() == "PRIVMSG")
handlePrivMsg(client, msg);
else if(msg.getCmd() == "NOTICE")
else if (msg.getCmd() == "NOTICE")
handleNotice(client, msg);
else if(msg.getCmd() == "KICK")
else if (msg.getCmd() == "KICK")
handleKick(client, msg);
else if(msg.getCmd() == "TOPIC")
else if (msg.getCmd() == "TOPIC")
handleTopic(client, msg);
else if(msg.getCmd() == "PING")
else if (msg.getCmd() == "PING")
handlePing(client, msg);
else if(msg.getCmd() == "MODE")
else if (msg.getCmd() == "MODE")
handleMode(client, msg);
else if(msg.getCmd() == "imfeelinglucky")
else if (msg.getCmd() == "imfeelinglucky")
handleRussian(client);
else
client->sendCode(ERR_UNKNOWNCOMMAND, "No such command");
@@ -198,9 +198,9 @@ namespace irc
bool Server::isUserKnown(const std::string& user) const
{
for(client_const_it it = _client.begin(); it < _client.end(); ++it)
for (client_const_it it = _client.begin(); it < _client.end(); ++it)
{
if(const_cast<unstd::SharedPtr<Client>&>(*it)->getNickName() == user)
if (const_cast<unstd::SharedPtr<Client>&>(*it)->getNickName() == user)
return true;
}
return false;
@@ -208,9 +208,9 @@ namespace irc
bool Server::isChannelKnown(const std::string& channel) const
{
for(channel_const_it it = _channels.begin(); it < _channels.end(); ++it)
for (channel_const_it it = _channels.begin(); it < _channels.end(); ++it)
{
if(it->getName() == channel)
if (it->getName() == channel)
return true;
}
return false;
@@ -218,9 +218,9 @@ namespace irc
Channel* Server::getChannelByName(const std::string& name)
{
for(channel_it it = _channels.begin(); it < _channels.end(); ++it)
for (channel_it it = _channels.begin(); it < _channels.end(); ++it)
{
if(it->getName() == name)
if (it->getName() == name)
return &*it;
}
return NULL;
@@ -228,9 +228,9 @@ namespace irc
unstd::SharedPtr<Client> Server::getClientByName(const std::string& name)
{
for(client_it it = _client.begin(); it < _client.end(); ++it)
for (client_it it = _client.begin(); it < _client.end(); ++it)
{
if((*it)->getNickName() == name)
if ((*it)->getNickName() == name)
return *it;
}
return unstd::SharedPtr<Client>(NULL);
@@ -238,13 +238,13 @@ namespace irc
Server::~Server()
{
for(client_it it = _client.begin(); it != _client.end(); ++it)
for (client_it it = _client.begin(); it != _client.end(); ++it)
{
if((*it)->isWelcomed())
if ((*it)->isWelcomed())
(*it)->kill("Server shutting down");
}
closeMainSocket();
for(int i = 3; i <= FD_MAX; ++i)
for (int i = 3; i <= FD_MAX; ++i)
close(i);
}
}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */
/* Updated: 2024/02/06 12:30:27 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -36,33 +36,33 @@ namespace irc
void Server::handleNick(unstd::SharedPtr<class Client> client, const Message& msg, const Server& server)
{
if(msg.getTokens().size() < 2)
if (msg.getTokens().size() < 2)
{
client->sendCode(ERR_NONICKNAMEGIVEN, " NICK : No nickname given");
return;
return ;
}
if(msg.getTokens().size() >= 3)
return;
if (msg.getTokens().size() >= 3)
return ;
const std::string& nickname = msg.getTokens()[1];
for(client_it it = _client.begin(); it != _client.end(); ++it)
for (client_it it = _client.begin(); it != _client.end(); ++it)
{
if((*it)->getNickName() == nickname)
if ((*it)->getNickName() == nickname)
{
if(client->getNickName().empty())
if (client->getNickName().empty())
{
client->sendCode(ERR_NICKCOLLISION, " NICK : Nickname is used");
client->kill(" NICK : Nickname already used");
client->requireDisconnect();
return;
return ;
}
client->sendCode(ERR_NICKCOLLISION, "NICK : Nickname is used");
return;
return ;
}
}
std::string oldNick = (client->getNickName().size() > 0) ? client->getNickName() : msg.getTokens()[1]; // get nickname before /nick execution inside server (oldNick)
client->printUserHeader();
client->setNewNickName(msg.getTokens()[1]);
for(client_it it = _client.begin(); it != _client.end(); ++it)
for (client_it it = _client.begin(); it != _client.end(); ++it)
(*it)->sendMsg(oldNick, "NICK", msg.getTokens()[1]);
client->welcome(server);
client->printUserHeader();
@@ -71,18 +71,18 @@ namespace irc
void Server::handleUser(unstd::SharedPtr<class Client> client, const Message& msg, const Server& server)
{
if(msg.getTokens().size() < 5)
if (msg.getTokens().size() < 5)
{
client->sendCode(ERR_NEEDMOREPARAMS, "USER : Need more parameters");
return;
return ;
}
if(client->isRegistered())
if (client->isRegistered())
{
client->sendCode(ERR_ALREADYREGISTRED, "USER : You are already registered");
return;
return ;
}
if(msg.getTokens()[4][0] != ':')
return;
if (msg.getTokens()[4][0] != ':')
return ;
client->printUserHeader();
client->setNewUserName(msg.getTokens()[1]);
std::cout << "new username, " << client->getUserName() << std::endl;
@@ -90,7 +90,7 @@ namespace irc
client->printUserHeader();
std::string realname;
for(std::vector<std::string>::const_iterator it = msg.getTokens().begin() + 4; it != msg.getTokens().end(); ++it)
for (std::vector<std::string>::const_iterator it = msg.getTokens().begin() + 4; it != msg.getTokens().end(); ++it)
{
realname.append(*it);
realname.append(" ");
@@ -105,12 +105,12 @@ namespace irc
void Server::handlePass(unstd::SharedPtr<class Client> client, const Message& msg, const Server& server)
{
if(client->isLogged())
if (client->isLogged())
{
client->sendCode(ERR_ALREADYREGISTRED, "PASS : You are already registered");
return;
return ;
}
if(msg.getTokens()[1] == _password)
if (msg.getTokens()[1] == _password)
{
client->login();
client->welcome(server);
@@ -124,7 +124,7 @@ namespace irc
void Server::handleQuit(unstd::SharedPtr<class Client> client, const Message& msg)
{
for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
for (channel_it it = _channels.begin(); it != _channels.end(); ++it)
it->removeClient(client, (msg.getReason().empty() ? "Leaving" : msg.getReason()), true);
client->printUserHeader();
std::cout << "quit" << std::endl;
@@ -132,34 +132,34 @@ namespace irc
void Server::handlePart(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() < 2 && msg.getTokens().size() > 3)
if (msg.getTokens().size() < 2 && msg.getTokens().size() > 3)
{
client->sendCode(ERR_NEEDMOREPARAMS, "PART : Parameters amount invalid");
return;
return ;
}
if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
return;
if(!isChannelKnown(msg.getArgs()[0]))
if (msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
return ;
if (!isChannelKnown(msg.getArgs()[0]))
{
client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel");
return;
return ;
}
Channel* channel = getChannelByName(msg.getArgs()[0]);
if(channel == NULL)
if (channel == NULL)
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())))
if (!channel->removeClient(client, (msg.getReason().empty() ? "" : msg.getReason())))
{
client->sendCode(ERR_NOTONCHANNEL, "PART : Not on channel");
return;
return ;
}
client->printUserHeader();
std::cout << "leaving channel, " << msg.getArgs()[0] << std::endl;
if(channel->getNumberOfClients() == 0)
if (channel->getNumberOfClients() == 0)
{
channel_it it;
for(it = _channels.begin(); it < _channels.end(); ++it)
for (it = _channels.begin(); it < _channels.end(); ++it)
{
if(it->getName() == msg.getArgs()[0])
if (it->getName() == msg.getArgs()[0])
break;
}
_channels.erase(it);
@@ -169,95 +169,95 @@ namespace irc
void Server::handleJoin(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getArgs().empty())
if (msg.getArgs().empty())
{
client->sendCode(ERR_NEEDMOREPARAMS, "JOIN : Need more params");
return;
return ;
}
if(msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
if (msg.getTokens()[1][0] != '#' && msg.getTokens()[1][0] != '&')
{
client->sendCode(ERR_NOSUCHCHANNEL, ": " + msg.getTokens()[1], "No such channel");
return;
return ;
}
channel_it it;
for(it = _channels.begin(); it != _channels.end(); ++it)
for (it = _channels.begin(); it != _channels.end(); ++it)
{
if(msg.getTokens()[1] == it->getName())
if (msg.getTokens()[1] == it->getName())
break;
}
if(it == _channels.end())
if (it == _channels.end())
{
_channels.push_back(Channel(msg.getTokens()[1]));
_channels.back().addClient(client, true);
logs::report(log_message, "channel '%s' has been created", msg.getTokens()[1].c_str());
return;
return ;
}
if((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0))
if ((msg.getTokens().size() == 3 && msg.getTokens()[2] != it->getPassword()) || (msg.getTokens().size() == 2 && it->getPassword().size() > 0))
client->sendCode(ERR_BADCHANNELKEY, "JOIN : Invalid password");
else if(it->getChannelSize() != -1 && it->getChannelSize() <= static_cast<int>(it->getNumberOfClients()))
else if (it->getChannelSize() != -1 && it->getChannelSize() <= static_cast<int>(it->getNumberOfClients()))
client->sendCode(ERR_CHANNELISFULL, "JOIN : Channel is full");
else if(it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName()))
else if (it->isInviteOnly() && !client->hasBeenInvitedTo(it->getName()))
client->sendCode(ERR_INVITEONLYCHAN, it->getName());
else if(it->getPassword().size() == 0)
else if (it->getPassword().size() == 0)
it->addClient(client);
else if(msg.getTokens().size() == 3 && it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword())
else if (msg.getTokens().size() == 3 && it->getPassword().size() > 0 && msg.getTokens()[2] == it->getPassword())
it->addClient(client);
}
void Server::handlePrivMsg(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getArgs().empty())
if (msg.getArgs().empty())
{
client->sendCode(ERR_NORECIPIENT, "PRIVMSG : No recipient given");
return;
return ;
}
if(msg.getTokens().size() < 3)
if (msg.getTokens().size() < 3)
{
client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send");
return;
return ;
}
if(msg.getReason().empty())
if (msg.getReason().empty())
{
client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send");
return;
return ;
}
if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
{
for(client_it itc = _client.begin(); itc != _client.end(); ++itc)
for (client_it itc = _client.begin(); itc != _client.end(); ++itc)
{
if((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
{
std::string complete_msg;
if(msg.getTokens().size() > 2)
if (msg.getTokens().size() > 2)
{
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
{
complete_msg.append(*tit);
complete_msg.append(" ");
}
complete_msg.erase(complete_msg.begin());
}
if(complete_msg.empty())
if (complete_msg.empty())
client->sendCode(ERR_NOTEXTTOSEND, "PRIVMSG : No text to send");
(*itc)->sendMsg(client->getNickName(), "PRIVMSG " + (*itc)->getNickName(), complete_msg);
break;
}
}
return;
return ;
}
for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
for (channel_it it = _channels.begin(); it != _channels.end(); ++it)
{
if(msg.getTokens()[1] == it->getName())
if (msg.getTokens()[1] == it->getName())
{
if(!it->hasClient(client))
if (!it->hasClient(client))
{
client->sendCode(ERR_CANNOTSENDTOCHAN, "PRIVMSG : You are not in the channel");
return;
return ;
}
std::string complete_msg;
if(msg.getTokens().size() > 2)
if (msg.getTokens().size() > 2)
{
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
{
complete_msg.append(*tit);
complete_msg.append(" ");
@@ -272,18 +272,18 @@ namespace irc
void Server::handleNotice(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getArgs().empty())
return;
if(msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
if (msg.getArgs().empty())
return ;
if (msg.getTokens()[1][0] != '&' && msg.getTokens()[1][0] != '#')
{
for(client_it itc = _client.begin(); itc != _client.end(); ++itc)
for (client_it itc = _client.begin(); itc != _client.end(); ++itc)
{
if((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
if ((*itc)->getNickName() == msg.getTokens()[1] && (*itc)->getNickName() != client->getNickName())
{
std::string complete_msg;
if(msg.getTokens().size() > 2)
if (msg.getTokens().size() > 2)
{
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
{
complete_msg.append(*tit);
complete_msg.append(" ");
@@ -294,16 +294,16 @@ namespace irc
break;
}
}
return;
return ;
}
for(channel_it it = _channels.begin(); it != _channels.end(); ++it)
for (channel_it it = _channels.begin(); it != _channels.end(); ++it)
{
if(msg.getTokens()[1] == it->getName())
if (msg.getTokens()[1] == it->getName())
{
std::string complete_msg;
if(msg.getTokens().size() > 2)
if (msg.getTokens().size() > 2)
{
for(std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
for (std::vector<std::string>::const_iterator tit = msg.getTokens().begin() + 2; tit < msg.getTokens().end(); ++tit)
{
complete_msg.append(*tit);
complete_msg.append(" ");
@@ -318,47 +318,47 @@ namespace irc
void Server::handleInvite(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getArgs().empty() || msg.getArgs().size() != 2)
if (msg.getArgs().empty() || msg.getArgs().size() != 2)
{
client->sendCode(ERR_NEEDMOREPARAMS, "INVITE : Invalid parameters");
return;
return ;
}
if(!isUserKnown(msg.getArgs()[0]))
if (!isUserKnown(msg.getArgs()[0]))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(msg.getArgs()[0]) + " no such nick");
return;
return ;
}
if(!isChannelKnown(msg.getArgs()[1]))
if (!isChannelKnown(msg.getArgs()[1]))
{
client->sendCode(ERR_NOSUCHCHANNEL, const_cast<std::string&>(msg.getArgs()[1]) + " no such channel");
return;
return ;
}
Channel* channel_target = getChannelByName(msg.getArgs()[1]);
if(channel_target == NULL)
if (channel_target == NULL)
logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
unstd::SharedPtr<Client> client_target = getClientByName(msg.getArgs()[0]);
if(client_target.get() == NULL)
if (client_target.get() == NULL)
logs::report(log_fatal_error, "(INVITE), cannot get client '%s' by name; panic !", msg.getArgs()[0].c_str());
if(!channel_target->hasClient(client))
if (!channel_target->hasClient(client))
{
logs::report(log_fatal_error, "(INVITE), cannot get channel '%s' by name; panic !", msg.getArgs()[1].c_str());
return;
return ;
}
if(channel_target->hasClient(client_target->getNickName()))
if (channel_target->hasClient(client_target->getNickName()))
{
client->sendCode(ERR_USERONCHANNEL, msg.getArgs()[0] + ' ' + msg.getArgs()[1], "is already on channel");
return;
return ;
}
if(channel_target->isInviteOnly() && !channel_target->isOp(client))
if (channel_target->isInviteOnly() && !channel_target->isOp(client))
{
client->sendCode(ERR_CHANOPRIVSNEEDED, msg.getArgs()[1], "INVITE : you're not channel operator");
return;
return ;
}
client_target->inviteToChannel(channel_target->getName());
@@ -368,52 +368,52 @@ namespace irc
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getArgs().empty())
if (msg.getArgs().empty())
{
client->sendCode(ERR_NONICKNAMEGIVEN, "KICK : No nickname given");
return;
return ;
}
typedef std::vector<std::string>::iterator name_it;
std::vector<std::string> channels = unstd::split(msg.getArgs()[0], ',');
std::vector<std::string> users = unstd::split(msg.getArgs()[1], ',');
if(users.size() != channels.size())
if (users.size() != channels.size())
{
client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters");
return;
return ;
}
for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
for (name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
{
if(!isUserKnown(*user))
if (!isUserKnown(*user))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*user) + " : no such nick");
continue;
}
if(!isChannelKnown(*channel))
if (!isChannelKnown(*channel))
{
client->sendCode(ERR_NOSUCHCHANNEL, const_cast<std::string&>(*channel) + " : no such channel");
continue;
}
Channel* channel_target = getChannelByName(*channel);
if(channel_target == NULL)
if (channel_target == NULL)
logs::report(log_fatal_error, "(KICK), cannot get channel '%s' by name; panic !", channel->c_str());
unstd::SharedPtr<Client> client_target = getClientByName(*user);
if(client_target.get() == NULL)
if (client_target.get() == NULL)
logs::report(log_fatal_error, "(KICK), cannot get client '%s' by name; panic !", user->c_str());
if(!channel_target->kick(client, client_target, msg.getReason()))
if (!channel_target->kick(client, client_target, msg.getReason()))
continue;
client->printUserHeader();
std::cout << "kicked " << *user << " from " << *channel << std::endl;
if(channel_target->getNumberOfClients() == 0)
if (channel_target->getNumberOfClients() == 0)
{
channel_it it;
for(it = _channels.begin(); it < _channels.end(); ++it)
for (it = _channels.begin(); it < _channels.end(); ++it)
{
if(it->getName() == msg.getArgs()[0])
if (it->getName() == msg.getArgs()[0])
break;
}
_channels.erase(it);
@@ -426,37 +426,37 @@ namespace irc
{
irc::Channel *chan;
if(msg.getTokens().size() != 2)
return;
if((chan = getChannelByName(msg.getTokens()[1])) == NULL)
if (msg.getTokens().size() != 2)
return ;
if ((chan = getChannelByName(msg.getTokens()[1])) == NULL)
{
client->sendCode(ERR_NOSUCHCHANNEL, "WHO : No such channel");
return;
return ;
}
chan->sendWho(client);
}
void Server::handleTopic(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getArgs().empty())
if (msg.getArgs().empty())
{
client->sendCode(ERR_NEEDMOREPARAMS, "TOPIC : Need more parameters");
return;
return ;
}
if(!isChannelKnown(msg.getArgs()[0]))
if (!isChannelKnown(msg.getArgs()[0]))
{
client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel");
return;
return ;
}
Channel* channel = getChannelByName(msg.getArgs()[0]);
if(channel == NULL)
if (channel == NULL)
logs::report(log_fatal_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str());
if(!channel->hasClient(client))
if (!channel->hasClient(client))
{
client->sendCode(ERR_NOTONCHANNEL, msg.getArgs()[0] + " you're not on that channel");
return;
return ;
}
if(!msg.getReason().empty())
if (!msg.getReason().empty())
{
channel->setTopic(client, msg.getReason());
client->printUserHeader();
@@ -468,10 +468,10 @@ namespace irc
void Server::handlePing(unstd::SharedPtr<class Client> client, const Message& msg)
{
if(msg.getTokens().size() == 1)
return;
if (msg.getTokens().size() == 1)
return ;
std::string out = "PONG";
for(std::vector<std::string>::const_iterator it = msg.getTokens().begin() + 1; it < msg.getTokens().end(); ++it)
for (std::vector<std::string>::const_iterator it = msg.getTokens().begin() + 1; it < msg.getTokens().end(); ++it)
out += ' ' + *it;
out += "\r\n";
client->sendPlainText(out);
@@ -482,35 +482,35 @@ namespace irc
void Server::handleMode(unstd::SharedPtr<class Client> client, const Message& msg)
{
irc::Channel *chan;
if(msg.getTokens().size() < 2)
if (msg.getTokens().size() < 2)
{
client->sendCode(ERR_NEEDMOREPARAMS, "MODE : Need more parameters");
return;
return ;
}
if(msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&'))
if (msg.getTokens().size() == 2 && (msg.getTokens()[1][0] == '#' || msg.getTokens()[1][0] == '&'))
{
chan = getChannelByName(msg.getTokens()[1]);
if(chan == NULL)
if (chan == NULL)
client->sendCode(ERR_NOSUCHCHANNEL, "MODE : No such channel");
else
chan->showModes(client);
return;
return ;
}
chan = getChannelByName(msg.getTokens()[1]);
if(chan == NULL)
if (chan == NULL)
{
client->sendCode(ERR_NOSUCHCHANNEL, "MODE : No such channel");
return;
return ;
}
if(!chan->isOp(client))
if (!chan->isOp(client))
{
client->sendCodeInChannel(ERR_CHANOPRIVSNEEDED, *chan, "MODE : You need operator privileges to execute this command");
return;
return ;
}
if(msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+')
if (msg.getTokens()[2][0] != '-' && msg.getTokens()[2][0] != '+')
{
client->sendCode(ERR_UNKNOWNMODE, "MODE : Invalid flags (missing +/-)");
return;
return ;
}
chan->changeMode(client, msg);
}
@@ -518,7 +518,7 @@ namespace irc
{
srand(time(NULL));
if(rand() % 6 == 0)
if (rand() % 6 == 0)
{
client->kill("Pew Pew");
usleep(100);

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */
/******************************************************************************/
/* */
/* ::: :::::::: */
/* string.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/25 15:19:30 by maldavid #+# #+# */
/* Updated: 2024/01/25 15:20:20 by maldavid ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/* ************************************************************************** */
/******************************************************************************/
#include <unstd/string.hpp>
@@ -18,13 +18,13 @@ namespace unstd
{
std::vector<std::string> elems;
std::string token;
for(std::string::const_iterator it = s.begin(); it != s.end();)
for (std::string::const_iterator it = s.begin(); it != s.end();)
{
if(*it == delim)
if (*it == delim)
{
elems.push_back(token);
token.clear();
while(*it == delim && it != s.end())
while (*it == delim && it != s.end())
it++;
}
else

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:54:56 by vvaas #+# #+# */
/* Updated: 2024/02/06 11:53:03 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:36:19 by vvaas ### ########.fr */
/* */
/******************************************************************************/
@@ -20,6 +20,7 @@
#include <cstdlib>
#include <signal.h>
#include <cerrno>
bool active = true;
Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
@@ -27,7 +28,7 @@ Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
Bot::~Bot()
{
if(_fd >= 0)
if (_fd >= 0)
close(_fd);
}
@@ -53,17 +54,17 @@ bool Bot::init(const std::string &ip, const std::string &port, const std::string
_connect_commands.push_back("USER greg_Bot 0 * :Botrealname\r\n");
_connect_commands.push_back("JOIN #greg\r\n");
_fd = socket(AF_INET, SOCK_STREAM, 0);
if(_fd == -1)
if (_fd == -1)
irc::logs::report(irc::log_fatal_error, "FD error");
_serv_addr.sin_family = AF_INET;
_serv_addr.sin_port = htons(portval);
_serv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
if(connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0)
if (connect(_fd, (struct sockaddr*)&_serv_addr, sizeof(_serv_addr)) < 0)
{
irc::logs::report(irc::log_error, "connect error");
return (false);
}
if(fcntl(_fd, F_SETFL, O_NONBLOCK) < 0)
if (fcntl(_fd, F_SETFL, O_NONBLOCK) < 0)
{
irc::logs::report(irc::log_error, "fcntl error");
return (false);
@@ -75,48 +76,48 @@ bool Bot::init(const std::string &ip, const std::string &port, const std::string
void Bot::send_message(const std::string &content)
{
if(send(_fd, content.c_str(), content.length(), 0) < 0)
if (send(_fd, content.c_str(), content.length(), 0) < 0)
irc::logs::report(irc::log_fatal_error, "send error");
}
void Bot::handle_response(std::string buffer)
{
if(!_logged && buffer == ":YipIRC 001 greg :Welcome to YipIRC 😀, your nickname is : greg\r\n")
if (!_logged && buffer == ":YipIRC 001 greg :Welcome to YipIRC 😀, your nickname is : greg\r\n")
{
_logged = true;
irc::logs::report(irc::log_message, "Logged in succesfully");
}
else if(!_logged)
else if (!_logged)
return ;
if(!_channel_created && buffer == ":greg JOIN :#greg\r\n")
if (!_channel_created && buffer == ":greg JOIN :#greg\r\n")
{
_channel_created = true;
irc::logs::report(irc::log_message, "Created the channel succesfully");
}
else if(!_channel_created)
else if (!_channel_created)
return ;
if(buffer.find("KICK #greg greg :") != std::string::npos || buffer.find("explose") != std::string::npos)
if (buffer.find("KICK #greg greg :") != std::string::npos || buffer.find("explose") != std::string::npos)
{
send_message("QUIT: Explose\r\n");
std::exit(0);
}
if(buffer.find("quoi") != std::string::npos)
if (buffer.find("quoi") != std::string::npos)
send_message("PRIVMSG #greg :feur\r\n");
}
void Bot::connect_to_server()
{
char buffer[1024];
for(std::vector<std::string>::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it)
for (std::vector<std::string>::iterator it = _connect_commands.begin(); it != _connect_commands.end(); ++it)
{
if(send(_fd, (*it).c_str(), (*it).size(), 0) < 0)
if (send(_fd, (*it).c_str(), (*it).size(), 0) < 0)
irc::logs::report(irc::log_fatal_error, "send error");
if(recv(_fd, buffer, 1024, 0) > 0)
if (recv(_fd, buffer, 1024, 0) > 0)
handle_response(buffer);
}
while (active)
{
if(recv(_fd, buffer, 1024, 0) > 0)
if (recv(_fd, buffer, 1024, 0) > 0)
handle_response(buffer);
std::memset(buffer, 0, sizeof(buffer));
}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */
/* Updated: 2024/02/06 11:47:20 by vvaas ### ########.fr */
/* Updated: 2024/02/06 12:35:38 by vvaas ### ########.fr */
/* */
/******************************************************************************/