This commit is contained in:
2024-01-25 21:19:54 +01:00
parent 51074bab78
commit 6727618122
6 changed files with 42 additions and 10 deletions

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/01/25 18:11:58 by maldavid ### ########.fr */ /* Updated: 2024/01/25 19:38:50 by vvaas ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@@ -47,7 +47,7 @@ namespace irc
void changeMode(unstd::SharedPtr<class Client> client, const Message& msg); void changeMode(unstd::SharedPtr<class Client> client, const Message& msg);
bool isOp(unstd::SharedPtr<Client> client) const; bool isOp(unstd::SharedPtr<Client> client) const;
void handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice = false) const; void handleMessage(const std::string& msg, unstd::SharedPtr<Client> client, bool notice = false) const;
void sendWho(unstd::SharedPtr<Client> client);
inline bool hasClient(unstd::SharedPtr<Client> client) const { return _clients.find(client) != _clients.end(); } inline bool hasClient(unstd::SharedPtr<Client> client) const { return _clients.find(client) != _clients.end(); }
bool hasClient(std::string client) const; bool hasClient(std::string client) const;

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/01/25 17:21:25 by maldavid ### ########.fr */ /* Updated: 2024/01/25 18:44:01 by vvaas ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@@ -57,6 +57,7 @@ namespace irc
void handleInvite(unstd::SharedPtr<class Client> client, const class Message& msg); void handleInvite(unstd::SharedPtr<class Client> client, const class Message& msg);
void handlePrivMsg(unstd::SharedPtr<class Client> client, const class Message& msg); void handlePrivMsg(unstd::SharedPtr<class Client> client, const class Message& msg);
void handleNotice(unstd::SharedPtr<class Client> client, const class Message& msg); void handleNotice(unstd::SharedPtr<class Client> client, const class Message& msg);
void handleWho(unstd::SharedPtr<class Client> client, const class Message& msg);
void handleKick(unstd::SharedPtr<class Client> client, const class Message& msg); void handleKick(unstd::SharedPtr<class Client> client, const class Message& msg);
void handleTopic(unstd::SharedPtr<class Client> client, const class Message& msg); void handleTopic(unstd::SharedPtr<class Client> client, const class Message& msg);
void handlePing(unstd::SharedPtr<class Client> client, const class Message& msg); void handlePing(unstd::SharedPtr<class Client> client, const class Message& msg);

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/01/25 18:25:27 by vvaas ### ########.fr */ /* Updated: 2024/01/25 21:15:06 by vvaas ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@@ -58,14 +58,28 @@ namespace irc
} }
bool Channel::removeClient(unstd::SharedPtr<Client> client) bool Channel::removeClient(unstd::SharedPtr<Client> client)
{ {
if(!_clients.erase(client)) if (!_clients.erase(client))
return (false); return (false);
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)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "PART", _name); const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "PART", _name);
client->sendMsg(client->getNickName(), "PART", _name); client->sendMsg(client->getNickName(), "PART", _name);
return (true); return (true);
} }
void Channel::sendWho(unstd::SharedPtr<Client> client)
{
std::string clientlist(RPL_NAMREPLY " " + client->getNickName() + " = " + getName() + ": ");
for (client_it it = _clients.begin(); it != _clients.end(); ++it)
{
clientlist += const_cast<unstd::SharedPtr<irc::Client>&>(*it)->getNickName() + ' ';
}
client->sendModular("%s", clientlist.c_str());
client->sendModular("%s %s %s : End of names list", RPL_ENDOFNAMES, client->getNickName().c_str(), getName().c_str());
}
void Channel::handleMessage(const std::string& msg, unstd::SharedPtr<Client> 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() : "";
@@ -189,6 +203,8 @@ namespace irc
} }
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))
_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/01/24 18:42:31 by vvaas ### ########.fr */ /* Updated: 2024/01/25 20:24:16 by vvaas ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@@ -28,13 +28,14 @@ namespace irc
void Client::sendCode(const std::string& code, const std::string& msg) void Client::sendCode(const std::string& code, const std::string& msg)
{ {
const std::string command = code + " :" + msg; const std::string command = code + " :" + msg;
//logs::report(log_message, "C<-%s", command.c_str()); logs::report(log_message, "C<-%s", command.c_str());
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());
} }
void Client::sendPlainText(const std::string& str) void Client::sendPlainText(const std::string& str)
{ {
logs::report(log_message,"<- S %s", str.c_str());
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());
} }
@@ -42,7 +43,7 @@ namespace irc
void Client::sendMsg(const std::string& sender, const std::string& cmd, const std::string& trailing) void Client::sendMsg(const std::string& sender, const std::string& cmd, const std::string& trailing)
{ {
const std::string out = ':' + sender + ' ' + cmd + " :" + trailing + "\r\n"; const std::string out = ':' + sender + ' ' + cmd + " :" + trailing + "\r\n";
//logs::report(log_message,"<- S %s", out.c_str()); logs::report(log_message,"<- S %s", out.c_str());
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());
} }
@@ -56,6 +57,7 @@ namespace irc
vsnprintf(buffer, LOGS_BUFFER_SIZE, message.c_str(), al); vsnprintf(buffer, LOGS_BUFFER_SIZE, message.c_str(), al);
va_end(al); va_end(al);
logs::report(log_message,"<- S %s", buffer);
if (send(_fd, buffer, std::strlen(buffer), 0) < static_cast<ssize_t>(std::strlen(buffer))) if (send(_fd, buffer, std::strlen(buffer), 0) < static_cast<ssize_t>(std::strlen(buffer)))
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());
} }

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/01/25 18:32:27 by vvaas ### ########.fr */ /* Updated: 2024/01/25 18:40:10 by vvaas ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@@ -143,6 +143,8 @@ namespace irc
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")
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")

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/01/25 18:15:16 by maldavid ### ########.fr */ /* Updated: 2024/01/25 20:28:19 by vvaas ### ########.fr */
/* */ /* */
/******************************************************************************/ /******************************************************************************/
@@ -344,6 +344,17 @@ namespace irc
} }
} }
void Server::handleWho(unstd::SharedPtr<class Client> client, const class Message& msg)
{
irc::Channel *chan;
logs::report(log_message, "tokensize ok : %d", msg.getTokens().size());
if (msg.getTokens().size() != 2)
return;
if ((chan = getChannelByName(msg.getTokens()[1])) == NULL)
client->sendCode(ERR_NOSUCHCHANNEL, "No such channel");
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)
{ {
(void)client; (void)client;