forked from cgray987/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
349 lines (302 loc) · 9.98 KB
/
Server.cpp
File metadata and controls
349 lines (302 loc) · 9.98 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "Server.hpp"
#include "Log.hpp"
Server::Server(){}
Server::Server(const Server &src) : _port(src._port), _password(src._password){*this = src;}
Server &Server::operator = (const Server &src)
{
_port = src._port;
_password = src._password;
return (*this);
}
Server::Server(int port, std::string password) : _port(port), _password(password)
{
init_command_map();
time_t rawtime;
time(&rawtime);
struct tm *timeinfo = localtime(&rawtime);
char buf[80];
strftime(buf, sizeof(buf), "%d-%m-%Y %H:%M:%S", timeinfo);
std::string str(buf);
_start_time = str;
//create server listening socket socket(domain, type, protocol)
_server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (_server_socket == -1)
throw std::runtime_error("Socket creation failure!");
//ip address schema
struct sockaddr_in server_address;
server_address.sin_family = AF_INET; //always req'd
server_address.sin_port = htons(_port); //convert port to network byte
server_address.sin_addr.s_addr = INADDR_ANY; //binds socket to all available interfaces (=0)
if (bind(_server_socket, reinterpret_cast<struct sockaddr *>(&server_address), sizeof(server_address)) == -1)
throw std::runtime_error("Bind failure!");
if (listen(_server_socket, SOMAXCONN) == -1)
throw std::runtime_error("Listen failure!");
//epoll socket
struct epoll_event event;
_epoll_socket = epoll_create1(0);
if (_epoll_socket == -1)
throw std::runtime_error("epoll error!");
_server_user = new User("ServerNick", "ServerUser", "");
_server_user->set_fd(_server_socket);
event.events = EPOLLIN | EPOLLET;
event.data.ptr = _server_user;
if (epoll_ctl(_epoll_socket, EPOLL_CTL_ADD, _server_socket, &event) == -1)
{
close(_epoll_socket);
throw std::runtime_error("cannot add server socket to epoll!");
}
std::cout << BLU << "~~~ ft_irc started ~~~\n" << RST;
}
Server::~Server()
{
std::cout << "Server Destructor\n";
close(_server_socket);
close(_epoll_socket);
while (!_users.empty())
{
delete _users.back();
_users.pop_back();
}
for (std::map<std::string, Channel *>::iterator it = _channels.begin(); it != _channels.end(); ++it)
delete it->second;
_channels.clear();
delete _server_user;
}
void Server::init_command_map()
{
typedef int (Server::*CommandFunc)(User *, std::stringstream &);
std::map<std::string, CommandFunc> command_map;
command_map["CAP"] = &Server::CAP;
command_map["PASS"] = &Server::PASS;
command_map["QUIT"] = &Server::QUIT;
command_map["KILL"] = &Server::KILL;
command_map["PING"] = &Server::PING;
command_map["LIST"] = &Server::LIST;
command_map["MODE"] = &Server::MODE;
command_map["INVITE"] = &Server::INVITE;
command_map["KICK"] = &Server::KICK;
command_map["JOIN"] = &Server::JOIN;
command_map["PART"] = &Server::PART;
command_map["PRIVMSG"] = &Server::PRIVMSG;
command_map["WHO"] = &Server::WHO;
command_map["NICK"] = &Server::NICK;
command_map["TOPIC"] = &Server::TOPIC;
command_map["USER"] = &Server::USER;
command_map["OPER"] = &Server::OPER;
command_map["REMOVE_CHANNEL"] = &Server::REMOVE_CHANNEL;
this->_command_map = command_map;
}
int Server::new_connection()
{
sockaddr_in new_address;
socklen_t address_len = sizeof(new_address);
int connection_socket = accept(_server_socket,
reinterpret_cast<struct sockaddr *>(&new_address),
reinterpret_cast<socklen_t *>(&address_len));
std::cout << connection_socket << "\n";
if (connection_socket == -1)
throw std::runtime_error("cannot connect to user");
//set conn_fd to non-blocking (must use F_SETFL to do so)
fcntl(connection_socket, F_SETFL, O_NONBLOCK);
std::string host = inet_ntoa(new_address.sin_addr);
std::cout << CYN << "Connected to: " << host
<< ":" << new_address.sin_port
<< " on FD: " << connection_socket << "\n" << RST;
//store user
User *new_user = new User("", "", host);
_users.push_back(new_user);
new_user->set_fd(connection_socket);
//might need to be somewhere else
// Server::register_client(new_user);
// new_user->set_auth(true); --now in PASS
// ^^ might have to use this here instead of register_client as we are now registering in USER and NICK
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = new_user;
if (epoll_ctl(_epoll_socket, EPOLL_CTL_ADD, connection_socket, &ev))
{
close(connection_socket);
throw std::runtime_error("cannot add server socket to epoll!");
}
return (0);
}
int Server::client_message(User *user)
{
char buf[1024];
memset(buf, 0, sizeof(buf));
int bytes = recv(user->get_fd(), buf, sizeof(buf), 0);
if (bytes == -1)
{
std::cout << RED << "~Server~ recv() failure [456]\n" << RST;
throw std::runtime_error("Error in recv()");
return (1);
}
else if (bytes == 0)
{
std::cout << "client disconnected\n";
remove_user(user);
return (1);
}
LOG("Received raw message from FD: " << user->get_fd() << ": " << std::string(buf, bytes));
_msg.append(buf);
// processing each commang in _msg
std::stringstream ss(_msg);
std::string line;
int ret = 0;
if (_msg.find("\n") == std::string::npos && _msg.find("\r") == std::string::npos)
return 0;
while(std::getline(ss, line, '\n'))
{
if(line.empty()) continue;
ret = get_command(user, line);
// Clear _msg only after successfully processing a command
if (ret == 0 || ret == -1)
_msg.clear();
else
{
// If command fails, keeping unprocessed data
if (_msg == "\n" || _msg == "\r" || _msg == " ")
return ret;
std::string remaining;
if (ss.rdbuf()->in_avail() > 0)
remaining.assign(std::istreambuf_iterator<char>(ss), std::istreambuf_iterator<char>());
if (ret == 1 && !_msg.empty()
&& _msg[_msg.size() - 1] != '\r'
&& _msg[_msg.size() - 1] != '\n')
_msg.clear();
else
_msg = remaining;
}
}
return ret;
}
int Server::get_command(User *user, std::string msg)
{
std::stringstream ss(msg);
std::string word;
ss >> word;
std::transform(word.begin(), word.end(), word.begin(), ::toupper);
std::map<std::string, CommandFunc>::iterator it = _command_map.find(word);
if (it != _command_map.end())
{
std::stringstream params;
params << ss.rdbuf(); // Get the remaining parameters
LOG("Calling command function for: " + word + " params: " + params.str());
// std::cout << "Calling command function for: " << word << " with params: " << RED << params.str() << std::endl << RST;
// clearing after processing the command
// if (ret == 0)
// _msg.clear();
int ret = 0;
ret = (this->*(it->second))(user, params);
return ret;
}
else
{
if (!_msg.empty())
reply(user, "", "421", user->get_nick() + " " + word, "Unknown command"); // ERR_UNKNOWNCOMMAND
// clearing invalids also --can't do this because command might be valid, but not complete (nc ctrl-d from subject)
// _msg.clear();
return 1;
}
}
void reply(User *user, std::string prefix, std::string command,
std::string target, std::string message)
{
std::string reply;
if (!prefix.empty())
reply.append(prefix + " ");
else
reply.append(":ft_irc ");
reply.append(command + " ");
if (!target.empty())
reply.append(target + " ");
else
reply.append(user->get_nick() + " ");
if (!message.empty())
reply.append(":" + message);
else
reply.append(":");
if (reply.find_last_of("\n") == std::string::npos)
reply.append("\n");
int bytes_sent = send(user->get_fd(), reply.c_str(), reply.length(), 0);
if (bytes_sent <= 0)
LOG(RED << "Failed to send to FD:" << user->get_fd() << ":\t" << reply << "\n" << RST);
LOG("Sent reply to FD " << user->get_fd() << ": " << reply);
};
User *Server::get_user_from_nick(std::string nick)
{
for (std::vector<User *>::iterator it = _users.begin(); it != _users.end(); ++it)
{
if ((*it)->get_nick() == nick)
return (*it);
}
return (NULL);
}
void Server::add_user(User *user)
{
_users.push_back(user);
// register_client(user);
}
void Server::remove_user(User *user)
{
_users.erase(std::find(_users.begin(), _users.end(), user));
//TODO will need to remove from each channel as well
delete user;
}
void Server::register_client(User *user)
{
user->set_reg(true);
user->set_send_buf(RPL_WELCOME(user_id(user->get_nick(), user->get_user()), user->get_nick()));
user->set_send_buf(RPL_YOURHOST(user->get_nick(), "ft_irc", "v0.01"));
user->set_send_buf(RPL_CREATED(user->get_nick(), this->get_start_time()));
user->set_send_buf(RPL_MYINFO(user->get_nick(), "ft_irc", "0.01", "io", "kost", "k"));
user->set_send_buf(RPL_ISUPPORT(user->get_nick(), "CHANNELLEN=32 NICKLEN=9 TOPICLEN=307"));
LOG("Registered user: " + user->get_user() + "@" + user->get_host() + " known as " + user->get_nick());
Server::send_server_response(user, user->get_read_buf());
}
void Server::send_server_response(User *user, std::string send_buf)
{
std::istringstream buf(send_buf);
std::string reply;
send(user->get_fd(), send_buf.c_str(), send_buf.size(), 0);
while (getline(buf, reply))
std::cout << "Server Message to client [" << user->get_fd() << "]: " << BLU << reply << "\n" << RST;
}
std::string Server::get_password(){return (_password);}
std::string Server::get_start_time(){return (_start_time);}
int Server::get_epoll_socket(){return (_epoll_socket);}
int Server::get_server_socket(){return (_server_socket);}
// channels
Channel *Server::get_channel(const std::string &name)
{
std::map<std::string, Channel *>::iterator it = _channels.find(name);
if (it != _channels.end())
return it->second;
return NULL;
}
Channel *Server::create_channel(const std::string &name)
{
Channel *channel = new Channel(name);
_channels[name] = channel;
return channel;
}
// I added basically notification for users that the channel
// is being removed
// and removing from users channel list
void Server::remove_channel(const std::string &name)
{
std::map<std::string, Channel *>::iterator it = _channels.find(name);
if (it != _channels.end())
{
Channel *channel = it->second;
for (std::set<User *>::iterator user_it = channel->get_members().begin();
user_it != channel->get_members().end(); ++user_it)
{
User *user = *user_it;
user->leave_channel(channel);
reply(user, "", "NOTICE", "", "Channel " + name + " has been removed.");
}
delete it->second;
_channels.erase(it);
}
}