TrinityCore
Loading...
Searching...
No Matches
ChannelHandler.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "WorldSession.h"
19#include "Channel.h"
20#include "ChannelMgr.h"
21#include "DBCStores.h"
22#include "Log.h"
23#include "ObjectMgr.h" // for normalizePlayerName
24#include "Player.h"
25#include <cctype>
26
27static size_t const MAX_CHANNEL_PASS_STR = 31;
28static size_t const MAX_CHANNEL_NAME_STR = 31;
29
31{
32 uint32 channelId;
33 uint8 unknown1, unknown2;
34 std::string channelName, password;
35
36 recvPacket >> channelId >> unknown1 >> unknown2 >> channelName >> password;
37
38 TC_LOG_DEBUG("chat.system", "CMSG_JOIN_CHANNEL {} Channel: {}, unk1: {}, unk2: {}, channel: {}, password: {}",
39 GetPlayerInfo(), channelId, unknown1, unknown2, channelName, password);
40
41 AreaTableEntry const* zone = sAreaTableStore.LookupEntry(GetPlayer()->GetZoneId());
42 if (channelId)
43 {
44 ChatChannelsEntry const* channel = sChatChannelsStore.LookupEntry(channelId);
45 if (!channel)
46 return;
47
48 if (!zone || !GetPlayer()->CanJoinConstantChannelInZone(channel, zone))
49 return;
50 }
51
52 if (channelName.empty() || isdigit((unsigned char)channelName[0]))
53 {
54 WorldPacket data(SMSG_CHANNEL_NOTIFY, 1 + channelName.size());
55 data << uint8(CHAT_INVALID_NAME_NOTICE) << channelName;
56 SendPacket(&data);
57 return;
58 }
59
60 if (password.length() > MAX_CHANNEL_PASS_STR)
61 {
62 TC_LOG_ERROR("network", "Player {} tried to create a channel with a password more than {} characters long - blocked", GetPlayer()->GetGUID().ToString(), MAX_CHANNEL_PASS_STR);
63 return;
64 }
65
66 if (!DisallowHyperlinksAndMaybeKick(channelName))
67 return;
68
69 if (ChannelMgr* cMgr = ChannelMgr::forTeam(GetPlayer()->GetTeam()))
70 {
71 if (channelId)
72 { // system channel
73 if (Channel* channel = cMgr->GetSystemChannel(channelId, zone))
74 channel->JoinChannel(GetPlayer());
75 }
76 else
77 { // custom channel
78 if (channelName.length() > MAX_CHANNEL_NAME_STR)
79 {
80 TC_LOG_ERROR("network", "Player {} tried to create a channel with a name more than {} characters long - blocked", GetPlayer()->GetGUID().ToString(), MAX_CHANNEL_NAME_STR);
81 return;
82 }
83
84 if (Channel* channel = cMgr->GetCustomChannel(channelName))
85 channel->JoinChannel(GetPlayer(), password);
86 else if (Channel* channel = cMgr->CreateCustomChannel(channelName))
87 {
88 channel->SetPassword(password);
89 channel->JoinChannel(GetPlayer(), password);
90 }
91 }
92 }
93}
94
96{
97 uint32 channelId;
98 std::string channelName;
99 recvPacket >> channelId >> channelName;
100
101 TC_LOG_DEBUG("chat.system", "CMSG_LEAVE_CHANNEL {} Channel: {}, channelId: {}",
102 GetPlayerInfo(), channelName, channelId);
103
104 if (channelName.empty() && !channelId)
105 return;
106
107 AreaTableEntry const* zone = sAreaTableStore.LookupEntry(GetPlayer()->GetZoneId());
108 if (channelId)
109 {
110 ChatChannelsEntry const* channel = sChatChannelsStore.LookupEntry(channelId);
111 if (!channel)
112 return;
113
114 if (!zone || !GetPlayer()->CanJoinConstantChannelInZone(channel, zone))
115 return;
116 }
117
118 if (ChannelMgr* cMgr = ChannelMgr::forTeam(GetPlayer()->GetTeam()))
119 {
120 if (Channel* channel = cMgr->GetChannel(channelId, channelName, GetPlayer(), true, zone))
121 channel->LeaveChannel(GetPlayer(), true);
122
123 if (channelId)
124 cMgr->LeftChannel(channelId, zone);
125 }
126}
127
129{
130 std::string channelName;
131 recvPacket >> channelName;
132
133 TC_LOG_DEBUG("chat.system", "{} {} Channel: {}",
134 recvPacket.GetOpcode() == CMSG_CHANNEL_DISPLAY_LIST ? "CMSG_CHANNEL_DISPLAY_LIST" : "CMSG_CHANNEL_LIST",
135 GetPlayerInfo(), channelName);
136
137 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
138 channel->List(GetPlayer());
139}
140
142{
143 std::string channelName, password;
144 recvPacket >> channelName >> password;
145
146 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_PASSWORD {} Channel: {}, Password: {}",
147 GetPlayerInfo(), channelName, password);
148
149 if (password.length() > MAX_CHANNEL_PASS_STR)
150 return;
151
152 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
153 channel->Password(GetPlayer(), password);
154}
155
157{
158 std::string channelName, targetName;
159 recvPacket >> channelName >> targetName;
160
161 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_SET_OWNER {} Channel: {}, Target: {}",
162 GetPlayerInfo(), channelName, targetName);
163
164 if (!normalizePlayerName(targetName))
165 return;
166
167 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
168 channel->SetOwner(GetPlayer(), targetName);
169}
170
172{
173 std::string channelName;
174 recvPacket >> channelName;
175
176 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_OWNER {} Channel: {}",
177 GetPlayerInfo(), channelName);
178
179 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
180 channel->SendWhoOwner(GetPlayer()->GetGUID());
181}
182
184{
185 std::string channelName, targetName;
186 recvPacket >> channelName >> targetName;
187
188 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_MODERATOR {} Channel: {}, Target: {}",
189 GetPlayerInfo(), channelName, targetName);
190
191 if (!normalizePlayerName(targetName))
192 return;
193
194 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
195 channel->SetModerator(GetPlayer(), targetName);
196}
197
199{
200 std::string channelName, targetName;
201 recvPacket >> channelName >> targetName;
202
203 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_UNMODERATOR {} Channel: {}, Target: {}",
204 GetPlayerInfo(), channelName, targetName);
205
206 if (!normalizePlayerName(targetName))
207 return;
208
209 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
210 channel->UnsetModerator(GetPlayer(), targetName);
211}
212
214{
215 std::string channelName, targetName;
216 recvPacket >> channelName >> targetName;
217
218 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_MUTE {} Channel: {}, Target: {}",
219 GetPlayerInfo(), channelName, targetName);
220
221 if (!normalizePlayerName(targetName))
222 return;
223
224 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
225 channel->SetMute(GetPlayer(), targetName);
226}
227
229{
230 std::string channelName, targetName;
231 recvPacket >> channelName >> targetName;
232
233 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_UNMUTE {} Channel: {}, Target: {}",
234 GetPlayerInfo(), channelName, targetName);
235
236 if (!normalizePlayerName(targetName))
237 return;
238
239 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
240 channel->UnsetMute(GetPlayer(), targetName);
241}
242
244{
245 std::string channelName, targetName;
246 recvPacket >> channelName >> targetName;
247
248 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_INVITE {} Channel: {}, Target: {}",
249 GetPlayerInfo(), channelName, targetName);
250
251 if (!normalizePlayerName(targetName))
252 return;
253
254 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
255 channel->Invite(GetPlayer(), targetName);
256}
257
259{
260 std::string channelName, targetName;
261 recvPacket >> channelName >> targetName;
262
263 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_KICK {} Channel: {}, Target: {}",
264 GetPlayerInfo(), channelName, targetName);
265
266 if (!normalizePlayerName(targetName))
267 return;
268
269 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
270 channel->Kick(GetPlayer(), targetName);
271}
272
274{
275 std::string channelName, targetName;
276 recvPacket >> channelName >> targetName;
277
278 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_BAN {} Channel: {}, Target: {}",
279 GetPlayerInfo(), channelName, targetName);
280
281 if (!normalizePlayerName(targetName))
282 return;
283
284 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
285 channel->Ban(GetPlayer(), targetName);
286}
287
289{
290 std::string channelName, targetName;
291 recvPacket >> channelName >> targetName;
292
293 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_UNBAN {} Channel: {}, Target: {}",
294 GetPlayerInfo(), channelName, targetName);
295
296 if (!normalizePlayerName(targetName))
297 return;
298
299 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
300 channel->UnBan(GetPlayer(), targetName);
301}
302
304{
305 std::string channelName;
306 recvPacket >> channelName;
307
308 TC_LOG_DEBUG("chat.system", "CMSG_CHANNEL_ANNOUNCEMENTS {} Channel: {}",
309 GetPlayerInfo(), channelName);
310
311 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
312 channel->Announce(GetPlayer());
313}
314
316{
317 // this should be OK because the 2 function _were_ the same
318 HandleChannelList(recvPacket);
319}
320
322{
323 std::string channelName;
324 recvPacket >> channelName;
325
326 TC_LOG_DEBUG("chat.system", "CMSG_GET_CHANNEL_MEMBER_COUNT {} Channel: {}",
327 GetPlayerInfo(), channelName);
328
329 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
330 {
331 TC_LOG_DEBUG("chat.system", "SMSG_CHANNEL_MEMBER_COUNT {} Channel: {} Count: {}",
332 GetPlayerInfo(), channelName, channel->GetNumPlayers());
333
334 std::string name = channel->GetName(GetSessionDbcLocale());
335 WorldPacket data(SMSG_CHANNEL_MEMBER_COUNT, name.size() + 1 + 4);
336 data << name;
337 data << uint8(channel->GetFlags());
338 data << uint32(channel->GetNumPlayers());
339 SendPacket(&data);
340 }
341}
342
344{
345 std::string channelName;
346 recvPacket >> channelName;
347
348 TC_LOG_DEBUG("chat.system", "CMSG_SET_CHANNEL_WATCH {} Channel: {}",
349 GetPlayerInfo(), channelName);
350
351 /*
352 if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(channelName, GetPlayer()))
353 channel->JoinNotify(GetPlayer());
354 */
355}
static size_t const MAX_CHANNEL_PASS_STR
static size_t const MAX_CHANNEL_NAME_STR
@ CHAT_INVALID_NAME_NOTICE
Definition Channel.h:62
DBCStorage< ChatChannelsEntry > sChatChannelsStore(ChatChannelsEntryfmt)
DBCStorage< AreaTableEntry > sAreaTableStore(AreaTableEntryfmt)
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
#define TC_LOG_DEBUG(filterType__,...)
Definition Log.h:156
#define TC_LOG_ERROR(filterType__,...)
Definition Log.h:165
bool normalizePlayerName(std::string &name)
static ChannelMgr * forTeam(uint32 team)
static Channel * GetChannelForPlayerByNamePart(std::string const &namePart, Player *playerSearcher)
uint16 GetOpcode() const
Definition WorldPacket.h:80
void HandleGetChannelMemberCount(WorldPacket &recvPacket)
void HandleChannelInvite(WorldPacket &recvPacket)
void SendPacket(WorldPacket const *packet)
Send a packet to the client.
bool DisallowHyperlinksAndMaybeKick(std::string const &str)
void HandleChannelList(WorldPacket &recvPacket)
void HandleChannelPassword(WorldPacket &recvPacket)
LocaleConstant GetSessionDbcLocale() const
std::string GetPlayerInfo() const
Player * GetPlayer() const
void HandleJoinChannel(WorldPacket &recvPacket)
void HandleSetChannelWatch(WorldPacket &recvPacket)
void HandleChannelUnban(WorldPacket &recvPacket)
void HandleChannelMute(WorldPacket &recvPacket)
void HandleChannelUnmute(WorldPacket &recvPacket)
void HandleChannelBan(WorldPacket &recvPacket)
void HandleChannelUnmoderator(WorldPacket &recvPacket)
void HandleChannelModerator(WorldPacket &recvPacket)
void HandleLeaveChannel(WorldPacket &recvPacket)
void HandleChannelSetOwner(WorldPacket &recvPacket)
void HandleChannelOwner(WorldPacket &recvPacket)
void HandleChannelAnnouncements(WorldPacket &recvPacket)
void HandleChannelKick(WorldPacket &recvPacket)
void HandleChannelDisplayListQuery(WorldPacket &recvPacket)
@ SMSG_CHANNEL_NOTIFY
Definition Opcodes.h:182
@ CMSG_CHANNEL_DISPLAY_LIST
Definition Opcodes.h:1007
@ SMSG_CHANNEL_MEMBER_COUNT
Definition Opcodes.h:1010