forked from cosullivan/SmtpServer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEhloCommand.cs
More file actions
104 lines (89 loc) · 3.69 KB
/
Copy pathEhloCommand.cs
File metadata and controls
104 lines (89 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SmtpServer.Authentication;
using SmtpServer.IO;
namespace SmtpServer.Protocol
{
/// <summary>
/// Ehlo Command
/// </summary>
public class EhloCommand : SmtpCommand
{
/// <summary>
/// Smtp Ehlo Command
/// </summary>
public const string Command = "EHLO";
/// <summary>
/// Constructor.
/// </summary>
/// <param name="domainOrAddress">The domain name or address literal.</param>
public EhloCommand(string domainOrAddress) : base(Command)
{
DomainOrAddress = domainOrAddress;
}
/// <summary>
/// Execute the command.
/// </summary>
/// <param name="context">The execution context to operate on.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Returns true if the command executed successfully such that the transition to the next state should occurr, false
/// if the current state is to be maintained.</returns>
internal override async Task<bool> ExecuteAsync(SmtpSessionContext context, CancellationToken cancellationToken)
{
var output = new[] { GetGreeting(context) }.Union(GetExtensions(context)).ToArray();
for (var i = 0; i < output.Length - 1; i++)
{
context.Pipe.Output.WriteLine($"250-{output[i]}");
}
context.Pipe.Output.WriteLine($"250 {output[output.Length - 1]}");
await context.Pipe.Output.FlushAsync(cancellationToken).ConfigureAwait(false);
return true;
}
/// <summary>
/// Returns the greeting to display to the remote host.
/// </summary>
/// <param name="context">The session context.</param>
/// <returns>The greeting text to display to the remote host.</returns>
protected virtual string GetGreeting(ISessionContext context)
{
return $"{context.ServerOptions.ServerName} Hello {DomainOrAddress}, haven't we met before?";
}
/// <summary>
/// Returns the list of extensions that are current for the context.
/// </summary>
/// <param name="context">The session context.</param>
/// <returns>The list of extensions that are current for the context.</returns>
protected virtual IEnumerable<string> GetExtensions(ISessionContext context)
{
yield return "PIPELINING";
yield return "8BITMIME";
yield return "SMTPUTF8";
if (context.Pipe.IsSecure == false && context.EndpointDefinition.CertificateFactory != null)
{
yield return "STARTTLS";
}
if (context.ServerOptions.MaxMessageSizeOptions.Length > 0)
{
yield return $"SIZE {context.ServerOptions.MaxMessageSizeOptions.Length}";
}
if (IsPlainLoginAllowed(context))
{
yield return "AUTH PLAIN LOGIN";
}
static bool IsPlainLoginAllowed(ISessionContext context)
{
if (context.ServiceProvider.GetService(typeof(IUserAuthenticatorFactory)) == null && context.ServiceProvider.GetService(typeof(IUserAuthenticator)) == null)
{
return false;
}
return context.Pipe.IsSecure || context.EndpointDefinition.AllowUnsecureAuthentication;
}
}
/// <summary>
/// Gets the domain name or address literal.
/// </summary>
public string DomainOrAddress { get; }
}
}