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

68 lines
1.7 KiB
C++

#include "Cap.hpp"
#include "../IRC.hpp"
#include <string>
bool Cap::validate(const User &user, const std::string &msg)
{
std::istringstream iss(msg);
std::string word;
iss >> word; // should be CAP
iss >> word; // should be the mode
if (word == "LS")
{
this->mode = 1; // In this mode run will print the capabilities of the server
iss >> word; // Should be a code
this->code = word; // Not sure if we need it but if we do we have it.
return (true);
}
if (word == "REQ")
{
this->mode = 2; // in this mode run will acknowledge the capabilities sent by the client
size_t pos;
size_t leng;
iss >> word; // Should be a all the capabilities with a : in front of them
leng = word.length();
pos = word.find(":") + 1;
this->capabilities = word.substr(pos, leng - pos); // remove the :
return (true);
}
std::cout << "Command without mode match" << word << std::endl;
return (false);
}
int Cap::run(User &user, IRC::t_send_f &send)
{
if (this->mode == 1) // Mode is LS
{
send(this->targetUser, IRCManager::getHostName + " CAP * LS :" + IRCManager::getCapabilities();
return (0);
}
if (this->mode == 2) // mode is REQ
{
std::istringstream serverCap(IRCManager::getCapabilities);
std::istringstream clientCap;
std::string server;
std::string client;
std::string matches;
serverCap >> server;
while server[0])
{
clientCap(this->capabilities);
clientCap >> client
while (client[0])
{
if (client == server)
{
matches = matches + client + " ";
break;
}
clientCap >> client
}
serverCap >> server;
}
send(this->targetUser, IRCManager::getHostName + " CAP " + user.getNickName() + " ACK :" + matches;
return (0);
}
return (0);
}