-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.cpp
285 lines (240 loc) · 6.88 KB
/
channel.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
#include "channel.hpp"
/* Constructors and Deconstructors */
//===================================================//
Channel::Channel(std::string newName) : _countClients(0), _userLogList(), _server(), _channelName(newName), _maxClients(60), _creatorUser(), _operatorList(), _bannedList() {
std::cout << "New channel created with name " << _channelName << std::endl;
}
Channel& Channel::operator=(const Channel &src) {
this->_channelName = src._channelName;
this->_bannedList = src._bannedList;
this->_operatorList = src._operatorList;
return (*this);
}
Channel::~Channel() {
std::cout << "Channel destroyed" << std::endl;
}
/* Getters and Setters */
//===================================================//
void Channel::addCreator(User* creator) {
this->_creatorUser = creator;
this->setOperator(creator);
}
std::vector<User*> Channel::getUsers() {
return (this->_userLogList);
}
std::vector<User*> Channel::getOperators() {
return (this->_operatorList);
}
void Channel::setOperator(User* newOp) {
this->_operatorList.push_back(newOp);
}
const std::string Channel::getName()
{
return (this->_channelName);
}
size_t Channel::getMaxClients() const
{
return (this->_maxClients);
}
size_t Channel::getClientCount() const
{
return (this->_countClients);
}
void Channel::addUser(User* user)
{
this->_userLogList.push_back(user);
std::cout << "This nick is stored on the userLogList: " << user->getNick() << std::endl;
}
void Channel::addBanned(User* toBan)
{
this->_bannedList.push_back(toBan);
}
/* Removes a user from a channel, and removes channel if last user has left. Notifies other clients of action. */
void Channel::deleteUser(std::string _nick)
{
std::string str;
std::vector<User*>::iterator itro;
for (itro=this->_operatorList.begin(); itro != this->_operatorList.end(); itro++)
{
if (_nick == (*itro)->_nick)
{
_operatorList.erase(itro);
break ;
}
}
std::vector<User*>::iterator itr;
for (itr=this->_userLogList.begin(); itr != this->_userLogList.end(); itr++)
{
if (_nick == (*itr)->_nick) {
_userLogList.erase(itr);
break ;
}
}
if (_userLogList.empty() == false) {
std::string allUserList = this->findAllUsers();
std::string reply_construct = ":" + this->_server->_serverName + " 353 " + (*itr)->getNick() + " = " + this->getName() + " :" + allUserList;
(*itr)->replyLightAll(reply_construct.c_str(), this);
}
if (_userLogList.empty()) {
std::cout << (*(this->_server->_channelList.begin()))->getName() << std::endl;
if (!this->_server->_channelList.empty()) {
std::vector<Channel*>::iterator iterc = this->_server->_channelList.begin();
long unsigned int i = 0;
while (i < this->_server->_channelList.size())
{
if (this->getName() == this->_server->_channelList.at(i)->getName()) {
this->_server->_channelList.erase(iterc);
break ;
}
iterc++;
i++;
}
}
delete this;
}
}
void Channel::partUser(std::string _nick) {
std::vector<User*>::iterator itro;
for (itro=this->_operatorList.begin(); itro != this->_operatorList.end(); itro++)
{
if (_nick == (*itro)->_nick) {
_operatorList.erase(itro);
break ;
}
}
std::vector<User*>::iterator itr;
for (itr=this->_userLogList.begin(); itr != this->_userLogList.end(); itr++)
{
if (_nick == (*itr)->_nick) {
_userLogList.erase(itr);
break ;
}
}
if (_userLogList.empty()) {
std::cout << (*(this->_server->_channelList.begin()))->getName() << std::endl;
if (!this->_server->_channelList.empty()) {
std::vector<Channel*>::iterator iterc = this->_server->_channelList.begin();
unsigned long int i = 0;
while (i < this->_server->_channelList.size())
{
if (this->getName() == this->_server->_channelList.at(i)->getName()) {
this->_server->_channelList.erase(iterc);
break ;
}
iterc++;
i++;
}
}
delete this;
}
}
/* Checkers */
//===================================================//
/* Finds if Nick in Channel and returns a Pointer to User Obj or NULL */
User* Channel::isUserinChannelbys(std::string _nick)
{
std::vector<User*>::iterator itr;
for (itr=this->_userLogList.begin(); itr != this->_userLogList.end(); itr++)
{
if(_nick == (*itr)->getNick())
return(*itr);
}
return (NULL);
}
/* Finds if User Obj Pointer in Channel and returns a Pointer to User Obj or NULL */
User* Channel::isUserinChannelbyp(User* user)
{
std::vector<User*>::iterator itr;
for (itr=this->_userLogList.begin(); itr != this->_userLogList.end(); itr++)
{
if(user == *itr)
return(*itr);
}
return (NULL);
}
/* Not Needed anymore, Use Channel::isUserinChannel */
int Channel::ifJoined(std::string _nick)
{
std::string str;
std::vector<User*>::iterator itr;
for (itr=this->_userLogList.begin(); itr != this->_userLogList.end(); itr++)
{
if(_nick == (*itr)->getNick())
return(1);
}
return (0);
}
/* Finds a User Obj Pointer from the _userLogList return 1 or 0 */
int Channel::is_User_Operator(User* user)
{
// Protection: Check if the _userLogList is empty
/* if (this->_channelList.size() == 0) {
return;
} */
std::vector<User*>::iterator itr;
for (itr=this->_operatorList.begin(); itr != this->_operatorList.end(); itr++)
{
if(user == *itr)
return(1);
}
return (0);
}
/* Utilities */
//=====================================================================
std::string Channel::findAllUsers()
{
std::string str;
std::vector<User*>::iterator itr;
for (itr = this->_userLogList.begin(); itr != this->_userLogList.end(); itr++)
{
if (this->ifOperator((*itr)->getNick()) == 1) {
str += '@';
}
std::string temp = (*itr)->getNick();
str += temp + " ";
}
return (str);
}
bool Channel::ifBanned(User* ifBan) {
std::vector<User*>::iterator itr;
for (itr=this->_bannedList.begin(); itr != this->_bannedList.end(); itr++) {
if ((char *)ifBan->getHost() == *(*itr)->getHost())
return true;
}
std::cerr << "User not found as banned" << std::endl;
return false;
}
int Channel::ifOperator(std::string _nick)
{
std::string str;
std::vector<User*>::iterator itr;
for (itr=this->_operatorList.begin(); itr != this->_operatorList.end(); itr++)
{
if(_nick == (*itr)->getNick())
return(1);
}
return (0);
}
void Channel::notify_others(std::string cmd_name, const std::string& msg, User* skip)
{
std::string tosend = ":" + skip->getPrefix() + " " + cmd_name + " " + this->getName() + " " + msg;
std::vector<User*>::iterator itr;
for (itr=this->_userLogList.begin(); itr != this->_userLogList.end(); itr++) {
if (skip->getNick() != (*itr)->getNick())
(*itr)->replyLight(tosend.c_str());
}
}
void Channel::deleteOperator(User* target)
{
std::vector<User*>::iterator start = this->_operatorList.begin();
std::vector<User*>::iterator end = this->_operatorList.end();
while (start != end)
{
if (target->getNick() == (*start)->getNick())
{
this->_operatorList.erase(start);
return;
}
start++;
}
}