Skip to content

Commit

Permalink
Merge pull request #52 from ArthanIRC/case-mapping
Browse files Browse the repository at this point in the history
fix: use case-mapping for nick and chan names
  • Loading branch information
aurlic authored Sep 12, 2024
2 parents 75ec5ae + fea30a7 commit ec91d4e
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 36 deletions.
3 changes: 3 additions & 0 deletions include/Commands/WhoCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <vector>

class WhoCommand : public Command {
private:
std::string _target;

public:
WhoCommand(std::string source, std::vector<std::string> params,
Client* client);
Expand Down
4 changes: 3 additions & 1 deletion include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "Epoll.hpp"
#include "ServerSocket.hpp"

std::string toLowerCase(std::string s);

class Server {
private:
bool _running;
Expand Down Expand Up @@ -102,4 +104,4 @@ class Server {
public:
virtual const char* what() const throw();
};
};
};
17 changes: 12 additions & 5 deletions src/Commands/InviteCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Client.hpp"
#include "Exception.hpp"
#include "Replies.hpp"
#include "Server.hpp"

using std::string;
using std::vector;
Expand All @@ -15,34 +16,40 @@ InviteCommand::InviteCommand(string source, vector<string> params,
}

Channel* chan;
string chanName = toLowerCase(params[1]);
this->_targetNickname = toLowerCase(params[0]);

if (params.size() < 2) {
client->sendMessage(Replies::ERR_NEEDMOREPARAMS(client, "INVITE"));
throw ClientException();
}

try {
chan = Server::getInstance().findChannel(params[1]);
chan = Server::getInstance().findChannel(chanName);
} catch (Server::ChannelNotFoundException&) {
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, params[1]));
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, chanName));
throw ClientException();
}

if (!chan->isInChannel(client)) {
client->sendMessage(Replies::ERR_NOTONCHANNEL(client, chan));
throw ClientException();
}

if (chan->isInviteOnly() && !chan->isOperator(client)) {
client->sendMessage(Replies::ERR_CHANOPRIVSNEEDED(client, chan));
throw ClientException();
}
if (chan->isInChannel(params[0])) {

if (chan->isInChannel(_targetNickname)) {
client->sendMessage(
Replies::ERR_USERONCHANNEL(client, params[0], chan));
Replies::ERR_USERONCHANNEL(client, _targetNickname, chan));
throw ClientException();
}

this->_source = source;
this->_params = params;
this->_client = client;
this->_targetNickname = params[0];
this->_channel = chan;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Commands/JoinCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "JoinCommand.hpp"
#include "PartCommand.hpp"
#include "Replies.hpp"
#include "Server.hpp"

using std::map;
using std::string;
Expand Down Expand Up @@ -45,6 +46,7 @@ void JoinCommand::parseParams() {
size_t i = 0;

while (std::getline(iss2, chanName, ',')) {
chanName = toLowerCase(chanName);
try {
_channels.push_back(Server::getInstance().findChannel(chanName));
} catch (const Server::ChannelNotFoundException&) {
Expand Down
25 changes: 19 additions & 6 deletions src/Commands/KickCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "KickCommand.hpp"
#include "Server.hpp"

using std::string;
using std::vector;
Expand All @@ -17,35 +18,45 @@ void KickCommand::checkParams(Client* client, vector<string> params) {
client->sendMessage(Replies::ERR_NOTREGISTERED());
throw ClientException();
}

if (params.size() < 2) {
client->sendMessage(Replies::ERR_NEEDMOREPARAMS(client, "KICK"));
throw ClientException();
}

if (params[1][params[1].size() - 1] == ',') {
params[1] = params[1].substr(0, params[1].size() - 1);
}

Channel* chan;
this->_targetNickname = toLowerCase(params[1]);
string chanName = toLowerCase(params[0]);

try {
chan = Server::getInstance().findChannel(params[0]);
chan = Server::getInstance().findChannel(chanName);
} catch (const Server::ChannelNotFoundException&) {
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, params[0]));
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, chanName));
throw ClientException();
}

this->_channel = chan;

if (!chan->isInChannel(client)) {
client->sendMessage(Replies::ERR_NOTONCHANNEL(client, chan));
throw ClientException();
}

if (!chan->isOperator(client)) {
client->sendMessage(Replies::ERR_CHANOPRIVSNEEDED(client, chan));
throw ClientException();
}
if (!chan->isInChannel(params[1])) {

if (!chan->isInChannel(_targetNickname)) {
client->sendMessage(
Replies::ERR_USERNOTINCHANNEL(_client, params[1], _channel));
Replies::ERR_USERNOTINCHANNEL(_client, _targetNickname, _channel));
throw ClientException();
}
this->_targetNickname = params[1];

if (params[params.size() - 2].find(',') == string::npos) {
this->_comment = params[params.size() - 1];
} else
Expand All @@ -54,20 +65,22 @@ void KickCommand::checkParams(Client* client, vector<string> params) {

void KickCommand::run() {
string reply;

reply = ":" + _client->getSource() + " KICK " + _channel->getName() + " " +
_targetNickname + " " + _comment;
Message::create(reply);
Client* target;

try {
target = Server::getInstance().findClient(_targetNickname);
} catch (Server::ClientNotFoundException&) {
_client->sendMessage(Replies::ERR_NOSUCHNICK(_client, _targetNickname));
return;
}

_channel->removeClient(target);
if (_channel->isOperator(target))
_channel->removeOperator(target);

target->sendMessage(reply);
Server::getInstance().sendMessage(_channel, reply, NULL);
}
8 changes: 4 additions & 4 deletions src/Commands/ModeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ModeCommand::ModeCommand(string source, vector<string> params, Client* client) {
throw ClientException();
}

this->_target = params[0];
this->_target = toLowerCase(params[0]);

if (_target[0] == '#') {
this->_isChan = true;
Expand Down Expand Up @@ -79,7 +79,7 @@ void ModeCommand::invisibleMode(bool oper, size_t& p) {
}

void ModeCommand::banMode(bool oper, size_t& p) {
string param = retrieveParam(_params, p);
string param = toLowerCase(retrieveParam(_params, p));
if (param.empty()) {
vector<Client*> banlist = _channel->getBanList();
for (vector<Client*>::iterator it = banlist.begin();
Expand Down Expand Up @@ -164,7 +164,7 @@ void ModeCommand::protectedTopicMode(bool oper, size_t& p) {
}

void ModeCommand::operatorMode(bool oper, size_t& p) {
string param = retrieveParam(_params, p);
string param = toLowerCase(retrieveParam(_params, p));
if (param.empty())
return;
p++;
Expand All @@ -187,7 +187,7 @@ void ModeCommand::operatorMode(bool oper, size_t& p) {
}

void ModeCommand::voiceMode(bool oper, size_t& p) {
string param = retrieveParam(_params, p);
string param = toLowerCase(retrieveParam(_params, p));
if (param.empty())
return;
p++;
Expand Down
1 change: 1 addition & 0 deletions src/Commands/MotdCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ MotdCommand::MotdCommand(string source, vector<string> params, Client* client) {
client->sendMessage(Replies::ERR_NOTREGISTERED());
throw ClientException();
}

this->_params = params;
this->_source = source;
this->_client = client;
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/NamesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Channel.hpp"
#include "Client.hpp"
#include "Replies.hpp"
#include "Server.hpp"

using std::map;
using std::string;
Expand All @@ -15,7 +16,7 @@ NamesCommand::NamesCommand(string source, vector<string> params,
}

if (params.size() > 0) {
this->_params[0] = params[0];
this->_params[0] = toLowerCase(params[0]);
} else {
this->_params.push_back("");
}
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/NickCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Client.hpp"
#include "NickCommand.hpp"
#include "Server.hpp"

using std::string;
using std::vector;
Expand All @@ -24,7 +25,7 @@ NickCommand::NickCommand(string source, vector<string> params, Client* client) {
throw RegFailedException();
}

string nick = params[0];
string nick = toLowerCase(params[0]);

if (find_if(nick.begin(), nick.end(), isInvalidNick) != nick.end()) {
client->sendMessage(Replies::ERR_ERRONEUSNICKNAME(client, nick));
Expand Down
4 changes: 4 additions & 0 deletions src/Commands/PartCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "PartCommand.hpp"
#include "Replies.hpp"
#include "Server.hpp"

using std::string;
using std::vector;
Expand All @@ -19,6 +20,7 @@ void PartCommand::checkParams(Client* client, vector<string> params) {
client->sendMessage(Replies::ERR_NOTREGISTERED());
throw ClientException();
}

if (params.size() < 1) {
client->sendMessage(Replies::ERR_NEEDMOREPARAMS(client, "PART"));
throw ClientException();
Expand All @@ -31,13 +33,15 @@ void PartCommand::parseParams(Client* client, vector<string> params) {
this->_reason = "";

while (std::getline(iss, chanName, ',')) {
chanName = toLowerCase(chanName);
try {
_channels.push_back(Server::getInstance().findChannel(chanName));
} catch (const Server::ChannelNotFoundException&) {
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, chanName));
continue;
}
}

if (params.size() > 1) {
_reason = params[1];
}
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/PrivmsgCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "PrivmsgCommand.hpp"
#include "Channel.hpp"
#include "Server.hpp"

using std::string;
using std::vector;
Expand All @@ -18,8 +19,8 @@ PrivmsgCommand::PrivmsgCommand(string source, vector<string> params,
client->sendMessage(Replies::ERR_NOTEXTTOSEND(client));
throw ClientException();
}

this->_targets = split(params[0], ',');
string lowered = toLowerCase(params[0]);
this->_targets = split(lowered, ',');
this->_message = params[1];
this->_source = source;
this->_params = params;
Expand Down
7 changes: 5 additions & 2 deletions src/Commands/TopicCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ void TopicCommand::checkParams(Client* client, vector<string> params) {
client->sendMessage(Replies::ERR_NOTREGISTERED());
throw ClientException();
}

if (params.size() == 0) {
client->sendMessage(Replies::ERR_NEEDMOREPARAMS(client, "TOPIC"));
throw ClientException();
}

Channel* chan;
string chanName = toLowerCase(params[0]);

try {
chan = Server::getInstance().findChannel(params[0]);
chan = Server::getInstance().findChannel(chanName);
} catch (const Server::ChannelNotFoundException&) {
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, params[0]));
client->sendMessage(Replies::ERR_NOSUCHCHANNEL(client, chanName));
throw ClientException();
}

Expand Down
28 changes: 14 additions & 14 deletions src/Commands/WhoCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ WhoCommand::WhoCommand(string source, vector<string> params, Client* client) {
this->_source = source;
this->_params = params;
this->_client = client;
this->_target = toLowerCase(_params[0]);
}

WhoCommand::~WhoCommand() {}
Expand All @@ -25,38 +26,37 @@ void WhoCommand::run() {
return;

Channel* chan;
Client* target;
Client* cli;

try {
chan = Server::getInstance().findChannel(_params[0]);
chan = Server::getInstance().findChannel(_target);
} catch (const Server::ChannelNotFoundException&) {
chan = NULL;
}

try {
target = Server::getInstance().findClient(_params[0]);
cli = Server::getInstance().findClient(_target);
} catch (const Server::ClientNotFoundException&) {
target = NULL;
cli = NULL;
}

if (chan == NULL && target == NULL)
if (chan == NULL && cli == NULL)
return;

if (target != NULL) {
if (cli != NULL) {
map<string, Channel*> channels = _client->getChannels();
for (map<string, Channel*>::iterator it = channels.begin();
it != channels.end(); it++) {
if (it->second->isInChannel(target)) {
if (it->second->isInChannel(cli)) {
_client->sendMessage(
Replies::RPL_WHOREPLY(_client, target, it->second));
_client->sendMessage(
Replies::RPL_ENDOFWHO(_client, _params[0]));
Replies::RPL_WHOREPLY(_client, cli, it->second));
_client->sendMessage(Replies::RPL_ENDOFWHO(_client, _target));
return;
}
}
if (!target->isInvisible()) {
_client->sendMessage(Replies::RPL_WHOREPLY(_client, target, NULL));
_client->sendMessage(Replies::RPL_ENDOFWHO(_client, _params[0]));
if (!cli->isInvisible()) {
_client->sendMessage(Replies::RPL_WHOREPLY(_client, cli, NULL));
_client->sendMessage(Replies::RPL_ENDOFWHO(_client, _target));
}
} else {
vector<Client*> clients = chan->getClients();
Expand All @@ -65,6 +65,6 @@ void WhoCommand::run() {
if (chan->isInChannel(_client))
_client->sendMessage(Replies::RPL_WHOREPLY(_client, *it, chan));
}
_client->sendMessage(Replies::RPL_ENDOFWHO(_client, _params[0]));
_client->sendMessage(Replies::RPL_ENDOFWHO(_client, _target));
}
}
Loading

0 comments on commit ec91d4e

Please sign in to comment.