183 lines
4.6 KiB
C#
183 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Unosquare.Swan.Networking {
|
|
/// <summary>
|
|
/// Represents the state of an SMTP session associated with a client.
|
|
/// </summary>
|
|
public class SmtpSessionState {
|
|
#region Constructors
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SmtpSessionState"/> class.
|
|
/// </summary>
|
|
public SmtpSessionState() {
|
|
this.DataBuffer = new List<Byte>();
|
|
this.Reset(true);
|
|
this.ResetAuthentication();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Gets the contents of the data buffer.
|
|
/// </summary>
|
|
public List<Byte> DataBuffer {
|
|
get; protected set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether this instance has initiated.
|
|
/// </summary>
|
|
public Boolean HasInitiated {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the current session supports extensions.
|
|
/// </summary>
|
|
public Boolean SupportsExtensions {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the client hostname.
|
|
/// </summary>
|
|
public String ClientHostname {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the session is currently receiving DATA.
|
|
/// </summary>
|
|
public Boolean IsInDataMode {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the sender address.
|
|
/// </summary>
|
|
public String SenderAddress {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the recipients.
|
|
/// </summary>
|
|
public List<String> Recipients { get; } = new List<String>();
|
|
|
|
/// <summary>
|
|
/// Gets or sets the extended data supporting any additional field for storage by a responder implementation.
|
|
/// </summary>
|
|
public Object ExtendedData {
|
|
get; set;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AUTH State
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether this instance is in authentication mode.
|
|
/// </summary>
|
|
public Boolean IsInAuthMode {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the username.
|
|
/// </summary>
|
|
public String Username {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the password.
|
|
/// </summary>
|
|
public String Password {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether this instance has provided username.
|
|
/// </summary>
|
|
public Boolean HasProvidedUsername => String.IsNullOrWhiteSpace(this.Username) == false;
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether this instance is authenticated.
|
|
/// </summary>
|
|
public Boolean IsAuthenticated {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the authentication mode.
|
|
/// </summary>
|
|
public String AuthMode {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether this instance is channel secure.
|
|
/// </summary>
|
|
public Boolean IsChannelSecure {
|
|
get; set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the authentication state.
|
|
/// </summary>
|
|
public void ResetAuthentication() {
|
|
this.Username = String.Empty;
|
|
this.Password = String.Empty;
|
|
this.AuthMode = String.Empty;
|
|
this.IsInAuthMode = false;
|
|
this.IsAuthenticated = false;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Resets the data mode to false, clears the recipients, the sender address and the data buffer.
|
|
/// </summary>
|
|
public void ResetEmail() {
|
|
this.IsInDataMode = false;
|
|
this.Recipients.Clear();
|
|
this.SenderAddress = String.Empty;
|
|
this.DataBuffer.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the state table entirely.
|
|
/// </summary>
|
|
/// <param name="clearExtensionData">if set to <c>true</c> [clear extension data].</param>
|
|
public void Reset(Boolean clearExtensionData) {
|
|
this.HasInitiated = false;
|
|
this.SupportsExtensions = false;
|
|
this.ClientHostname = String.Empty;
|
|
this.ResetEmail();
|
|
|
|
if(clearExtensionData) {
|
|
this.ExtendedData = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new object that is a copy of the current instance.
|
|
/// </summary>
|
|
/// <returns>A clone.</returns>
|
|
public virtual SmtpSessionState Clone() {
|
|
SmtpSessionState clonedState = this.CopyPropertiesToNew<SmtpSessionState>(new[] { nameof(this.DataBuffer) });
|
|
clonedState.DataBuffer.AddRange(this.DataBuffer);
|
|
clonedState.Recipients.AddRange(this.Recipients);
|
|
|
|
return clonedState;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |