-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.cpp
306 lines (273 loc) · 6.09 KB
/
User.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
#include "include/User.hpp"
#include "include/Session.hpp"
#include "include/Frame.hpp"
User::User(void)
: _didNick(false), _didUser(false), _manager(false), _password(false), _isProperlyQuit(false)
{
}
User::User(User const& other)
{
*this = other;
}
User& User::operator=(User const& other)
{
if (this == &other)
return *this;
return *this;
}
User::~User(void)
{
if (isRegistered() && !_isProperlyQuit)
{
Frame *frame;
ChannelMap::iterator it;
std::vector<std::string> quitmsg;
quitmsg.push_back("QUIT");
quitmsg.push_back(":User unexpectedly lost connection");
frame = Frame::instance();
frame->cmdQuit(frame->findUser(_sNickname), quitmsg);
}
}
void User::setName(std::string const& s)
{
_sRealname = s;
}
void User::setNick(std::string const& s)
{
_sNickname = s;
}
void User::setHost(std::string const& s)
{
_sHostname = s;
}
void User::setUser(std::string const& s)
{
_sUsername = s;
}
void User::setPass(bool pass)
{
_password = pass;
}
std::string User::name(void) const
{
return _sRealname;
}
std::string User::nick(void) const
{
return _sNickname;
}
std::string User::host(void) const
{
return _sHostname;
}
std::string User::user(void) const
{
return _sUsername;
}
bool User::pass(void) const
{
return _password;
}
std::string User::msgHeader(void) const
{
return std::string(_sNickname + "!" + _sUsername + "@" + _sHostname);
}
bool User::checkNick(void) const
{
return _didNick;
}
bool User::checkUser(void) const
{
return _didUser;
}
bool User::checkQuit(void) const
{
if (_isProperlyQuit)
return true;
return false;
}
/*
* Check both Nick and User
*/
bool User::isRegistered(void) const
{
if (checkNick() && checkUser() && pass())
return true;
return false;
}
bool User::checkManager(void) const
{
return _manager;
}
/*
* Restore Nick name for connection
*/
bool User::addNick(std::vector<std::string> const& sets)
{
if (_didNick)
return false; //alreadyRegistered
_sNickname = sets[1];
_didNick = true;
return true;
}
/*
* Change Nick name
*/
void User::cmdNick(std::vector<std::string> const& sets)
{
_pastNick.insert(_pastNick.end(), _sNickname);
for (ChannelMap::iterator it = _mChannels.begin(); it != _mChannels.end(); it++)
it->second->cmdNick(_sNickname, sets[1]);
_sNickname = sets[1];
return ;
}
bool User::cmdUser(std::vector<std::string> const& sets)
{
std::string str;
std::string::size_type i;
if (_didUser == true)
return false;
_didUser = true;
setUser(sets[1]);
str = sets[4];
if (sets[4][0] == ':')
str = sets[4].substr(1);
for (i = 5; i < sets.size(); i++)
str += " " + sets[i];
setName(str);
return true;
}
void User::cmdJoin(Channel* ch)
{
_mChannels.insert(std::pair<std::string, Channel*>(ch->name(), ch));
}
void User::optionJoin(Session *ss, std::string const& msg)
{
ChannelMap::iterator it;
for (it = _mChannels.begin(); it != _mChannels.end(); it++)
{
it->second->broadcast(ss, msg);
it->second->cmdPart(this->nick());
if (it->second->userCount() == 0)
{
delete it->second;
Frame::instance()->removeChannel(it->first);
}
_mChannels.erase(it);
}
}
/*
* Change Upper characters to Lower
*/
std::string User::makeLower(std::string const& str)
{
std::string low;
std::string::size_type i;
low = "";
for (i = 0; i < str.size(); i++)
low += std::tolower((char)str[i]);
return low;
}
bool User::cmdPart(Session *ss, std::string const& chname, std::vector<std::string> const& sets)
{
std::string str;
std::string::size_type i;
ChannelMap::iterator it;
it = _mChannels.find(makeLower(chname.substr(1)));
if (it == _mChannels.end())
return false;
str = sets[0] + " " + chname;
for (i = 2; i < sets.size(); i++)
str += " " + sets[i];
it->second->broadcast(ss, str);
it->second->cmdPart(this->nick());
if (it->second->userCount() == 0)
{
delete it->second;
Frame::instance()->removeChannel(it->first);
}
_mChannels.erase(it);
return true;
}
void User::cmdQuit(void)
{
ChannelMap::iterator it;
ChannelMap::iterator tt;
_isProperlyQuit = true;
for (it = _mChannels.begin(); it != _mChannels.end(); )
{
tt = it++;
tt->second->cmdPart(this->nick());
if (tt->second->userCount() == 0)
{
delete tt->second;
Frame::instance()->removeChannel(tt->first);
}
_mChannels.erase(tt);
}
_didNick = false;
_didUser = false;
}
void User::cmdOper(void)
{
_manager = true;
}
bool User::isMemOfChannel(std::string const& chname) const
{
ChannelMap::const_iterator res;
res = _mChannels.find(chname);
if (res == _mChannels.end())
return false;
return true;
}
/*
* Fit to a RPL_WHOISCHANNELS format
*/
void User::cmdWhois(Session *ss)
{
ChannelMap::iterator it;
for (it = _mChannels.begin(); it != _mChannels.end(); ++it)
{
if (it->second->hasOper(nick()))
ss->rep319(nick() + " :@" + it->first + " ");
else
ss->rep319(nick() + " :" + it->first + " ");
}
}
void User::setProperlyQuit(bool state)
{
_isProperlyQuit = state;
}
void User::partChannel(Channel *ch)
{
_mChannels.erase(ch->name());
}
/*
* Fit to a RPL_WHOREPLY format
*/
std::vector<std::string> User::userVectorOper(std::vector<std::string> const& sets)
{
ChannelMap::iterator it = _mChannels.begin();
std::vector<std::string> res;
std::string servername = "ft_irc";
// has not Channel
if (it == _mChannels.end())
{
if (checkManager())
res.push_back("<no Channel> " + user() + " " + host() + " " + servername + " " + nick() + " H @ :0 " + name());
else
res.push_back("<no Channel> " + user() + " " + host() + " " + servername + " " + nick() + " H :0 " + name());
}
for (it = _mChannels.begin(); it != _mChannels.end(); ++it)
{
if (sets.size() > 2
&& sets[2] == "o"
&& it->second->hasOper(nick()))
res.push_back("#" + it->second->name() + " " + user() + " " + host() + " " + servername + " " + nick() + " H @ :0 " + name());
else if (it->second->hasOper(nick()))
res.push_back("#" + it->second->name() + " " + user() + " " + host() + " " + servername + " " + nick() + " H @ :0 " + name());
else
res.push_back("#" + it->second->name() + " " + user() + " " + host() + " " + servername + " " + nick() + " H :0 " + name());
}
return res;
}