TrinityCore
Loading...
Searching...
No Matches
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 "Channel.h"
27#include "ChannelMgr.h"
28#include "Chat.h"
29#include "DatabaseEnv.h"
30#include "DBCStores.h"
31#include "Language.h"
32#include "ObjectAccessor.h"
33#include "ObjectMgr.h"
34#include "Player.h"
35#include "RBAC.h"
36#include "World.h"
37#include "WorldSession.h"
38
39using namespace Trinity::ChatCommands;
40
42{
43public:
44 message_commandscript() : CommandScript("message_commandscript") { }
45
47 {
48 static ChatCommandTable commandTable =
49 {
50 { "channel set ownership", HandleChannelSetOwnership, rbac::RBAC_PERM_COMMAND_CHANNEL_SET_OWNERSHIP, Console::No },
51 { "nameannounce", HandleNameAnnounceCommand, rbac::RBAC_PERM_COMMAND_NAMEANNOUNCE, Console::Yes },
53 { "announce", HandleAnnounceCommand, rbac::RBAC_PERM_COMMAND_ANNOUNCE, Console::Yes },
54 { "gmannounce", HandleGMAnnounceCommand, rbac::RBAC_PERM_COMMAND_GMANNOUNCE, Console::Yes },
55 { "notify", HandleNotifyCommand, rbac::RBAC_PERM_COMMAND_NOTIFY, Console::Yes },
56 { "gmnotify", HandleGMNotifyCommand, rbac::RBAC_PERM_COMMAND_GMNOTIFY, Console::Yes },
57 { "whispers", HandleWhispersCommand, rbac::RBAC_PERM_COMMAND_WHISPERS, Console::No },
58 };
59 return commandTable;
60 }
61
62 static bool HandleChannelSetOwnership(ChatHandler* handler, std::string channelName, bool grantOwnership)
63 {
64 uint32 channelId = 0;
65 for (uint32 i = 0; i < sChatChannelsStore.GetNumRows(); ++i)
66 {
67 ChatChannelsEntry const* entry = sChatChannelsStore.LookupEntry(i);
68 if (!entry)
69 continue;
70
71 if (StringContainsStringI(entry->Name[handler->GetSessionDbcLocale()], channelName))
72 {
73 channelId = i;
74 break;
75 }
76 }
77
78 AreaTableEntry const* zoneEntry = nullptr;
79 for (uint32 i = 0; i < sAreaTableStore.GetNumRows(); ++i)
80 {
81 AreaTableEntry const* entry = sAreaTableStore.LookupEntry(i);
82 if (!entry)
83 continue;
84
85 if (StringContainsStringI(entry->AreaName[handler->GetSessionDbcLocale()], channelName))
86 {
87 zoneEntry = entry;
88 break;
89 }
90 }
91
92 Player* player = handler->GetSession()->GetPlayer();
93 Channel* channel = nullptr;
94
95 if (ChannelMgr* cMgr = ChannelMgr::forTeam(player->GetTeam()))
96 channel = cMgr->GetChannel(channelId, channelName, player, false, zoneEntry);
97
98 if (grantOwnership)
99 {
100 if (channel)
101 channel->SetOwnership(true);
103 stmt->setUInt8 (0, 1);
104 stmt->setString(1, channelName);
105 CharacterDatabase.Execute(stmt);
106 handler->PSendSysMessage(LANG_CHANNEL_ENABLE_OWNERSHIP, channelName.c_str());
107 }
108 else
109 {
110 if (channel)
111 channel->SetOwnership(false);
113 stmt->setUInt8 (0, 0);
114 stmt->setString(1, channelName);
115 CharacterDatabase.Execute(stmt);
116 handler->PSendSysMessage(LANG_CHANNEL_DISABLE_OWNERSHIP, channelName.c_str());
117 }
118
119 return true;
120 }
121
122 static bool HandleNameAnnounceCommand(ChatHandler* handler, Tail message)
123 {
124 if (message.empty())
125 return false;
126
127 std::string name("Console");
128 if (WorldSession* session = handler->GetSession())
129 name = session->GetPlayer()->GetName();
130
131 sWorld->SendWorldText(LANG_ANNOUNCE_COLOR, name.c_str(), message.data());
132 return true;
133 }
134
135 static bool HandleGMNameAnnounceCommand(ChatHandler* handler, Tail message)
136 {
137 if (message.empty())
138 return false;
139
140 std::string name("Console");
141 if (WorldSession* session = handler->GetSession())
142 name = session->GetPlayer()->GetName();
143
144 sWorld->SendGMText(LANG_GM_ANNOUNCE_COLOR, name.c_str(), message.data());
145 return true;
146 }
147
148 // global announce
149 static bool HandleAnnounceCommand(ChatHandler* handler, Tail message)
150 {
151 if (message.empty())
152 return false;
153
154 sWorld->SendServerMessage(SERVER_MSG_STRING, handler->PGetParseString(LANG_SYSTEMMESSAGE, message.data()));
155 return true;
156 }
157
158 // announce to logged in GMs
159 static bool HandleGMAnnounceCommand(ChatHandler* /*handler*/, Tail message)
160 {
161 if (message.empty())
162 return false;
163
164 sWorld->SendGMText(LANG_GM_BROADCAST, message.data());
165 return true;
166 }
167
168 // send on-screen notification to players
169 static bool HandleNotifyCommand(ChatHandler* handler, Tail message)
170 {
171 if (message.empty())
172 return false;
173
174 std::string str = handler->GetTrinityString(LANG_GLOBAL_NOTIFY);
175 str += message;
176
177 WorldPacket data(SMSG_NOTIFICATION, (str.size() + 1));
178 data << str;
179 sWorld->SendGlobalMessage(&data);
180
181 return true;
182 }
183
184 // send on-screen notification to GMs
185 static bool HandleGMNotifyCommand(ChatHandler* handler, Tail message)
186 {
187 if (message.empty())
188 return false;
189
190 std::string str = handler->GetTrinityString(LANG_GM_NOTIFY);
191 str += message;
192
193 WorldPacket data(SMSG_NOTIFICATION, (str.size() + 1));
194 data << str;
195 sWorld->SendGlobalGMMessage(&data);
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
@ CHAR_UPD_CHANNEL_OWNERSHIP
#define EXACT_SEQUENCE(str)
DBCStorage< ChatChannelsEntry > sChatChannelsStore(ChatChannelsEntryfmt)
DBCStorage< AreaTableEntry > sAreaTableStore(AreaTableEntryfmt)
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
uint32_t uint32
Definition Define.h:133
@ LANG_ANNOUNCE_COLOR
Definition Language.h:751
@ LANG_CHANNEL_DISABLE_OWNERSHIP
Definition Language.h:1082
@ LANG_GLOBAL_NOTIFY
Definition Language.h:134
@ LANG_GM_NOTIFY
Definition Language.h:1173
@ LANG_COMMAND_WHISPEROFF
Definition Language.h:337
@ LANG_USE_BOL
Definition Language.h:309
@ LANG_OFF
Definition Language.h:71
@ LANG_COMMAND_WHISPERACCEPTING
Definition Language.h:335
@ LANG_ON
Definition Language.h:70
@ LANG_GM_ANNOUNCE_COLOR
Definition Language.h:1174
@ LANG_COMMAND_WHISPERON
Definition Language.h:336
@ LANG_PLAYER_NOT_FOUND
Definition Language.h:570
@ LANG_GM_BROADCAST
Definition Language.h:1172
@ LANG_CHANNEL_ENABLE_OWNERSHIP
Definition Language.h:1081
@ LANG_SYSTEMMESSAGE
Definition Language.h:35
@ LANG_COMMAND_WHISPEROFFPLAYER
Definition Language.h:418
bool normalizePlayerName(std::string &name)
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:711
static ChannelMgr * forTeam(uint32 team)
void SetOwnership(bool ownership)
Definition Channel.h:219
static std::string PGetParseString(std::string_view fmt, Args &&... args)
Definition Chat.h:81
WorldSession * GetSession()
Definition Chat.h:46
virtual LocaleConstant GetSessionDbcLocale() const
Definition Chat.cpp:692
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
virtual char const * GetTrinityString(uint32 entry) const
Definition Chat.cpp:36
uint32 GetTeam() const
Definition Player.h:1832
void SetAcceptWhispers(bool on)
Definition Player.h:997
void ClearWhisperWhiteList()
Definition Player.h:2248
void RemoveFromWhisperWhiteList(ObjectGuid guid)
Definition Player.h:2251
bool isAcceptWhispers() const
Definition Player.h:996
void setUInt8(uint8 index, uint8 value)
void setString(uint8 index, std::string const &value)
Player session in the World.
Player * GetPlayer() const
static bool HandleNotifyCommand(ChatHandler *handler, Tail message)
static bool HandleGMNotifyCommand(ChatHandler *handler, Tail message)
static bool HandleGMAnnounceCommand(ChatHandler *, Tail message)
static bool HandleNameAnnounceCommand(ChatHandler *handler, Tail message)
static bool HandleAnnounceCommand(ChatHandler *handler, Tail message)
static bool HandleWhispersCommand(ChatHandler *handler, Optional< Variant< bool, EXACT_SEQUENCE("remove")> > operationArg, Optional< std::string > playerNameArg)
ChatCommandTable GetCommands() const override
static bool HandleGMNameAnnounceCommand(ChatHandler *handler, Tail message)
static bool HandleChannelSetOwnership(ChatHandler *handler, std::string channelName, bool grantOwnership)
void AddSC_message_commandscript()
@ SMSG_NOTIFICATION
Definition Opcodes.h:488
#define sWorld
Definition World.h:900
@ SERVER_MSG_STRING
Definition World.h:49
TC_GAME_API Player * FindPlayerByName(std::string_view name)
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:50
@ RBAC_PERM_COMMAND_GMNOTIFY
Definition RBAC.h:338
@ RBAC_PERM_COMMAND_NOTIFY
Definition RBAC.h:340
@ RBAC_PERM_COMMAND_NAMEANNOUNCE
Definition RBAC.h:339
@ RBAC_PERM_COMMAND_GMNAMEANNOUNCE
Definition RBAC.h:337
@ RBAC_PERM_COMMAND_CHANNEL_SET_OWNERSHIP
Definition RBAC.h:335
@ RBAC_PERM_COMMAND_GMANNOUNCE
Definition RBAC.h:336
@ RBAC_PERM_COMMAND_WHISPERS
Definition RBAC.h:341
@ RBAC_PERM_COMMAND_ANNOUNCE
Definition RBAC.h:332
char const * AreaName[16]
char const * Name[16]