Browse Source

File position is now stored for ban entries; added BanDatabase::deactivate().

pull/3/head
JustinAJ 10 years ago
parent
commit
c4aeb7ce58
  1. 20
      RenX.Core/RenX_BanDatabase.cpp
  2. 11
      RenX.Core/RenX_BanDatabase.h

20
RenX.Core/RenX_BanDatabase.cpp

@ -45,6 +45,7 @@ bool RenX::BanDatabase::load(const Jupiter::ReadableString &fname)
while (!feof(file)) while (!feof(file))
{ {
entry = new Entry(); entry = new Entry();
fgetpos(file, &entry->pos);
fread(&entry->active, 1, 1, file); fread(&entry->active, 1, 1, file);
fread(&entry->timestamp, sizeof(time_t), 1, file); fread(&entry->timestamp, sizeof(time_t), 1, file);
fread(&entry->length, sizeof(time_t), 1, file); fread(&entry->length, sizeof(time_t), 1, file);
@ -124,6 +125,7 @@ void RenX::BanDatabase::add(RenX::Server *server, const RenX::PlayerInfo *player
FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "ab"); FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "ab");
if (file != nullptr) if (file != nullptr)
{ {
fgetpos(file, &entry->pos);
fwrite(&entry->active, 1, 1, file); fwrite(&entry->active, 1, 1, file);
fwrite(&entry->timestamp, sizeof(time_t), 1, file); fwrite(&entry->timestamp, sizeof(time_t), 1, file);
fwrite(&entry->length, sizeof(time_t), 1, file); fwrite(&entry->length, sizeof(time_t), 1, file);
@ -150,6 +152,24 @@ void RenX::BanDatabase::add(RenX::Server *server, const RenX::PlayerInfo *player
} }
} }
bool RenX::BanDatabase::deactivate(size_t index)
{
RenX::BanDatabase::Entry *entry = RenX::BanDatabase::entries.get(index);
if (entry->active)
{
entry->active = 0;
FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "r+b");
if (file != nullptr)
{
fsetpos(file, &entry->pos);
fputc(entry->active, file);
fclose(file);
}
return true;
}
return false;
}
uint8_t RenX::BanDatabase::getVersion() const uint8_t RenX::BanDatabase::getVersion() const
{ {
return RenX::BanDatabase::version; return RenX::BanDatabase::version;

11
RenX.Core/RenX_BanDatabase.h

@ -46,6 +46,7 @@ namespace RenX
*/ */
struct RENX_API Entry struct RENX_API Entry
{ {
fpos_t pos; /** Position of the entry in the database */
unsigned char active; /** 1 if the ban is active, 0 otherwise */ unsigned char active; /** 1 if the ban is active, 0 otherwise */
time_t timestamp /** Time the ban was created */; time_t timestamp /** Time the ban was created */;
time_t length /** Duration of the ban; 0 if permanent */; time_t length /** Duration of the ban; 0 if permanent */;
@ -65,7 +66,7 @@ namespace RenX
bool load(const Jupiter::ReadableString &fname); bool load(const Jupiter::ReadableString &fname);
/** /**
* @param Adds a ban entry for a player and immediately writes it to the database. * @brief Adds a ban entry for a player and immediately writes it to the database.
* *
* @param server Server the player is playing in * @param server Server the player is playing in
* @param player Data of the player to be banned * @param player Data of the player to be banned
@ -73,6 +74,14 @@ namespace RenX
*/ */
void add(RenX::Server *server, const RenX::PlayerInfo *player, time_t length); void add(RenX::Server *server, const RenX::PlayerInfo *player, time_t length);
/**
* @brief Deactivates a ban entry.
*
* @param index Index of the entry to deactivate.
* @param True if the entry was active and is now inactive, false otherwise.
*/
bool deactivate(size_t index);
/** /**
* @brief Fetches the version of the database file. * @brief Fetches the version of the database file.
* *

Loading…
Cancel
Save