TrinityCore
cs_message.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/* ScriptData
19Name: message_commandscript
20%Complete: 100
21Comment: All message related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "Chat.h"
27#include "ChatCommand.h"
28#include "ChatPackets.h"
29#include "Channel.h"
30#include "ChannelMgr.h"
31#include "DatabaseEnv.h"
32#include "DB2Stores.h"
33#include "Language.h"
34#include "ObjectAccessor.h"
35#include "ObjectMgr.h"
36#include "Player.h"
37#include "RBAC.h"
38#include "World.h"
39#include "WorldSession.h"
40
41using namespace Trinity::ChatCommands;
42
44{
45public:
46 message_commandscript() : CommandScript("message_commandscript") { }
47
49 {
50 static ChatCommandTable commandTable =
51 {
52 { "channel set ownership", HandleChannelSetOwnership, rbac::RBAC_PERM_COMMAND_CHANNEL_SET_OWNERSHIP, Console::No },
53 { "nameannounce", HandleNameAnnounceCommand, rbac::RBAC_PERM_COMMAND_NAMEANNOUNCE, Console::Yes },
55 { "announce", HandleAnnounceCommand, rbac::RBAC_PERM_COMMAND_ANNOUNCE, Console::Yes },
56 { "gmannounce", HandleGMAnnounceCommand, rbac::RBAC_PERM_COMMAND_GMANNOUNCE, Console::Yes },
57 { "notify", HandleNotifyCommand, rbac::RBAC_PERM_COMMAND_NOTIFY, Console::Yes },
58 { "gmnotify", HandleGMNotifyCommand, rbac::RBAC_PERM_COMMAND_GMNOTIFY, Console::Yes },
59 { "whispers", HandleWhispersCommand, rbac::RBAC_PERM_COMMAND_WHISPERS, Console::No },
60 };
61 return commandTable;
62 }
63
64 static bool HandleChannelSetOwnership(ChatHandler* handler, std::string channelName, bool grantOwnership)
65 {
66 uint32 channelId = 0;
67 for (uint32 i = 0; i < sChatChannelsStore.GetNumRows(); ++i)
68 {
69 ChatChannelsEntry const* channelEntry = sChatChannelsStore.LookupEntry(i);
70 if (!channelEntry)
71 continue;
72
73 if (StringContainsStringI(channelEntry->Name[handler->GetSessionDbcLocale()], channelName))
74 {
75 channelId = i;
76 break;
77 }
78 }
79
80 AreaTableEntry const* zoneEntry = nullptr;
81 for (uint32 i = 0; i < sAreaTableStore.GetNumRows(); ++i)
82 {
83 AreaTableEntry const* entry = sAreaTableStore.LookupEntry(i);
84 if (!entry)
85 continue;
86
87 if (StringContainsStringI(entry->AreaName[handler->GetSessionDbcLocale()], channelName))
88 {
89 zoneEntry = entry;
90 break;
91 }
92 }
93
94 Player* player = handler->GetSession()->GetPlayer();
95 Channel* channel = nullptr;
96
97 if (ChannelMgr* cMgr = ChannelMgr::ForTeam(player->GetTeam()))
98 channel = cMgr->GetChannel(channelId, channelName, player, false, zoneEntry);
99
100 if (grantOwnership)
101 {
102 if (channel)
103 channel->SetOwnership(true);
104
106 stmt->setUInt8 (0, 1);
107 stmt->setString(1, channelName);
108 CharacterDatabase.Execute(stmt);
109 handler->PSendSysMessage(LANG_CHANNEL_ENABLE_OWNERSHIP, channelName.c_str());
110 }
111 else
112 {
113 if (channel)
114 channel->SetOwnership(false);
115
117 stmt->setUInt8 (0, 0);
118 stmt->setString(1, channelName);
119 CharacterDatabase.Execute(stmt);
120 handler->PSendSysMessage(LANG_CHANNEL_DISABLE_OWNERSHIP, channelName.c_str());
121 }
122
123 return true;
124 }
125
126 static bool HandleNameAnnounceCommand(ChatHandler* handler, Tail message)
127 {
128 if (message.empty())
129 return false;
130
131 std::string name("Console");
132 if (WorldSession* session = handler->GetSession())
133 name = session->GetPlayer()->GetName();
134
135 sWorld->SendWorldText(LANG_ANNOUNCE_COLOR, name.c_str(), message.data());
136 return true;
137 }
138
139 static bool HandleGMNameAnnounceCommand(ChatHandler* handler, Tail message)
140 {
141 if (message.empty())
142 return false;
143
144 std::string name("Console");
145 if (WorldSession* session = handler->GetSession())
146 name = session->GetPlayer()->GetName();
147
148 sWorld->SendGMText(LANG_GM_ANNOUNCE_COLOR, name.c_str(), message.data());
149 return true;
150 }
151
152 // global announce
153 static bool HandleAnnounceCommand(ChatHandler* handler, Tail message)
154 {
155 if (message.empty())
156 return false;
157
158 sWorld->SendServerMessage(SERVER_MSG_STRING, handler->PGetParseString(LANG_SYSTEMMESSAGE, message.data()));
159 return true;
160 }
161
162 // announce to logged in GMs
163 static bool HandleGMAnnounceCommand(ChatHandler* /*handler*/, Tail message)
164 {
165 if (message.empty())
166 return false;
167
168 sWorld->SendGMText(LANG_GM_BROADCAST, message.data());
169 return true;
170 }
171
172 // send on-screen notification to players
173 static bool HandleNotifyCommand(ChatHandler* handler, Tail message)
174 {
175 if (message.empty())
176 return false;
177
178 std::string str = handler->GetTrinityString(LANG_GLOBAL_NOTIFY);
179 str += message;
180
181 sWorld->SendGlobalMessage(WorldPackets::Chat::PrintNotification(str).Write());
182
183 return true;
184 }
185
186 // send on-screen notification to GMs
187 static bool HandleGMNotifyCommand(ChatHandler* handler, Tail message)
188 {
189 if (message.empty())
190 return false;
191
192 std::string str = handler->GetTrinityString(LANG_GM_NOTIFY);
193 str += message;
194
195 sWorld->SendGlobalGMMessage(WorldPackets::Chat::PrintNotification(str).Write());
196
197 return true;
198 }
199
200 // Enable/Disable accepting whispers (for GM)
201 static bool HandleWhispersCommand(ChatHandler* handler, Optional<Variant<bool, EXACT_SEQUENCE("remove")>> operationArg, Optional<std::string> playerNameArg)
202 {
203 if (!operationArg)
204 {
206 return true;
207 }
208
209 if (operationArg->holds_alternative<bool>())
210 {
211 if (operationArg->get<bool>())
212 {
213 handler->GetSession()->GetPlayer()->SetAcceptWhispers(true);
215 return true;
216 }
217 else
218 {
219 // Remove all players from the Gamemaster's whisper whitelist
221 handler->GetSession()->GetPlayer()->SetAcceptWhispers(false);
223 return true;
224 }
225 }
226
227 if (operationArg->holds_alternative<EXACT_SEQUENCE("remove")>())
228 {
229 if (!playerNameArg)
230 return false;
231
232 if (normalizePlayerName(*playerNameArg))
233 {
234 if (Player* player = ObjectAccessor::FindPlayerByName(*playerNameArg))
235 {
236 handler->GetSession()->GetPlayer()->RemoveFromWhisperWhiteList(player->GetGUID());
237 handler->PSendSysMessage(LANG_COMMAND_WHISPEROFFPLAYER, playerNameArg->c_str());
238 return true;
239 }
240 else
241 {
242 handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND, playerNameArg->c_str());
243 handler->SetSentErrorMessage(true);
244 return false;
245 }
246 }
247 }
249 handler->SetSentErrorMessage(true);
250 return false;
251 }
252};
253
255{
257}
@ CHAR_UPD_CHANNEL_OWNERSHIP
#define EXACT_SEQUENCE(str)
DB2Storage< ChatChannelsEntry > sChatChannelsStore("ChatChannels.db2", &ChatChannelsLoadInfo::Instance)
DB2Storage< AreaTableEntry > sAreaTableStore("AreaTable.db2", &AreaTableLoadInfo::Instance)
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
uint32_t uint32
Definition: Define.h:142
@ LANG_ANNOUNCE_COLOR
Definition: Language.h:748
@ LANG_CHANNEL_DISABLE_OWNERSHIP
Definition: Language.h:1079
@ LANG_GLOBAL_NOTIFY
Definition: Language.h:134
@ LANG_GM_NOTIFY
Definition: Language.h:1170
@ LANG_COMMAND_WHISPEROFF
Definition: Language.h:338
@ LANG_USE_BOL
Definition: Language.h:310
@ LANG_OFF
Definition: Language.h:71
@ LANG_COMMAND_WHISPERACCEPTING
Definition: Language.h:336
@ LANG_ON
Definition: Language.h:70
@ LANG_GM_ANNOUNCE_COLOR
Definition: Language.h:1171
@ LANG_COMMAND_WHISPERON
Definition: Language.h:337
@ LANG_PLAYER_NOT_FOUND
Definition: Language.h:567
@ LANG_GM_BROADCAST
Definition: Language.h:1169
@ LANG_CHANNEL_ENABLE_OWNERSHIP
Definition: Language.h:1078
@ LANG_SYSTEMMESSAGE
Definition: Language.h:35
@ LANG_COMMAND_WHISPEROFFPLAYER
Definition: Language.h:419
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:154
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
Role Based Access Control related classes definition.
bool StringContainsStringI(std::string_view haystack, std::string_view needle)
Definition: Util.cpp:896
static ChannelMgr * ForTeam(uint32 team)
Definition: ChannelMgr.cpp:114
void SetOwnership(bool ownership)
Definition: Channel.h:221
WorldSession * GetSession()
Definition: Chat.h:42
virtual LocaleConstant GetSessionDbcLocale() const
Definition: Chat.cpp:592
std::string PGetParseString(uint32 entry, Args &&... args) const
Definition: Chat.h:69
void PSendSysMessage(const char *fmt, Args &&... args)
Definition: Chat.h:57
void SetSentErrorMessage(bool val)
Definition: Chat.h:114
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition: Chat.cpp:113
virtual char const * GetTrinityString(uint32 entry) const
Definition: Chat.cpp:48
void SetAcceptWhispers(bool on)
Definition: Player.h:1177
void ClearWhisperWhiteList()
Definition: Player.h:2677
void RemoveFromWhisperWhiteList(ObjectGuid guid)
Definition: Player.h:2680
bool isAcceptWhispers() const
Definition: Player.h:1176
Team GetTeam() const
Definition: Player.h:2235
void setUInt8(const uint8 index, const uint8 value)
void setString(const uint8 index, const std::string &value)
Player session in the World.
Definition: WorldSession.h:963
Player * GetPlayer() const
static bool HandleNotifyCommand(ChatHandler *handler, Tail message)
Definition: cs_message.cpp:173
static bool HandleGMNotifyCommand(ChatHandler *handler, Tail message)
Definition: cs_message.cpp:187
static bool HandleGMAnnounceCommand(ChatHandler *, Tail message)
Definition: cs_message.cpp:163
static bool HandleNameAnnounceCommand(ChatHandler *handler, Tail message)
Definition: cs_message.cpp:126
static bool HandleAnnounceCommand(ChatHandler *handler, Tail message)
Definition: cs_message.cpp:153
static bool HandleWhispersCommand(ChatHandler *handler, Optional< Variant< bool, EXACT_SEQUENCE("remove")> > operationArg, Optional< std::string > playerNameArg)
Definition: cs_message.cpp:201
ChatCommandTable GetCommands() const override
Definition: cs_message.cpp:48
static bool HandleGMNameAnnounceCommand(ChatHandler *handler, Tail message)
Definition: cs_message.cpp:139
static bool HandleChannelSetOwnership(ChatHandler *handler, std::string channelName, bool grantOwnership)
Definition: cs_message.cpp:64
void AddSC_message_commandscript()
Definition: cs_message.cpp:254
#define sWorld
Definition: World.h:931
@ SERVER_MSG_STRING
Definition: World.h:52
TC_GAME_API Player * FindPlayerByName(std::string_view name)
std::vector< ChatCommandBuilder > ChatCommandTable
Definition: ChatCommand.h:49
@ RBAC_PERM_COMMAND_GMNOTIFY
Definition: RBAC.h:340
@ RBAC_PERM_COMMAND_NOTIFY
Definition: RBAC.h:342
@ RBAC_PERM_COMMAND_NAMEANNOUNCE
Definition: RBAC.h:341
@ RBAC_PERM_COMMAND_GMNAMEANNOUNCE
Definition: RBAC.h:339
@ RBAC_PERM_COMMAND_CHANNEL_SET_OWNERSHIP
Definition: RBAC.h:337
@ RBAC_PERM_COMMAND_GMANNOUNCE
Definition: RBAC.h:338
@ RBAC_PERM_COMMAND_WHISPERS
Definition: RBAC.h:343
@ RBAC_PERM_COMMAND_ANNOUNCE
Definition: RBAC.h:334
LocalizedString AreaName
Definition: DB2Structure.h:129
LocalizedString Name
Definition: DB2Structure.h:615