AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ChannelMgr Class Reference

#include "ChannelMgr.h"

Inheritance diagram for ChannelMgr:
AllianceChannelMgr HordeChannelMgr

Public Member Functions

 ChannelMgr (TeamId teamId)
 
 ~ChannelMgr ()
 
ChannelGetJoinChannel (std::string const &name, uint32 channel_id)
 
ChannelGetChannel (std::string const &name, Player *p, bool pkt=true)
 

Static Public Member Functions

static ChannelMgrforTeam (TeamId teamId)
 
static void LoadChannels ()
 
static void LoadChannelRights ()
 
static const ChannelRightsGetChannelRightsFor (const std::string &name)
 
static void SetChannelRightsFor (const std::string &name, const uint32 &flags, const uint32 &speakDelay, const std::string &joinmessage, const std::string &speakmessage, const std::set< uint32 > &moderators)
 

Static Public Attributes

static uint32 _channelIdMax = 0
 

Private Types

typedef std::unordered_map< std::wstring, Channel * > ChannelMap
 
typedef std::map< std::string, ChannelRightsChannelRightsMap
 

Private Member Functions

void MakeNotOnPacket (WorldPacket *data, std::string const &name)
 

Private Attributes

ChannelMap channels
 
TeamId _teamId
 

Static Private Attributes

static ChannelRightsMap channels_rights
 
static ChannelRights channelRightsEmpty
 

Detailed Description

Member Typedef Documentation

◆ ChannelMap

typedef std::unordered_map<std::wstring, Channel*> ChannelMgr::ChannelMap
private

◆ ChannelRightsMap

typedef std::map<std::string, ChannelRights> ChannelMgr::ChannelRightsMap
private

Constructor & Destructor Documentation

◆ ChannelMgr()

ChannelMgr::ChannelMgr ( TeamId  teamId)
inline
35 : _teamId(teamId)
36 { }
TeamId _teamId
Definition: ChannelMgr.h:53

◆ ~ChannelMgr()

ChannelMgr::~ChannelMgr ( )
26{
27 for (ChannelMap::iterator itr = channels.begin(); itr != channels.end(); ++itr)
28 delete itr->second;
29
30 channels.clear();
31}
ChannelMap channels
Definition: ChannelMgr.h:52

References channels.

Member Function Documentation

◆ forTeam()

ChannelMgr * ChannelMgr::forTeam ( TeamId  teamId)
static
34{
35 static ChannelMgr allianceChannelMgr(TEAM_ALLIANCE);
36 static ChannelMgr hordeChannelMgr(TEAM_HORDE);
37
39 return &allianceChannelMgr; // cross-faction
40
41 if (teamId == TEAM_ALLIANCE)
42 return &allianceChannelMgr;
43
44 if (teamId == TEAM_HORDE)
45 return &hordeChannelMgr;
46
47 return nullptr;
48}
@ CONFIG_ALLOW_TWO_SIDE_INTERACTION_CHANNEL
Definition: IWorld.h:79
@ TEAM_ALLIANCE
Definition: SharedDefines.h:760
@ TEAM_HORDE
Definition: SharedDefines.h:761
#define sWorld
Definition: World.h:444
Definition: ChannelMgr.h:30

References CONFIG_ALLOW_TWO_SIDE_INTERACTION_CHANNEL, sWorld, TEAM_ALLIANCE, and TEAM_HORDE.

Referenced by WorldSession::HandleChannelAnnouncements(), WorldSession::HandleChannelBan(), WorldSession::HandleChannelInvite(), WorldSession::HandleChannelKick(), WorldSession::HandleChannelList(), WorldSession::HandleChannelModerateOpcode(), WorldSession::HandleChannelModerator(), WorldSession::HandleChannelMute(), WorldSession::HandleChannelOwner(), WorldSession::HandleChannelPassword(), WorldSession::HandleChannelSetOwner(), WorldSession::HandleChannelUnban(), WorldSession::HandleChannelUnmoderator(), WorldSession::HandleChannelUnmute(), WorldSession::HandleClearChannelWatch(), WorldSession::HandleGetChannelMemberCount(), WorldSession::HandleJoinChannel(), WorldSession::HandleLeaveChannel(), WorldSession::HandleMessagechatOpcode(), WorldSession::HandleSetChannelWatch(), LoadChannels(), and Player::UpdateLocalChannels().

◆ GetChannel()

Channel * ChannelMgr::GetChannel ( std::string const &  name,
Player p,
bool  pkt = true 
)
140{
141 std::wstring wname;
142 Utf8toWStr(name, wname);
143 wstrToLower(wname);
144
145 ChannelMap::const_iterator i = channels.find(wname);
146
147 if (i == channels.end())
148 {
149 if (pkt)
150 {
151 WorldPacket data;
152 MakeNotOnPacket(&data, name);
153 player->GetSession()->SendPacket(&data);
154 }
155
156 return nullptr;
157 }
158
159 return i->second;
160}
bool Utf8toWStr(char const *utf8str, std::size_t csize, wchar_t *wstr, std::size_t &wsize)
Definition: Util.cpp:281
void wstrToLower(std::wstring &str)
Definition: Util.cpp:382
void MakeNotOnPacket(WorldPacket *data, std::string const &name)
Definition: ChannelMgr.cpp:225
Definition: WorldPacket.h:27

References channels, Player::GetSession(), MakeNotOnPacket(), WorldSession::SendPacket(), Utf8toWStr(), and wstrToLower().

◆ GetChannelRightsFor()

const ChannelRights & ChannelMgr::GetChannelRightsFor ( const std::string &  name)
static
209{
210 std::string nameStr = name;
211 std::transform(nameStr.begin(), nameStr.end(), nameStr.begin(), ::tolower);
212 ChannelRightsMap::const_iterator itr = channels_rights.find(nameStr);
213 if (itr != channels_rights.end())
214 return itr->second;
215 return channelRightsEmpty;
216}
static ChannelRightsMap channels_rights
Definition: ChannelMgr.h:54
static ChannelRights channelRightsEmpty
Definition: ChannelMgr.h:55

References channelRightsEmpty, and channels_rights.

Referenced by Channel::Channel().

◆ GetJoinChannel()

Channel * ChannelMgr::GetJoinChannel ( std::string const &  name,
uint32  channel_id 
)
122{
123 std::wstring wname;
124 Utf8toWStr(name, wname);
125 wstrToLower(wname);
126
127 ChannelMap::const_iterator i = channels.find(wname);
128
129 if (i == channels.end())
130 {
131 Channel* nchan = new Channel(name, channelId, 0, _teamId);
132 channels[wname] = nchan;
133 return nchan;
134 }
135
136 return i->second;
137}
Definition: Channel.h:148

References _teamId, channels, Utf8toWStr(), and wstrToLower().

Referenced by Player::UpdateLocalChannels().

◆ LoadChannelRights()

void ChannelMgr::LoadChannelRights ( )
static
167{
168 uint32 oldMSTime = getMSTime();
169 channels_rights.clear();
170
171 QueryResult result = CharacterDatabase.Query("SELECT name, flags, speakdelay, joinmessage, delaymessage, moderators FROM channels_rights");
172 if (!result)
173 {
174 LOG_WARN("server.loading", ">> Loaded 0 Channel Rights!");
175 LOG_INFO("server.loading", " ");
176 return;
177 }
178
179 uint32 count = 0;
180 do
181 {
182 Field* fields = result->Fetch();
183 std::set<uint32> moderators;
184 auto moderatorList = fields[5].Get<std::string_view>();
185
186 if (!moderatorList.empty())
187 {
188 for (auto const& itr : Acore::Tokenize(moderatorList, ' ', false))
189 {
190 uint64 moderator_acc = Acore::StringTo<uint64>(itr).value_or(0);
191
192 if (moderator_acc && ((uint32)moderator_acc) == moderator_acc)
193 {
194 moderators.insert((uint32)moderator_acc);
195 }
196 }
197 }
198
199 SetChannelRightsFor(fields[0].Get<std::string>(), fields[1].Get<uint32>(), fields[2].Get<uint32>(), fields[3].Get<std::string>(), fields[4].Get<std::string>(), moderators);
200
201 ++count;
202 } while (result->NextRow());
203
204 LOG_INFO("server.loading", ">> Loaded {} Channel Rights in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
205 LOG_INFO("server.loading", " ");
206}
#define LOG_INFO(filterType__,...)
Definition: Log.h:164
#define LOG_WARN(filterType__,...)
Definition: Log.h:160
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
uint32 getMSTime()
Definition: Timer.h:103
std::uint32_t uint32
Definition: Define.h:107
std::uint64_t uint64
Definition: Define.h:106
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:28
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Tokenize.cpp:20
Class used to access individual fields of database query result.
Definition: Field.h:99
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:113
static void SetChannelRightsFor(const std::string &name, const uint32 &flags, const uint32 &speakDelay, const std::string &joinmessage, const std::string &speakmessage, const std::set< uint32 > &moderators)
Definition: ChannelMgr.cpp:218

References channels_rights, CharacterDatabase, Field::Get(), getMSTime(), GetMSTimeDiffToNow(), LOG_INFO, LOG_WARN, SetChannelRightsFor(), and Acore::Tokenize().

Referenced by World::SetInitialWorldSettings().

◆ LoadChannels()

void ChannelMgr::LoadChannels ( )
static
51{
52 uint32 oldMSTime = getMSTime();
53 uint32 count = 0;
54
55 // 0 1 2 3 4 5
56 QueryResult result = CharacterDatabase.Query("SELECT channelId, name, team, announce, ownership, password FROM channels ORDER BY channelId ASC");
57 if (!result)
58 {
59 LOG_WARN("server.loading", ">> Loaded 0 channels. DB table `channels` is empty.");
60 return;
61 }
62
63 std::vector<std::pair<std::string, uint32>> toDelete;
64 do
65 {
66 Field* fields = result->Fetch();
67
68 uint32 channelDBId = fields[0].Get<uint32>();
69 std::string channelName = fields[1].Get<std::string>();
70 TeamId team = TeamId(fields[2].Get<uint32>());
71 std::string password = fields[5].Get<std::string>();
72
73 std::wstring channelWName;
74 if (!Utf8toWStr(channelName, channelWName))
75 {
76 LOG_ERROR("server.loading", "Failed to load channel '{}' from database - invalid utf8 sequence? Deleted.", channelName);
77 toDelete.push_back({ channelName, team });
78 continue;
79 }
80
81 ChannelMgr* mgr = forTeam(team);
82 if (!mgr)
83 {
84 LOG_ERROR("server.loading", "Failed to load custom chat channel '{}' from database - invalid team {}. Deleted.", channelName, team);
85 toDelete.push_back({ channelName, team });
86 continue;
87 }
88
89 Channel* newChannel = new Channel(channelName, 0, channelDBId, team, fields[3].Get<uint8>(), fields[4].Get<uint8>());
90 newChannel->SetPassword(password);
91 mgr->channels[channelWName] = newChannel;
92
93 if (QueryResult banResult = CharacterDatabase.Query("SELECT playerGUID, banTime FROM channels_bans WHERE channelId = {}", channelDBId))
94 {
95 do
96 {
97 Field* banFields = banResult->Fetch();
98 if (!banFields)
99 break;
100 newChannel->AddBan(ObjectGuid::Create<HighGuid::Player>(banFields[0].Get<uint32>()), banFields[1].Get<uint32>());
101 } while (banResult->NextRow());
102 }
103
104 if (channelDBId > ChannelMgr::_channelIdMax)
105 ChannelMgr::_channelIdMax = channelDBId;
106 ++count;
107 } while (result->NextRow());
108
109 for (auto& pair : toDelete)
110 {
112 stmt->SetData(0, pair.first);
113 stmt->SetData(1, pair.second);
114 CharacterDatabase.Execute(stmt);
115 }
116
117 LOG_INFO("server.loading", ">> Loaded {} channels in {}ms", count, GetMSTimeDiffToNow(oldMSTime));
118 LOG_INFO("server.loading", " ");
119}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:156
@ CHAR_DEL_CHANNEL
Definition: CharacterDatabase.h:185
TeamId
Definition: SharedDefines.h:759
Definition: PreparedStatement.h:158
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition: PreparedStatement.h:78
void SetPassword(std::string const &npassword)
Definition: Channel.h:190
void AddBan(ObjectGuid guid, uint32 time)
Definition: Channel.h:200
static uint32 _channelIdMax
Definition: ChannelMgr.h:49
static ChannelMgr * forTeam(TeamId teamId)
Definition: ChannelMgr.cpp:33

References _channelIdMax, Channel::AddBan(), channels, CHAR_DEL_CHANNEL, CharacterDatabase, forTeam(), Field::Get(), getMSTime(), GetMSTimeDiffToNow(), LOG_ERROR, LOG_INFO, LOG_WARN, PreparedStatementBase::SetData(), Channel::SetPassword(), and Utf8toWStr().

Referenced by World::SetInitialWorldSettings().

◆ MakeNotOnPacket()

void ChannelMgr::MakeNotOnPacket ( WorldPacket data,
std::string const &  name 
)
private
226{
227 data->Initialize(SMSG_CHANNEL_NOTIFY, 1 + name.size());
228 (*data) << uint8(5) << name;
229}
std::uint8_t uint8
Definition: Define.h:109
@ SMSG_CHANNEL_NOTIFY
Definition: Opcodes.h:183
void Initialize(uint16 opcode, std::size_t newres=200)
Definition: WorldPacket.h:69

References WorldPacket::Initialize(), and SMSG_CHANNEL_NOTIFY.

Referenced by GetChannel().

◆ SetChannelRightsFor()

void ChannelMgr::SetChannelRightsFor ( const std::string &  name,
const uint32 flags,
const uint32 speakDelay,
const std::string &  joinmessage,
const std::string &  speakmessage,
const std::set< uint32 > &  moderators 
)
static
219{
220 std::string nameStr = name;
221 std::transform(nameStr.begin(), nameStr.end(), nameStr.begin(), ::tolower);
222 channels_rights[nameStr] = ChannelRights(flags, speakDelay, joinmessage, speakmessage, moderators);
223}
Definition: Channel.h:123

References channels_rights.

Referenced by LoadChannelRights().

Member Data Documentation

◆ _channelIdMax

uint32 ChannelMgr::_channelIdMax = 0
static

Referenced by Channel::Channel(), and LoadChannels().

◆ _teamId

TeamId ChannelMgr::_teamId
private

Referenced by GetJoinChannel().

◆ channelRightsEmpty

ChannelRights ChannelMgr::channelRightsEmpty
staticprivate

Referenced by GetChannelRightsFor().

◆ channels

ChannelMap ChannelMgr::channels
private

◆ channels_rights

ChannelMgr::ChannelRightsMap ChannelMgr::channels_rights
staticprivate