-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientAuthentification.cpp
124 lines (108 loc) · 4.02 KB
/
ClientAuthentification.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
#include "Server.hpp"
void Server::ERR_NEEDMOREPARAMS(int fd)
{
SendMessage(fd, ":HALAL_TINDER 461 Client PASS :Not enough parameters\r\n");
}
void Server::ERR_ALREADYREGISTERED(int fd)
{
SendMessage(fd, ":HALAL_TINDER 462 Client :You may not reregister\r\n");
}
bool Server::duplicateNick(std::string NICK)
{
std::map<int, Client>::iterator it;
for (it = clients.begin(); it != clients.end(); it++)
{
if (it->second.get_nick() == NICK)
return 1;
}
return 0;
}
bool Server::checkNICK(std::string NICK)
{
if ((NICK[0] < 'a' || NICK[0] > 'z') && (NICK[0] < 'A' || NICK[0] > 'Z'))
return 1;
for (unsigned long i = 1; i < NICK.size(); i++)
{
if(!isalnum(NICK[i]) && NICK[i] != '[' && NICK[i] != ']' && NICK[i] != '{' && NICK[i] != '}' && NICK[i] != '\\' && NICK[i] != '|')
return 1;
}
return 0;
}
void Server::cmd_PASS(std::vector<std::string> &cmd, int fd)
{
// SendMessage(fd, ":HALAL_TINDER 461 Client PASS :Not enough parameters\r\n");
if (cmd.size() > 2)
SendMessage(fd, ":HALAL_TINDER Client :Too many arguments:\r\n");
else if (cmd.size() == 1)
ERR_NEEDMOREPARAMS(fd);
else if (password == cmd[1])
{
clients[fd].set_registered(1);
SendMessage(fd, "Please enter your NICK:\r\n");
}
else
SendMessage(fd, ":HALAL_TINDER 464 Client :Password incorrect\r\n");
}
void Server::RegisterClient(std::vector<std::string> &cmd, int fd)
{
std::string message;
if (cmd[0] == "PASS" || cmd[0] == "pass")
{
cmd_PASS(cmd, fd);
}
else if (clients[fd].get_registered() == 1 && (cmd[0] == "NICK" || cmd[0] == "nick"))
{
int flag = 0;
if (cmd.size() == 1)
{
flag = 1;
message = ":HALAL_TINDER 431 Client :No nickname given\r\n";
send(fd, message.c_str(), message.size(), 0);
}
else if (cmd.size() > 1)
{
if (duplicateNick(cmd[1]) == 1)
{
flag = 1;
message = ":HALAL_TINDER 433 Client "+ cmd[1] +" :Nickname is already in use\r\n";
send(fd, message.c_str(), message.size(), 0);
}
else if (cmd.size() > 2 || checkNICK(cmd[1]))
{
flag = 1;
message = ":HALAL_TINDER 432 Client " + cmd[1] + ":Erroneus nickname\r\n";
send(fd, message.c_str(), message.size(), 0);
}
}
if (flag == 0)
{
clients[fd].set_nick(cmd[1]);
clients[fd].set_registered(2);
message = "Please enter your USER:\r\n";
send(fd, message.c_str(), message.size(), 0);
}
}
else if (cmd[0] == "USER")
{
if(clients[fd].get_registered() == 2)
{
if (cmd.size() > 5)
SendMessage(fd, ":HALAL_TINDER 461 Client USER :Too many parameters\r\n");
else if (cmd.size() < 5)
SendMessage(fd, ":HALAL_TINDER 461 Client USER :Not enough parameters\r\n");
else {
clients[fd].set_user(cmd[1]);
clients[fd].set_real_name(cmd[4]);
clients[fd].set_registered(3);
message = ":HALAL_TINDER 001 " + clients[fd].get_nick() + " :Welcome to the HALAL_TINDER Network, " + clients[fd].get_nick() + "[!" + clients[fd].get_user() + "@HALAL_TINDER]\r\n";
send(fd, message.c_str(), message.size(), 0);
message = ":HALAL_TINDER 002 " + clients[fd].get_nick() + " :Your host is HALAL_TINDER, running version 2.0\r\n";
send(fd, message.c_str(), message.size(), 0);
message = ":HALAL_TINDER 003 " + clients[fd].get_nick() + " :This server was created " + creationTime + "\r\n";
send(fd, message.c_str(), message.size(), 0);
message = ":HALAL_TINDER 004 " + clients[fd].get_nick() + " :HALAL_TINDER 2.0 \r\n";
send(fd, message.c_str(), message.size(), 0);
}
}
}
}