2024-03-16 23:06:13 +00:00
|
|
|
// ----------------------------------------------------------------------------------------------
|
2023-02-09 23:37:26 +00:00
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
2024-03-16 23:06:13 +00:00
|
|
|
// ----------------------------------------------------------------------------------------------
|
2024-03-17 01:35:40 +00:00
|
|
|
// |
|
2024-01-08 10:33:28 +00:00
|
|
|
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
|
2023-02-09 23:37:26 +00:00
|
|
|
// Contact: JustArchi@JustArchi.net
|
2024-03-17 01:35:40 +00:00
|
|
|
// |
|
2023-02-09 23:37:26 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2024-03-17 01:35:40 +00:00
|
|
|
// |
|
2023-02-09 23:37:26 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2024-03-17 01:35:40 +00:00
|
|
|
// |
|
2023-02-09 23:37:26 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using ArchiSteamFarm.Core;
|
2024-02-21 02:09:36 +00:00
|
|
|
using ArchiSteamFarm.Helpers.Json;
|
2023-02-09 23:37:26 +00:00
|
|
|
using ArchiSteamFarm.Localization;
|
|
|
|
using ArchiSteamFarm.Steam;
|
|
|
|
using SteamKit2;
|
|
|
|
using SteamKit2.Internal;
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm.OfficialPlugins.MobileAuthenticator;
|
|
|
|
|
|
|
|
internal static class Commands {
|
|
|
|
internal static async Task<string?> OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) {
|
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
|
|
|
|
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:12:33 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(message);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
if ((args == null) || (args.Length == 0)) {
|
|
|
|
throw new ArgumentNullException(nameof(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(steamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (args.Length) {
|
|
|
|
case 1:
|
|
|
|
switch (args[0].ToUpperInvariant()) {
|
2023-04-20 19:55:19 +00:00
|
|
|
case "2FAFINALIZEDFORCE":
|
2023-04-20 19:31:30 +00:00
|
|
|
return await ResponseTwoFactorFinalized(access, bot).ConfigureAwait(false);
|
2023-02-09 23:37:26 +00:00
|
|
|
case "2FAINIT":
|
|
|
|
return await ResponseTwoFactorInit(access, bot).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (args[0].ToUpperInvariant()) {
|
2023-02-09 23:39:28 +00:00
|
|
|
case "2FAFINALIZE" when args.Length > 2:
|
2023-02-09 23:37:26 +00:00
|
|
|
return await ResponseTwoFactorFinalize(access, args[1], Utilities.GetArgsAsText(message, 2), steamID).ConfigureAwait(false);
|
2023-02-09 23:39:28 +00:00
|
|
|
case "2FAFINALIZE":
|
2023-02-09 23:37:26 +00:00
|
|
|
return await ResponseTwoFactorFinalize(access, bot, args[1]).ConfigureAwait(false);
|
2023-04-20 19:55:19 +00:00
|
|
|
case "2FAFINALIZED" when args.Length > 2:
|
|
|
|
return await ResponseTwoFactorFinalized(access, args[1], Utilities.GetArgsAsText(message, 2), steamID).ConfigureAwait(false);
|
2023-04-20 19:31:30 +00:00
|
|
|
case "2FAFINALIZED":
|
2023-04-20 19:55:19 +00:00
|
|
|
return await ResponseTwoFactorFinalized(access, bot, args[1]).ConfigureAwait(false);
|
|
|
|
case "2FAFINALIZEDFORCE":
|
|
|
|
return await ResponseTwoFactorFinalized(access, Utilities.GetArgsAsText(args, 1, ","), steamID: steamID).ConfigureAwait(false);
|
2023-02-09 23:39:28 +00:00
|
|
|
case "2FAINIT":
|
|
|
|
return await ResponseTwoFactorInit(access, Utilities.GetArgsAsText(args, 1, ","), steamID).ConfigureAwait(false);
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:39:28 +00:00
|
|
|
private static async Task<string?> ResponseTwoFactorFinalize(EAccess access, Bot bot, string activationCode) {
|
2023-02-09 23:37:26 +00:00
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(bot);
|
2023-11-14 18:12:33 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(activationCode);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
if (access < EAccess.Master) {
|
|
|
|
return access > EAccess.None ? bot.Commands.FormatBotResponse(Strings.ErrorAccessDenied) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bot.HasMobileAuthenticator) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(bot.HasMobileAuthenticator)));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(e.Message));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(json)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatErrorIsEmpty(nameof(json)));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 02:09:36 +00:00
|
|
|
Steam.Security.MobileAuthenticator? mobileAuthenticator = json.ToJsonObject<Steam.Security.MobileAuthenticator>();
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
if (mobileAuthenticator == null) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatErrorIsEmpty(nameof(json)));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mobileAuthenticator.Init(bot);
|
|
|
|
|
|
|
|
MobileAuthenticatorHandler? mobileAuthenticatorHandler = bot.GetHandler<MobileAuthenticatorHandler>();
|
|
|
|
|
|
|
|
if (mobileAuthenticatorHandler == null) {
|
|
|
|
throw new InvalidOperationException(nameof(mobileAuthenticatorHandler));
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong steamTime = await mobileAuthenticator.GetSteamTime().ConfigureAwait(false);
|
|
|
|
|
2024-03-19 11:40:54 +00:00
|
|
|
string? code = mobileAuthenticator.GenerateTokenForTime(steamTime);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
2024-03-19 11:40:54 +00:00
|
|
|
if (string.IsNullOrEmpty(code)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(mobileAuthenticator.GenerateTokenForTime)));
|
2024-03-19 11:40:54 +00:00
|
|
|
}
|
2023-02-09 23:37:26 +00:00
|
|
|
|
2024-03-19 11:40:54 +00:00
|
|
|
CTwoFactor_FinalizeAddAuthenticator_Response? response = await mobileAuthenticatorHandler.FinalizeAuthenticator(bot.SteamID, activationCode, code, steamTime).ConfigureAwait(false);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
2024-03-19 11:40:54 +00:00
|
|
|
if (response == null) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(mobileAuthenticatorHandler.FinalizeAuthenticator)));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 11:40:54 +00:00
|
|
|
if (!response.success) {
|
|
|
|
EResult result = (EResult) response.status;
|
|
|
|
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(result));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bot.TryImportAuthenticator(mobileAuthenticator)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(bot.TryImportAuthenticator)));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string maFileFinishedPath = $"{maFilePath}.NEW";
|
|
|
|
|
|
|
|
try {
|
|
|
|
File.Move(maFilePendingPath, maFileFinishedPath, true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
bot.ArchiLogger.LogGenericException(e);
|
|
|
|
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(e.Message));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bot.Commands.FormatBotResponse(Strings.Done);
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:39:28 +00:00
|
|
|
private static async Task<string?> ResponseTwoFactorFinalize(EAccess access, string botNames, string activationCode, ulong steamID = 0) {
|
2023-02-09 23:37:26 +00:00
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:12:33 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(botNames);
|
|
|
|
ArgumentException.ThrowIfNullOrEmpty(activationCode);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(steamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
HashSet<Bot>? bots = Bot.GetBots(botNames);
|
|
|
|
|
|
|
|
if ((bots == null) || (bots.Count == 0)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return access >= EAccess.Owner ? Steam.Interaction.Commands.FormatStaticResponse(Strings.FormatBotNotFound(botNames)) : null;
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:39:28 +00:00
|
|
|
IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseTwoFactorFinalize(Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), bot, activationCode))).ConfigureAwait(false);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
2024-04-11 21:24:01 +00:00
|
|
|
List<string> responses = [..results.Where(static result => !string.IsNullOrEmpty(result))!];
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
|
|
|
|
}
|
|
|
|
|
2023-04-20 19:55:19 +00:00
|
|
|
private static async Task<string?> ResponseTwoFactorFinalized(EAccess access, Bot bot, string? activationCode = null) {
|
2023-04-20 19:31:30 +00:00
|
|
|
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) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(bot.HasMobileAuthenticator)));
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(e.Message));
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(json)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatErrorIsEmpty(nameof(json)));
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 02:09:36 +00:00
|
|
|
Steam.Security.MobileAuthenticator? mobileAuthenticator = json.ToJsonObject<Steam.Security.MobileAuthenticator>();
|
2023-04-20 19:31:30 +00:00
|
|
|
|
|
|
|
if (mobileAuthenticator == null) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatErrorIsEmpty(nameof(json)));
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mobileAuthenticator.Init(bot);
|
|
|
|
|
2023-04-20 19:55:19 +00:00
|
|
|
if (!string.IsNullOrEmpty(activationCode)) {
|
|
|
|
string? generatedCode = await mobileAuthenticator.GenerateToken().ConfigureAwait(false);
|
|
|
|
|
2024-07-26 14:20:31 +00:00
|
|
|
if (string.IsNullOrEmpty(generatedCode)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(generatedCode)));
|
2024-07-26 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!generatedCode.Equals(activationCode, StringComparison.OrdinalIgnoreCase)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError($"{generatedCode} != {activationCode}"));
|
2023-04-20 19:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 19:31:30 +00:00
|
|
|
if (!bot.TryImportAuthenticator(mobileAuthenticator)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(bot.TryImportAuthenticator)));
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string maFileFinishedPath = $"{maFilePath}.NEW";
|
|
|
|
|
|
|
|
try {
|
|
|
|
File.Move(maFilePendingPath, maFileFinishedPath, true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
bot.ArchiLogger.LogGenericException(e);
|
|
|
|
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(e.Message));
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bot.Commands.FormatBotResponse(Strings.Done);
|
|
|
|
}
|
|
|
|
|
2023-04-20 19:55:19 +00:00
|
|
|
private static async Task<string?> ResponseTwoFactorFinalized(EAccess access, string botNames, string? activationCode = null, ulong steamID = 0) {
|
2023-04-20 19:31:30 +00:00
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:12:33 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(botNames);
|
2023-04-20 19:31:30 +00:00
|
|
|
|
|
|
|
if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(steamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
HashSet<Bot>? bots = Bot.GetBots(botNames);
|
|
|
|
|
|
|
|
if ((bots == null) || (bots.Count == 0)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return access >= EAccess.Owner ? Steam.Interaction.Commands.FormatStaticResponse(Strings.FormatBotNotFound(botNames)) : null;
|
2023-04-20 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 19:55:19 +00:00
|
|
|
IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseTwoFactorFinalized(Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), bot, activationCode))).ConfigureAwait(false);
|
2023-04-20 19:31:30 +00:00
|
|
|
|
2024-04-11 21:24:01 +00:00
|
|
|
List<string> responses = [..results.Where(static result => !string.IsNullOrEmpty(result))!];
|
2023-04-20 19:31:30 +00:00
|
|
|
|
|
|
|
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:37:26 +00:00
|
|
|
private static async Task<string?> ResponseTwoFactorInit(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) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(bot.HasMobileAuthenticator)));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bot.IsConnectedAndLoggedOn) {
|
|
|
|
return bot.Commands.FormatBotResponse(Strings.BotNotConnected);
|
|
|
|
}
|
|
|
|
|
|
|
|
MobileAuthenticatorHandler? mobileAuthenticatorHandler = bot.GetHandler<MobileAuthenticatorHandler>();
|
|
|
|
|
|
|
|
if (mobileAuthenticatorHandler == null) {
|
|
|
|
throw new InvalidOperationException(nameof(mobileAuthenticatorHandler));
|
|
|
|
}
|
|
|
|
|
|
|
|
string deviceID = $"android:{Guid.NewGuid()}";
|
|
|
|
|
|
|
|
CTwoFactor_AddAuthenticator_Response? response = await mobileAuthenticatorHandler.AddAuthenticator(bot.SteamID, deviceID).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
return bot.Commands.FormatBotResponse(Strings.WarningFailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
EResult result = (EResult) response.status;
|
|
|
|
|
|
|
|
if (result != EResult.OK) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(result));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 16:00:50 +00:00
|
|
|
MaFileData maFileData = new(response, bot.SteamID, deviceID);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
string maFilePendingPath = $"{bot.GetFilePath(Bot.EFileType.MobileAuthenticator)}.PENDING";
|
2024-02-21 02:09:36 +00:00
|
|
|
string json = maFileData.ToJsonText(true);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await File.WriteAllTextAsync(maFilePendingPath, json).ConfigureAwait(false);
|
|
|
|
} catch (Exception e) {
|
|
|
|
bot.ArchiLogger.LogGenericException(e);
|
|
|
|
|
2024-08-05 00:37:50 +00:00
|
|
|
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(e.Message));
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bot.Commands.FormatBotResponse(Strings.Done);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task<string?> ResponseTwoFactorInit(EAccess access, string botNames, ulong steamID = 0) {
|
|
|
|
if (!Enum.IsDefined(access)) {
|
|
|
|
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:12:33 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(botNames);
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
if ((steamID != 0) && !new SteamID(steamID).IsIndividualAccount) {
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(steamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
HashSet<Bot>? bots = Bot.GetBots(botNames);
|
|
|
|
|
|
|
|
if ((bots == null) || (bots.Count == 0)) {
|
2024-08-05 00:37:50 +00:00
|
|
|
return access >= EAccess.Owner ? Steam.Interaction.Commands.FormatStaticResponse(Strings.FormatBotNotFound(botNames)) : null;
|
2023-02-09 23:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IList<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseTwoFactorInit(Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), bot))).ConfigureAwait(false);
|
|
|
|
|
2024-04-11 21:24:01 +00:00
|
|
|
List<string> responses = [..results.Where(static result => !string.IsNullOrEmpty(result))!];
|
2023-02-09 23:37:26 +00:00
|
|
|
|
|
|
|
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
|
|
|
|
}
|
|
|
|
}
|