Beispiel #1
0
 public void ApplyStreamFilter(Func <Stream, Stream> filter)
 {
     ConnectionChannel.ApplyStreamFilter(filter);
 }
Beispiel #2
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);
        }