using System; using System.Threading; using System.Threading.Tasks; using Wino.Core.Domain.Exceptions; using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Models.Connectivity; using Wino.Core.Domain.Models.Server; using Wino.Messaging.Server; using Wino.Server.Core; namespace Wino.Server.MessageHandlers; public class ImapConnectivityTestHandler : ServerMessageHandler { private readonly IImapTestService _imapTestService; public override WinoServerResponse FailureDefaultResponse(Exception ex) => WinoServerResponse.CreateErrorResponse(ex.Message); public ImapConnectivityTestHandler(IImapTestService imapTestService) { _imapTestService = imapTestService; } protected override async Task> HandleAsync(ImapConnectivityTestRequested message, CancellationToken cancellationToken = default) { try { await _imapTestService.TestImapConnectionAsync(message.ServerInformation, message.IsSSLHandshakeAllowed); return WinoServerResponse.CreateSuccessResponse(ImapConnectivityTestResults.Success()); } catch (ImapTestSSLCertificateException sslTestException) { // User must confirm to continue ignoring the SSL certificate. return WinoServerResponse.CreateSuccessResponse(ImapConnectivityTestResults.CertificateUIRequired(sslTestException.Issuer, sslTestException.ExpirationDateString, sslTestException.ValidFromDateString)); } catch (ImapClientPoolException clientPoolException) { // Connectivity failed with protocol log. return WinoServerResponse.CreateSuccessResponse(ImapConnectivityTestResults.Failure(clientPoolException, clientPoolException.ProtocolLog)); } catch (Exception exception) { // Unknown error return WinoServerResponse.CreateSuccessResponse(ImapConnectivityTestResults.Failure(exception, string.Empty)); } } }