-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKick.cpp
82 lines (81 loc) · 2.6 KB
/
Kick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "Server.hpp"
#include "Client.hpp"
#include "Channel.hpp"
void Server::Kick(std::vector<std::string> command, Client *client)
{
std::string response;
if (command.size() < 3)
{
// display error
response = ":HALAL_TINDER 461 :Not enough parameters\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (command[1][0] != '#')
{
// display error
response = ":HALAL_TINDER 403 :No such channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
Channel *channel = NULL;
for (std::vector<Channel *>::iterator it = channels.begin(); it != channels.end(); ++it)
{
if ((*it)->GetName() == command[1])
{
channel = *it;
break;
}
}
if (channel == NULL)
{
// display error
response = ":HALAL_TINDER 403 :No such channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (!channel->IsInChannel(client))
{
// display error
response = ":HALAL_TINDER 442 :You're not on that channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (!channel->IsChannelOperator(client))
{
// display error
response = ":HALAL_TINDER 482 " + client->get_nick() + " " + channel->GetName() + " :You're not channel operator\r\n";
std::cout << response;
// send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
Client *target = NULL;
std::vector<Client*> clients = channel->GetClients();
for (std::vector<Client *>::iterator it = clients.begin(); it != clients.end(); ++it)
{
if ((*it)->get_nick() == command[2])
{
target = *it;
break;
}
}
if (target == NULL)
{
// display error
response = ":HALAL_TINDER 401 :No such nick/channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (target->get_last_channel() != channel)
{
// display error
response = ":HALAL_TINDER 441 :They aren't on that channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
std::string reason = (command.size() > 3) ? command[3] : client->get_nick();
response = ":HALAL_TINDER KICK " + command[1] + " " + command[2] + " :" + reason + "\r\n";
channel->SendChannelMessage(response);
channel->RemoveUser(target);
target->set_last_channel(NULL);
}