-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.cpp
More file actions
318 lines (255 loc) · 7.87 KB
/
channel.cpp
File metadata and controls
318 lines (255 loc) · 7.87 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
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
307
308
309
310
311
312
313
314
315
316
317
318
#include <algorithm>
#include "constants.h"
#include "channel.h"
#include "client.h"
#include "db.h"
#include <connection.h>
#include <Logger.h>
using namespace FTS;
using namespace FTSSrv2;
using namespace std;
FTSSrv2::Channel::Channel(const channel_parameter_t & in_param, DataBase* in_pDataBase )
: m_iID(in_param.id)
, m_bPublic(in_param.isPublic)
, m_sName(in_param.name)
, m_sMotto(in_param.motto)
, m_sAdmin(in_param.admin)
, m_pDataBase(in_pDataBase)
{
m_lsOperators.clear( );
m_lpUsers.clear( );
FTSMSGDBG("Created channel "+m_sName+" ("+string(m_bPublic ? "public" : "private")+", admin: "+m_sAdmin+")", 1);
}
FTSSrv2::Channel::~Channel( )
{
// Kick all users that are still in the channel.
while(!m_lpUsers.empty()) {
this->kick(nullptr, m_lpUsers.front()->getNick());
}
m_lsOperators.clear();
}
int FTSSrv2::Channel::join(Client *in_pUser)
{
string sJoinName = in_pUser->getNick();
// Add the user to the users list.
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_lpUsers.push_back(in_pUser);
// Tell everybody that somebody joined.
for(const auto& i : m_lpUsers) {
// But don't tell myself !
if( in_pUser == i )
continue;
i->sendChatJoins( sJoinName );
}
}
in_pUser->setMyChannel(this);
FTSMSGDBG(in_pUser->getNick() + " joined " + m_sName, 4);
return ERR_OK;
}
int FTSSrv2::Channel::quit(Client *in_pUser)
{
string sQuitName = in_pUser->getNick();
// Remove the user from the list.
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_lpUsers.remove(in_pUser);
// Tell everybody that somebody left.
for(const auto& i : m_lpUsers) {
i->sendChatQuits(sQuitName);
}
}
in_pUser->setMyChannel(nullptr);
FTSMSGDBG(in_pUser->getNick() + " left " + m_sName, 4);
return ERR_OK;
}
int FTSSrv2::Channel::save()
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
// Create this channel from scratch.
if( m_iID < 0 ) {
auto record = make_tuple(m_bPublic, m_sName, m_sMotto, m_sAdmin);
auto rc = m_pDataBase->channelCreate( record );
if( !rc ) {
return -1;
}
// Or just save modifications ?
} else {
auto record = make_tuple(m_iID, m_bPublic, m_sName, m_sMotto, m_sAdmin);
m_pDataBase->channelUpdate(record);
}
// Now we have to update the operators table.
for(const auto& i : m_lsOperators) {
auto record = make_tuple(i, m_iID);
m_pDataBase->channelAddOp(record);
}
return ERR_OK;
}
int FTSSrv2::Channel::destroyDB(const string &in_sWhoWantsIt)
{
// Not yet in database, ignore!
if(m_iID < 0)
return ERR_OK;
auto record = make_tuple(in_sWhoWantsIt, m_sName);
return m_pDataBase->channelDestroy(record);
}
int FTSSrv2::Channel::op(const string & in_sUser, bool in_bOninit)
{
// Don't check all these things during the startup.
if(!in_bOninit) {
// Check if the user is in the channel.
if(nullptr == this->getUserIfPresent(in_sUser)) {
FTSMSGDBG(in_sUser+" does not exist in this channel", 4);
return -31;
}
// Do not add the user to the operators list if he already is.
if(this->isop(in_sUser)) {
FTSMSGDBG(in_sUser+" is already operator in this channel", 4);
return -32;
}
}
if(in_sUser == m_sAdmin) {
FTSMSGDBG(in_sUser+" is the channel admin!", 4);
return -33;
}
// add to the operators list.
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_lsOperators.push_back(in_sUser);
if(!in_bOninit) {
// Tell everybody that somebody op's, including to me.
for(const auto& i : m_lpUsers) {
i->sendChatOped( in_sUser );
}
}
}
// Store the changes in the database.
this->save();
FTSMSGDBG(in_sUser+" op in "+m_sName, 4);
return ERR_OK;
}
int FTSSrv2::Channel::deop( const string & in_sUser )
{
// Check if the user is op.
if(!this->isop(in_sUser)) {
FTSMSGDBG(in_sUser+" is not operator in this channel", 4);
return -1;
}
// remove from the operators list.
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_lsOperators.remove(in_sUser);
// Tell everybody that somebody Deop's, including to me.
for(const auto& i : m_lpUsers) {
i->sendChatDeOped( in_sUser );
}
}
// Store the changes in the database.
this->save();
FTSMSGDBG(in_sUser+" deop in "+m_sName, 4);
return ERR_OK;
}
bool FTSSrv2::Channel::isop( const string & in_sUser )
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
for(const auto& i : m_lsOperators) {
if(in_sUser == i) {
return true;
}
}
return false;
}
int FTSSrv2::Channel::messageToAll( const Client &in_From, const string & in_sMessage, uint8_t in_cFlags )
{
Packet p(DSRV_MSG_CHAT_GETMSG);
p.append(DSRV_CHAT_TYPE::NORMAL);
p.append(in_cFlags);
p.append(in_From.getNick());
p.append(in_sMessage);
FTSMSGDBG(in_From.getNick()+" says "+in_sMessage, 4);
this->sendPacketToAll(&p);
return ERR_OK;
}
Packet FTSSrv2::Channel::makeSystemMessagePacket( const string &in_sMessageID )
{
Packet p(DSRV_MSG_CHAT_GETMSG);
p.append(DSRV_CHAT_TYPE::SYSTEM);
p.append((uint8_t)0);
p.append(in_sMessageID);
return p;
}
int FTSSrv2::Channel::sendPacketToAll( Packet *in_pPacket )
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
for(const auto& i : m_lpUsers) {
i->sendPacket(in_pPacket);
}
return ERR_OK;
}
int FTSSrv2::Channel::setMotto( const string & in_sMotto, const string & in_sUser )
{
// Only op or admin can do this !
if( !this->isop(in_sUser) && in_sUser != m_sAdmin )
return -20;
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_sMotto = in_sMotto;
// Tell everybody that somebody changed the motto, including to me.
for(const auto& i : m_lpUsers) {
i->sendChatMottoChanged( in_sUser, in_sMotto );
}
}
return this->save( );
}
int FTSSrv2::Channel::kick( const Client *in_pFrom, const string & in_sUser )
{
string sKicker;
Client *pKicked = nullptr;
// If the system wants to kick him out.
if(in_pFrom == nullptr) {
sKicker = "Arkana-FTS Server";
} else {
sKicker = in_pFrom->getNick();
// Only op or admin can do this !
if(!this->isop(sKicker) && sKicker != m_sAdmin)
return -20;
// The admin can't be kicked.
if(in_sUser == m_sAdmin) {
return -21;
}
// Only admins can kick operators.
if(sKicker != m_sAdmin && this->isop(in_sUser)) {
return -22;
}
}
// Find the user that we want to kick.
pKicked = this->getUserIfPresent(in_sUser);
// User not in the channel ?
if(pKicked == nullptr)
return -21;
// Kick him out to the default channel.
quit(pKicked);
auto defaultChannel = FTSSrv2::ChannelManager::getSingletonPtr()->getDefaultChannel();
defaultChannel->join(pKicked);
// Tell him about it.
Packet p(DSRV_MSG_CHAT_KICKED);
p.append(sKicker);
p.append(this->getName());
pKicked->sendPacket(&p);
// And tell all others in this channel about it.
auto kickMsg = this->makeSystemMessagePacket("Chat_Kicked");
kickMsg.append(sKicker);
kickMsg.append(pKicked->getNick());
sendPacketToAll(&kickMsg);
return ERR_OK;
}
Client *FTSSrv2::Channel::getUserIfPresent(const string &in_sUsername)
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
for(const auto& pCli : m_lpUsers) {
if(pCli->getNick() == in_sUsername) {
return pCli;
}
}
return nullptr;
}