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

#include "BanMgr.h"

Public Member Functions

BanReturn BanAccount (std::string const &AccountName, std::string const &Duration, std::string const &Reason, std::string const &Author)
 Ban an account, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.
 
BanReturn BanAccountByPlayerName (std::string const &CharacterName, std::string const &Duration, std::string const &Reason, std::string const &Author)
 Ban an account by player name, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.
 
BanReturn BanIP (std::string const &IP, std::string const &Duration, std::string const &Reason, std::string const &Author)
 Ban an IP address, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.
 
BanReturn BanCharacter (std::string const &CharacterName, std::string const &Duration, std::string const &Reason, std::string const &Author)
 Ban an character, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.
 
bool RemoveBanAccount (std::string const &AccountName)
 Remove a ban from an account.
 
bool RemoveBanAccountByPlayerName (std::string const &CharacterName)
 Remove a ban from an player name.
 
bool RemoveBanIP (std::string const &IP)
 Remove a ban from an account.
 
bool RemoveBanCharacter (std::string const &CharacterName)
 Remove a ban from a character.
 

Static Public Member Functions

static BanMgrinstance ()
 

Detailed Description

Member Function Documentation

◆ BanAccount()

BanReturn BanMgr::BanAccount ( std::string const &  AccountName,
std::string const &  Duration,
std::string const &  Reason,
std::string const &  Author 
)

Ban an account, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.

  • Disconnect all affected players (for IP it can be several)
39{
40 if (AccountName.empty() || Duration.empty())
41 return BAN_SYNTAX_ERROR;
42
43 uint32 DurationSecs = TimeStringToSecs(Duration);
44
45 uint32 AccountID = AccountMgr::GetId(AccountName);
46 if (!AccountID)
47 return BAN_NOTFOUND;
48
50 LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
51
52 // pussywizard: check existing ban to prevent overriding by a shorter one! >_>
53 LoginDatabasePreparedStatement* stmtAccountBanned = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_BANNED);
54 stmtAccountBanned->SetData(0, AccountID);
55
56 PreparedQueryResult banresult = LoginDatabase.Query(stmtAccountBanned);
57 if (banresult && ((*banresult)[0].Get<uint32>() == (*banresult)[1].Get<uint32>() || ((*banresult)[1].Get<uint32>() > GameTime::GetGameTime().count() + DurationSecs && DurationSecs)))
58 return BAN_LONGER_EXISTS;
59
60 // make sure there is only one active ban
62 stmt->SetData(0, AccountID);
63 trans->Append(stmt);
64
65 // No SQL injection with prepared statements
66 stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_ACCOUNT_BANNED);
67 stmt->SetData(0, AccountID);
68 stmt->SetData(1, DurationSecs);
69 stmt->SetData(2, Author);
70 stmt->SetData(3, Reason);
71 trans->Append(stmt);
72
73 if (WorldSession* session = sWorldSessionMgr->FindSession(AccountID))
74 if (session->GetPlayerName() != Author)
75 session->KickPlayer("Ban Account at condition 'FindSession(account)->GetPlayerName() != author'");
76
77 if (WorldSession* session = sWorldSessionMgr->FindOfflineSession(AccountID))
78 if (session->GetPlayerName() != Author)
79 session->KickPlayer("Ban Account at condition 'FindOfflineSession(account)->GetPlayerName() != author'");
80
81 LoginDatabase.CommitTransaction(trans);
82
83 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
84 {
85 bool IsPermanetly = true;
86
87 if (TimeStringToSecs(Duration) > 0)
88 IsPermanetly = false;
89
90 if (!IsPermanetly)
91 ChatHandler(nullptr).SendWorldText(LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD, Author, AccountName, secsToTimeString(TimeStringToSecs(Duration), true), Reason);
92 else
93 ChatHandler(nullptr).SendWorldText(LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD, Author, AccountName, Reason);
94 }
95
96 return BAN_SUCCESS;
97}
@ BAN_SYNTAX_ERROR
Definition BanMgr.h:27
@ BAN_NOTFOUND
Definition BanMgr.h:28
@ BAN_SUCCESS
Definition BanMgr.h:26
@ BAN_LONGER_EXISTS
Definition BanMgr.h:29
SQLTransaction< LoginDatabaseConnection > LoginDatabaseTransaction
Definition DatabaseEnvFwd.h:70
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition DatabaseEnvFwd.h:45
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition DatabaseEnv.cpp:22
std::uint32_t uint32
Definition Define.h:107
@ CONFIG_SHOW_BAN_IN_WORLD
Definition IWorld.h:130
@ LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD
Definition Language.h:1296
@ LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD
Definition Language.h:1297
@ LOGIN_INS_ACCOUNT_BANNED
Definition LoginDatabase.h:60
@ LOGIN_UPD_ACCOUNT_NOT_BANNED
Definition LoginDatabase.h:61
@ LOGIN_SEL_ACCOUNT_BANNED
Definition LoginDatabase.h:37
std::string secsToTimeString(uint64 timeInSecs, bool shortText)
Definition Util.cpp:73
uint32 TimeStringToSecs(const std::string &timestring)
Definition Util.cpp:163
#define sWorldSessionMgr
Definition WorldSessionMgr.h:110
Definition Chat.h:37
void SendWorldText(std::string_view str)
Definition Chat.cpp:131
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition PreparedStatement.h:77
Definition PreparedStatement.h:157
Player session in the World.
Definition WorldSession.h:330
#define sWorld
Definition World.h:363
uint32 GetId(std::string const &username)
Definition AccountMgr.cpp:229
Seconds GetGameTime()
Definition GameTime.cpp:38

References BAN_LONGER_EXISTS, BAN_NOTFOUND, BAN_SUCCESS, BAN_SYNTAX_ERROR, CONFIG_SHOW_BAN_IN_WORLD, GameTime::GetGameTime(), AccountMgr::GetId(), LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD, LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD, LOGIN_INS_ACCOUNT_BANNED, LOGIN_SEL_ACCOUNT_BANNED, LOGIN_UPD_ACCOUNT_NOT_BANNED, LoginDatabase, secsToTimeString(), ChatHandler::SendWorldText(), PreparedStatementBase::SetData(), sWorld, sWorldSessionMgr, and TimeStringToSecs().

◆ BanAccountByPlayerName()

BanReturn BanMgr::BanAccountByPlayerName ( std::string const &  CharacterName,
std::string const &  Duration,
std::string const &  Reason,
std::string const &  Author 
)

Ban an account by player name, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.

  • Disconnect all affected players (for IP it can be several)
101{
102 if (CharacterName.empty() || Duration.empty())
103 return BAN_SYNTAX_ERROR;
104
105 uint32 DurationSecs = TimeStringToSecs(Duration);
106
107 uint32 AccountID = sCharacterCache->GetCharacterAccountIdByName(CharacterName);
108 if (!AccountID)
109 return BAN_NOTFOUND;
110
112 LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
113
114 // pussywizard: check existing ban to prevent overriding by a shorter one! >_>
115 LoginDatabasePreparedStatement* stmtAccountBanned = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_BANNED);
116 stmtAccountBanned->SetData(0, AccountID);
117
118 PreparedQueryResult banresult = LoginDatabase.Query(stmtAccountBanned);
119 if (banresult && ((*banresult)[0].Get<uint32>() == (*banresult)[1].Get<uint32>() || ((*banresult)[1].Get<uint32>() > GameTime::GetGameTime().count() + DurationSecs && DurationSecs)))
120 return BAN_LONGER_EXISTS;
121
122 // make sure there is only one active ban
124 stmt->SetData(0, AccountID);
125 trans->Append(stmt);
126
127 // No SQL injection with prepared statements
128 stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_ACCOUNT_BANNED);
129 stmt->SetData(0, AccountID);
130 stmt->SetData(1, DurationSecs);
131 stmt->SetData(2, Author);
132 stmt->SetData(3, Reason);
133 trans->Append(stmt);
134
135 if (WorldSession* session = sWorldSessionMgr->FindSession(AccountID))
136 if (session->GetPlayerName() != Author)
137 session->KickPlayer("Ban Account at condition 'FindSession(account)->GetPlayerName() != author'");
138
139 if (WorldSession* session = sWorldSessionMgr->FindOfflineSession(AccountID))
140 if (session->GetPlayerName() != Author)
141 session->KickPlayer("Ban Account at condition 'FindOfflineSession(account)->GetPlayerName() != author'");
142
143 LoginDatabase.CommitTransaction(trans);
144
145 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
146 {
147 bool IsPermanetly = true;
148
149 if (TimeStringToSecs(Duration) > 0)
150 IsPermanetly = false;
151
152 std::string AccountName;
153
154 AccountMgr::GetName(AccountID, AccountName);
155
156 if (!IsPermanetly)
157 ChatHandler(nullptr).SendWorldText(LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD, Author, AccountName, secsToTimeString(TimeStringToSecs(Duration), true), Reason);
158 else
159 ChatHandler(nullptr).SendWorldText(LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD, Author, AccountName, Reason);
160 }
161
162 return BAN_SUCCESS;
163}
#define sCharacterCache
Definition CharacterCache.h:83
bool GetName(uint32 accountId, std::string &name)
Definition AccountMgr.cpp:257

References BAN_LONGER_EXISTS, BAN_NOTFOUND, BAN_SUCCESS, BAN_SYNTAX_ERROR, CONFIG_SHOW_BAN_IN_WORLD, GameTime::GetGameTime(), AccountMgr::GetName(), LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD, LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD, LOGIN_INS_ACCOUNT_BANNED, LOGIN_SEL_ACCOUNT_BANNED, LOGIN_UPD_ACCOUNT_NOT_BANNED, LoginDatabase, sCharacterCache, secsToTimeString(), ChatHandler::SendWorldText(), PreparedStatementBase::SetData(), sWorld, sWorldSessionMgr, and TimeStringToSecs().

◆ BanCharacter()

BanReturn BanMgr::BanCharacter ( std::string const &  CharacterName,
std::string const &  Duration,
std::string const &  Reason,
std::string const &  Author 
)

Ban an character, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.

Pick a player to ban if not online

225{
226 Player* target = ObjectAccessor::FindPlayerByName(CharacterName, false);
227 uint32 DurationSecs = TimeStringToSecs(Duration);
228 ObjectGuid TargetGUID;
229
231 if (!target)
232 {
233 TargetGUID = sCharacterCache->GetCharacterGuidByName(CharacterName);
234 if (!TargetGUID)
235 return BAN_NOTFOUND;
236 }
237 else
238 TargetGUID = target->GetGUID();
239
240 // make sure there is only one active ban
242 stmt->SetData(0, TargetGUID.GetCounter());
243 CharacterDatabase.Execute(stmt);
244
245 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_BAN);
246 stmt->SetData(0, TargetGUID.GetCounter());
247 stmt->SetData(1, DurationSecs);
248 stmt->SetData(2, Author);
249 stmt->SetData(3, Reason);
250 CharacterDatabase.Execute(stmt);
251
252 if (target)
253 target->GetSession()->KickPlayer("Ban");
254
255 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
256 {
257 bool IsPermanetly = true;
258
259 if (TimeStringToSecs(Duration) > 0)
260 IsPermanetly = false;
261
262 if (!IsPermanetly)
263 ChatHandler(nullptr).SendWorldText(LANG_BAN_CHARACTER_YOUBANNEDMESSAGE_WORLD, Author, CharacterName, secsToTimeString(TimeStringToSecs(Duration), true), Reason);
264 else
265 ChatHandler(nullptr).SendWorldText(LANG_BAN_CHARACTER_YOUPERMBANNEDMESSAGE_WORLD, Author, CharacterName, Reason);
266 }
267
268 return BAN_SUCCESS;
269}
@ CHAR_INS_CHARACTER_BAN
Definition CharacterDatabase.h:41
@ CHAR_UPD_CHARACTER_BAN
Definition CharacterDatabase.h:42
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition DatabaseEnv.cpp:21
@ LANG_BAN_CHARACTER_YOUBANNEDMESSAGE_WORLD
Definition Language.h:1294
@ LANG_BAN_CHARACTER_YOUPERMBANNEDMESSAGE_WORLD
Definition Language.h:1295
Definition ObjectGuid.h:118
LowType GetCounter() const
Definition ObjectGuid.h:145
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:112
Definition Player.h:1081
WorldSession * GetSession() const
Definition Player.h:2005
void KickPlayer(bool setKicked=true)
Definition WorldSession.h:414
Player * FindPlayerByName(std::string const &name, bool checkInWorld=true)
Definition ObjectAccessor.cpp:271

References BAN_NOTFOUND, BAN_SUCCESS, CHAR_INS_CHARACTER_BAN, CHAR_UPD_CHARACTER_BAN, CharacterDatabase, CONFIG_SHOW_BAN_IN_WORLD, ObjectAccessor::FindPlayerByName(), ObjectGuid::GetCounter(), Object::GetGUID(), Player::GetSession(), WorldSession::KickPlayer(), LANG_BAN_CHARACTER_YOUBANNEDMESSAGE_WORLD, LANG_BAN_CHARACTER_YOUPERMBANNEDMESSAGE_WORLD, sCharacterCache, secsToTimeString(), ChatHandler::SendWorldText(), PreparedStatementBase::SetData(), sWorld, and TimeStringToSecs().

◆ BanIP()

BanReturn BanMgr::BanIP ( std::string const &  IP,
std::string const &  Duration,
std::string const &  Reason,
std::string const &  Author 
)

Ban an IP address, duration will be parsed using TimeStringToSecs if it is positive, otherwise permban.

  • Disconnect all affected players (for IP it can be several)
167{
168 if (IP.empty() || Duration.empty())
169 return BAN_SYNTAX_ERROR;
170
171 uint32 DurationSecs = TimeStringToSecs(Duration);
172
173 // No SQL injection with prepared statements
175 stmt->SetData(0, IP);
176 PreparedQueryResult resultAccounts = LoginDatabase.Query(stmt);
177
178 stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_IP_BANNED);
179 stmt->SetData(0, IP);
180 stmt->SetData(1, DurationSecs);
181 stmt->SetData(2, Author);
182 stmt->SetData(3, Reason);
183 LoginDatabase.Execute(stmt);
184
185 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
186 {
187 bool IsPermanetly = true;
188
189 if (TimeStringToSecs(Duration) > 0)
190 IsPermanetly = false;
191
192 if (IsPermanetly)
194 else
196 }
197
198 if (!resultAccounts)
199 return BAN_SUCCESS;
200
202 LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
203
204 do
205 {
206 Field* fields = resultAccounts->Fetch();
207 uint32 AccountID = fields[0].Get<uint32>();
208
209 if (WorldSession* session = sWorldSessionMgr->FindSession(AccountID))
210 if (session->GetPlayerName() != Author)
211 session->KickPlayer("Ban IP at condition 'FindSession(account)->GetPlayerName() != author'");
212
213 if (WorldSession* session = sWorldSessionMgr->FindOfflineSession(AccountID))
214 if (session->GetPlayerName() != Author)
215 session->KickPlayer("Ban IP at condition 'FindOfflineSession(account)->GetPlayerName() != author'");
216 } while (resultAccounts->NextRow());
217
218 LoginDatabase.CommitTransaction(trans);
219
220 return BAN_SUCCESS;
221}
@ LANG_BAN_IP_YOUPERMBANNEDMESSAGE_WORLD
Definition Language.h:1312
@ LANG_BAN_IP_YOUBANNEDMESSAGE_WORLD
Definition Language.h:1311
@ LOGIN_SEL_ACCOUNT_BY_IP
Definition LoginDatabase.h:54
@ LOGIN_INS_IP_BANNED
Definition LoginDatabase.h:55
Class used to access individual fields of database query result.
Definition Field.h:98
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition Field.h:112

References BAN_SUCCESS, BAN_SYNTAX_ERROR, CONFIG_SHOW_BAN_IN_WORLD, Field::Get(), LANG_BAN_IP_YOUBANNEDMESSAGE_WORLD, LANG_BAN_IP_YOUPERMBANNEDMESSAGE_WORLD, LOGIN_INS_IP_BANNED, LOGIN_SEL_ACCOUNT_BY_IP, LoginDatabase, secsToTimeString(), ChatHandler::SendWorldText(), PreparedStatementBase::SetData(), sWorld, sWorldSessionMgr, and TimeStringToSecs().

◆ instance()

BanMgr * BanMgr::instance ( )
static
32{
33 static BanMgr instance;
34 return &instance;
35}
Definition BanMgr.h:33
static BanMgr * instance()
Definition BanMgr.cpp:31

References instance().

Referenced by instance().

◆ RemoveBanAccount()

bool BanMgr::RemoveBanAccount ( std::string const &  AccountName)

Remove a ban from an account.

273{
274 uint32 AccountID = AccountMgr::GetId(AccountName);
275 if (!AccountID)
276 return false;
277
278 // NO SQL injection as account is uint32
280 stmt->SetData(0, AccountID);
281 LoginDatabase.Execute(stmt);
282
283 return true;
284}

References AccountMgr::GetId(), LOGIN_UPD_ACCOUNT_NOT_BANNED, LoginDatabase, and PreparedStatementBase::SetData().

◆ RemoveBanAccountByPlayerName()

bool BanMgr::RemoveBanAccountByPlayerName ( std::string const &  CharacterName)

Remove a ban from an player name.

288{
289 uint32 AccountID = sCharacterCache->GetCharacterAccountIdByName(CharacterName);
290 if (!AccountID)
291 return false;
292
293 // NO SQL injection as account is uint32
295 stmt->SetData(0, AccountID);
296 LoginDatabase.Execute(stmt);
297
298 return true;
299}

References LOGIN_UPD_ACCOUNT_NOT_BANNED, LoginDatabase, sCharacterCache, and PreparedStatementBase::SetData().

◆ RemoveBanCharacter()

bool BanMgr::RemoveBanCharacter ( std::string const &  CharacterName)

Remove a ban from a character.

Pick a player to ban if not online

313{
314 Player* pBanned = ObjectAccessor::FindPlayerByName(CharacterName, false);
315 ObjectGuid guid;
316
318 if (!pBanned)
319 guid = sCharacterCache->GetCharacterGuidByName(CharacterName);
320 else
321 guid = pBanned->GetGUID();
322
323 if (!guid)
324 return false;
325
327 stmt->SetData(0, guid.GetCounter());
328 CharacterDatabase.Execute(stmt);
329 return true;
330}

References CHAR_UPD_CHARACTER_BAN, CharacterDatabase, ObjectAccessor::FindPlayerByName(), ObjectGuid::GetCounter(), Object::GetGUID(), sCharacterCache, and PreparedStatementBase::SetData().

◆ RemoveBanIP()

bool BanMgr::RemoveBanIP ( std::string const &  IP)

Remove a ban from an account.

303{
305 stmt->SetData(0, IP);
306 LoginDatabase.Execute(stmt);
307
308 return true;
309}
@ LOGIN_DEL_IP_NOT_BANNED
Definition LoginDatabase.h:56

References LOGIN_DEL_IP_NOT_BANNED, LoginDatabase, and PreparedStatementBase::SetData().


The documentation for this class was generated from the following files: