using AIStudio.Settings; using AIStudio.Settings.DataModel; using AIStudio.Tools.PluginSystem; namespace AIStudio.Tools.Mail; /// /// Which mail servers AI Studio may connect to, whose mailboxes the AI may read, and in which encoding /// a mailbox sends its username and password. /// /// /// Read anew for every decision rather than kept: all parts come from configuration plugins, which /// may change while AI Studio runs. /// /// Whether the organization allows only its own mail servers, see DataMailboxes.AllowOnlyOrganizationMailServers. /// The mail servers which the configuration plugins offer. public sealed record MailServerPolicy(bool AllowsOnlyOrganizationMailServers, IReadOnlyList OrganizationServers) { /// /// Every server: what holds as long as no organization rules otherwise. /// public static readonly MailServerPolicy ANY_SERVER = new(false, []); /// /// Reads the policy which holds right now. /// /// The settings, which hold the switch of the organization. /// The policy. public static MailServerPolicy Read(SettingsManager settingsManager) => new( settingsManager.ConfigurationData.MailboxSettings.AllowOnlyOrganizationMailServers, PluginFactory.GetMailboxProviders().Select(provider => new OrganizationMailServer(provider.Host, provider.PasswordEncoding)).ToList()); /// /// Whether AI Studio may connect to a server, and the AI read the mailboxes on it. /// /// /// Port and encryption make no difference: a mailbox on the right host but another port is /// still on the server of the organization. /// /// The host of the server. /// True when the server is allowed. public bool IsAllowed(string host) => !this.AllowsOnlyOrganizationMailServers || this.OrganizationServers.Any(server => MailServerHosts.AreSame(server.Host, host)); /// /// The encoding the organization names for a server. /// /// /// Found by the host, like IsAllowed: the user may have typed the host rather than chosen the /// mail server of the organization. It holds whether or not the organization allows only its own /// mail servers. /// /// The host of the server. /// The encoding, or null when the organization names none for this server. public MailboxPasswordEncoding? GetOrganizationPasswordEncoding(string host) => this.OrganizationServers .FirstOrDefault(server => server.PasswordEncoding is not null && MailServerHosts.AreSame(server.Host, host))? .PasswordEncoding; /// /// The encoding in which a mailbox sends its username and password. /// /// /// The encoding the organization names for the server comes first: it knows what its server /// expects, and it may name the encoding only after the mailbox was added. Every sign-in, the /// connection test and the synchronization alike, takes the encoding from here. /// /// The mailbox. /// The encoding. public MailboxPasswordEncoding GetPasswordEncoding(DataSourceMailbox mailbox) => this.GetPasswordEncoding(mailbox.Host, mailbox.PasswordEncoding); /// /// The encoding in which a mailbox on a server sends its username and password, cf. GetPasswordEncoding of a mailbox. /// /// The host of the server. /// The encoding of the mailbox. /// The encoding. public MailboxPasswordEncoding GetPasswordEncoding(string host, MailboxPasswordEncoding mailboxEncoding) => this.GetOrganizationPasswordEncoding(host) ?? mailboxEncoding; }