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. More...
 
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. More...
 
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. More...
 
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. More...
 
bool RemoveBanAccount (std::string const &AccountName)
 Remove a ban from an account. More...
 
bool RemoveBanAccountByPlayerName (std::string const &CharacterName)
 Remove a ban from an player name. More...
 
bool RemoveBanIP (std::string const &IP)
 Remove a ban from an account. More...
 
bool RemoveBanCharacter (std::string const &CharacterName)
 Remove a ban from a character. More...
 

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

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

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

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)
166{
167 if (IP.empty() || Duration.empty())
168 return BAN_SYNTAX_ERROR;
169
170 uint32 DurationSecs = TimeStringToSecs(Duration);
171
172 // No SQL injection with prepared statements
174 stmt->SetData(0, IP);
175 PreparedQueryResult resultAccounts = LoginDatabase.Query(stmt);
176
177 stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_IP_BANNED);
178 stmt->SetData(0, IP);
179 stmt->SetData(1, DurationSecs);
180 stmt->SetData(2, Author);
181 stmt->SetData(3, Reason);
182 LoginDatabase.Execute(stmt);
183
184 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
185 {
186 bool IsPermanetly = true;
187
188 if (TimeStringToSecs(Duration) > 0)
189 IsPermanetly = false;
190
191 if (IsPermanetly)
193 else
195 }
196
197 if (!resultAccounts)
198 return BAN_SUCCESS;
199
201 LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
202
203 do
204 {
205 Field* fields = resultAccounts->Fetch();
206 uint32 AccountID = fields[0].Get<uint32>();
207
208 if (WorldSession* session = sWorld->FindSession(AccountID))
209 if (session->GetPlayerName() != Author)
210 session->KickPlayer("Ban IP at condition 'FindSession(account)->GetPlayerName() != author'");
211
212 if (WorldSession* session = sWorld->FindOfflineSession(AccountID))
213 if (session->GetPlayerName() != Author)
214 session->KickPlayer("Ban IP at condition 'FindOfflineSession(account)->GetPlayerName() != author'");
215 } while (resultAccounts->NextRow());
216
217 LoginDatabase.CommitTransaction(trans);
218
219 return BAN_SUCCESS;
220}
@ LANG_BAN_IP_YOUPERMBANNEDMESSAGE_WORLD
Definition: Language.h:1309
@ LANG_BAN_IP_YOUBANNEDMESSAGE_WORLD
Definition: Language.h:1308
@ 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:99
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:113

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, and TimeStringToSecs().

◆ instance()

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

References instance().

Referenced by instance().

◆ RemoveBanAccount()

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

Remove a ban from an account.

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

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.

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

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

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

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.

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

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