private static bool ConsoleCtrlCheck(CtrlTypes ctrlType) { // Put your own handler here switch (ctrlType) { case CtrlTypes.CTRL_C_EVENT: case CtrlTypes.CTRL_BREAK_EVENT: case CtrlTypes.CTRL_CLOSE_EVENT: case CtrlTypes.CTRL_LOGOFF_EVENT: case CtrlTypes.CTRL_SHUTDOWN_EVENT: if (manager != null) { manager.StopBots(); } isclosing = true; break; } return(true); }
// This mode is to manage child bot processes and take use command line inputs private static void BotManagerMode() { Console.Title = "Bot Manager"; manager = new BotManager(); bool loadedOk = manager.LoadConfiguration("settings.json"); if (!loadedOk) { Console.WriteLine("Configuration file Does not exist or is corrupt. Please rename 'settings-template.json' " + "to 'settings.json' and modify the settings to match your environment"); Console.Write("Press Enter to exit..."); Console.ReadLine(); if (!File.Exists(BotManager.DATA_FOLDER + "settings-template.json")) { File.WriteAllText(BotManager.DATA_FOLDER + "settings-template.json", defSettingsStr); return; } } else { if (manager.ConfigObject.UseSeparateProcesses) { SetConsoleCtrlHandler(ConsoleCtrlCheck, true); } if (manager.ConfigObject.AutoStartAllBots) { var startedOk = manager.StartBots(); if (!startedOk) { Console.WriteLine("Error starting the bots because either the configuration was bad " + "or because the log file was not opened."); Console.Write("Press Enter to exit..."); Console.ReadLine(); } } else { foreach (var botInfo in manager.ConfigObject.Bots) { if (botInfo.AutoStart) { // auto start this particual bot... manager.StartBot(botInfo.Username); } } } Console.WriteLine("Type help for bot manager commands. "); Console.Write("botmgr > "); var bmi = new BotManagerInterpreter(manager); ConsoleReadLineTask = Console.In.ReadLineAsync(); // command interpreter loop. do { if (ConsoleReadLineTask.IsCompleted) { if (!IsMaskedInput) { string inputText = ConsoleReadLineTask.Result; if (string.IsNullOrEmpty(inputText)) { continue; } if (inputText.ToLower() == "exit") { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Exiting bot manager..."); manager.StopBots(); isclosing = true; break; } if (inputText.ToLower() == "clearpassword") { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Clearing saved passwords..."); Console.WriteLine("You will need to re-enter it next time you log in."); ClearSavedPasswords(); } bmi.CommandInterpreter(inputText); Console.Write("botmgr> "); ConsoleReadLineTask = Console.In.ReadLineAsync(); } else { // get masked input PasswordRequestingBot.DoSetPassword(); IsMaskedInput = false; PasswordRequestingBot = null; ConsoleReadLineTask = Console.In.ReadLineAsync(); } } } while (!isclosing); } }
// This mode is to manage child bot processes and take use command line inputs private static void BotManagerMode() { Console.Title = "Bot Manager"; manager = new BotManager(); bool loadedOk = manager.LoadConfiguration("settings.json"); if (!loadedOk) { Console.WriteLine("Configuration file Does not exist or is corrupt. Please rename 'settings-template.json' " + "to 'settings.json' and modify the settings to match your environment"); Console.Write("Press Enter to exit..."); Console.ReadLine(); if (!File.Exists(BotManager.DATA_FOLDER + "settings-template.json")) { File.WriteAllText(BotManager.DATA_FOLDER + "settings-template.json", defSettingsStr); return; } } else { if (manager.ConfigObject.UseSeparateProcesses) SetConsoleCtrlHandler(ConsoleCtrlCheck, true); if (manager.ConfigObject.AutoStartAllBots) { var startedOk = manager.StartBots(); if (!startedOk) { Console.WriteLine("Error starting the bots because either the configuration was bad " + "or because the log file was not opened."); Console.Write("Press Enter to exit..."); Console.ReadLine(); } } else { foreach (var botInfo in manager.ConfigObject.Bots) { if (botInfo.AutoStart) { // auto start this particual bot... manager.StartBot(botInfo.Username); } } } Console.WriteLine("Type help for bot manager commands. "); Console.Write("botmgr > "); var bmi = new BotManagerInterpreter(manager); ConsoleReadLineTask = Console.In.ReadLineAsync(); // command interpreter loop. do { if (ConsoleReadLineTask.IsCompleted) { if (!IsMaskedInput) { string inputText = ConsoleReadLineTask.Result; if (string.IsNullOrEmpty(inputText)) continue; if (inputText.ToLower() == "exit") { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Exiting bot manager..."); manager.StopBots(); isclosing = true; break; } if (inputText.ToLower() == "clearpassword") { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Clearing saved passwords..."); Console.WriteLine("You will need to re-enter it next time you log in."); ClearSavedPasswords(); } bmi.CommandInterpreter(inputText); Console.Write("botmgr> "); ConsoleReadLineTask = Console.In.ReadLineAsync(); } else { // get masked input PasswordRequestingBot.DoSetPassword(); IsMaskedInput = false; PasswordRequestingBot = null; ConsoleReadLineTask = Console.In.ReadLineAsync(); } } } while (!isclosing); } }