blabla/Part.cpp
Danny Jonker d426297b2a bla
2024-08-03 21:04:44 +02:00

62 lines
1.7 KiB
C++

#include "Part.hpp"
#include "../IRC.hpp"
#include <string>
#include <sstream>
Part::Part() {}
Part::~Part() {}
bool Part::validate(const User &user, const std::string &msg)
{
size_t pos;
size_t leng;
std::istringstream iss(msg);
std::string iter;
iss >> iter; //iter will be PART, which we already know
iss >> iter; //iter will be The channel that the user is leaving
if (iter[0] == '#') // If the string starts with a # it is a channel
{
this->targetChannel = IRCManager::getChannelByName(iter); // identify channel
if (!this->targetChannel)
{
std::cout << "Channel " + iter + " Does not exist, maybe we should send something back?"; // Not sure what is the proper response here, for now we print
return false; // return false to Dmitry
}
else // The channel exists
{
if (!IRCManager::checkChannelMember(user, iter)) // check if the user is part of the channel
{
std::cout << user.getUserName() + " is not a member of " + iter + " So we would be leaving nothing";
return false;
}
}
}
else
{
std::cout << "You can't part with a user, you can only part with channels" << std::endl;
return (false);
}
leng = msg.length(); // get the length of the message
pos = msg.find(":") + 1; // get position of the reason
this->reason = msg.substr(pos, leng - pos); // store the reason
return true;
}
int Part::run(User &user, IRC::t_send_f &send)
{
std::istringstream iss(targetChannel->getUserList());
std::string target;
iss >> target;
while (target[0])
{
send(IRCManager::getUserFromName(target), user.getNickName() + user.getHostName() + " PART " + this->targetChannel->getChannelName() + " " + this->reason);
iss >> target;
}
this->targetChannel->removeUser(user);
return (0);
}