forked from Nouakchi/FT_IRC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (109 loc) · 4.11 KB
/
main.cpp
File metadata and controls
120 lines (109 loc) · 4.11 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: onouakch <onouakch@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/17 14:02:26 by onouakch #+# #+# */
/* Updated: 2024/01/24 11:26:32 by onouakch ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/irc.h"
#include <sstream>
#include <vector>
int ft_parse_port( char *port )
{
int i = -1;
if (port[0] && port[0] == '+')
i++;
while (port[++i])
if (!isdigit(port[i]))
return (ft_error("Port must be a number !!"));
if(atoi(port) < 1024 || atoi(port) > 65535)
return (ft_error("Port must be between 1024 and 65535 !!" ));
return (EXIT_SUCCESS);
}
int parse_pwd( char *pwd )
{
if(strlen(pwd) < 1)
return (ft_error("Password must be at least 1 char long !!"));
return (EXIT_SUCCESS);
}
void clear_events(struct kevent *new_event, int num_events)
{
int i = -1;
while (++i < num_events)
{
new_event[i].ident = 0;
new_event[i].filter = 0;
new_event[i].flags = 0;
new_event[i].fflags = 0;
new_event[i].data = 0;
new_event[i].udata = NULL;
}
}
int main(int ac , char **av)
{
int i , num_events;
char tmp_host[32];
t_server server;
struct kevent new_event[512];
time_t tt;
struct tm *ti;
time(&tt);
ti = localtime(&tt);
if (ac != 3)
return (ft_error("Usage: ./ircserv <port> <password>"));
if (EXIT_FAILURE == parse_pwd(av[2]))
return (EXIT_FAILURE);
if (EXIT_FAILURE == ft_parse_port(av[1]))
return (EXIT_FAILURE);
server.serv_pass = av[2];
server.port = atoi(av[1]);
server.server_date = asctime(ti);
gethostname(tmp_host, sizeof(tmp_host));
server.server_name = std::string(tmp_host);
server.host_name = ":" + server.server_name;
if (EXIT_FAILURE == ft_create_socket(&server))
return (EXIT_FAILURE);
if (EXIT_FAILURE == ft_bind_to_listen(&server))
return (EXIT_FAILURE);
if (EXIT_FAILURE == ft_setup_kernel_queue(&server))
return (EXIT_FAILURE);
try
{
while (true)
{
//check for a new connection or a received message
num_events = kevent(server.kq, NULL, 0, new_event, 1, NULL);
if (num_events == -1)
return (ft_error("Failed to check on events !!"));
i = -1;
while (++i < num_events)
{
int event_fd = new_event[i].ident;
//check for new client
if (new_event[i].flags & EV_EOF )
{
std::map<int, Client*>::iterator it = server.clients.find(event_fd);
std::set<std::string>::iterator s_it = std::find(server.nicknames.begin(), server.nicknames.end(), it->second->getNickName());
if (s_it != server.nicknames.end())
server.nicknames.erase(s_it);
ft_disconnect( &server, event_fd );
}
else if (event_fd == server.socket)
ft_setup_new_connection( &server, event_fd );
//check for an event from an existant client
else if (new_event[i].filter & EVFILT_READ)
ft_check_event(&server, event_fd);
}
clear_events(new_event, num_events);
}
}
catch(const std::exception& e)
{
std::cerr << "Error: " << e.what() << '\n';
}
return (0);
}