forked from codewhite7777/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.cpp
174 lines (151 loc) · 4.12 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Channel.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 11:19:20 by mgo #+# #+# */
/* Updated: 2022/10/12 11:19:23 by mgo ### ########.fr */
/* */
/* ************************************************************************** */
#include "Channel.hpp"
#include "Client.hpp"
#include "Server.hpp"
#include <string>
#include <map>
#include <list>
Channel::Channel(std::string name)
: name_(name)
, curr_user_count_(0) {}
Channel::~Channel(void) {}
std::size_t Channel::getCurrUserCount(void) const
{
return curr_user_count_;
}
void Channel::assignAsUser(Client* clnt)
{
users_.insert(std::make_pair(clnt->getNickname(), clnt));
++curr_user_count_;
}
void Channel::assignAsOperator(Client* clnt)
{
opers_.insert(std::make_pair(clnt->getNickname(), clnt));
}
void Channel::eraseAsUser(Client* clnt)
{
std::map<std::string, Client*>::iterator user_it;
user_it = users_.find(clnt->getNickname());
if (user_it != users_.end())
{
users_.erase(user_it);
--curr_user_count_;
}
}
void Channel::eraseAsOperator(Client* clnt)
{
std::map<std::string, Client*>::iterator oper_it;
oper_it = opers_.find(clnt->getNickname());
if (oper_it != opers_.end())
opers_.erase(oper_it);
}
const std::string& Channel::getName(void) const
{
return name_;
}
std::string Channel::getUserListStr(void)
{
std::string ret("");
for (std::map<std::string, Client*>::iterator user_it = users_.begin()
; user_it != users_.end()
; ++user_it)
{
if (isOperator(user_it->second))
ret += "@";
ret += user_it->second->getNickname() + " ";
}
return ret;
}
bool Channel::isOperator(Client* clnt)
{
for (std::map<std::string, Client*>::iterator oper_it = opers_.begin()
; oper_it != opers_.end()
; ++oper_it)
{
if (oper_it->second == clnt)
{
return true;
}
}
return false;
}
void Channel::sendToAll(std::string msg)
{
for (std::map<std::string, Client*>::iterator user_it = users_.begin()
; user_it != users_.end()
; ++user_it)
{
user_it->second->appendToSendBuf(msg);
}
}
void Channel::sendToOthers(Client* clnt, std::string msg)
{
for (std::map<std::string, Client*>::iterator user_it = users_.begin()
; user_it != users_.end()
; ++user_it)
{
if (user_it->second != clnt)
user_it->second->appendToSendBuf(msg);
}
}
bool Channel::isUserIn(Client* clnt)
{
std::map<std::string, Client*>::iterator clnt_it;
clnt_it = users_.find(clnt->getNickname());
if (clnt_it == users_.end())
return (false);
return (true);
}
std::list<Client*>* Channel::makeClntListInChannExceptOne(Client* clnt_to_excpt)
{
std::list<Client*>* ret(NULL);
Client* each_clnt(NULL);
ret = new std::list<Client*>;
for (std::map<std::string, Client*>::iterator user_it(users_.begin())
; user_it != users_.end()
; ++user_it)
{
each_clnt = user_it->second;
if (each_clnt != clnt_to_excpt)
ret->push_back(each_clnt);
each_clnt = NULL;
}
return ret;
}
void Channel::eraseClntIfIs(Client* clnt)
{
eraseAsOperator(clnt);
eraseAsUser(clnt);
}
void Channel::replaceClntKeyNick(Client* clnt, \
std::string nick_to_key)
{
std::map<std::string, Client*>::iterator user_it;
std::map<std::string, Client*>::iterator oper_it;
user_it = users_.find(clnt->getNickname());
if (user_it != users_.end())
{
users_.erase(user_it);
users_.insert(std::make_pair(nick_to_key, clnt));
}
oper_it = opers_.find(clnt->getNickname());
if (oper_it != opers_.end())
{
opers_.erase(oper_it);
opers_.insert(std::make_pair(nick_to_key, clnt));
}
}
ChatBot& Channel::GetChatBot()
{
return chatbot;
}