aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Handlers/ChannelHandler.cpp
blob: 6b06758449a2eccaa1ee07a19a0ad45a8c127f47 (plain)
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
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "WorldSession.h"
#include "Channel.h"
#include "ChannelMgr.h"
#include "ChannelPackets.h"
#include "DB2Stores.h"
#include "Log.h"
#include "ObjectMgr.h"                                      // for normalizePlayerName
#include "Player.h"
#include <cctype>

static size_t const MAX_CHANNEL_NAME_STR = 0x31;
static size_t const MAX_CHANNEL_PASS_STR = 31;

void WorldSession::HandleJoinChannel(WorldPackets::Channel::JoinChannel& packet)
{
    TC_LOG_DEBUG("chat.system", "CMSG_JOIN_CHANNEL %s ChatChannelId: %u, CreateVoiceSession: %u, Internal: %u, ChannelName: %s, Password: %s",
        GetPlayerInfo().c_str(), packet.ChatChannelId, packet.CreateVoiceSession, packet.Internal, packet.ChannelName.c_str(), packet.Password.c_str());

    AreaTableEntry const* zone = sAreaTableStore.LookupEntry(GetPlayer()->GetZoneId());
    if (packet.ChatChannelId)
    {
        ChatChannelsEntry const* channel = sChatChannelsStore.LookupEntry(packet.ChatChannelId);
        if (!channel)
            return;

        if (!zone || !GetPlayer()->CanJoinConstantChannelInZone(channel, zone))
            return;
    }

    if (packet.ChannelName.empty())
        return;

    if (isdigit(packet.ChannelName[0]))
        return;

    if (ChannelMgr* cMgr = ChannelMgr::ForTeam(GetPlayer()->GetTeam()))
        if (Channel* channel = cMgr->GetJoinChannel(packet.ChatChannelId, packet.ChannelName, zone))
            channel->JoinChannel(GetPlayer(), packet.Password);
}

void WorldSession::HandleLeaveChannel(WorldPackets::Channel::LeaveChannel& packet)
{
    TC_LOG_DEBUG("chat.system", "CMSG_LEAVE_CHANNEL %s ChannelName: %s, ZoneChannelID: %u",
        GetPlayerInfo().c_str(), packet.ChannelName.c_str(), packet.ZoneChannelID);

    if (packet.ChannelName.empty() && !packet.ZoneChannelID)
        return;

    AreaTableEntry const* zone = sAreaTableStore.LookupEntry(GetPlayer()->GetZoneId());
    if (packet.ZoneChannelID)
    {
        ChatChannelsEntry const* channel = sChatChannelsStore.LookupEntry(packet.ZoneChannelID);
        if (!channel)
            return;

        if (!zone || !GetPlayer()->CanJoinConstantChannelInZone(channel, zone))
            return;
    }

    if (ChannelMgr* cMgr = ChannelMgr::ForTeam(GetPlayer()->GetTeam()))
    {
        if (Channel* channel = cMgr->GetChannel(packet.ZoneChannelID, packet.ChannelName, GetPlayer(), true, zone))
            channel->LeaveChannel(GetPlayer(), true);

        if (packet.ZoneChannelID)
            cMgr->LeftChannel(packet.ZoneChannelID, zone);
        else
            cMgr->LeftChannel(packet.ChannelName);
    }
}

void WorldSession::HandleChannelCommand(WorldPackets::Channel::ChannelCommand& packet)
{
    TC_LOG_DEBUG("chat.system", "%s %s ChannelName: %s",
        GetOpcodeNameForLogging(packet.GetOpcode()).c_str(), GetPlayerInfo().c_str(), packet.ChannelName.c_str());

    if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(packet.ChannelName, GetPlayer()))
    {
        switch (packet.GetOpcode())
        {
            case CMSG_CHAT_CHANNEL_ANNOUNCEMENTS:
                channel->Announce(GetPlayer());
                break;
            case CMSG_CHAT_CHANNEL_DECLINE_INVITE:
                channel->DeclineInvite(GetPlayer());
                break;
            case CMSG_CHAT_CHANNEL_DISPLAY_LIST:
            case CMSG_CHAT_CHANNEL_LIST:
                channel->List(GetPlayer());
                break;
            case CMSG_CHAT_CHANNEL_OWNER:
                channel->SendWhoOwner(GetPlayer());
                break;
            default:
                break;
        }
    }
}

void WorldSession::HandleChannelPlayerCommand(WorldPackets::Channel::ChannelPlayerCommand& packet)
{
    if (packet.Name.length() >= MAX_CHANNEL_NAME_STR)
    {
        TC_LOG_DEBUG("chat.system", "%s %s ChannelName: %s, Name: %s, Name too long.",
            GetOpcodeNameForLogging(packet.GetOpcode()).c_str(), GetPlayerInfo().c_str(), packet.ChannelName.c_str(), packet.Name.c_str());
        return;
    }

    TC_LOG_DEBUG("chat.system", "%s %s ChannelName: %s, Name: %s",
        GetOpcodeNameForLogging(packet.GetOpcode()).c_str(), GetPlayerInfo().c_str(), packet.ChannelName.c_str(), packet.Name.c_str());

    if (!normalizePlayerName(packet.Name))
        return;

    if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(packet.ChannelName, GetPlayer()))
    {
        switch (packet.GetOpcode())
        {
            case CMSG_CHAT_CHANNEL_BAN:
                channel->Ban(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_INVITE:
                channel->Invite(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_KICK:
                channel->Kick(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_MODERATOR:
                channel->SetModerator(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_SET_OWNER:
                channel->SetOwner(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_SILENCE_ALL:
                channel->SilenceAll(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_UNBAN:
                channel->UnBan(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_UNMODERATOR:
                channel->UnsetModerator(GetPlayer(), packet.Name);
                break;
            case CMSG_CHAT_CHANNEL_UNSILENCE_ALL:
                channel->UnsilenceAll(GetPlayer(), packet.Name);
                break;
            default:
                break;
        }
    }
}

void WorldSession::HandleChannelPassword(WorldPackets::Channel::ChannelPassword& packet)
{
    if (packet.Password.length() > MAX_CHANNEL_PASS_STR)
    {
        TC_LOG_DEBUG("chat.system", "%s %s ChannelName: %s, Password: %s, Password too long.",
            GetOpcodeNameForLogging(packet.GetOpcode()).c_str(), GetPlayerInfo().c_str(), packet.ChannelName.c_str(), packet.Password.c_str());
        return;
    }

    TC_LOG_DEBUG("chat.system", "%s %s ChannelName: %s, Password: %s",
        GetOpcodeNameForLogging(packet.GetOpcode()).c_str(), GetPlayerInfo().c_str(), packet.ChannelName.c_str(), packet.Password.c_str());

    if (Channel* channel = ChannelMgr::GetChannelForPlayerByNamePart(packet.ChannelName, GetPlayer()))
        channel->Password(GetPlayer(), packet.Password);
}