-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMode.cpp
180 lines (176 loc) · 5.68 KB
/
Mode.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "Server.hpp"
#include "Client.hpp"
#include "Channel.hpp"
void Server::Mode(std::vector<std::string> command, Client *client)
{
std::string response;
Channel *channel = NULL;
if (command.size() <= 2)
{
response = ":HALAL_TINDER 461 :Not enough parameters\r\n";
return;
}
if (command.size() > 4)
return;
for (std::vector<Channel *>::iterator it = channels.begin(); it != channels.end(); ++it)
{
if ((*it)->GetName() == command[1])
{
channel = *it;
break;
}
}
if(channel == NULL)
{
response = ":HALAL_TINDER 403 :No such channel\r\n";
return;
}
if (!channel->IsInChannel(client))
{
response = ":HALAL_TINDER 442 " + client->get_nick() + " " + channel->GetName() + " :You're not on that channel\r\n";
return;
}
if(channel->GetClientByNick(client->get_nick()) == "")
{
response = ":HALAL_TINDER 401 " + client->get_nick() + " " + command[1] + " :No such nick/channel\r\n";
return;
}
if (!(channel->IsChannelOperator(client)))
{
response = ":HALAL_TINDER 482 " + client->get_nick() + " " + channel->GetName() + " :You're not channel operator\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return ;
}
if (command[1][0] != '#')
{
response = ":HALAL_TINDER 403 :No such channel\r\n";
send(client->get_fd(), response.c_str(), response.size(), 0);
return;
}
std::string mode;
std::string argument;
if (channel->IsInChannel(client))
{
if (command.size() == 3)
{
mode = command[2];
argument = "";
}
else if (command.size() == 4)
{
mode = command[3];
argument = command[2];
}
}
else
{
return;
}
if (command.size() == 4 && (command[3][0] == '+' || command[3][0] == '-'))
{
argument = command[2];
mode = command[3];
}
else if (command.size() == 4 && (command[2][0] == '+' || command[2][0] == '-'))
{
argument = command[3];
mode = command[2];
}
else
{
return;
}
bool set = false;
bool chanop = false;
for (std::size_t i = 0; i < mode.size(); ++i)
{
char c = mode[i];
if (c == '+')
set = true;
else if (c == '-')
set = false;
else if (c == 'i')
{
channel->SetInviteOnly(set);
if (set)
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :+i\r\n";
else
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :-i\r\n";
channel->SendChannelMessage(response);
}
else if (c == 't')
{
channel->SetOpsChangeTopic(set);
if (set)
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :+t\r\n";
else
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :-t\r\n";
channel->SendChannelMessage(response);
}
else if (c == 'k' && !chanop)
{
chanop = true;
if (set)
{
channel->SetKey(argument);
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :+k " + argument + "\r\n";
}
else
{
channel->UnsetKey();
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :-k\r\n";
}
channel->SendChannelMessage(response);
}
else if (c == 'l' && !chanop)
{
chanop = true;
if (set)
{
if (argument == "")
return ;
int limit;
int pos = 0;
while (pos < (int)argument.size() && isdigit(argument[pos]))
{
pos++;
}
if (pos + 1 == (int)argument.size())
return;
std::istringstream(argument.substr(0, pos)) >> limit;
channel->SetLimit(limit);
std::stringstream ss;
ss << limit;
std::cout << "LIMIT IS: " << limit << std::endl;
std::string slimit = ss.str();
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :+l " + slimit + "\r\n";
}
else
{
channel->SetLimit(MAX_USER_LIMIT);
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :-l\r\n";
}
channel->SendChannelMessage(response);
}
else if (c == 'o' && !chanop)
{
chanop = true;
if (set)
{
if(channel->AddOperator(argument))
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :+o " + argument + "\r\n";
else
response = ":HALAL_TINDER 441 " + client->get_nick() + " " + argument + " :They aren't on that channel\r\n";
channel->SendChannelMessage(response);
}
else
{
if(channel->RemoveOperator(argument))
response = ":" + client->get_nick() + " MODE " + channel->GetName() + " :-o " + argument + "\r\n";
else
response = ":HALAL_TINDER 441 " + client->get_nick() + " " + argument + " :They aren't on that channel\r\n";
channel->SendChannelMessage(response);
}
}
}
}