// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later using System.Text; namespace Progmasoft.Progmaweb.Api.Accounts; /// /// The account commands of the server. /// /// /// /// accounts provision-reserved <Name> <email> provisions a reserved account. Public registration /// refuses the reserved names (Progmasoft, Leitwolf); this is the trusted operations tooling that creates them. The /// password is read from the terminal without echo, twice, or from one line of standard input when it is not a /// terminal; it never appears on a command line. /// /// /// accounts notify-policies [--dry-run] tells every account holder who accepted an earlier version of the /// policies about , once: each notice sent is recorded, so the command can /// run again after a failure and sends only what is left. --dry-run counts them and sends nothing. /// /// internal static class AccountCommand { /// Runs an account command. /// The services of the API, not started. /// The arguments after accounts. /// Where the password comes from. /// Where results go. /// Where errors go. /// Cancels the command. /// The exit code: 0 on success, 1 when refused, 2 for a wrong command line. public static async Task RunAsync( IServiceProvider services, string[] args, IPasswordInput input, TextWriter output, TextWriter errors, CancellationToken cancellationToken) { bool isNotice = args is ["notify-policies"] or ["notify-policies", "--dry-run"]; if (!isNotice && args is not ["provision-reserved", _, _]) { await errors.WriteLineAsync("usage: accounts provision-reserved "); await errors.WriteLineAsync(" accounts notify-policies [--dry-run]"); return 2; } // The service brings the account tables to the current schema when it starts; a command may run before. if (services.GetService() is { } dataSource) { await AccountSchema.ApplyAsync(dataSource, cancellationToken); } if (isNotice) { return await NotifyPoliciesAsync(services, args.Length == 2, output, errors, cancellationToken); } string name = args[1]; string email = args[2]; string? password = input.Read("Password: "); if (input.IsTerminal && password != input.Read("Password again: ")) { await errors.WriteLineAsync("The two passwords differ; nothing was created."); return 1; } AccountService accounts = services.GetRequiredService(); (AccountRecord? account, string? error) = await accounts.ProvisionReservedAsync(name, email, password, cancellationToken); if (account is null) { await errors.WriteLineAsync(error); return 1; } await output.WriteLineAsync($"Created the reserved account {account.AccountName} ({account.Email})."); return 0; } /// Sends the notices of the newest version of the policies that were not sent yet. /// The services of the API. /// Whether to count them only. /// Where results go. /// Where errors go. /// Cancels the command. /// 0 when every notice went out, 1 when one failed. private static async Task NotifyPoliciesAsync( IServiceProvider services, bool isDryRun, TextWriter output, TextWriter errors, CancellationToken cancellationToken) { IAccountStore store = services.GetRequiredService(); string version = PolicyAcceptance.CurrentVersion; IReadOnlyList accounts = await store.ListAccountsToNotifyAsync(version, cancellationToken); if (isDryRun) { await output.WriteLineAsync($"{accounts.Count} account holders would be told about the policies of {version}."); return 0; } IPolicyNoticeSender sender = services.GetRequiredService(); TimeProvider time = services.GetService() ?? TimeProvider.System; int sent = 0; foreach (AccountRecord account in accounts) { try { await sender.SendAsync(PolicyNotice.For(account), cancellationToken); } catch (Exception exception) when (exception is IOException or MailKit.ProtocolException or MailKit.Net.Smtp.SmtpCommandException or System.Net.Sockets.SocketException) { await errors.WriteLineAsync( $"Sent {sent} of {accounts.Count}; the notice to {account.AccountName} failed: {exception.Message}"); return 1; } await store.RecordPolicyNoticeAsync(account.Id, version, time.GetUtcNow(), cancellationToken); sent++; } await output.WriteLineAsync($"Told {sent} account holders about the policies of {version}."); return 0; } } /// Where reads a password from. internal interface IPasswordInput { /// Whether the input is a terminal, which asks twice and does not echo. bool IsTerminal { get; } /// Reads one password. /// What to show before it, on a terminal. /// The password, or at the end of the input. string? Read(string prompt); } /// The console: a terminal without echo, or one line of redirected standard input. internal sealed class ConsolePasswordInput : IPasswordInput { /// public bool IsTerminal => !Console.IsInputRedirected; /// public string? Read(string prompt) { if (!IsTerminal) { return Console.In.ReadLine(); } Console.Error.Write(prompt); StringBuilder password = new(); while (true) { ConsoleKeyInfo key = Console.ReadKey(intercept: true); if (key.Key == ConsoleKey.Enter) { Console.Error.WriteLine(); return password.ToString(); } if (key.Key == ConsoleKey.Backspace) { if (password.Length > 0) { password.Length--; } continue; } if (!char.IsControl(key.KeyChar)) { password.Append(key.KeyChar); } } } }