-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cpp
More file actions
245 lines (231 loc) · 7.65 KB
/
Channel.cpp
File metadata and controls
245 lines (231 loc) · 7.65 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
234
235
236
237
238
239
240
241
242
243
244
245
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hlabouit <hlabouit@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 14:02:10 by frukundo #+# #+# */
/* Updated: 2024/10/02 10:22:46 by hlabouit ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/Channel.hpp"
Channel::Channel(){
this->invit_only = 0;
this->topic = 0;
this->key = 0;
this->limit = 0;
this->topic_restriction = false;
this->name = "";
this->topic_name = "";
char charaters[5] = {'i', 't', 'k', 'o', 'l'};
for(int i = 0; i < 5; i++)
modes.push_back(std::make_pair(charaters[i],false));
this->created_at = "";
}
Channel::~Channel(){}
Channel::Channel(Channel const &src){*this = src;}
Channel &Channel::operator=(Channel const &src){
if (this != &src){
this->invit_only = src.invit_only;
this->topic = src.topic;
this->key = src.key;
this->limit = src.limit;
this->topic_restriction = src.topic_restriction;
this->name = src.name;
this->password = src.password;
this->created_at = src.created_at;
this->topic_name = src.topic_name;
this->clients = src.clients;
this->admins = src.admins;
this->modes = src.modes;
}
return *this;
}
void Channel::SetInvitOnly(int invit_only){this->invit_only = invit_only;}
void Channel::SetTopic(int topic){this->topic = topic;}
void Channel::SetTime(std::string time){this->time_creation = time;}
void Channel::SetKey(int key){this->key = key;}
void Channel::SetLimit(int limit){this->limit = limit;}
void Channel::SetTopicName(std::string topic_name){this->topic_name = topic_name;}
void Channel::SetPassword(std::string password){this->password = password;}
void Channel::SetName(std::string name){this->name = name;}
void Channel::set_topicRestriction(bool value){this->topic_restriction = value;}
void Channel::setModeAtindex(size_t index, bool mode){modes[index].second = mode;}
void Channel::set_createiontime(){
std::time_t _time = std::time(NULL);
std::ostringstream oss;
oss << _time;
this->created_at = std::string(oss.str());
}
int Channel::GetInvitOnly(){return this->invit_only;}
int Channel::GetTopic(){return this->topic;}
int Channel::GetKey(){return this->key;}
int Channel::GetLimit(){return this->limit;}
int Channel::GetClientsNumber(){return this->clients.size() + this->admins.size();}
bool Channel::Gettopic_restriction() const{return this->topic_restriction;}
bool Channel::getModeAtindex(size_t index){return modes[index].second;}
bool Channel::clientInChannel(std::string &nick){
for(size_t i = 0; i < clients.size(); i++){
if(clients[i].GetNickName() == nick)
return true;
}
for(size_t i = 0; i < admins.size(); i++){
if(admins[i].GetNickName() == nick)
return true;
}
return false;
}
std::string Channel::GetTopicName(){return this->topic_name;}
std::string Channel::GetPassword(){return this->password;}
std::string Channel::GetName(){return this->name;}
std::string Channel::GetTime(){return this->time_creation;}
std::string Channel::get_creationtime(){return created_at;}
std::string Channel::getModes(){
std::string mode;
for(size_t i = 0; i < modes.size(); i++){
if(modes[i].first != 'o' && modes[i].second)
mode.push_back(modes[i].first);
}
if(!mode.empty())
mode.insert(mode.begin(),'+');
return mode;
}
std::string Channel::clientChannel_list(){
std::string list;
for(size_t i = 0; i < admins.size(); i++){
list += "@" + admins[i].GetNickName();
if((i + 1) < admins.size())
list += " ";
}
if(clients.size())
list += " ";
for(size_t i = 0; i < clients.size(); i++){
list += clients[i].GetNickName();
if((i + 1) < clients.size())
list += " ";
}
return list;
}
Client *Channel::get_client(int fd){
for (std::vector<Client>::iterator it = clients.begin(); it != clients.end(); ++it){
if (it->GetFd() == fd)
return &(*it);
}
return NULL;
}
Client *Channel::get_admin(int fd){
for (std::vector<Client>::iterator it = admins.begin(); it != admins.end(); ++it){
if (it->GetFd() == fd)
return &(*it);
}
return NULL;
}
Client* Channel::GetClientInChannel(std::string name)
{
for (std::vector<Client>::iterator it = clients.begin(); it != clients.end(); ++it){
if (it->GetNickName() == name)
return &(*it);
}
for (std::vector<Client>::iterator it = admins.begin(); it != admins.end(); ++it){
if (it->GetNickName() == name)
return &(*it);
}
return NULL;
}
void Channel::add_client(Client newClient){clients.push_back(newClient);}
void Channel::add_admin(Client newClient){admins.push_back(newClient);}
void Channel::remove_client(int fd){
for (std::vector<Client>::iterator it = clients.begin(); it != clients.end(); ++it){
if (it->GetFd() == fd)
{clients.erase(it); break;}
}
}
void Channel::remove_admin(int fd){
for (std::vector<Client>::iterator it = admins.begin(); it != admins.end(); ++it){
if (it->GetFd() == fd)
{admins.erase(it); break;}
}
}
bool Channel::change_clientToAdmin(std::string& nick){
size_t i = 0;
for(; i < clients.size(); i++){
if(clients[i].GetNickName() == nick)
break;
}
if(i < clients.size()){
admins.push_back(clients[i]);
clients.erase(i + clients.begin());
return true;
}
return false;
}
bool Channel::change_adminToClient(std::string& nick){
size_t i = 0;
for(; i < admins.size(); i++){
if(admins[i].GetNickName() == nick)
break;
}
if(i < admins.size()){
clients.push_back(admins[i]);
admins.erase(i + admins.begin());
promoteClientToAdminIfNone();
return true;
}
return false;
}
void Channel::sendTo_all(std::string rpl1)
{
for(size_t i = 0; i < admins.size(); i++)
if(send(admins[i].GetFd(), rpl1.c_str(), rpl1.size(),0) == -1)
std::cerr << "send() faild" << std::endl;
for(size_t i = 0; i < clients.size(); i++)
if(send(clients[i].GetFd(), rpl1.c_str(), rpl1.size(),0) == -1)
std::cerr << "send() faild" << std::endl;
}
void Channel::sendTo_all(std::string rpl1, int fd)
{
for(size_t i = 0; i < admins.size(); i++){
if(admins[i].GetFd() != fd)
if(send(admins[i].GetFd(), rpl1.c_str(), rpl1.size(),0) == -1)
std::cerr << "send() faild" << std::endl;
}
for(size_t i = 0; i < clients.size(); i++){
if(clients[i].GetFd() != fd)
if(send(clients[i].GetFd(), rpl1.c_str(), rpl1.size(),0) == -1)
std::cerr << "send() faild" << std::endl;
}
}
void Channel::update_nickname(std::string old_nickname, std::string new_nickname)
{
for (std::vector<Client>::iterator it = clients.begin(); it != clients.end(); ++it)
{
if (it->GetNickName() == old_nickname)
{
it->SetNickname(new_nickname);
return;
}
}
for (std::vector<Client>::iterator it = admins.begin(); it != admins.end(); ++it)
{
if (it->GetNickName() == old_nickname)
{
it->SetNickname(new_nickname);
return;
}
}
}
void Channel::promoteClientToAdminIfNone()
{
if (admins.empty())
{
if (!clients.empty())
{
admins.push_back(clients.front());
clients.erase(clients.begin());
Client cl = clients.front();
std::string rpl = ":" + cl.get_client_host(cl.getIpAdd()) + " MODE " + GetName() + " +o " + cl.GetNickName() + "\r\n";
sendTo_all(rpl);
}
}
}