-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauthenticateUser.cpp
More file actions
134 lines (127 loc) · 6.09 KB
/
authenticateUser.cpp
File metadata and controls
134 lines (127 loc) · 6.09 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
#include "server.hpp"
#include "Client.hpp"
#include "numeric_replies.hpp"
void Server::welcome_user(int client_socket)
{
std::string msg = ":" + this->get_srv_ip() + " " + RPL_WELCOME + " " + this->clients[client_socket].get_nickname() + " :𝕎𝕖𝕝𝕔𝕠𝕞𝕖 𝕥𝕠 𝕥𝕙𝕖 𝕀ℝℂ 𝕤𝕖𝕣𝕧𝕖𝕣. 𝕄𝕒𝕕𝕖 𝕓𝕪: 𝕒𝕖𝕝𝕪𝕒𝕜𝕠𝕦, 𝕒𝕓𝕖𝕝𝕒𝕙𝕔𝕖. 𝕒𝕓𝕤𝕖𝕝𝕒.\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
msg = ":" + this->get_srv_ip() + " " + RPL_YOURHOST + " " + this->clients[client_socket].get_nickname() + " :Your host is " + this->get_srv_ip() + ", running version 1.0\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
msg = ":" + this->get_srv_ip() + " " + RPL_CREATED + " " + this->clients[client_socket].get_nickname() + " :This server was created when we locked team XD\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
msg = ":" + this->get_srv_ip() + " " + RPL_MYINFO + " " + this->clients[client_socket].get_nickname() + " :this server is a project of 42_cursus\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
msg = ":" + this->get_srv_ip() + " " + RPL_MOTDSTART + " " + this->clients[client_socket].get_nickname() + " :- " + this->get_srv_ip() + " Get ready to experience the wonkiest irc server in existence (no server to server communication XD)\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
msg = ":" + this->get_srv_ip() + " " + RPL_MOTD + " " + this->clients[client_socket].get_nickname() + " :- Remember, he's not dumb, he's just beyond our understanding " + this->clients[client_socket].get_nickname() + "!" + this->clients[client_socket].get_username() + "@" + this->clients[client_socket].get_hostname() + "\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
msg = ":" + this->get_srv_ip() + " " + RPL_ENDOFMOTD + " " + this->clients[client_socket].get_nickname() + " :End of message of the day\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
return;
}
void Server::auth_client(int client_socket)
{
if(this->clients[client_socket].get_grade() == AUTHENTICATED)
return;
if(this->clients[client_socket].get_pass_state() == PASS
&& this->clients[client_socket].get_nick_state() == NICK
&& this->clients[client_socket].get_user_state() == USER)
{
this->clients[client_socket].set_grade(AUTHENTICATED);
welcome_user(client_socket);
return;
}
return;
}
void Server::pass_cmd(int client_socket, std::string buffer)
{
if(this->clients[client_socket].get_pass_state() == PASS)
{
std::string msg = ":" + this->get_srv_ip() + " " + ERR_ALREADYREGISTRED + " " " :Unauthorized command (already registered)\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
return;
}
if(buffer.empty() || buffer != this->srv_password)
{
std::string msg = ":" + this->get_srv_ip() + " " + ERR_PASSWDMISMATCH + " " " :Password incorrect\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
return;
}
this->clients[client_socket].set_pass_state(PASS);
auth_client(client_socket);
}
void Server::nick_cmd(int client_socket, std::string buffer)
{
if(buffer.empty() || buffer == ":")
{
std::string msg = ":" + this->get_srv_ip() + " " + ERR_NONICKNAMEGIVEN + " * " + ":No nickname given\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
return;
}
if(buffer[0] == ':')
{
buffer.erase(0, 1);
std::replace(buffer.begin(), buffer.end(), ' ', '_');
}
else
buffer = buffer.substr(0, buffer.find(" "));
for(std::map<int, Client>::iterator it = this->clients.begin(); it != this->clients.end(); it++)
{
if(it->second.get_nickname() == buffer)
{
std::string msg = ":" + this->get_srv_ip() + " " + ERR_NICKNAMEINUSE + " " + buffer + ":Nickname is already in use\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
return;
}
}
std::string msg = ":" + this->clients[client_socket].get_nickname() + " NICK :" + buffer + "\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
this->clients[client_socket].set_nickname(buffer);
this->clients[client_socket].set_nick_state(NICK);
auth_client(client_socket);
}
int process_user(std::string buffer, Client &client)
{
std::string username;
std::string hostname;
std::string servername;
std::string realname;
if(buffer.empty())
{
std::string msg = ":" + client.get_nickname() + " " + ERR_NEEDMOREPARAMS + " USER :Not enough parameters\r\n";
send(client.get_socket(), msg.c_str(), msg.length(), 0);
return 1;
}
username = buffer.substr(0, buffer.find(" "));
buffer.erase(0, username.length() + 1);
hostname = buffer.substr(0, buffer.find(" "));
buffer.erase(0, hostname.length() + 1);
servername = buffer.substr(0, buffer.find(" "));
buffer.erase(0, servername.length() + 1);
realname = buffer.substr(0, buffer.find(" "));
buffer.erase(0, realname.length() + 1);
if(username.empty() || hostname.empty() || servername.empty() || realname.empty())
{
std::string msg = ":" + client.get_nickname() + " " + ERR_NEEDMOREPARAMS + " USER :Not enough parameters\r\n";
send(client.get_socket(), msg.c_str(), msg.length(), 0);
return 1;
}
client.set_username(username);
// client.set_hostname(hostname);
client.set_servername(servername);
client.set_realname(realname);
return 0;
}
void Server::user_cmd(int client_socket, std::string buffer)
{
if(this->clients[client_socket].get_user_state() == USER)
{
std::string msg = ":" + this->get_srv_ip() + " " + ERR_ALREADYREGISTRED + " " + this->clients[client_socket].get_nickname() + " :Unauthorized command (already registered)\r\n";
send(client_socket, msg.c_str(), msg.length(), 0);
return;
}
if(process_user(buffer, this->clients[client_socket]))
return;
this->clients[client_socket].set_user_state(USER);
auth_client(client_socket);
}