Repository navigation
Expand file tree
/
Copy pathServer.cpp
More file actions
280 lines (244 loc) · 6.89 KB
/
Copy pathServer.cpp
File metadata and controls
280 lines (244 loc) · 6.89 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "Server.hpp"
int g_stop = 0;
void ft_signal(int signum)
{
(void)signum;
std::cout << "Signal reçu ! Arrêt du serveur" << std::endl;
g_stop = 1;
}
struct pollfd create_pollfd(int sock)
{
struct pollfd nfd;
nfd.fd = sock;
nfd.events = POLLIN;
nfd.revents = 0;
return (nfd);
}
Server::Server(std::string password, int port): _password(password), _port(port){}
Server::~Server(){}
void Server::checkRegistration(Client *client)
{
if (client->getConnected())
{
std::string errorRPL = ERR_ALREADYREGISTRED + client->getNick() + " :You may not reregister\r\n";
send(client->getFd(), errorRPL.c_str(), errorRPL.size(), 0); return;
}
if (client->getVerif() && !client->getNick().empty() && !client->getUser().empty())
{
std::string nick = client->getNick();
std::string user = client->getUser();
// Envoyer les messages de bienvenue
// RPL_WELCOME (001)
std::string welcome = RPL_WELCOME + nick + " :Welcome to the IRC Network " + nick + "!" + user + "@localhost\r\n";
send(client->getFd(), welcome.c_str(), welcome.size(), 0);
// RPL_YOURHOST (002)
std::string yourhost = RPL_YOURHOST + nick + " :Your host is localhost, running version 1.0\r\n";
send(client->getFd(), yourhost.c_str(), yourhost.size(), 0);
// RPL_CREATED (003)
// std::string created = ":localhost 003 " + nick + " :This server was created June 2025\r\n";
// send(client.getFd(), created.c_str(), created.size(), 0);
// RPL_MYINFO (004)
std::string myinfo = RPL_MYINFO + nick + " ircserv\r\n";
send(client->getFd(), myinfo.c_str(), myinfo.size(), 0);
client->setConnected(true);
}
}
Channel* Server::findChannel(std::string name)
{
std::vector<Channel*>::iterator it;
for (it = this->_channels.begin(); it != this->_channels.end(); it++)
{
if ((*it)->getName() == name)
return *it;
}
return (NULL);
}
Client *Server::findClient(const std::string& nick)
{
std::vector<Client *>::iterator end = _clients.end();
for (std::vector<Client *>::iterator it = _clients.begin(); it != end; it++)
if ((*it)->getNick() == nick)
return *it;
return (NULL);
}
void Server::handleCommand(Client *client, const std::string &line)
{
std::istringstream iss(line);
std::string command;
iss >> command;
t_NameToFunc cmds[] = {
{ "PASS", &Server::handlePass },
{ "NICK", &Server::handleNick },
{ "USER", &Server::handleUser },
{ "JOIN", &Server::handleJoin },
{ "PRIVMSG", &Server::handlePrivateMessage },
{ "PING", &Server::handlePing },
{ "KICK", &Server::handleKick },
{ "TOPIC", &Server::handleTopic },
{ "MODE", &Server::handleMode },
{ "INVITE", &Server::handleInvite },
{ "PART", &Server::handlePart },
{ "QUIT", &Server::handleQuit },
{ "", NULL }
};
short i = -1;
while (cmds[++i].RAW != "")
if (cmds[i].RAW == command)
{
if (command != "PASS" && command != "NICK" && command != "USER" && !client->getConnected())
{
std::string errorReply = std::string(ERR_NOTREGISTERED) + "* :You have not registered\r\n";
send(client->getFd(), errorReply.c_str(), errorReply.size(), 0); return;
}
return ((this->*cmds[i].handle)(client, line), (void)0);
}
}
void Server::add_client()
{
int sock = this->_fds[0].fd;
sockaddr_in client;
socklen_t client_size = sizeof(client);
int clientfd = accept(sock, (struct sockaddr *)&client, &client_size);
if (clientfd < 0)
return;
Client * new_client = new Client(clientfd);
this->_clients.push_back(new_client);
this->_fds.push_back(create_pollfd(clientfd));
}
void Server::handleClientInput(Client *client, const std::string &input, size_t fd_index)
{
(void)fd_index;
std::istringstream iss(input);
std::string line;
while (std::getline(iss, line))
{
if (!line.empty() && line[line.length() - 1] != '\r')
{
client->addBuffer(line);
return;
}
if (!line.empty() && line[line.length() - 1] == '\r')
{
if (!client->getBuffer().empty())
{
line = client->getBuffer() + line.substr(0, line.length() - 1);
client->setBufferEmpty();
}
else
line = line.substr(0, line.length() - 1);
}
handleCommand(client, line);
}
}
void Server::eraseUserServer(Client* user)
{
std::vector<Client*>::iterator it = std::find(this->_clients.begin(), this->_clients.end(), user);
if (it != this->_clients.end())
this->_clients.erase(it);
}
void Server::eraseChannelServer(Channel* channel)
{
std::vector<Channel*>::iterator it = std::find(this->_channels.begin(), this->_channels.end(), channel);
if (it != this->_channels.end())
this->_channels.erase(it);
}
void Server::read_client()
{
for (size_t i = 0; i < this->_fds.size(); i++)
{
if (this->_fds[i].revents & POLLIN)
{
if (i == 0)
{
add_client();
}
else
{
char receipt[4096];
int bytes_receive = recv(this->_fds[i].fd, receipt, sizeof(receipt) - 1, 0);
if (bytes_receive > 0)
{
receipt[bytes_receive] = '\0';
std::string recu = receipt;
std::cout << receipt;
for (size_t j = 0; j < this->_clients.size(); ++j)
{
if (this->_clients[j]->getFd() == this->_fds[i].fd)
{
handleClientInput(this->_clients[j], recu, i);
break;
}
}
}
else
{
close(this->_fds[i].fd);
this->_fds.erase(this->_fds.begin() + i);
for (size_t j = 0; j < this->_clients.size(); ++j)
{
if (this->_clients[j]->getFd() == this->_fds[i].fd)
{
this->eraseUserServer(this->_clients[j]);
}
}
--i;
}
}
}
else if (this->_fds[i].revents & POLLHUP)
{
close(this->_fds[i].fd);
this->_fds.erase(this->_fds.begin() + i);
for (size_t j = 0; j < this->_clients.size(); ++j)
{
if (this->_clients[j]->getFd() == this->_fds[i].fd)
{
this->eraseUserServer(this->_clients[j]);
}
}
--i;
}
}
}
void Server::init()
{
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (sock == -1)
std::cerr << "Error : Socket failed to create\n";
sockaddr_in ipport;
ipport.sin_family = AF_INET;
ipport.sin_port = htons(this->_port);
ipport.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(sock, (struct sockaddr *)&ipport, sizeof(ipport)) == -1)
{
close(sock);
throw BindFailed();
}
if (listen(sock, 10) == -1)
{
close(sock);
throw ListenFailed();
}
this->_fds.push_back(create_pollfd(sock));
}
void Server::start()
{
this->init();
signal(SIGINT, ft_signal);
signal(SIGQUIT, ft_signal);
while (!g_stop)
{
int ret = poll(&this->_fds[0], this->_fds.size(), 100);
if (ret > 0)
read_client();
}
// Fermeture de tous les sockets;
for (size_t i=0; i < this->_fds.size(); i++)
close(this->_fds[i].fd);
for (size_t i=0; i < this->_clients.size(); i++)
delete this->_clients[i];
for (size_t i=0; i < this->_channels.size(); i++)
delete this->_channels[i];
}