Exemple #1
0
        public void Process(IConnection connection, SmtpCommand command)
        {
            SmtpCommand subrequest    = new SmtpCommand(command.ArgumentsText);
            IVerb       verbProcessor = SubVerbMap.GetVerbProcessor(subrequest.Verb);

            if (verbProcessor != null)
            {
                verbProcessor.Process(connection, subrequest);
            }
            else
            {
                connection.WriteResponse(
                    new SmtpResponse(StandardSmtpResponseCode.CommandParameterNotImplemented,
                                     "Subcommand {0} not implemented", subrequest.Verb));
            }
        }
        /// <summary>
        /// Dispatches a command to the registered sub command matching the next verb in the command
        /// or writes an error to the client is no match was found.
        /// </summary>
        /// <param name="connection">The connection<see cref="Rnwood.SmtpServer.IConnection" />.</param>
        /// <param name="command">The command<see cref="Rnwood.SmtpServer.SmtpCommand" />.</param>
        /// <returns>
        /// A <see cref="System.Threading.Tasks.Task" /> representing the async operation.
        /// </returns>
        public virtual async Task Process(IConnection connection, SmtpCommand command)
        {
            SmtpCommand subrequest    = new SmtpCommand(command.ArgumentsText);
            IVerb       verbProcessor = this.SubVerbMap.GetVerbProcessor(subrequest.Verb);

            if (verbProcessor != null)
            {
                await verbProcessor.Process(connection, subrequest).ConfigureAwait(false);
            }
            else
            {
                await connection.WriteResponse(
                    new SmtpResponse(
                        StandardSmtpResponseCode.CommandParameterNotImplemented,
                        "Subcommand {0} not implemented",
                        subrequest.Verb)).ConfigureAwait(false);
            }
        }
Exemple #3
0
        public void Start()
        {
            try
            {
                Server.Behaviour.OnSessionStarted(this, Session);

                if (Server.Behaviour.IsSSLEnabled(this))
                {
                    SslStream sslStream = new SslStream(_stream);
                    sslStream.AuthenticateAsServer(Server.Behaviour.GetSSLCertificate(this));
                    Session.SecureConnection = true;
                    _stream = sslStream;
                    SetupReaderAndWriter();
                }

                WriteResponse(new SmtpResponse(StandardSmtpResponseCode.ServiceReady,
                                               Server.Behaviour.DomainName + " smtp4dev ready"));

                while (_tcpClient.Client.Connected)
                {
                    SmtpCommand command = new SmtpCommand(ReadLine());
                    Server.Behaviour.OnCommandReceived(this, command);

                    if (command.IsValid)
                    {
                        IVerb verbProcessor = VerbMap.GetVerbProcessor(command.Verb);

                        if (verbProcessor != null)
                        {
                            try
                            {
                                verbProcessor.Process(this, command);
                            }
                            catch (SmtpServerException exception)
                            {
                                WriteResponse(exception.SmtpResponse);
                            }
                        }
                        else
                        {
                            WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorCommandUnrecognised,
                                                           "Command unrecognized"));
                        }
                    }
                    else if (command.IsEmpty)
                    {
                    }
                    else
                    {
                        WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorCommandUnrecognised,
                                                       "Command unrecognized"));
                    }
                }
            }
            catch (IOException ioException)
            {
                Session.SessionError = ioException.Message;
            }

            CloseConnection();

            Session.EndDate = DateTime.Now;
            Server.Behaviour.OnSessionCompleted(this, Session);
        }
Exemple #4
0
        /// <summary>
        /// Starts processing of this connection.
        /// </summary>
        /// <returns>A <see cref="Task{T}"/> representing the async operation.</returns>
        internal async Task ProcessAsync()
        {
            try
            {
                await this.Server.Behaviour.OnSessionStarted(this, this.Session).ConfigureAwait(false);

                if (await this.Server.Behaviour.IsSSLEnabled(this).ConfigureAwait(false))
                {
                    await this.ConnectionChannel.ApplyStreamFilter(async s =>
                    {
                        SslStream sslStream = new SslStream(s);
                        await sslStream.AuthenticateAsServerAsync(await this.Server.Behaviour.GetSSLCertificate(this).ConfigureAwait(false)).ConfigureAwait(false);
                        return(sslStream);
                    }).ConfigureAwait(false);

                    this.Session.SecureConnection = true;
                }

                await this.WriteResponse(new SmtpResponse(
                                             StandardSmtpResponseCode.ServiceReady,
                                             this.Server.Behaviour.DomainName + " smtp4dev ready")).ConfigureAwait(false);

                int numberOfInvalidCommands = 0;
                while (this.ConnectionChannel.IsConnected)
                {
                    bool        badCommand = false;
                    SmtpCommand command    = new SmtpCommand(await this.ReadLine().ConfigureAwait(false));
                    await this.Server.Behaviour.OnCommandReceived(this, command).ConfigureAwait(false);

                    if (command.IsValid)
                    {
                        IVerb verbProcessor = this.VerbMap.GetVerbProcessor(command.Verb);

                        if (verbProcessor != null)
                        {
                            try
                            {
                                await verbProcessor.Process(this, command).ConfigureAwait(false);
                            }
                            catch (SmtpServerException exception)
                            {
                                await this.WriteResponse(exception.SmtpResponse).ConfigureAwait(false);
                            }
                        }
                        else
                        {
                            badCommand = true;
                        }
                    }
                    else if (command.IsEmpty)
                    {
                    }
                    else
                    {
                        badCommand = true;
                    }

                    if (badCommand)
                    {
                        numberOfInvalidCommands++;

                        if (this.Server.Behaviour.MaximumNumberOfSequentialBadCommands > 0 &&
                            numberOfInvalidCommands >= this.Server.Behaviour.MaximumNumberOfSequentialBadCommands)
                        {
                            await this.WriteResponse(new SmtpResponse(StandardSmtpResponseCode.ClosingTransmissionChannel, "Too many bad commands. Bye!")).ConfigureAwait(false);

                            await this.CloseConnection().ConfigureAwait(false);
                        }
                        else
                        {
                            await this.WriteResponse(new SmtpResponse(
                                                         StandardSmtpResponseCode.SyntaxErrorCommandUnrecognised,
                                                         "Command unrecognised")).ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (IOException ioException)
            {
                this.Session.SessionError     = ioException;
                this.Session.SessionErrorType = SessionErrorType.NetworkError;
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception exception)
            {
                this.Session.SessionError     = exception;
                this.Session.SessionErrorType = SessionErrorType.UnexpectedException;
            }
#pragma warning restore CA1031 // Do not catch general exception types

            await this.CloseConnection().ConfigureAwait(false);

            this.Session.EndDate = DateTime.Now;
            await this.Server.Behaviour.OnSessionCompleted(this, this.Session).ConfigureAwait(false);
        }
Exemple #5
0
        public void Process()
        {
            Thread = System.Threading.Thread.CurrentThread;

            try
            {
                Server.Behaviour.OnSessionStarted(this, Session);
                SetReaderEncoding(Server.Behaviour.GetDefaultEncoding(this));

                if (Server.Behaviour.IsSSLEnabled(this))
                {
                    ConnectionChannel.ApplyStreamFilter(s =>
                    {
                        SslStream sslStream = new SslStream(s);
                        sslStream.AuthenticateAsServer(Server.Behaviour.GetSSLCertificate(this));
                        return(sslStream);
                    });

                    Session.SecureConnection = true;
                }

                WriteResponse(new SmtpResponse(StandardSmtpResponseCode.ServiceReady,
                                               Server.Behaviour.DomainName + " smtp4dev ready"));

                int numberOfInvalidCommands = 0;
                while (ConnectionChannel.IsConnected)
                {
                    bool        badCommand = false;
                    SmtpCommand command    = new SmtpCommand(ReadLine());
                    Server.Behaviour.OnCommandReceived(this, command);

                    if (command.IsValid)
                    {
                        IVerb verbProcessor = VerbMap.GetVerbProcessor(command.Verb);

                        if (verbProcessor != null)
                        {
                            try
                            {
                                verbProcessor.Process(this, command);
                            }
                            catch (SmtpServerException exception)
                            {
                                WriteResponse(exception.SmtpResponse);
                            }
                        }
                        else
                        {
                            badCommand = true;
                        }
                    }
                    else if (command.IsEmpty)
                    {
                    }
                    else
                    {
                        badCommand = true;
                    }

                    if (badCommand)
                    {
                        numberOfInvalidCommands++;

                        if (Server.Behaviour.MaximumNumberOfSequentialBadCommands > 0 &&
                            numberOfInvalidCommands >= Server.Behaviour.MaximumNumberOfSequentialBadCommands)
                        {
                            WriteResponse(new SmtpResponse(StandardSmtpResponseCode.ClosingTransmissionChannel, "Too many bad commands. Bye!"));
                            CloseConnection();
                        }
                        else
                        {
                            WriteResponse(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorCommandUnrecognised,
                                                           "Command unrecognised"));
                        }
                    }
                }
            }
            catch (IOException ioException)
            {
                Session.SessionError     = ioException;
                Session.SessionErrorType = SessionErrorType.NetworkError;
            }
            catch (ThreadInterruptedException exception)
            {
                Session.SessionError     = exception;
                Session.SessionErrorType = SessionErrorType.ServerShutdown;
            } catch (Exception exception)
            {
                Session.SessionError     = exception;
                Session.SessionErrorType = SessionErrorType.UnexpectedException;
            }

            CloseConnection();

            Session.EndDate = DateTime.Now;
            Server.Behaviour.OnSessionCompleted(this, Session);
        }