From 869904e9381c3cfe504823fbea2cc9c63fba8798 Mon Sep 17 00:00:00 2001 From: Archi Date: Thu, 20 Apr 2023 21:31:30 +0200 Subject: [PATCH] Add 2fafinalized command --- .../Commands.cs | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/ArchiSteamFarm.OfficialPlugins.MobileAuthenticator/Commands.cs b/ArchiSteamFarm.OfficialPlugins.MobileAuthenticator/Commands.cs index 34c52c5fe..46f5ac902 100644 --- a/ArchiSteamFarm.OfficialPlugins.MobileAuthenticator/Commands.cs +++ b/ArchiSteamFarm.OfficialPlugins.MobileAuthenticator/Commands.cs @@ -60,6 +60,8 @@ internal static class Commands { switch (args.Length) { case 1: switch (args[0].ToUpperInvariant()) { + case "2FAFINALIZED": + return await ResponseTwoFactorFinalized(access, bot).ConfigureAwait(false); case "2FAINIT": return await ResponseTwoFactorInit(access, bot).ConfigureAwait(false); } @@ -71,6 +73,8 @@ internal static class Commands { return await ResponseTwoFactorFinalize(access, args[1], Utilities.GetArgsAsText(message, 2), steamID).ConfigureAwait(false); case "2FAFINALIZE": return await ResponseTwoFactorFinalize(access, bot, args[1]).ConfigureAwait(false); + case "2FAFINALIZED": + return await ResponseTwoFactorFinalized(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false); case "2FAINIT": return await ResponseTwoFactorInit(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false); } @@ -228,6 +232,97 @@ internal static class Commands { return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null; } + private static async Task ResponseTwoFactorFinalized(EAccess access, Bot bot) { + if (!Enum.IsDefined(access)) { + throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess)); + } + + ArgumentNullException.ThrowIfNull(bot); + + if (access < EAccess.Master) { + return access > EAccess.None ? bot.Commands.FormatBotResponse(Strings.ErrorAccessDenied) : null; + } + + if (bot.HasMobileAuthenticator) { + return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(bot.HasMobileAuthenticator))); + } + + if (!bot.IsConnectedAndLoggedOn) { + return bot.Commands.FormatBotResponse(Strings.BotNotConnected); + } + + string maFilePath = bot.GetFilePath(Bot.EFileType.MobileAuthenticator); + string maFilePendingPath = $"{maFilePath}.PENDING"; + + if (!File.Exists(maFilePendingPath)) { + return bot.Commands.FormatBotResponse(Strings.NothingFound); + } + + string json; + + try { + json = await File.ReadAllTextAsync(maFilePendingPath).ConfigureAwait(false); + } catch (Exception e) { + bot.ArchiLogger.LogGenericException(e); + + return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, e.Message)); + } + + if (string.IsNullOrEmpty(json)) { + return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json))); + } + + Steam.Security.MobileAuthenticator? mobileAuthenticator = JsonConvert.DeserializeObject(json); + + if (mobileAuthenticator == null) { + return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json))); + } + + mobileAuthenticator.Init(bot); + + if (!bot.TryImportAuthenticator(mobileAuthenticator)) { + return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(bot.TryImportAuthenticator))); + } + + string maFileFinishedPath = $"{maFilePath}.NEW"; + + try { + File.Move(maFilePendingPath, maFileFinishedPath, true); + } catch (Exception e) { + bot.ArchiLogger.LogGenericException(e); + + return bot.Commands.FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, e.Message)); + } + + return bot.Commands.FormatBotResponse(Strings.Done); + } + + private static async Task ResponseTwoFactorFinalized(EAccess access, string botNames, ulong steamID = 0) { + if (!Enum.IsDefined(access)) { + throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess)); + } + + if (string.IsNullOrEmpty(botNames)) { + throw new ArgumentNullException(nameof(botNames)); + } + + if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) { + throw new ArgumentOutOfRangeException(nameof(steamID)); + } + + HashSet? bots = Bot.GetBots(botNames); + + if ((bots == null) || (bots.Count == 0)) { + return access >= EAccess.Owner ? Steam.Interaction.Commands.FormatStaticResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames)) : null; + } + + IList results = await Utilities.InParallel(bots.Select(bot => ResponseTwoFactorFinalized(Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), bot))).ConfigureAwait(false); + + List responses = new(results.Where(static result => !string.IsNullOrEmpty(result))!); + + return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null; + } + private static async Task ResponseTwoFactorInit(EAccess access, Bot bot) { if (!Enum.IsDefined(access)) { throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));