Add 2fafinalized command

This commit is contained in:
Archi 2023-04-20 21:31:30 +02:00
parent 68265993f7
commit 869904e938
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA

View file

@ -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<string?> 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<Steam.Security.MobileAuthenticator>(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<string?> 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<Bot>? 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<string?> results = await Utilities.InParallel(bots.Select(bot => ResponseTwoFactorFinalized(Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), bot))).ConfigureAwait(false);
List<string> responses = new(results.Where(static result => !string.IsNullOrEmpty(result))!);
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
private static async Task<string?> ResponseTwoFactorInit(EAccess access, Bot bot) {
if (!Enum.IsDefined(access)) {
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));