add utils_cmd.cpp and fix part delete all data

This commit is contained in:
kinou-p 2023-03-09 01:51:48 +01:00
parent 9074d655a5
commit f25e6f46a6
7 changed files with 309 additions and 55 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/12/01 17:27:29 by apommier #+# #+# #
# Updated: 2023/02/19 22:11:26 by apommier ### ########.fr #
# Updated: 2023/03/09 00:20:47 by apommier ### ########.fr #
# #
# **************************************************************************** #
@ -43,6 +43,7 @@ SRCS := main.cpp\
commands/channel_op/invite.cpp\
commands/channel_op/kick.cpp\
commands/channel_op/mode.cpp\
commands/channel_op/utils_mode.cpp\
commands/channel_op/topic.cpp\
\
commands/operator/squit.cpp\

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 17:27:37 by apommier #+# #+# */
/* Updated: 2023/03/03 22:19:08 by apommier ### ########.fr */
/* Updated: 2023/03/09 01:11:57 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,6 +33,8 @@
#include <algorithm>
#include <list>
#include <sstream>
#include "function_tab.hpp"
#include "accessList.hpp"
@ -60,8 +62,8 @@ struct channelData;
struct chanMode
{
bool o; //- donne/retire les privilèges d'opérateur de canal
bool p; //- drapeau de canal privé
bool s; //- drapeau de canal secret
bool p; //- drapeau de canal privé *
bool s; //- drapeau de canal secret *
bool i; //- drapeau de canal accessible uniquement sur invitation
bool t; //- drapeau de sujet de canal modifiable uniquement par les opérateurs
bool n; //- pas de messages dans un canal provenant de clients à l'extérieur du canal
@ -235,3 +237,11 @@ void connect_client(fdList &allFds, int userNbr);
void parse_commands(std::string buffer, fdList &allFds, int userNbr);
/* ************************************************************************** */
/* ******************************MODE UTILS********************************** */
/* ************************************************************************** */
std::string chan_reply(channelData &chan, clientData &user);
std::string user_reply(clientData &user);
int search_and_erase(std::string &str, std::string toFind);
bool str_to_int(int &i, const std::string s);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/19 22:10:07 by apommier #+# #+# */
/* Updated: 2023/03/03 22:18:44 by apommier ### ########.fr */
/* Updated: 2023/03/09 01:48:54 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,9 @@ void leave_chan(fdList &allFds, std::string chan, int userNbr, std::string msg)
cmd_error(allFds, allFds.userData[userNbr].fd, "422 * " + chan + " :You're not on that channel\n");
return ;
}
del_user_in_chan(&allFds.userData[userNbr], &allFds.channelList[chanPos]);
//del_user_in_chan(&allFds.userData[userNbr], &allFds.channelList[chanPos]);
int pos = find_client_list(allFds.channelList[chanPos].userList, &allFds.userData[userNbr]);
allFds.channelList[chanPos].userList.erase(allFds.channelList[chanPos].userList.begin() + pos);
del_chan_in_user(&allFds.userData[userNbr], &allFds.channelList[chanPos]);
//:WiZ!jto@tolsun.oulu.fi PART #playzone :I lost
//:awd!kinou@kinou PART #test

View File

@ -6,50 +6,192 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/11 19:19:30 by apommier #+# #+# */
/* Updated: 2023/02/15 17:02:12 by apommier ### ########.fr */
/* Updated: 2023/03/09 01:49:10 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/ft_irc.hpp"
int search_and_erase(std::string &str, std::string toFind)
{
// std::vector<string>::iterator i = str.begin;
int i = 1; //1 instead of 0 to skip +/-
while (str[i] != 0)
{
if (toFind.find(str[i]) == std::string::npos)
str.erase(str.begin() + i);
else
i++;
}
std::cout << "splitbuff[2]after? = " << str << std::endl;
return 1;
}
void do_chan_option(fdList &allFds, int userNbr, std::string opt /*, channel (string or direct reference or pointer but no copy)*/)
void chan_opt_o(fdList &allFds, int userNbr, std::vector<std::string> opt, int chanNbr, bool sign)
{
(void)allFds;
(void)userNbr;
for (int i = 0; opt[i]; i++)
(void)chanNbr;
if (opt.size() != 4)
{
//mode_i()
//call option
//switch ? if forest? map container ? 2 tab (name and function pointer) ?
// opt_i(int signe (+ ou -), channel, user);
std::cout << "ERR_NEEDMOREPARAMS" << std::endl;
return ;
}
if ((find_user(allFds, opt[3]) == -1))
{
std::cout << "User :" << opt[3] << " not found" << std::endl;
return ;
}
//A voir
if (allFds.userData[userNbr].mode.o == true && sign == true)
allFds.userData[find_user(allFds, opt[3])].mode.o = (sign = true) ? true : false;
std::cout << "-------> " << opt[3] << std::endl;
}
void chan_opt_k(fdList &allFds, std::vector<std::string> opt, int chanNbr, bool sign)
{
if (opt.size() != 4)
{
std::cout << "ERR_NEEDMOREPARAMS" << std::endl;
std::cout << "how to use it :/MODE <channel >+k <password>" << std::endl;
return ;
}
if (sign == true)
allFds.channelList[chanNbr].password = opt[3];
}
void chan_opt_v(fdList &allFds, int userNbr, std::vector<std::string> opt, int chanNbr, bool sign)
{
(void)userNbr;
(void)chanNbr;
(void)sign;
if (opt.size() < 4)
{
std::cout << "ERR_NEEDMOREPARAMS" << std::endl;
std::cout << "how to use it :/MODE <channel> +/-v <user>" << std::endl;
return ;
}
int target = find_user(allFds, opt[3]);
if (target == -1)
{
std::cout << "no user found " << std::endl;
return ;
}
int pos = find_client_list(allFds.channelList[chanNbr].verboseList, &allFds.userData[target]);
if (sign == true && pos == -1)
allFds.channelList[chanNbr].verboseList.push_back(&allFds.userData[target]);
if (sign == false && pos != -1)
allFds.channelList[chanNbr].verboseList.erase(allFds.channelList[chanNbr].verboseList.begin() + pos);
}
void chan_opt_b(fdList &allFds, int userNbr, std::vector<std::string> opt, int chanNbr, bool sign)
{
std::vector<clientData *> ban(allFds.channelList[chanNbr].banList);
(void)userNbr;
(void)opt;
(void)chanNbr;
(void)sign;
(void)ban;
if (opt.size() == 3 && sign == true)
{
if (ban.empty())
{
std::cout << "Nobody was banned on this channel" << std::endl;
return ;
}
for (long unsigned int i = 0; i < ban.size(); ++i)
{
std::cout << ban[i]->userName << std::endl;
}
}
if (opt.size() > 3)
{
int target_in_client = find_user(allFds, opt[3]);
if (target_in_client == -1)
{
std::cout << "No user found" << std::endl;
return ;
}
int target_in_ban = find_client_list(allFds.channelList[chanNbr].banList, &allFds.userData[target_in_client]);
if (sign == true && target_in_ban == -1)
allFds.channelList[chanNbr].banList.push_back(&allFds.userData[target_in_client]);
if (sign == false && target_in_ban != -1)
allFds.channelList[chanNbr].banList.erase(allFds.channelList[chanNbr].banList.begin() + (target_in_ban));
}
}
void do_chan_opt(fdList &allFds, int userNbr, std::vector<std::string> opt, int chanNbr /*, channel (string or direct reference or pointer but no copy)*/)
{
(void)allFds;
(void)userNbr;
bool sign = true;
if (opt[2][0] == '-')
sign = false;
for (int i = 1; opt[2][i]; i++)
{
switch (opt[2][i])
{
case 'o': chan_opt_o(allFds, userNbr, opt, chanNbr, sign); //std::cout << "launching option: " << opt[2][i] << std::endl;
break ;
case 'p': allFds.channelList[chanNbr].mode.p = (sign = true) ? true : false;
break ;
case 's': allFds.channelList[chanNbr].mode.s = (sign = true) ? true : false;
break ;
case 'i': allFds.channelList[chanNbr].mode.i = (sign = true) ? true : false;
break ;
case 't': allFds.channelList[chanNbr].mode.t = (sign = true) ? true : false;
break ;
case 'n': allFds.channelList[chanNbr].mode.n = (sign = true) ? true : false;
break ;
case 'm': allFds.channelList[chanNbr].mode.m = (sign = true) ? true : false;
break ;
case 'l':
if (sign == true)
str_to_int(allFds.channelList[chanNbr].maxUser, opt[3]);
break ;
case 'b': chan_opt_b(allFds, userNbr, opt, chanNbr, sign);
break ;
case 'v': chan_opt_v(allFds, userNbr, opt, chanNbr, sign);
break ;
case 'k': chan_opt_k(allFds, opt, chanNbr, sign);
break ;
default : std::cout << "Not launching option" << std::endl;
break ;
}
}
return;
}
void do_user_option(fdList &allFds, int userNbr, std::string opt)
void do_user_opt(fdList &allFds, int userNbr, std::vector<std::string> opt, int new_target)
{
(void)allFds;
(void)userNbr;
for (int i = 0; opt[i]; i++)
(void)new_target;
//char opts[4] = {'i', 's', 'w', 'o'};
bool sign = true;
if (opt[2][0] == '-')
sign = false;
for (int i = 1; opt[2][i]; i++)
{
//call option
//switch ? if forest? map container ? 2 tab (name and function pointer) ?
// opt_i(int signe (+ ou -), user);
//int j = 0;
//while (opts[j] && opts[j] != opt[2][i])
// j++;
switch(opt[2][i])
{
case 'i':
if (allFds.userData[userNbr].userName != opt[1])
{
cmd_error(allFds, allFds.userData[userNbr].fd, "401 *" + opt[1] + " :No such nick/channel\n");
return ;
}
allFds.userData[new_target].mode.i = (sign = true) ? true : false;
break ;
case 's': allFds.userData[new_target].mode.s = (sign = true) ? true : false;
break ;
case 'w': allFds.userData[new_target].mode.w = (sign = true) ? true : false;
break ;
case 'o':
if (allFds.userData[userNbr].mode.o == false)
{
cmd_error(allFds, allFds.userData[userNbr].fd, "482 *" + opt[1] + " :You're not channel\n");
return ;
}
allFds.userData[new_target].mode.o = (sign = true) ? true : false;
break ;
default: std::cout << "Default" << std::endl;
break ;
}
}
return;
}
@ -58,22 +200,18 @@ void MODE(std::string buffer, fdList &allFds, int userNbr)
{
(void)userNbr;
std::vector<std::string> splitBuff;
int pos;
split(buffer, ' ', splitBuff);
if (splitBuff.size() < 3)
{
std::cout << "ERR_NEEDMOREPARAMS" << std::endl;
return ;
}
//check if user is op? else error
//check if (splitBuff[2][0] == +/-) //splitbuff[2] always equal to {[+|-]|o|p|s|i|t|n|b|v}
//else ??? erreur j'imagine mais j'ai pas trouvé
if (splitBuff[1][0] == '#' || splitBuff[1][0] == '&') //splitbuff[1] always equal to <channel> or <nickname>
{
if (find_channel(allFds, splitBuff[1]) == -1) //if true chan doesn't exist
if ((pos = find_channel(allFds, splitBuff[1])) == -1) //if true chan doesn't exist
{
std::cout << "chan doesn't exist" << std::endl;
// 403 ERR_NOSUCHCHANNEL
// "<nom de canal> :No such channel"
std::cout << splitBuff[1] << ": No such channel" << std::endl;
return ;
}//else
//verify_option(allFds, str, i); //needed?
@ -82,17 +220,33 @@ void MODE(std::string buffer, fdList &allFds, int userNbr)
search_and_erase(splitBuff[2], "opsitnmlbvk");
std::cout << "splitbuff[2] after = " << splitBuff[2] << std::endl;
if ((splitBuff[2].size() < 2) || (splitBuff[2][0] != '-' && splitBuff[2][0] != '+'))
{
std::cout << "Bad params" << std::endl;
return ;
}
do_chan_opt(allFds, userNbr, splitBuff, pos);
//do_option one by one here (do_chan_opt)?
return ;
}
else if ((find_user(allFds, splitBuff[1]) == -1))
// pos = find_user(allFds, splitBuff[1]);
else if ((pos = find_user(allFds, splitBuff[1])) == -1)
{
std::cout << "user doesn't exist" << std::endl;
// 401 ERR_NOSUCHNICK
// "<pseudonyme> :No such nick/channel"
return ;
}
search_and_erase(splitBuff[2], "iwso");
else
{
search_and_erase(splitBuff[2], "iswo");
if ((splitBuff[2].size() < 2) && (splitBuff[2][0] != '-' || splitBuff[2][0] != '+'))
{
std::cout << "Bad params" << std::endl;
return ;
}
do_user_opt(allFds, userNbr, splitBuff, pos);
}
//do_option one by one here (do_user_opt)?
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_mode.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/09 00:13:32 by apommier #+# #+# */
/* Updated: 2023/03/09 01:47:02 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/ft_irc.hpp"
// std::string ban_reply
std::string chan_reply(channelData &chan, clientData &user)
{
//:irc.server.com 324 ClientName #channel +tnkLs 10 operator1 operator2
(void) chan;
//int mode;
//std::string reply = " +";
std::string reply = ":irc.local 324 " + user.nickname + " " + chan.name + " +";
if (chan.mode.p)
reply += 'p';
if (chan.mode.s)
reply += 's';
if (chan.mode.i)
reply += 'i';
if (chan.mode.t)
reply += 't';
if (chan.mode.n)
reply += 'n';
if (chan.mode.m)
reply += 'm';
if (chan.mode.l)
reply += 'l';
if (chan.mode.k)
reply += 'k';
if (chan.mode.l)
reply += chan.maxUser;
for (size_t i = 0; i < chan.opList.size(); i++)
reply += " " + chan.opList[i]->nickname;
//if (reply.size() > 2)
// reply_begin += reply;
return (reply);
}
std::string user_reply(clientData &user)
{
//:irc.server.com 221 ClientName +ix
//std::string reply;
std::string reply = " +";
std::string reply_begin = ":irc.local 221 " + user.nickname + " +";
if (user.mode.i)
reply += 'i';
if (user.mode.s)
reply += 's';
if (user.mode.w)
reply += 'w';
if (user.mode.o)
reply += 'o';
return (reply);
}
int search_and_erase(std::string &str, std::string toFind)
{
// std::vector<string>::iterator i = str.begin;
int i = 1; //1 instead of 0 to skip +/-
while (str[i] != 0)
{
if (toFind.find(str[i]) == std::string::npos)
str.erase(str.begin() + i);
else
i++;
}
std::cout << "splitbuff[2]after? = " << str << std::endl;
return 1;
}
bool str_to_int(int &i, const std::string s)
{
std::stringstream ss(s);
ss >> i;
if (ss.fail())
return false;
return true;
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/12 20:38:50 by apommier #+# #+# */
/* Updated: 2023/03/08 01:08:10 by apommier ### ########.fr */
/* Updated: 2023/03/09 01:44:08 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,16 +24,16 @@ void del_user_in_chan(clientData *user, channelData *chan)
chan->userList.erase(chan->userList.begin() + pos);
if ((pos = find_client_list(chan->banList, user)) != -1)
chan->userList.erase(chan->banList.begin() + pos);
chan->banList.erase(chan->banList.begin() + pos);
if ((pos = find_client_list(chan->invitedList, user)) != -1)
chan->userList.erase(chan->invitedList.begin() + pos);
chan->invitedList.erase(chan->invitedList.begin() + pos);
if ((pos = find_client_list(chan->verboseList, user)) != -1)
chan->userList.erase(chan->verboseList.begin() + pos);
chan->verboseList.erase(chan->verboseList.begin() + pos);
if ((pos = find_client_list(chan->opList, user)) != -1)
chan->userList.erase(chan->opList.begin() + pos);
chan->opList.erase(chan->opList.begin() + pos);
chan->nbrUser--;
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 21:46:41 by apommier #+# #+# */
/* Updated: 2023/03/03 22:56:34 by apommier ### ########.fr */
/* Updated: 2023/03/09 01:18:14 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -81,13 +81,11 @@ int not_contain_other(std::string str, std::string toFind)
int find_client_list(std::vector<clientData *> &list, clientData *client)
{
std::vector<clientData*>::iterator start = list.begin();
std::vector<clientData*>::iterator pastEnd = list.end();
while (start != pastEnd)
int size = list.size();
for (int i = 0; i < size; i++)
{
if (*start == client)
return (start - pastEnd - 1);
start++;
if (list[i] == client)
return (i);
}
return (-1);
}