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> +#+ +:+ +#+ # # By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/08/09 15:08:49 by vavaas #+# #+# # # 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 :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __ANSI__
#define __ANSI__ #define __ANSI__

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */ /* 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> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:33:17 by maldavid #+# #+# */ /* 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> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 09:45:10 by maldavid #+# #+# */ /* 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 :+: :+: :+: */ /* irc.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: vavaas <vavaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/29 13:24:01 by vavaas #+# #+# */ /* 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 :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __LOGS__
#define __LOGS__ #define __LOGS__

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */ /******************************************************************************/
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* message.hpp :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __MESSAGE__
#define __MESSAGE__ #define __MESSAGE__

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:12:28 by maldavid #+# #+# */ /* 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() inline void closeMainSocket()
{ {
if(_main_socket > NULL_SOCKET) if (_main_socket > NULL_SOCKET)
close(_main_socket); close(_main_socket);
_main_socket = NULL_SOCKET; _main_socket = NULL_SOCKET;
_active = false; _active = false;

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */ /******************************************************************************/
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* array.hpp :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __UNSTD_ARRAY__
#define __UNSTD_ARRAY__ #define __UNSTD_ARRAY__
@@ -25,13 +25,13 @@ namespace unstd
TArray() {} TArray() {}
TArray(const TArray<T, N>& rhs) 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]; _data[i] = rhs._data[i];
} }
inline TArray& operator=(const TArray<T, N>& rhs) 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]; _data[i] = rhs._data[i];
return *this; return *this;
} }

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */ /******************************************************************************/
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ref_counter.hpp :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __UNSTD_BITS_REFERENCE_COUNTER__
#define __UNSTD_BITS_REFERENCE_COUNTER__ #define __UNSTD_BITS_REFERENCE_COUNTER__

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */ /******************************************************************************/
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* shared_ptr.hpp :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __UNSTD_SHARED_PTR__
#define __UNSTD_SHARED_PTR__ #define __UNSTD_SHARED_PTR__

View File

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

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */ /******************************************************************************/
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* string.hpp :+: :+: :+: */ /* 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 #+# #+# */ /* 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__ #ifndef __UNSTD_STRING__
#define __UNSTD_STRING__ #define __UNSTD_STRING__

View File

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

View File

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

View File

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

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */ /* 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); struct tm tstruct = *localtime(&ltime);
char buf[100]; 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 !"); logs::report(log_error, "Password is invalid !");
return; return ;
} }
std::memset(&_s_data, 0, sizeof(sockaddr)); std::memset(&_s_data, 0, sizeof(sockaddr));
initSocket(); initSocket();
@@ -52,7 +52,7 @@ namespace irc
_s_data.sin_addr.s_addr = INADDR_ANY; _s_data.sin_addr.s_addr = INADDR_ANY;
_s_data.sin_port = htons(_port); _s_data.sin_port = htons(_port);
_main_socket = socket(AF_INET, SOCK_STREAM, 0); // AF_INET == IPv4, SOCK_STREAM == TCP _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_fatal_error, "socket error");
logs::report(log_message, "socket succesfully started"); logs::report(log_message, "socket succesfully started");
} }
@@ -62,14 +62,14 @@ namespace irc
int opt = 1; int opt = 1;
initSocketData(); 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)"); 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_fatal_error, "bind error");
logs::report(log_message, "bind successful, starting listen loop"); 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_fatal_error, "listen error");
logs::report(log_message, "listen queue created successfully"); logs::report(log_message, "listen queue created successfully");
@@ -80,27 +80,27 @@ namespace irc
{ {
char buffer[INPUT_SIZE] = { 0 }; char buffer[INPUT_SIZE] = { 0 };
ssize_t recv_size; 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; 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); (*it)->newMsgInFlight(buffer);
#ifdef DEBUG #ifdef DEBUG
logs::report(log_message,"processing '%s'", buffer); logs::report(log_message,"processing '%s'", buffer);
#endif #endif
while(handleMessage(*it)); while (handleMessage(*it));
std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining 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()); logs::report(log_message, "User %d disconnected", (*it)->getID());
close((*it)->getFD()); 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); 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()); logs::report(log_message, "channel '%s' has been destroyed", cit->getName().c_str());
cit = _channels.erase(cit); cit = _channels.erase(cit);
@@ -121,27 +121,27 @@ namespace irc
int i = 0; int i = 0;
socklen_t len = sizeof(sockaddr_in); socklen_t len = sizeof(sockaddr_in);
if(_main_socket == NULL_SOCKET) if (_main_socket == NULL_SOCKET)
return; return ;
while(_active) while (_active)
{ {
FD_ZERO(&_fd_set); FD_ZERO(&_fd_set);
FD_SET(_main_socket, &_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); 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 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"); 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; sockaddr_in cli_sock;
fd = accept(_main_socket, reinterpret_cast<sockaddr*>(&cli_sock), &len); // adds the new connection 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"); 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"); logs::report(log_fatal_error, "fcntl() error");
unstd::SharedPtr<Client> new_client(new Client(fd, cli_sock, i++)); unstd::SharedPtr<Client> new_client(new Client(fd, cli_sock, i++));
@@ -155,41 +155,41 @@ namespace irc
bool Server::handleMessage(unstd::SharedPtr<Client> client) 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; return false;
const Message msg(client, client->getNextMsg()); const Message msg(client, client->getNextMsg());
if(msg.getCmd() == "NICK") if (msg.getCmd() == "NICK")
handleNick(client, msg, *this); handleNick(client, msg, *this);
else if(msg.getCmd() == "USER") else if (msg.getCmd() == "USER")
handleUser(client, msg, *this); handleUser(client, msg, *this);
else if(msg.getCmd() == "PASS") else if (msg.getCmd() == "PASS")
handlePass(client, msg, *this); handlePass(client, msg, *this);
else if(!client->isWelcomed()) else if (!client->isWelcomed())
return true; return true;
else if(msg.getCmd() == "QUIT") else if (msg.getCmd() == "QUIT")
handleQuit(client, msg); handleQuit(client, msg);
else if(msg.getCmd() == "WHO") else if (msg.getCmd() == "WHO")
handleWho(client, msg); handleWho(client, msg);
else if(msg.getCmd() == "PART") else if (msg.getCmd() == "PART")
handlePart(client, msg); handlePart(client, msg);
else if(msg.getCmd() == "JOIN") else if (msg.getCmd() == "JOIN")
handleJoin(client, msg); handleJoin(client, msg);
else if(msg.getCmd() == "INVITE") else if (msg.getCmd() == "INVITE")
handleInvite(client, msg); handleInvite(client, msg);
else if(msg.getCmd() == "PRIVMSG") else if (msg.getCmd() == "PRIVMSG")
handlePrivMsg(client, msg); handlePrivMsg(client, msg);
else if(msg.getCmd() == "NOTICE") else if (msg.getCmd() == "NOTICE")
handleNotice(client, msg); handleNotice(client, msg);
else if(msg.getCmd() == "KICK") else if (msg.getCmd() == "KICK")
handleKick(client, msg); handleKick(client, msg);
else if(msg.getCmd() == "TOPIC") else if (msg.getCmd() == "TOPIC")
handleTopic(client, msg); handleTopic(client, msg);
else if(msg.getCmd() == "PING") else if (msg.getCmd() == "PING")
handlePing(client, msg); handlePing(client, msg);
else if(msg.getCmd() == "MODE") else if (msg.getCmd() == "MODE")
handleMode(client, msg); handleMode(client, msg);
else if(msg.getCmd() == "imfeelinglucky") else if (msg.getCmd() == "imfeelinglucky")
handleRussian(client); handleRussian(client);
else else
client->sendCode(ERR_UNKNOWNCOMMAND, "No such command"); client->sendCode(ERR_UNKNOWNCOMMAND, "No such command");
@@ -198,9 +198,9 @@ namespace irc
bool Server::isUserKnown(const std::string& user) const 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 true;
} }
return false; return false;
@@ -208,9 +208,9 @@ namespace irc
bool Server::isChannelKnown(const std::string& channel) const 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 true;
} }
return false; return false;
@@ -218,9 +218,9 @@ namespace irc
Channel* Server::getChannelByName(const std::string& name) 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 &*it;
} }
return NULL; return NULL;
@@ -228,9 +228,9 @@ namespace irc
unstd::SharedPtr<Client> Server::getClientByName(const std::string& name) 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 *it;
} }
return unstd::SharedPtr<Client>(NULL); return unstd::SharedPtr<Client>(NULL);
@@ -238,13 +238,13 @@ namespace irc
Server::~Server() 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"); (*it)->kill("Server shutting down");
} }
closeMainSocket(); closeMainSocket();
for(int i = 3; i <= FD_MAX; ++i) for (int i = 3; i <= FD_MAX; ++i)
close(i); close(i);
} }
} }

View File

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

View File

@@ -1,14 +1,14 @@
/* ************************************************************************** */ /******************************************************************************/
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* string.cpp :+: :+: :+: */ /* 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 #+# #+# */ /* 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> #include <unstd/string.hpp>
@@ -18,13 +18,13 @@ namespace unstd
{ {
std::vector<std::string> elems; std::vector<std::string> elems;
std::string token; 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); elems.push_back(token);
token.clear(); token.clear();
while(*it == delim && it != s.end()) while (*it == delim && it != s.end())
it++; it++;
} }
else else

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/30 01:54:56 by vvaas #+# #+# */ /* 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 <cstdlib>
#include <signal.h> #include <signal.h>
#include <cerrno> #include <cerrno>
bool active = true; bool active = true;
Bot::Bot() : _channel_created(false), _logged(false), _fd(-1) Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
@@ -27,7 +28,7 @@ Bot::Bot() : _channel_created(false), _logged(false), _fd(-1)
Bot::~Bot() Bot::~Bot()
{ {
if(_fd >= 0) if (_fd >= 0)
close(_fd); 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("USER greg_Bot 0 * :Botrealname\r\n");
_connect_commands.push_back("JOIN #greg\r\n"); _connect_commands.push_back("JOIN #greg\r\n");
_fd = socket(AF_INET, SOCK_STREAM, 0); _fd = socket(AF_INET, SOCK_STREAM, 0);
if(_fd == -1) if (_fd == -1)
irc::logs::report(irc::log_fatal_error, "FD error"); irc::logs::report(irc::log_fatal_error, "FD error");
_serv_addr.sin_family = AF_INET; _serv_addr.sin_family = AF_INET;
_serv_addr.sin_port = htons(portval); _serv_addr.sin_port = htons(portval);
_serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); _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"); irc::logs::report(irc::log_error, "connect error");
return (false); 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"); irc::logs::report(irc::log_error, "fcntl error");
return (false); 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) 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"); irc::logs::report(irc::log_fatal_error, "send error");
} }
void Bot::handle_response(std::string buffer) 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; _logged = true;
irc::logs::report(irc::log_message, "Logged in succesfully"); irc::logs::report(irc::log_message, "Logged in succesfully");
} }
else if(!_logged) else if (!_logged)
return ; return ;
if(!_channel_created && buffer == ":greg JOIN :#greg\r\n") if (!_channel_created && buffer == ":greg JOIN :#greg\r\n")
{ {
_channel_created = true; _channel_created = true;
irc::logs::report(irc::log_message, "Created the channel succesfully"); irc::logs::report(irc::log_message, "Created the channel succesfully");
} }
else if(!_channel_created) else if (!_channel_created)
return ; 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"); send_message("QUIT: Explose\r\n");
std::exit(0); std::exit(0);
} }
if(buffer.find("quoi") != std::string::npos) if (buffer.find("quoi") != std::string::npos)
send_message("PRIVMSG #greg :feur\r\n"); send_message("PRIVMSG #greg :feur\r\n");
} }
void Bot::connect_to_server() void Bot::connect_to_server()
{ {
char buffer[1024]; 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"); 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); handle_response(buffer);
} }
while (active) while (active)
{ {
if(recv(_fd, buffer, 1024, 0) > 0) if (recv(_fd, buffer, 1024, 0) > 0)
handle_response(buffer); handle_response(buffer);
std::memset(buffer, 0, sizeof(buffer)); std::memset(buffer, 0, sizeof(buffer));
} }

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 20:36:23 by vvaas #+# #+# */ /* 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 */
/* */ /* */
/******************************************************************************/ /******************************************************************************/