-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cpp
More file actions
224 lines (188 loc) · 4.55 KB
/
Copy pathChannel.cpp
File metadata and controls
224 lines (188 loc) · 4.55 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
#include "include/Channel.hpp"
#include "include/Session.hpp"
#include "include/Frame.hpp"
Channel::Channel(Session *creator, std::string const& name, std::string const& topic)
: _sName(name), _sTopic(topic)
{
_mUsers[creator->user().nick()] = creator;
_mOperators[creator->user().nick()] = creator;
}
Channel::Channel(Channel const& other)
: _sName(other._sName)
{
*this = other;
}
Channel& Channel::operator=(Channel const& other)
{
if (this == &other)
return *this;
this->_sTopic = other._sTopic;
this->_mUsers = other._mUsers;
this->_mOperators = other._mOperators;
return *this;
}
Channel::~Channel()
{
}
void Channel::addUser(Session *user)
{
_mUsers[user->user().nick()] = user;
}
void Channel::addOper(Session *user)
{
_mOperators[user->user().nick()] = user;
}
void Channel::removeUser(std::string const& nick)
{
_mUsers.erase(nick);
}
void Channel::removeOper(std::string const& nick)
{
_mOperators.erase(nick);
}
bool Channel::hasUser(std::string const& nick) const
{
UserMap::const_iterator it;
it = _mUsers.find(nick);
if (it == _mUsers.end())
return false;
return true;
}
bool Channel::hasOper(std::string const& nick) const
{
UserMap::const_iterator it;
it = _mOperators.find(nick);
if (it == _mOperators.end())
return false;
return true;
}
void Channel::broadcast(Session *ss, std::string const& message)
{
UserMap::iterator it;
it = _mUsers.begin();
for (; it != _mUsers.end() ; ++it)
ss->replyAsUser(it->second, message);
}
void Channel::privmsgBroadcast(Session *ss, std::string const& message)
{
UserMap::iterator it;
it = _mUsers.begin();
for (; it != _mUsers.end() ; ++it)
{
if (ss->user().nick() == it->first)
continue ;
ss->replyAsUser(it->second, message);
}
}
std::string Channel::topic() const
{
return _sTopic;
}
std::string Channel::name() const
{
return _sName;
}
int Channel::userCount() const
{
return _mUsers.size();
}
int Channel::operCount() const
{
return _mOperators.size();
}
bool Channel::hasPass() const
{
return false;
}
void Channel::cmdPart(std::string const& nick)
{
UserMap::iterator it;
removeUser(nick);
removeOper(nick);
//delete channel
if (_mUsers.size() == 0)
return ;
//pass operation to the first user
else if (_mOperators.size() == 0)
{
it = _mUsers.begin();
_mOperators[it->first] = it->second;
}
}
void Channel::cmdNick(std::string const& name, std::string const& nick)
{
UserMap::iterator it;
it = _mUsers.find(name);
if (it != _mUsers.end())
{
_mUsers.insert(std::pair<std::string, Session*>(nick, it->second));
_mUsers.erase(it);
}
it = _mOperators.find(name);
if (it != _mOperators.end())
{
_mOperators.insert(std::pair<std::string, Session*>(nick, it->second));
_mOperators.erase(it);
}
}
void Channel::cmdTopic(Session *ss, std::string const& topic, std::string const& msg)
{
UserMap::iterator it;
setTopic(topic);
broadcast(ss, msg);
}
void Channel::cmdJoin(Session *ss)
{
std::string str;
UserMap::iterator it;
if (topic() == "")
ss->rep331(name());
else
ss->rep332(name(), topic());
for (it = _mUsers.begin(); it != _mUsers.end(); it++)
{
if (it != _mUsers.begin())
str += " ";
if (_mOperators.find(it->first) != _mOperators.end())
str += "@";
str += it->first;
}
if (str != "")
ss->rep353(name(), str);
ss->rep366(name());
}
void Channel::setTopic(std::string const topic)
{
_sTopic = topic;
}
/*
* Fit to a RPL_WHOREPLY format
*/
std::vector<std::string> Channel::channelVector()
{
UserMap::iterator it;
std::vector<std::string> res;
std::string servername = Frame::instance()->getServer().name();
for (it = _mUsers.begin(); it != _mUsers.end(); ++it)
{
if (hasOper(it->second->user().nick()))
res.push_back("#" + name() + " " + it->second->user().user() + " " + it->second->user().host() + " " + servername + " " + it->second->user().nick() + " H @ :0 " + it->second->user().name());
else
res.push_back("#" + name() + " " + it->second->user().user() + " " + it->second->user().host() + " " + servername + " " + it->second->user().nick() + " H :0 " + it->second->user().name());
}
return res;
}
/*
* Only Channel Manager responded
*/
std::vector<std::string> Channel::channeloperVector()
{
UserMap::iterator it;
std::vector<std::string> res;
std::string servername = Frame::instance()->getServer().name();
for (it = _mOperators.begin(); it != _mOperators.end(); ++it)
{
res.push_back("#" + name() + " " + it->second->user().user() + " " + it->second->user().host() + " " + servername + " " + it->second->user().nick() + " H @ :0 " + it->second->user().name());
}
return res;
}