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)
37{
38 if (AccountName.empty() || Duration.empty())
39 return BAN_SYNTAX_ERROR;
40
41 uint32 DurationSecs = TimeStringToSecs(Duration);
42
43 uint32 AccountID = AccountMgr::GetId(AccountName);
44 if (!AccountID)
45 return BAN_NOTFOUND;
46
48 LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
49
50 // pussywizard: check existing ban to prevent overriding by a shorter one! >_>
51 LoginDatabasePreparedStatement* stmtAccountBanned = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_BANNED);
52 stmtAccountBanned->SetData(0, AccountID);
53
54 PreparedQueryResult banresult = LoginDatabase.Query(stmtAccountBanned);
55 if (banresult && ((*banresult)[0].Get<uint32>() == (*banresult)[1].Get<uint32>() || ((*banresult)[1].Get<uint32>() > GameTime::GetGameTime().count() + DurationSecs && DurationSecs)))
56 return BAN_LONGER_EXISTS;
57
58 // make sure there is only one active ban
60 stmt->SetData(0, AccountID);
61 trans->Append(stmt);
62
63 // No SQL injection with prepared statements
64 stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_ACCOUNT_BANNED);
65 stmt->SetData(0, AccountID);
66 stmt->SetData(1, DurationSecs);
67 stmt->SetData(2, Author);
68 stmt->SetData(3, Reason);
69 trans->Append(stmt);
70
71 if (WorldSession* session = sWorld->FindSession(AccountID))
72 if (session->GetPlayerName() != Author)
73 session->KickPlayer("Ban Account at condition 'FindSession(account)->GetPlayerName() != author'");
74
75 if (WorldSession* session = sWorld->FindOfflineSession(AccountID))
76 if (session->GetPlayerName() != Author)
77 session->KickPlayer("Ban Account at condition 'FindOfflineSession(account)->GetPlayerName() != author'");
78
79 LoginDatabase.CommitTransaction(trans);
80
81 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
82 {
83 bool IsPermanetly = true;
84
85 if (TimeStringToSecs(Duration) > 0)
86 IsPermanetly = false;
87
88 if (!IsPermanetly)
89 sWorld->SendWorldText(LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD, Author.c_str(), AccountName.c_str(), secsToTimeString(TimeStringToSecs(Duration), true).c_str(), Reason.c_str());
90 else
91 sWorld->SendWorldText(LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD, Author.c_str(), AccountName.c_str(), Reason.c_str());
92 }
93
94 return BAN_SUCCESS;
95}
std::uint32_t uint32
Definition: Define.h:108
std::string secsToTimeString(uint64 timeInSecs, bool shortText)
Definition: Util.cpp:73
uint32 TimeStringToSecs(const std::string &timestring)
Definition: Util.cpp:163
DatabaseWorkerPool< LoginDatabaseConnection > LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
SQLTransaction< LoginDatabaseConnection > LoginDatabaseTransaction
Definition: DatabaseEnvFwd.h:71
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:46
@ 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
@ 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
@ LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD
Definition: Language.h:1306
@ LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD
Definition: Language.h:1307
@ CONFIG_SHOW_BAN_IN_WORLD
Definition: IWorld.h:138
#define sWorld
Definition: World.h:447
uint32 GetId(std::string const &username)
Definition: AccountMgr.cpp:200
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
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(), 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)
99{
100 if (CharacterName.empty() || Duration.empty())
101 return BAN_SYNTAX_ERROR;
102
103 uint32 DurationSecs = TimeStringToSecs(Duration);
104
105 uint32 AccountID = sCharacterCache->GetCharacterAccountIdByName(CharacterName);
106 if (!AccountID)
107 return BAN_NOTFOUND;
108
110 LoginDatabaseTransaction trans = LoginDatabase.BeginTransaction();
111
112 // pussywizard: check existing ban to prevent overriding by a shorter one! >_>
113 LoginDatabasePreparedStatement* stmtAccountBanned = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_BANNED);
114 stmtAccountBanned->SetData(0, AccountID);
115
116 PreparedQueryResult banresult = LoginDatabase.Query(stmtAccountBanned);
117 if (banresult && ((*banresult)[0].Get<uint32>() == (*banresult)[1].Get<uint32>() || ((*banresult)[1].Get<uint32>() > GameTime::GetGameTime().count() + DurationSecs && DurationSecs)))
118 return BAN_LONGER_EXISTS;
119
120 // make sure there is only one active ban
122 stmt->SetData(0, AccountID);
123 trans->Append(stmt);
124
125 // No SQL injection with prepared statements
126 stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_ACCOUNT_BANNED);
127 stmt->SetData(0, AccountID);
128 stmt->SetData(1, DurationSecs);
129 stmt->SetData(2, Author);
130 stmt->SetData(3, Reason);
131 trans->Append(stmt);
132
133 if (WorldSession* session = sWorld->FindSession(AccountID))
134 if (session->GetPlayerName() != Author)
135 session->KickPlayer("Ban Account at condition 'FindSession(account)->GetPlayerName() != author'");
136
137 if (WorldSession* session = sWorld->FindOfflineSession(AccountID))
138 if (session->GetPlayerName() != Author)
139 session->KickPlayer("Ban Account at condition 'FindOfflineSession(account)->GetPlayerName() != author'");
140
141 LoginDatabase.CommitTransaction(trans);
142
143 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
144 {
145 bool IsPermanetly = true;
146
147 if (TimeStringToSecs(Duration) > 0)
148 IsPermanetly = false;
149
150 std::string AccountName;
151
152 AccountMgr::GetName(AccountID, AccountName);
153
154 if (!IsPermanetly)
155 sWorld->SendWorldText(LANG_BAN_ACCOUNT_YOUBANNEDMESSAGE_WORLD, Author.c_str(), AccountName.c_str(), secsToTimeString(TimeStringToSecs(Duration), true).c_str(), Reason.c_str());
156 else
157 sWorld->SendWorldText(LANG_BAN_ACCOUNT_YOUPERMBANNEDMESSAGE_WORLD, Author.c_str(), AccountName.c_str(), Reason.c_str());
158 }
159
160 return BAN_SUCCESS;
161}
#define sCharacterCache
Definition: CharacterCache.h:83
bool GetName(uint32 accountId, std::string &name)
Definition: AccountMgr.cpp:228

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(), 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

223{
224 Player* target = ObjectAccessor::FindPlayerByName(CharacterName, false);
225 uint32 DurationSecs = TimeStringToSecs(Duration);
226 ObjectGuid TargetGUID;
227
229 if (!target)
230 {
231 TargetGUID = sCharacterCache->GetCharacterGuidByName(CharacterName);
232 if (!TargetGUID)
233 return BAN_NOTFOUND;
234 }
235 else
236 TargetGUID = target->GetGUID();
237
238 // make sure there is only one active ban
240 stmt->SetData(0, TargetGUID.GetCounter());
241 CharacterDatabase.Execute(stmt);
242
243 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_BAN);
244 stmt->SetData(0, TargetGUID.GetCounter());
245 stmt->SetData(1, DurationSecs);
246 stmt->SetData(2, Author);
247 stmt->SetData(3, Reason);
248 CharacterDatabase.Execute(stmt);
249
250 if (target)
251 target->GetSession()->KickPlayer("Ban");
252
253 if (sWorld->getBoolConfig(CONFIG_SHOW_BAN_IN_WORLD))
254 {
255 bool IsPermanetly = true;
256
257 if (TimeStringToSecs(Duration) > 0)
258 IsPermanetly = false;
259
260 if (!IsPermanetly)
261 sWorld->SendWorldText(LANG_BAN_CHARACTER_YOUBANNEDMESSAGE_WORLD, Author.c_str(), CharacterName.c_str(), secsToTimeString(TimeStringToSecs(Duration), true).c_str(), Reason.c_str());
262 else
263 sWorld->SendWorldText(LANG_BAN_CHARACTER_YOUPERMBANNEDMESSAGE_WORLD, Author.c_str(), CharacterName.c_str(), Reason.c_str());
264 }
265
266 return BAN_SUCCESS;
267}
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
@ LANG_BAN_CHARACTER_YOUBANNEDMESSAGE_WORLD
Definition: Language.h:1304
@ LANG_BAN_CHARACTER_YOUPERMBANNEDMESSAGE_WORLD
Definition: Language.h:1305
Player * FindPlayerByName(std::string const &name, bool checkInWorld=true)
Definition: ObjectAccessor.cpp:274
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:106
Definition: ObjectGuid.h:120
LowType GetCounter() const
Definition: ObjectGuid.h:147
Definition: Player.h:1056
WorldSession * GetSession() const
Definition: Player.h:1961
void KickPlayer(bool setKicked=true)
Definition: WorldSession.h:402

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

◆ instance()

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

References instance().

Referenced by instance().

◆ RemoveBanAccount()

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

Remove a ban from an account.

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

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.

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

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

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

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.

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

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