-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.cpp
More file actions
233 lines (191 loc) · 5.96 KB
/
Client.cpp
File metadata and controls
233 lines (191 loc) · 5.96 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* Client.cpp :+: :+: */
/* +:+ */
/* By: emlicame <emlicame@student.42.fr> +#+ */
/* +#+ */
/* Created: 2023/09/18 19:24:58 by emlicame #+# #+# */
/* Updated: 2023/10/25 11:59:03 by emlicame ######## odam.nl */
/* */
/* ************************************************************************** */
#include "Client.hpp"
#include "colors.hpp"
#include "ansicolors.hpp"
#include "Parse.hpp"
// namespace Parse
#include "Command.hpp"
// namespace Command
#include <string.h>
// void bzero(void *s, size_t n);
// char *strerror(int errnum);
#include <unistd.h>
// int close(int fildes);
#include <arpa/inet.h>
// char *inet_ntoa(struct in_addr);
#include <poll.h>
// struct pollfd
#include <iostream>
// std::
#include <vector>
// std::vector
#include <tuple>
// std::tuple
#ifndef BUFFER_SIZE
# define BUFFER_SIZE 4096
#endif
/** ************************************************************************ **\
*
* Constructors
*
\* ************************************************************************** */
Client::Client(Server &server) : AClient(server), _password(false) {
if (verboseCheck() >= V_USER)
std::cout << C_DGREEN << "A wild "
<< C_GREEN << "Client"
<< C_DGREEN << " appeared."
<< C_RESET << std::endl;
initialize(server.getFD());
this->sendMsg(UHBLU ":" + this->_server + " NOTICE * :*** To register please use commands\n" HBLU "- PASS\n- USER <user_name> * <host> :<realname>\n- NICK" CRESET);
};
/******************************************************************************\
*
* Destructors
*
\* ************************************************************************** */
Client::~Client(void) {
if (verboseCheck() >= V_USER)
std::cout << C_DRED << "The wild "
<< C_RED << "Client " << this->getBestName()
<< C_DRED << " fled"
<< C_RESET << std::endl;
}
/** ************************************************************************ **\
*
* Member Functions
*
\* ************************************************************************** */
std::string ipAddress(const struct sockaddr_in& socketAddress) {
return inet_ntoa(socketAddress.sin_addr);
}
void Client::initialize(int serverFD) {
this->socketAddressLen = sizeof(this->socketAddress);
this->pollInfo.fd = accept(serverFD, (struct sockaddr *)&this->socketAddress, &this->socketAddressLen);
if (this->pollInfo.fd < 0)
throw (std::runtime_error("accept(): "));
this->pollInfo.events = POLLIN;
this->_clientIP = ipAddress(this->socketAddress);
}
void Client::sendMsg(std::string msg) {
if (pollConnection() == false)
return ;
msg += "\r\n";
ssize_t size = send(this->pollInfo.fd, msg.c_str(), msg.length(), 0);
if (verboseCheck() >= V_MSG)
std::cout << C_RESET << "Send [" << size << "]\t"
<< C_LORANGE << msg
<< C_RESET << std::flush;
}
bool Client::readReceive(void) {
char buffer[BUFFER_SIZE];
ssize_t recvLen;
bzero(buffer, sizeof(buffer));
recvLen = recv(this->pollInfo.fd, buffer, sizeof(buffer) - 1, 0);
if (recvLen < 0) {
if (errno != EWOULDBLOCK && errno != EAGAIN)
std::cerr << "Error recv(): " << strerror(errno) << std::endl;
return false;
}
if (recvLen == 0) {
this->closeFD();
std::cout << "Client " << getBestName() << " disconnected from server." << std::endl;
return false;
}
this->_buffer.append(buffer);
return true;
}
bool Client::pollConnection(void) {
if (poll(&this->pollInfo, 1, 0) == -1)
return (false);
if (this->pollInfo.revents & (POLLERR | POLLHUP)) {
close(this->pollInfo.fd);
this->pollInfo.fd = -1;
return (false);
}
return (true);
}
std::string Client::getMsg(void) {
if (pollConnection() == false)
return "";
if (this->pollInfo.revents & POLLIN) {
if(!readReceive()) {
return "";
}
}
std::string::size_type pos;
if (!this->_buffer.empty() && (pos = this->_buffer.find('\n')) != std::string::npos) {
// Extract the complete message including the delimiter
std::string msg;
msg = this->_buffer.substr(0, pos + 1);
this->_buffer.erase(0, pos + 1);
return msg;
}
return "";
}
bool Client::stillActive(void) const {
return (this->pollInfo.fd != -1);
}
bool Client::hasPassword(void) const {
return _password;
}
std::string Client::getBestName( void ) const {
if (!this->_nickName.empty())
return this->_nickName;
else if (!this->_clientIP.empty())
return _clientIP;
return "";
}
void Client::setPollInfofd(int val) {
this->pollInfo.fd = val;
}
void Client::passwordValidation(bool val) {
this->_password = val;
}
void Client::setIsRegistered(bool val) {
if (hasPassword() == true && !getNickName().empty() && !getUserName().empty()) {
this->_isRegistered = true;
if (verboseCheck() >= V_USER) {
std::cout << "User " C_CYAN << this->getNickName()
<< C_RESET " is registered by IRC Othello Magic Server" << std::endl;
this->printInfo();
}
}
(void)val;
}
#include <unordered_map>
const char* verboseToString(e_verbose verbose) {
switch (verbose) {
case V_ERR:
return "V_ERR";
case V_SILENT:
return "V_SILENT";
case V_ADMIN:
return "V_ADMIN";
case V_CHANNEL:
return "V_CHANNEL";
case V_USER:
return "V_USER";
case V_DETAILS:
return "V_DETAILS";
case V_MSG:
return "V_MSG";
default:
return "Unknown";
}
}
void Client::closeFD(void) {
if (this->pollInfo.fd > 2) {
close(this->pollInfo.fd);
this->pollInfo.fd = -1;
}
}