-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvite.cpp
67 lines (66 loc) · 2.17 KB
/
Invite.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
#include "Server.hpp"
#include "Client.hpp"
#include "Channel.hpp"
void Server::Invite(std::vector<std::string> command, Client *client)
{
std::string response;
if (command.size() != 3)
{
response = ":HALAL_TINDER 461 :Not enough parameters\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (command[2][0] != '#')
{
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[2])
{
channel = *it;
break;
}
}
if (channel == NULL)
{
response = ":HALAL_TINDER 403 :No such channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (!channel->IsInChannel(client))
{
response = ":HALAL_TINDER 442 :You're not on that channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
Client *invitee = NULL;
for (std::map<int, Client>::iterator it = clients.begin(); it != clients.end(); ++it)
{
if (it->second.get_nick() == command[1])
{
invitee = &it->second;
break;
}
}
if (invitee == NULL)
{
response = ":HALAL_TINDER 401 :No such nick/channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
if (channel->IsInChannel(invitee))
{
response = ":HALAL_TINDER 443 :User is already in channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
std::string message = ":" + client->get_nick() + "!" + client->get_user() + "@HALAL_TINDER INVITE " + invitee->get_nick() + " " + channel->GetName() + "\r\n";
send(invitee->get_fd(), message.c_str(), message.size(), 0);
response = ":HALAL_TINDER 341 :Invite sent\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
channel->InviteUser(invitee);
}