Skip to content

Commit c53252f

Browse files
committed
PRIVMSG AND KICK done
1 parent 8a43452 commit c53252f

File tree

6 files changed

+101
-111
lines changed

6 files changed

+101
-111
lines changed

headers/Channels.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class Channel
5151
void setNeedPassword(bool _needpassword);
5252
void setTopic(std::string _topic);
5353
std::string getTopic();
54-
5554
bool isAMember(std::string _user);
5655
void deleteMember(std::string _user);
5756
std::string get_modes();

headers/Client.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class Client
2626
bool valid_pswd;
2727
struct pollfd fd_client;
2828
struct sockaddr_in addr_client;
29-
3029
std::vector<Channel*> operator_channels;
3130
std::vector<std::string> invitations_channels;
3231

@@ -41,7 +40,6 @@ class Client
4140
void setNickname(std::string _nickname);
4241
void setConnected(bool _connected);
4342
void setValidPswd(bool _validpswd);
44-
4543
std::vector<Channel*> getOperatorChannels();
4644
void addOperatorChannels(Channel* _channel);
4745
void removeOperatorChannels(Channel* _channel);

headers/IrcServer.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class IrcServer
1616
INVALID_COMMAND,
1717
PASS,
1818
NICK,
19-
CPRIVMSG,
2019
PRIVMSG,
2120
JOIN,
2221
MODE,
@@ -58,13 +57,13 @@ class IrcServer
5857
void kFlag(std::vector<std::string> _command, std::string _flag, int _pos, unsigned int i);
5958
void oFlag(std::vector<std::string> _command, std::string _flag, int _pos, unsigned int i);
6059
void lFlag(std::vector<std::string> _command, std::string _flag, int _pos, unsigned int i);
61-
6260
int userExist(std::string _nickname);
6361
void checkInvite(std::string _msg, int _pos);
6462
int searchUserInChannel(std::string _user, std::string _channel);
65-
6663
void checkKick(std::string _msg, int _pos);
6764
void checkTopic(std::string _msg, int _pos);
65+
int splitPrivmsgCommand(int _pos, std::string _msg, std::vector<std::string>& _receivers);
66+
int splitKickCommand(int _pos, std::string _msg, std::vector<std::string>& _users, std::vector<std::string>& _channels);
6867
};
6968

7069
bool isInt(const char* str_nb);

srcs/IrcServer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,6 @@ int IrcServer::whichCommand(std::string _msg)
283283
return (PASS);
284284
else if (_msg.size() >= 4 && _msg.substr(0, 4) == "NICK")
285285
return (NICK);
286-
else if (_msg.size() >= 8 && _msg.substr(0, 8) == "CPRIVMSG")
287-
return (CPRIVMSG);
288286
else if (_msg.size() >= 7 && _msg.substr(0, 7) == "PRIVMSG")
289287
return (PRIVMSG);
290288
else if (_msg.size() >= 4 && _msg.substr(0, 4) == "JOIN")

srcs/kick.cpp

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,93 @@
22
#include "../headers/Colors.hpp"
33
#include "sstream"
44

5-
void IrcServer::checkKick(std::string _msg, int _pos)
5+
int IrcServer::splitKickCommand(int _pos, std::string _msg, std::vector<std::string>& _users, std::vector<std::string>& _channels)
66
{
7-
//voir ERR_BADCHANMASK
87
std::istringstream split_msg(_msg);
98
std::string token, msg_send;
109
std::vector<std::string> command;
11-
int target_user, target_channel, step, i = 1, command_size;
10+
unsigned int i;
1211

1312
while(split_msg >> token)
1413
command.push_back(token);
1514

16-
command_size = command.size();
17-
if (command_size < 3)
15+
if (command.size() < 3)
1816
{
1917
// ERR_NEEDMOREPARAMS
20-
return;
18+
return (0);
19+
}
20+
else if (command.size() > 3 && command[3].find(':') == std::string::npos)
21+
{
22+
msg_send = RED ITALIC "[Error: Invalid command: syntax = KICK channel user :...]\n" END_STYLE;
23+
send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
24+
return (0);
2125
}
2226

23-
step = command_size / 2;
24-
if (command_size % 2 == 0)
25-
step -= 1;
26-
while (i < step + 1)
27+
for (i = 0 ; i < command[1].size() ; i++)
28+
{
29+
std::string channel;
30+
while (i < command[1].size() && command[1][i] != ',')
31+
channel += command[1][i++];
32+
_channels.push_back(channel);
33+
if (i == command[1].size())
34+
break;
35+
}
36+
37+
for (i = 0 ; i < command[2].size() ; i++)
38+
{
39+
std::string user;
40+
while (i < command[2].size() && command[2][i] != ',')
41+
user += command[2][i++];
42+
_users.push_back(user);
43+
if (i == command[2].size())
44+
break;
45+
}
46+
47+
return (1);
48+
}
49+
50+
void IrcServer::checkKick(std::string _msg, int _pos)
51+
{
52+
//voir ERR_BADCHANMASK
53+
std::string msg_send;
54+
std::vector<std::string> users, chan;
55+
int target_user, target_channel;
56+
unsigned int i = 0;
57+
58+
if (!splitKickCommand(_pos, _msg, users, chan))
59+
return ;
60+
while (i < users.size())
2761
{
28-
target_user = userExist(command[i + step]);
29-
target_channel = channelExist(command[i]);
62+
target_user = userExist(users[i]);
63+
target_channel = channelExist(chan[i]);
3064

3165
if (target_user == -1)
3266
{
33-
msg_send = BLUE ITALIC "[User '" + command[i + step] + "' doesn't exist.]\n" END_STYLE;
67+
msg_send = BLUE ITALIC "[User '" + users[i] + "' doesn't exist.]\n" END_STYLE;
3468
send(fd_sockets[target_user + 1].fd, msg_send.c_str(), msg_send.size(), 0);
3569
}
3670
else if (target_channel == -1)
3771
{
3872
std::cout << "ERR_NOSUCHCHANNEL" << std::endl;
3973
// ERR_NOSUCHCHANNEL
4074
}
41-
else if (searchUserInChannel(command[i + step], command[i]) == -1)
75+
else if (searchUserInChannel(users[i], chan[i]) == -1)
4276
{
4377
std::cout << "ERR_NOTONCHANNEL" << std::endl;
4478
//ERR_NOTONCHANNEL
4579
}
46-
else if (!clients[_pos - 1]->operatorOfThisChannel(command[i]))
80+
else if (!clients[_pos - 1]->operatorOfThisChannel(chan[i]))
4781
{
4882
std::cout << "ERR_CHANOPRIVSNEEDED" << std::endl;
4983
// ERR_CHANOPRIVSNEEDED
5084
}
5185
else
5286
{
53-
channels[target_channel]->deleteMember(command[i + step]);
54-
if (command_size % 2 != 0)
55-
msg_send = BLUE ITALIC "[You were kicked from " + command[i] + " channel]\n" END_STYLE;
87+
channels[target_channel]->deleteMember(users[i]);
88+
if (_msg.find(':') == std::string::npos)
89+
msg_send = BLUE ITALIC "[You were kicked from " + chan[i] + " channel]\n" END_STYLE;
5690
else
57-
msg_send = BLUE ITALIC "[You were kicked from " + command[i] + " channel. Reason: " + command[command_size - 1] + "]\n" END_STYLE;
91+
msg_send = BLUE ITALIC "[You were kicked from " + chan[i] + " channel. Reason: " + _msg.substr(_msg.find(':') + 1, _msg.size()) + "]\n" END_STYLE;
5892
send(fd_sockets[target_user + 1].fd, msg_send.c_str(), msg_send.size(), 0);
5993
}
6094
i++;

srcs/privmsg.cpp

Lines changed: 46 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,67 @@
22
#include "../headers/Colors.hpp"
33
#include "sstream"
44

5-
int IrcServer::receiver(std::string _nickname)
6-
{
7-
for (unsigned int i = 0 ; i < clients.size() ; i++)
8-
{
9-
if (_nickname == clients[i]->getNickname())
10-
return (i);
11-
}
12-
return (-1);
13-
}
14-
155
//remplacer nicknameAlreadyUsed par nicknameExist
166

17-
// void IrcServer::checkCprivmsg(std::string _msg, int _pos)
18-
// {
19-
// std::istringstream split_msg(_msg);
20-
// std::string token;
21-
// std::string msg_send;
22-
// std::vector<std::string> command_msg;
23-
24-
// while(split_msg >> token)
25-
// command_msg.push_back(token);
26-
27-
// if (command_msg.size() < 3)
28-
// {
29-
// msg_send = RED ITALIC "[Error: Incomplete command: syntax = CPRIVMSG name :...]\n" END_STYLE;
30-
// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
31-
// return;
32-
// }
33-
// else
34-
// {
35-
// if (command_msg[1].find(':') != std::string::npos || command_msg[2].find(':') == std::string::npos)
36-
// {
37-
// msg_send = RED ITALIC "[Error: Incomplete command: syntax = CPRIVMSG name :...]\n" END_STYLE;
38-
// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
39-
// return;
40-
// }
41-
// else
42-
// {
43-
// if (_msg.size() == _msg.find(':'))
44-
// {
45-
// msg_send = RED ITALIC "[Error: Incomplete command: syntax = CPRIVMSG name :...]\n" END_STYLE;
46-
// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
47-
// return;
48-
// }
49-
// msg_send = _msg.substr(_msg.find(':') + 1, _msg.size()) + "\n";
50-
// if (!find_channel(command_msg[1], msg_send, _pos))
51-
// {
52-
// msg_send = RED ITALIC "[Error: This nickname does not match any channel]\n" END_STYLE;
53-
// send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
54-
// }
55-
// }
56-
// }
57-
// }
58-
59-
void IrcServer::checkPrivmsg(std::string _msg, int _pos)
7+
int IrcServer::splitPrivmsgCommand(int _pos, std::string _msg, std::vector<std::string>& _receivers)
608
{
619
std::istringstream split_msg(_msg);
62-
std::string token;
63-
std::string msg_send;
64-
std::vector<std::string> command_msg;
10+
std::string token, msg_send;
11+
std::vector<std::string> command;
12+
unsigned int i;
6513

6614
while(split_msg >> token)
67-
command_msg.push_back(token);
68-
69-
if (command_msg.size() < 3)
15+
command.push_back(token);
16+
17+
if (command.size() < 3 || command[2].find(':') == std::string::npos || _msg.size() == _msg.find(':'))
7018
{
71-
msg_send = RED ITALIC "[Error: Incomplete command: syntax = PRIVMSG name :...]\n" END_STYLE;
19+
// ERR_NEEDMOREPARAMS
20+
msg_send = RED ITALIC "[Error: Invalid command: syntax = PRIVMSG name :message]\n" END_STYLE;
7221
send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
22+
return (0);
7323
}
74-
else
24+
25+
for (i = 0 ; i < command[1].size() ; i++)
7526
{
76-
if (command_msg[1].find(':') != std::string::npos || command_msg[2].find(':') == std::string::npos)
27+
std::string receiver;
28+
while (i < command[1].size() && command[1][i] != ',')
29+
receiver += command[1][i++];
30+
_receivers.push_back(receiver);
31+
if (i == command[1].size())
32+
break;
33+
}
34+
35+
return (1);
36+
}
37+
38+
void IrcServer::checkPrivmsg(std::string _msg, int _pos)
39+
{
40+
std::string msg_send;
41+
std::vector<std::string> receivers;
42+
unsigned int i = 0;
43+
int user, channel;
44+
45+
if (!splitPrivmsgCommand(_pos, _msg, receivers))
46+
return ;
47+
while (i < receivers.size())
48+
{
49+
user = userExist(receivers[i]);
50+
channel = channelExist(receivers[i]);
51+
52+
msg_send = _msg.substr(_msg.find(':') + 1, _msg.size()) + "\n";
53+
if (user == -1 && channel == -1)
7754
{
78-
msg_send = RED ITALIC "[Error: Incomplete command: syntax = PRIVMSG name :...]\n" END_STYLE;
55+
msg_send = RED ITALIC "[Error: This nickname does not match any user or channel]\n" END_STYLE;
7956
send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
8057
}
81-
else
58+
else if (user != -1)
8259
{
83-
int dest = receiver(command_msg[1]);
84-
if (_msg.size() == _msg.find(':'))
85-
{
86-
msg_send = RED ITALIC "[Error: Incomplete command: syntax = PRIVMSG name :...]\n" END_STYLE;
87-
send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
88-
return;
89-
}
90-
msg_send = _msg.substr(_msg.find(':') + 1, _msg.size()) + "\n";
91-
if (dest != -1)
92-
{
93-
std::string temp = PURPLE "[From: " + clients[_pos - 1]->getNickname() + "] => " END_STYLE BOLD + msg_send + END_STYLE;
94-
send(clients[dest]->getFdClient().fd, temp.c_str(), temp.size(), 0);
95-
}
96-
if (find_channel(command_msg[1], msg_send, _pos))
97-
return ;
98-
else
99-
{
100-
msg_send = RED ITALIC "[Error: This nickname does not match any user or channel]\n" END_STYLE;
101-
send(fd_sockets[_pos].fd, msg_send.c_str(), msg_send.size(), 0);
102-
}
60+
std::string temp = PURPLE "[From: " + clients[_pos - 1]->getNickname() + "] => " END_STYLE BOLD + msg_send + END_STYLE;
61+
send(clients[user]->getFdClient().fd, temp.c_str(), temp.size(), 0);
10362
}
63+
else if (channel != -1)
64+
find_channel(receivers[i], msg_send, _pos);
65+
i++;
10466
}
10567
}
10668

0 commit comments

Comments
 (0)