Some more cleanup.

This commit is contained in:
Burak Kaan Köse
2026-04-05 13:18:50 +02:00
parent c1ab49fb1d
commit ca19297b92
22 changed files with 444 additions and 302 deletions
+3 -28
View File
@@ -1,12 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
@@ -48,7 +46,6 @@ public class ImapClientPool : IDisposable
private readonly ILogger _logger = Log.ForContext<ImapClientPool>();
private readonly CustomServerInformation _customServerInformation;
private readonly Stream _protocolLogStream;
private readonly ConcurrentDictionary<WinoImapClient, ImapClientState> _clientStates = new();
private readonly Channel<WinoImapClient> _availableClients;
private readonly CancellationTokenSource _maintenanceCts = new();
@@ -78,7 +75,6 @@ public class ImapClientPool : IDisposable
public ImapClientPool(ImapClientPoolOptions imapClientPoolOptions)
{
_customServerInformation = imapClientPoolOptions.ServerInformation;
_protocolLogStream = imapClientPoolOptions.ProtocolLog;
ImapClientPoolOptions = imapClientPoolOptions;
_quirks = ImapServerQuirks.Resolve(_customServerInformation.IncomingServer);
@@ -620,14 +616,7 @@ public class ImapClientPool : IDisposable
private WinoImapClient CreateNewClient()
{
IProtocolLogger protocolLogger = null;
if (_protocolLogStream != null)
{
protocolLogger = new ProtocolLogger(_protocolLogStream, leaveOpen: true);
}
var client = protocolLogger != null ? new WinoImapClient(protocolLogger) : new WinoImapClient();
var client = new WinoImapClient();
if (!string.IsNullOrEmpty(_customServerInformation.ProxyServer))
{
@@ -679,11 +668,9 @@ public class ImapClientPool : IDisposable
private ImapClientPoolException CreatePoolException(string message, Exception innerException = null)
{
var protocolLog = GetProtocolLogContent() ?? string.Empty;
return innerException == null
? new ImapClientPoolException(message, _customServerInformation, protocolLog)
: new ImapClientPoolException(innerException, protocolLog);
? new ImapClientPoolException(message, _customServerInformation)
: new ImapClientPoolException(innerException);
}
private static ImapImplementation CreateImplementation()
@@ -740,17 +727,6 @@ public class ImapClientPool : IDisposable
return true;
}
public string GetProtocolLogContent()
{
if (_protocolLogStream == null) return default;
if (_protocolLogStream.CanSeek)
_protocolLogStream.Seek(0, SeekOrigin.Begin);
using var reader = new StreamReader(_protocolLogStream, Encoding.UTF8, true, 1024, leaveOpen: true);
return reader.ReadToEnd();
}
// Legacy compatibility methods
public Task<bool> EnsureConnectedAsync(IImapClient client) =>
Task.FromResult(client.IsConnected);
@@ -784,7 +760,6 @@ public class ImapClientPool : IDisposable
_dedicatedIdleClient = null;
}
_protocolLogStream?.Dispose();
}
_disposedValue = true;
+1 -6
View File
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using MailKit;
using MailKit.Net.Imap;
@@ -24,11 +24,6 @@ public class WinoImapClient : ImapClient
HookEvents();
}
public WinoImapClient(IProtocolLogger protocolLogger) : base(protocolLogger)
{
HookEvents();
}
private void HookEvents()
{
Disconnected += ClientDisconnected;
+19 -58
View File
@@ -1,5 +1,3 @@
using System;
using System.IO;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using Wino.Core.Domain.Entities.Shared;
@@ -11,69 +9,32 @@ namespace Wino.Core.Services;
public class ImapTestService : IImapTestService
{
public const string ProtocolLogFileName = "ImapProtocolLog.log";
private readonly IPreferencesService _preferencesService;
private readonly IApplicationConfiguration _appInitializerService;
private Stream _protocolLogStream;
public ImapTestService(IPreferencesService preferencesService, IApplicationConfiguration appInitializerService)
public ImapTestService()
{
_preferencesService = preferencesService;
_appInitializerService = appInitializerService;
}
private void EnsureProtocolLogFileExists()
{
// Create new file for protocol logger.
var localAppFolderPath = _appInitializerService.ApplicationDataFolderPath;
var logFile = Path.Combine(localAppFolderPath, ProtocolLogFileName);
if (File.Exists(logFile))
File.Delete(logFile);
_protocolLogStream = File.Create(logFile);
}
public async Task TestImapConnectionAsync(CustomServerInformation serverInformation, bool allowSSLHandShake)
{
try
var poolOptions = ImapClientPoolOptions.CreateTestPool(serverInformation);
using (var clientPool = new ImapClientPool(poolOptions)
{
EnsureProtocolLogFileExists();
var poolOptions = ImapClientPoolOptions.CreateTestPool(serverInformation, _protocolLogStream);
var clientPool = new ImapClientPool(poolOptions)
{
ThrowOnSSLHandshakeCallback = !allowSSLHandShake
};
using (clientPool)
{
// This call will make sure that everything is authenticated + connected successfully.
var client = await clientPool.GetClientAsync();
clientPool.Release(client);
}
// Test SMTP connectivity.
using var smtpClient = new SmtpClient();
if (!smtpClient.IsConnected)
await smtpClient.ConnectAsync(serverInformation.OutgoingServer, int.Parse(serverInformation.OutgoingServerPort), MailKit.Security.SecureSocketOptions.Auto);
if (!smtpClient.IsAuthenticated)
await smtpClient.AuthenticateAsync(serverInformation.OutgoingServerUsername, serverInformation.OutgoingServerPassword);
}
catch (Exception)
ThrowOnSSLHandshakeCallback = !allowSSLHandShake
})
{
throw;
}
finally
{
_protocolLogStream?.Dispose();
// This call will make sure that everything is authenticated + connected successfully.
var client = await clientPool.GetClientAsync();
clientPool.Release(client);
}
// Test SMTP connectivity.
using var smtpClient = new SmtpClient();
if (!smtpClient.IsConnected)
await smtpClient.ConnectAsync(serverInformation.OutgoingServer, int.Parse(serverInformation.OutgoingServerPort), MailKit.Security.SecureSocketOptions.Auto);
if (!smtpClient.IsAuthenticated)
await smtpClient.AuthenticateAsync(serverInformation.OutgoingServerUsername, serverInformation.OutgoingServerPassword);
}
}
+3 -3
View File
@@ -114,13 +114,13 @@ public class SynchronizationManager : ISynchronizationManager
}
catch (ImapClientPoolException clientPoolException)
{
_logger.Error(clientPoolException, "IMAP connectivity test failed with protocol log");
return ImapConnectivityTestResults.Failure(clientPoolException, clientPoolException.ProtocolLog);
_logger.Error(clientPoolException, "IMAP connectivity test failed");
return ImapConnectivityTestResults.Failure(clientPoolException);
}
catch (Exception exception)
{
_logger.Error(exception, "IMAP connectivity test failed");
return ImapConnectivityTestResults.Failure(exception, string.Empty);
return ImapConnectivityTestResults.Failure(exception);
}
}
+1 -15
View File
@@ -83,7 +83,6 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
IAutoDiscoveryService autoDiscoveryService,
ICalendarService calendarService) : base(account, WeakReferenceMessenger.Default)
{
// Create client pool with account protocol log.
_imapChangeProcessor = imapChangeProcessor;
_applicationConfiguration = applicationConfiguration;
_unifiedSynchronizer = unifiedSynchronizer;
@@ -92,26 +91,13 @@ public class ImapSynchronizer : WinoSynchronizer<ImapRequest, ImapMessageCreatio
_autoDiscoveryService = autoDiscoveryService;
_calendarService = calendarService;
var protocolLogStream = CreateAccountProtocolLogFileStream();
var poolOptions = ImapClientPoolOptions.CreateDefault(Account.ServerInformation, protocolLogStream);
var poolOptions = ImapClientPoolOptions.CreateDefault(Account.ServerInformation);
_clientPool = new ImapClientPool(poolOptions);
_localCalendarOperationHandler = new LocalCalendarOperationHandler(Account, _imapChangeProcessor, _calendarService, _applicationConfiguration.ApplicationDataFolderPath, "local");
_calDavCalendarOperationHandler = new CalDavCalendarOperationHandler(this, Account, _calendarService, _calDavClient);
}
private Stream CreateAccountProtocolLogFileStream()
{
if (Account == null) throw new ArgumentNullException(nameof(Account));
var logFile = Path.Combine(_applicationConfiguration.ApplicationDataFolderPath, $"Protocol_{Account.Address}_{Account.Id}.log");
// Each session should start a new log.
if (File.Exists(logFile)) File.Delete(logFile);
return new FileStream(logFile, FileMode.CreateNew);
}
/// <summary>
/// Returns UniqueId for the given mail copy id.
/// </summary>