forked from codewhite7777/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
341 lines (293 loc) · 6.77 KB
/
Client.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 11:19:48 by mgo #+# #+# */
/* Updated: 2022/10/12 11:19:50 by mgo ### ########.fr */
/* */
/* ************************************************************************** */
#include "Client.hpp"
#include "Server.hpp"
#include "Command.hpp"
#include "Protocol.hpp"
#include <string>
#include <iostream>
Client::Client(SOCKET sdes, std::string hostname, Server* sv)
: sock_des_(sdes)
, pass_flag_(false)
, nick_flag_(false)
, user_flag_(false)
, nickname_("*")
, hostname_(hostname)
, disconnect_flag_(false)
, sv_oper_flag_(false)
, passed_(false)
, welcomed_(false)
, sv_(sv)
, cmd_(sv_->getCommand())
, proto_(sv_->getProtocol()) {}
Client::~Client(void) {}
SOCKET& Client::getSocket(void)
{
return sock_des_;
}
void Client::appendToRecvBuf(unsigned char* buf)
{
getRecvBuf().append(reinterpret_cast<char *>(buf));
}
size_t Client::getRecvBufLength(void)
{
return (getRecvBuf().length());
}
void Client::appendToSendBuf(const std::string& str)
{
getSendBuf().append(str);
}
const char* Client::getSendBufCStr(void)
{
return (getSendBuf().c_str());
}
size_t Client::getSendBufLength(void)
{
return (getSendBuf().length());
}
void Client::eraseSendBufSize(int size)
{
getSendBuf().erase(0, size);
}
void Client::processMessageInRecvBuf(void)
{
marshalMessageToCmdAndParam();
processProtocol();
clearCommandAndParam();
}
const std::string& Client::getCommand(void) const
{
return command_;
}
const std::string& Client::getParam(void) const
{
return param_;
}
bool Client::getDisconnectFlag(void) const
{
return disconnect_flag_;
}
void Client::setDisconnectFlag(bool flag)
{
disconnect_flag_ = flag;
}
void Client::setPassFlag(bool flag)
{
pass_flag_ = flag;
}
bool Client::getPassFlag(void) const
{
return pass_flag_;
}
void Client::setNickname(std::string nickname)
{
nickname_ = nickname;
}
const std::string& Client::getNickname(void) const
{
return nickname_;
}
void Client::setNickFlagOn(void)
{
nick_flag_ = true;
}
bool Client::getNickFlag(void) const
{
return nick_flag_;
}
void Client::setUsername(std::string username)
{
username_ = username;
}
void Client::setHostname(std::string hostname)
{
hostname_ = hostname;
}
void Client::setRealname(std::string realname)
{
realname_ = realname;
}
const std::string& Client::getUsername(void) const
{
return username_;
}
const std::string& Client::getHostname(void) const
{
return hostname_;
}
const std::string& Client::getRealname(void) const
{
return realname_;
}
const std::string Client::getUserRealHostNamesInfo(void) const
{
return (nickname_ + "!" + username_ + "@" + hostname_);
}
void Client::setUserFlagOn(void)
{
user_flag_ = true;
}
bool Client::getUserFlag(void) const
{
return user_flag_;
}
std::string Client::getNamesPrefix(void) const
{
return (":" + nickname_ + "!" + username_ + "@" + hostname_ + " ");
}
void Client::setSvOperFlagOn(void)
{
this->sv_oper_flag_ = true;
}
bool Client::isSvOper(void) const
{
return (this->sv_oper_flag_);
}
void Client::promptRecvedMsg(void)
{
std::cout << "Received message from <SD: " << this->getSocket();
std::cout << " | nickname: " << this->getNickname() << ">\n" \
<< "[" << this->getRecvBuf() << "]\n";
}
void Client::promptSendedMsg(void)
{
std::cout << "Sended message to <SD: " << this->getSocket();
std::cout << " | nickname: " << this->getNickname() << ">\n"\
<< "[" << this->getSendBuf() << "]\n\n";
}
void Client::marshalMessageToCmdAndParam(void)
{
std::string tmp_msg;
tmp_msg = extractFirstMsg(getRecvBuf());
if (tmp_msg.find(' ') != std::string::npos)
{
command_ = tmp_msg.substr(0, tmp_msg.find(' '));
param_ = tmp_msg.substr(tmp_msg.find(' ') + 1);
}
else
{
command_ = tmp_msg;
param_ = "";
}
promptCommandAndParam();
}
void Client::promptCommandAndParam(void)
{
std::cout << "\tCommand: [" << command_ << "]\n";
std::cout << "\tParameter: [" << param_ << "]\n\n";
}
std::string Client::extractFirstMsg(std::string& recv_buf)
{
std::string first_msg("");
size_t found(0);
found = recv_buf.find("\r\n");
if (found != std::string::npos)
{
first_msg = recv_buf.substr(0, found);
recv_buf.erase(0, found + 2);
}
else
{
found = recv_buf.find("\n");
if (found != std::string::npos)
{
first_msg = recv_buf.substr(0, found);
recv_buf.erase(0, found + 1);
}
}
return (first_msg);
}
void Client::processProtocol(void)
{
if (command_.empty() && param_.empty())
return ;
if (command_ == "PING")
cmd_->ping(this);
else if (isWelcomed() == false)
processAuthToWelcome();
else
processCommand();
}
void Client::processAuthToWelcome(void)
{
if (getPassFlag() == false)
{
if (command_ == "CAP")
return ;
else if (command_ == "PASS")
cmd_->pass(this);
else
appendToSendBuf(proto_->errNotPassCmd());
}
else if (getNickFlag() == false || getUserFlag() == false)
{
if (command_ == "NICK")
cmd_->nick(this);
else if (command_ == "USER")
cmd_->user(this);
}
if (getPassFlag() && getNickFlag() && getUserFlag())
{
welcomed_ = true;
appendToSendBuf(proto_->rplWelcome(this));
appendToSendBuf(proto_->rplYourHost(this));
appendToSendBuf(proto_->rplCreated(this));
appendToSendBuf(proto_->rplMyInfo(this));
appendToSendBuf(proto_->svPrivmsgClntWhenInit(this));
}
}
bool Client::isPassed(void) const
{
return passed_;
}
bool Client::isWelcomed(void) const
{
return welcomed_;
}
void Client::processCommand(void)
{
if (command_ == "JOIN")
cmd_->join(this);
else if (command_ == "PART")
cmd_->part(this);
else if (command_ == "PRIVMSG")
cmd_->privmsg(this);
else if (command_ == "NOTICE")
cmd_->notice(this);
else if (command_ == "QUIT")
cmd_->quit(this);
else if (command_ == "KICK")
cmd_->kick(this);
else if (command_ == "INVITE")
cmd_->invite(this);
else if (command_ == "NICK")
cmd_->nick(this);
else if (command_ == "OPER")
cmd_->oper(this);
else if (command_ == "kill")
cmd_->kill(this);
else if (command_ == "die")
cmd_->die(this);
}
void Client::clearCommandAndParam(void)
{
command_.clear();
param_.clear();
}
std::string& Client::getSendBuf(void)
{
return send_buf_;
}
std::string& Client::getRecvBuf(void)
{
return recv_buf_;
}