This commit is contained in:
Kbz-8
2024-01-25 21:20:10 +01:00
parent 6727618122
commit 5642baf23e
6 changed files with 58 additions and 23 deletions

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:34:25 by maldavid #+# #+# */
/* Updated: 2024/01/25 19:38:50 by vvaas ### ########.fr */
/* Updated: 2024/01/25 21:21:19 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -54,7 +54,8 @@ namespace irc
inline bool isInviteOnly() const { return _invite_only; }
void setTopic(unstd::SharedPtr<Client> client, const std::string& new_topic);
void showModes(void) const;
void relayTopic(unstd::SharedPtr<Client> client);
void showModes() const;
bool kick(unstd::SharedPtr<Client> op, unstd::SharedPtr<Client> target, const std::string& reason);

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:36:21 by maldavid #+# #+# */
/* Updated: 2024/01/25 21:15:06 by vvaas ### ########.fr */
/* Updated: 2024/01/25 21:21:12 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -35,6 +35,7 @@ namespace irc
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "JOIN", _name);
}
void Channel::ModOperator(unstd::SharedPtr<class Client> client, const std::string &clientname, bool mode)
{
client_it it;
@@ -56,6 +57,7 @@ namespace irc
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendMsg(client->getNickName(), "MODE", out);
}
}
bool Channel::removeClient(unstd::SharedPtr<Client> client)
{
if (!_clients.erase(client))
@@ -109,6 +111,7 @@ namespace irc
for(client_it it = _clients.begin(); it != _clients.end(); ++it)
const_cast<unstd::SharedPtr<irc::Client>&>(*it)->sendCode(RPL_CHANNELMODEIS, modes);
}
void Channel::changeMode(unstd::SharedPtr<class Client> client, const Message& msg)
{
bool modevalue = (msg.getTokens()[2][0] != '-');
@@ -169,9 +172,19 @@ namespace irc
if(_topic_op_restrict && !isOp(client))
{
client->sendCode(ERR_CHANOPRIVSNEEDED, "You need operator privileges");
return ;
return;
}
_topic = new_topic;
relayTopic(client);
}
void Channel::relayTopic(unstd::SharedPtr<Client> client)
{
if(!hasClient(client))
return;
if(_topic.empty())
return client->sendCode(RPL_NOTOPIC " " + _name, " no topic is set");
return client->sendCode(RPL_TOPIC " " + _name, _topic);
}
bool Channel::isOp(unstd::SharedPtr<Client> client) const
@@ -193,7 +206,7 @@ namespace irc
}
if(!hasClient(target))
{
op->sendCode(ERR_USERNOTINCHANNEL, const_cast<std::string&>(target->getNickName()) + std::string(" " + _name) + " they aren't on that channel");
op->sendCode(ERR_USERNOTINCHANNEL, const_cast<std::string&>(target->getNickName()) + ' ' + _name + " they aren't on that channel");
return false;
}
if(!isOp(op))

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 10:35:52 by maldavid #+# #+# */
/* Updated: 2024/01/25 20:24:16 by vvaas ### ########.fr */
/* Updated: 2024/01/25 21:20:41 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -27,15 +27,19 @@ namespace irc
void Client::sendCode(const std::string& code, const std::string& msg)
{
const std::string command = code + " :" + msg;
logs::report(log_message, "C<-%s", command.c_str());
const std::string command = code + " :" + msg + "\r\n";
#ifdef DEBUG
logs::report(log_message, "sending '%s'", command.c_str());
#endif
if(send(_fd, command.c_str(), command.size(), 0) != static_cast<ssize_t>(command.length()))
logs::report(log_error, "server failed to send a code to '%s' (:sadge:)", _username.c_str());
}
void Client::sendPlainText(const std::string& str)
{
logs::report(log_message,"<- S %s", str.c_str());
#ifdef DEBUG
logs::report(log_message,"sending '%s'", str.c_str());
#endif
if(send(_fd, str.c_str(), str.length(), 0) != static_cast<ssize_t>(str.length()))
logs::report(log_error, "server failed to send a message to '%s' (:sadge:)", _username.c_str());
}
@@ -44,6 +48,9 @@ namespace irc
{
const std::string out = ':' + sender + ' ' + cmd + " :" + trailing + "\r\n";
logs::report(log_message,"<- S %s", out.c_str());
#ifdef DEBUG
logs::report(log_message,"sending '%s'", out.c_str());
#endif
if(send(_fd, out.c_str(), out.length(), 0) != static_cast<ssize_t>(out.length()))
logs::report(log_error, "server failed to send a message from '%s' to '%s' (:sadge:)", sender.c_str(), _username.c_str());
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 11:38:34 by maldavid #+# #+# */
/* Updated: 2024/01/25 18:06:49 by maldavid ### ########.fr */
/* Updated: 2024/01/25 20:30:51 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -64,6 +64,8 @@ namespace irc
_args.push_back(*it);
else
{
if((*it)[0] == ':')
continue;
_reason.append(*it);
_reason.push_back(' ');
}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/21 09:31:17 by maldavid #+# #+# */
/* Updated: 2024/01/25 18:40:10 by vvaas ### ########.fr */
/* Updated: 2024/01/25 21:20:52 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -76,7 +76,9 @@ namespace irc
while(recv((*it)->getFD(), buffer, INPUT_SIZE, 0) > 0) // read() but for socket fd
{
(*it)->newMsgInFlight(buffer);
std::cout << buffer << std::endl;
#ifdef DEBUG
logs::report(log_message,"processing '%s'", buffer);
#endif
while(handleMessage(*it));
std::memset(buffer, 0, sizeof(buffer)); // clear the buffer to avoid trash remaining
}

View File

@@ -6,7 +6,7 @@
/* By: vvaas <vvaas@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 17:31:06 by maldavid #+# #+# */
/* Updated: 2024/01/25 20:28:19 by vvaas ### ########.fr */
/* Updated: 2024/01/25 21:21:04 by maldavid ### ########.fr */
/* */
/******************************************************************************/
@@ -293,13 +293,11 @@ namespace irc
void Server::handleKick(unstd::SharedPtr<class Client> client, const Message& msg)
{
std::cout << "kick" << std::endl;
if(msg.getArgs().empty())
{
logs::report(log_error, "KICK, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
std::cout << "kick" << std::endl;
typedef std::vector<std::string>::iterator name_it;
std::vector<std::string> channels = unstd::split(msg.getArgs()[0], ',');
@@ -310,22 +308,19 @@ namespace irc
client->sendCode(ERR_NEEDMOREPARAMS, "KICK : Not enough parameters");
return;
}
std::cout << "kick" << std::endl;
for(name_it user = users.begin(), channel = channels.begin(); user < users.end(); ++user, ++channel)
{
std::cout << *user << " " << *channel << std::endl;
if(!isUserKnown(*user))
{
client->sendCode(ERR_NOSUCHNICK, const_cast<std::string&>(*user) + " no such nick");
continue;
}
std::cout << *user << " " << *channel << std::endl;
if(!isChannelKnown(*channel))
{
client->sendCode(ERR_NOSUCHCHANNEL, const_cast<std::string&>(*channel) + " no such channel");
continue;
}
std::cout << *user << " " << *channel << std::endl;
Channel* channel_target = getChannelByName(*channel);
if(channel_target == NULL)
@@ -357,17 +352,32 @@ namespace irc
}
void Server::handleTopic(unstd::SharedPtr<class Client> client, const Message& msg)
{
(void)client;
if(msg.getArgs().empty())
{
logs::report(log_error, "TOPIC, invalid command '%s'", msg.getRawMsg().c_str());
return;
}
if(msg.getArgs()[0][0] != '#' && msg.getArgs()[0][0] != '&')
if(!isChannelKnown(msg.getArgs()[0]))
{
logs::report(log_error, "TOPIC, invalid channel name '%s'", msg.getArgs()[0].c_str());
client->sendCode(ERR_NOSUCHCHANNEL, msg.getArgs()[0] + " no such channel");
return;
}
Channel* channel = getChannelByName(msg.getArgs()[0]);
if(channel == NULL)
logs::report(log_fatal_error, "(TOPIC), cannot get channel '%s' by name; panic !", channel->getName().c_str());
if(!channel->hasClient(client))
{
client->sendCode(ERR_NOTONCHANNEL, msg.getArgs()[0] + " you're not on that channel");
return;
}
if(!msg.getReason().empty())
{
channel->setTopic(client, msg.getReason());
client->printUserHeader();
std::cout << "changed topic to " << msg.getReason() << std::endl;
}
else
channel->relayTopic(client);
}
void Server::handlePing(unstd::SharedPtr<class Client> client, const Message& msg)