ff_u_password

Lock the server with a random player password until the module has started.

Keeps players from joining while the module is still starting, before every unit is registered and every area is ready.

When the unit registers, the current player password (from the server settings, or NWN_PLAYERPASSWORD) is kept on the module, and replaced by a random one: players cannot join, since they do not know it.

On ON_MODULE_START, the previous player password is restored. If the server had no player password, the password is cleared and the server is open again.

The random password only exists in memory: if the server stops during startup, the next start uses the configured password again.

Variables

ENABLE__FF_U_PASSWORD: (string) (opt-in) Set to "Y" on module to enable the unit.

Source code

// @code

#include "ff_i_core"

//! Module flag to enable the unit.
const string ENABLE__FF_U_PASSWORD = "ENABLE__FF_U_PASSWORD";

//! Local string on the module: the player password to restore on module start.
const string FF_U_PASSWORD = "__FF_U_PASSWORD";


// -----------------------------------------------------------------------------

//! @brief Keep the current player password, and replace it with a random one.
void LockServer();
void LockServer()
{
  SetLocalString(GetModule(), FF_U_PASSWORD, NWNX_Administration_GetPlayerPassword());
  NWNX_Administration_SetPlayerPassword(IntToString(Random(1000)));
  LogNotice("Locked the server with a random password.");
}


// -----------------------------------------------------------------------------

//! @brief Restore the player password kept by LockServer(), or clear it if there was none.
void UnlockServer();
void UnlockServer()
{
  object oModule = GetModule();
  string sPassword = GetLocalString(oModule, FF_U_PASSWORD);
  if (sPassword != "")
    NWNX_Administration_SetPlayerPassword(sPassword);
  else
    NWNX_Administration_ClearPlayerPassword();
  DeleteLocalString(oModule, FF_U_PASSWORD);
  LogNotice("Unlocked the server by restoring the previous player password.");
}


// -----------------------------------------------------------------------------

//! @brief Unit entry point.
//! - ON_REGISTER: lock the server.
//! - ON_MODULE_START: unlock the server.
void main()
{
  string sEvent = NWNX_Events_GetCurrentEvent();
  if (sEvent == ON_REGISTER)
  {
    if (!GetModuleFlag(ENABLE__FF_U_PASSWORD, FALSE))
      return;

    SubscribeToEvent(ON_MODULE_START, __FILE__);
    LockServer();
  }
  else if (sEvent == ON_MODULE_START)
    UnlockServer();
}