blabla/Privmsg.cpp

65 lines
1.9 KiB
C++
Raw Normal View History

2024-08-03 21:04:44 +02:00
#include "Privmsg.hpp"
#include "../IRC.hpp"
#include <string>
#include <sstream>
Privmsg::Privmsg() {}
Privmsg::~Privmsg() {}
bool Privmsg::validate(const User &user, const std::string &msg)
{
size_t pos;
size_t leng;
std::istringstream iss(message);
std::string iter;
iss >> iter;
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;
return false;
}
}
}
else // else it is a user
{
this->targetUser = IRCManager::getUserByName(iter); // identify user
if (!this->targetUser)
{
std::cout << "User " + 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
}
}
while (iter[0]) // put the rest of the string in the message
{
iss >> iter;
message = message + " " + iter;
}
return true;
}
int Privmsg::run(User &user, IRC::t_send_f &send)
{
if (this->targetUser)
{
send(user, user.getNickName() + user.getHostName() + " PRIVMSG " + this->targetUser->getUserName() + " " + this->message);
std::cout << user.getUserName() + " sent a message to " + this->targetUser.getUserName();
}
else
send(user, user.getNickName() + user.getHostName() + " PRIVMSG " + this->targetChannel->getChannelName() + " " + this->message);
std::cout << user.getUserName() + " sent a message to " + this->targetChannel.getChannelName();
return (0);
}