Skip to content

Commit

Permalink
Add -PWRESET Command Line Argument (#603)
Browse files Browse the repository at this point in the history
* Add -PWRESET Command Line Argument

- Add `-PWRESET` Command Line Argument to reset Sysop Password
- Moved Password Prompt to common method (used by `-DBRESET` as well)
- Moved GUI to start after pre-launch items (Password reset, etc.)

* Address PR Notes

- Just make Password Prompt and infinite loop until they match
- Set `_doResetPassword` to false when the password is reset

* Do not enter GUI if in CONSOLE mode
  • Loading branch information
enusbaum authored Nov 24, 2023
1 parent dd9c93a commit da908ee
Showing 1 changed file with 84 additions and 43 deletions.
127 changes: 84 additions & 43 deletions MBBSEmu/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class Program
/// </summary>
private bool _doResetDatabase;

/// <summary>
/// Specified if -PWRESET Command Line Argument was Passed
/// </summary>
private bool _doResetPassword;

/// <summary>
/// New Sysop Password specified by the -DBRESET Command Line Argument
/// </summary>
Expand Down Expand Up @@ -254,6 +259,17 @@ private void Run(string[] args)
_isConsoleSession = true;
break;
}
case "-PWRESET":
{
_doResetPassword = true;
if (i + 1 < args.Length && args[i + 1][0] != '-')
{
_newSysopPassword = args[i + 1];
i++;
}

break;
}
default:
Console.WriteLine($"Unknown Command Line Argument: {args[i]}");
return;
Expand Down Expand Up @@ -295,28 +311,6 @@ private void Run(string[] args)
return;
}

//If the user specified CLI mode, don't start the UI
if (!_cliMode)
{
var mainMBBSEmuWindow = new MainView(_serviceResolver);

//Start UI in a Task as to not block this thread
Task.Run(() =>
{
mainMBBSEmuWindow.Setup();
mainMBBSEmuWindow.Run();
});

//Wait for the UI to be running before continuing
Console.Write("Waiting for MBBSEmu GUI to start...");
while (!mainMBBSEmuWindow.isRunning)
{
Thread.Sleep(500);
Console.Write(".");
}
Console.WriteLine();
}

var configuration = _serviceResolver.GetService<AppSettingsManager>();
var textVariableService = _serviceResolver.GetService<ITextVariableService>();
var resourceManager = _serviceResolver.GetService<IResourceManager>();
Expand All @@ -343,6 +337,10 @@ private void Run(string[] args)
if (_doResetDatabase)
DatabaseReset();

//Password Reset
if (_doResetPassword)
PasswordReset();

//Database Sanity Checks
var databaseFile = configuration.DatabaseFile;
if (!File.Exists($"{databaseFile}"))
Expand Down Expand Up @@ -433,6 +431,28 @@ private void Run(string[] args)
return;
}

//If the user specified CLI mode, don't start the UI
if (!_cliMode && !_isConsoleSession)
{
var mainMBBSEmuWindow = new MainView(_serviceResolver);

//Start UI in a Task as to not block this thread
Task.Run(() =>
{
mainMBBSEmuWindow.Setup();
mainMBBSEmuWindow.Run();
});

//Wait for the UI to be running before continuing
Console.Write("Waiting for MBBSEmu GUI to start...");
while (!mainMBBSEmuWindow.isRunning)
{
Thread.Sleep(500);
Console.Write(".");
}
Console.WriteLine();
}

//Setup and Run Host
var host = _serviceResolver.GetService<IMbbsHost>();
host.Start(_moduleConfigurations);
Expand Down Expand Up @@ -519,7 +539,7 @@ private void CancelKeyPressHandler(object sender, ConsoleCancelEventArgs args)

_cancellationRequests++;

_logger.Warn("BBS Shutting down");
_logger.Warn("MBBSEmu Shutting down...");

foreach (var runningService in _runningServices)
{
Expand All @@ -536,27 +556,8 @@ private void DatabaseReset()
{
_logger.Info("Resetting Database...");


if (string.IsNullOrEmpty(_newSysopPassword))
{
var bPasswordMatch = false;
while (!bPasswordMatch)
{
Console.Write("Enter New Sysop Password: ");
var password1 = Console.ReadLine();
Console.Write("Re-Enter New Sysop Password: ");
var password2 = Console.ReadLine();
if (password1 == password2)
{
bPasswordMatch = true;
_newSysopPassword = password1;
}
else
{
Console.WriteLine("Password mismatch, please try again.");
}
}
}
_newSysopPassword = PasswordPrompt("sysop");

var acct = _serviceResolver.GetService<IAccountRepository>();
acct.Reset(_newSysopPassword);
Expand All @@ -576,5 +577,45 @@ private void DatabaseReset()

_logger.Info("Database Reset!");
}

/// <summary>
/// Prompts the user via the CLI to enter a new password for the specified username
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private string PasswordPrompt(string username)
{
while (true)
{
Console.Write($"Enter New Password for {username}: ");
var password1 = Console.ReadLine();
Console.Write($"Re-Enter New Password for {username}: ");
var password2 = Console.ReadLine();

//If they match, return the password
if (password1 == password2)
return password1;

//Otherwise infinite loop until they match
Console.WriteLine("Password mismatch, please try again.");
}
}

/// <summary>
/// Reset the Sysop Password to the specified value via the console
/// </summary>
private void PasswordReset()
{
//Interactive Password Reset via Console if one wasn't specified in the command line
if (string.IsNullOrEmpty(_newSysopPassword))
_newSysopPassword = PasswordPrompt("sysop");

var acct = _serviceResolver.GetService<IAccountRepository>();
var sysopAccount = acct.GetAccountByUsername("sysop");
acct.UpdateAccountById(sysopAccount.accountId, sysopAccount.userName, _newSysopPassword, sysopAccount.email);
_logger.Info("Sysop Password Reset!");
_doResetPassword = false;
}
}
}

0 comments on commit da908ee

Please sign in to comment.