JustinAJ
10 years ago
13 changed files with 337 additions and 15 deletions
@ -1 +1 @@ |
|||||
Subproject commit 94bda33c248ef6fd9c0e09ecda6b27184865df6c |
Subproject commit 21326918e9caf39c37d487183801715724908f85 |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,147 @@ |
|||||
|
/**
|
||||
|
* Copyright (C) 2014 Justin James. |
||||
|
* |
||||
|
* This license must be preserved. |
||||
|
* Any applications, libraries, or code which make any use of any |
||||
|
* component of this program must not be commercial, unless explicit |
||||
|
* permission is granted from the original author. The use of this |
||||
|
* program for non-profit purposes is permitted. |
||||
|
* |
||||
|
* This program is distributed in the hope that it will be useful, |
||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||||
|
* |
||||
|
* In the event that this license restricts you from making desired use of this program, contact the original author. |
||||
|
* Written by Justin James <justin.aj@hotmail.com> |
||||
|
*/ |
||||
|
|
||||
|
#include <cstdio> |
||||
|
#include "Jupiter/IRC_Client.h" |
||||
|
#include "Jupiter/INIFile.h" |
||||
|
#include "RenX_PlayerInfo.h" |
||||
|
#include "RenX_BanDatabase.h" |
||||
|
#include "RenX_Core.h" |
||||
|
#include "RenX_Plugin.h" |
||||
|
|
||||
|
RenX::BanDatabase _banDatabase; |
||||
|
RenX::BanDatabase *RenX::banDatabase = &_banDatabase; |
||||
|
RenX::BanDatabase &RenX::defaultBanDatabase = _banDatabase; |
||||
|
|
||||
|
bool RenX::BanDatabase::load(const Jupiter::ReadableString &fname) |
||||
|
{ |
||||
|
RenX::BanDatabase::filename = fname; |
||||
|
FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "rb"); |
||||
|
if (file != nullptr) |
||||
|
{ |
||||
|
RenX::BanDatabase::version = fgetc(file); |
||||
|
while (!feof(file)) |
||||
|
if (fgetc(file) == '\n') |
||||
|
break; |
||||
|
Entry *entry; |
||||
|
char c; |
||||
|
while (!feof(file)) |
||||
|
{ |
||||
|
entry = new Entry(); |
||||
|
fread(&entry->active, 1, 1, file); |
||||
|
fread(&entry->timestamp, sizeof(time_t), 1, file); |
||||
|
fread(&entry->length, sizeof(time_t), 1, file); |
||||
|
fread(&entry->steamid, sizeof(uint64_t), 1, file); |
||||
|
fread(&entry->ip, sizeof(uint32_t), 1, file); |
||||
|
if (feof(file)) |
||||
|
{ |
||||
|
delete entry; |
||||
|
break; |
||||
|
} |
||||
|
c = fgetc(file); |
||||
|
while (c != '\n' && c != EOF) |
||||
|
{ |
||||
|
if (c == '\0') |
||||
|
{ |
||||
|
// add plugin data.
|
||||
|
break; |
||||
|
} |
||||
|
entry->name += c; |
||||
|
c = fgetc(file); |
||||
|
} |
||||
|
entries.add(entry); |
||||
|
} |
||||
|
fclose(file); |
||||
|
return true; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
RenX::BanDatabase::version = 0; |
||||
|
file = fopen(RenX::BanDatabase::filename.c_str(), "ab"); |
||||
|
if (file != nullptr) |
||||
|
{ |
||||
|
fputc(RenX::BanDatabase::version, file); |
||||
|
fputc('\n', file); |
||||
|
fclose(file); |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void RenX::BanDatabase::add(RenX::Server *server, const RenX::PlayerInfo *player, time_t length) |
||||
|
{ |
||||
|
Entry *entry = new Entry(); |
||||
|
entry->active = 1; |
||||
|
entry->timestamp = time(0); |
||||
|
entry->length = length; |
||||
|
entry->steamid = player->steamid; |
||||
|
entry->ip = player->ip32; |
||||
|
entry->name = player->name; |
||||
|
entries.add(entry); |
||||
|
|
||||
|
FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "ab"); |
||||
|
if (file != nullptr) |
||||
|
{ |
||||
|
fwrite(&entry->active, 1, 1, file); |
||||
|
fwrite(&entry->timestamp, sizeof(time_t), 1, file); |
||||
|
fwrite(&entry->length, sizeof(time_t), 1, file); |
||||
|
fwrite(&entry->steamid, sizeof(uint64_t), 1, file); |
||||
|
fwrite(&entry->ip, sizeof(uint32_t), 1, file); |
||||
|
fwrite(entry->name.ptr(), sizeof(char), entry->name.size(), file); |
||||
|
// add plugin data
|
||||
|
Jupiter::String pluginData; |
||||
|
Jupiter::ArrayList<RenX::Plugin> &xPlugins = *RenX::getCore()->getPlugins(); |
||||
|
for (size_t i = 0; i < xPlugins.size(); i++) |
||||
|
if (xPlugins.get(i)->RenX_OnBan(server, player, pluginData)) |
||||
|
{ |
||||
|
fputc('\0', file); |
||||
|
fwrite(xPlugins.get(i)->getName().ptr(), sizeof(char), xPlugins.get(i)->getName().size(), file); |
||||
|
fputc('\0', file); |
||||
|
fwrite(pluginData.ptr(), sizeof(char), pluginData.size(), file); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
fputc('\n', file); |
||||
|
fclose(file); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
uint8_t RenX::BanDatabase::getVersion() const |
||||
|
{ |
||||
|
return RenX::BanDatabase::version; |
||||
|
} |
||||
|
|
||||
|
const Jupiter::ReadableString &RenX::BanDatabase::getFileName() const |
||||
|
{ |
||||
|
return RenX::BanDatabase::filename; |
||||
|
} |
||||
|
|
||||
|
const Jupiter::ArrayList<RenX::BanDatabase::Entry> &RenX::BanDatabase::getEntries() const |
||||
|
{ |
||||
|
return RenX::BanDatabase::entries; |
||||
|
} |
||||
|
|
||||
|
RenX::BanDatabase::BanDatabase() |
||||
|
{ |
||||
|
RenX::BanDatabase::load(Jupiter::IRC::Client::Config->get(STRING_LITERAL_AS_REFERENCE("RenX"), STRING_LITERAL_AS_REFERENCE("BanDB"), STRING_LITERAL_AS_REFERENCE("Bans.db"))); |
||||
|
} |
||||
|
|
||||
|
RenX::BanDatabase::~BanDatabase() |
||||
|
{ |
||||
|
RenX::BanDatabase::entries.emptyAndDelete(); |
||||
|
} |
@ -0,0 +1,114 @@ |
|||||
|
/**
|
||||
|
* Copyright (C) 2014 Justin James. |
||||
|
* |
||||
|
* This license must be preserved. |
||||
|
* Any applications, libraries, or code which make any use of any |
||||
|
* component of this program must not be commercial, unless explicit |
||||
|
* permission is granted from the original author. The use of this |
||||
|
* program for non-profit purposes is permitted. |
||||
|
* |
||||
|
* This program is distributed in the hope that it will be useful, |
||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||||
|
* |
||||
|
* In the event that this license restricts you from making desired use of this program, contact the original author. |
||||
|
* Written by Justin James <justin.aj@hotmail.com> |
||||
|
*/ |
||||
|
|
||||
|
#if !defined _RENX_BANDATABASE_H_HEADER |
||||
|
#define _RENX_BANDATABASE_H_HEADER |
||||
|
|
||||
|
#include <ctime> |
||||
|
#include <cstdint> |
||||
|
#include "Jupiter/String.h" |
||||
|
#include "Jupiter/CString.h" |
||||
|
#include "Jupiter/ArrayList.h" |
||||
|
|
||||
|
/** DLL Linkage Nagging */ |
||||
|
#if defined _MSC_VER |
||||
|
#pragma warning(push) |
||||
|
#pragma warning(disable: 4251) |
||||
|
#endif |
||||
|
|
||||
|
namespace RenX |
||||
|
{ |
||||
|
struct PlayerInfo; |
||||
|
class Server; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Represents the local ban database. |
||||
|
*/ |
||||
|
class RENX_API BanDatabase |
||||
|
{ |
||||
|
public: |
||||
|
/**
|
||||
|
* @brief Represents a Ban entry in the database. |
||||
|
*/ |
||||
|
struct RENX_API Entry |
||||
|
{ |
||||
|
unsigned char active; /** 1 if the ban is active, 0 otherwise */ |
||||
|
time_t timestamp /** Time the ban was created */; |
||||
|
time_t length /** Duration of the ban; 0 if permanent */; |
||||
|
uint64_t steamid /** SteamID of the banned player */; |
||||
|
uint32_t ip /** IPv4 address of the banned player */; |
||||
|
Jupiter::StringS name /** Name of the banned player */; |
||||
|
}; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Loads a file into the ban system. |
||||
|
* Note: This will generate a database file if none is found. |
||||
|
* |
||||
|
* @param fname String containing the name of the file to load |
||||
|
* @return True on success, false otherwise. |
||||
|
*/ |
||||
|
bool load(const Jupiter::ReadableString &fname); |
||||
|
|
||||
|
/**
|
||||
|
* @param Adds a ban entry for a player and immediately writes it to the database. |
||||
|
* |
||||
|
* @param server Server the player is playing in |
||||
|
* @param player Data of the player to be banned |
||||
|
* @param length Duration of the ban |
||||
|
*/ |
||||
|
void add(RenX::Server *server, const RenX::PlayerInfo *player, time_t length); |
||||
|
|
||||
|
/**
|
||||
|
* @brief Fetches the version of the database file. |
||||
|
* |
||||
|
* @return Database version |
||||
|
*/ |
||||
|
uint8_t getVersion() const; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Fetches the name of the database file. |
||||
|
* |
||||
|
* @return Database file name |
||||
|
*/ |
||||
|
const Jupiter::ReadableString &getFileName() const; |
||||
|
|
||||
|
/**
|
||||
|
* @brief Fetches the list of ban entries. |
||||
|
* |
||||
|
* @return List of entries |
||||
|
*/ |
||||
|
const Jupiter::ArrayList<RenX::BanDatabase::Entry> &getEntries() const; |
||||
|
|
||||
|
BanDatabase(); |
||||
|
~BanDatabase(); |
||||
|
|
||||
|
private: |
||||
|
uint8_t version; |
||||
|
Jupiter::CStringS filename; |
||||
|
Jupiter::ArrayList<RenX::BanDatabase::Entry> entries; |
||||
|
}; |
||||
|
|
||||
|
RENX_API extern RenX::BanDatabase *banDatabase; |
||||
|
RENX_API extern RenX::BanDatabase &defaultBanDatabase; |
||||
|
} |
||||
|
|
||||
|
/** Re-enable warnings */ |
||||
|
#if defined _MSC_VER |
||||
|
#pragma warning(pop) |
||||
|
#endif |
||||
|
|
||||
|
#endif // _RENX_BANDATABASE_H_HEADER
|
Loading…
Reference in new issue