Exemplo n.º 1
0
        public void SendGreeting(NetworkStream stream)
        {
            SMTP_Message messg = new SMTP_Message((int)States.service_ready, Dns.GetHostName(), "Service ready");

            byte[] msg = Encoding.ASCII.GetBytes(messg.ToString());
            StreamWrite(stream, msg, 0, msg.Length);
        }
Exemplo n.º 2
0
        public void StartSession(object session)
        {
            Console.WriteLine("New user has connected! ");
            Session       s          = (Session)session;
            TcpClient     tempClient = s.client;
            NetworkStream stream     = tempClient.GetStream();

            IMail mail;

            bool   disconnect = false;
            int    i          = 0;
            string data       = null;

            byte[] bytes = new byte[256];

            byte[] msg;
            SendGreeting(stream);

            StringBuilder tempSb = new StringBuilder();

            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0 && !disconnect)
            {
                //Logic here
                data = Encoding.ASCII.GetString(bytes, 0, i);
                //data = data.ToUpper();

                if (string.Compare(data, "/exit") == 0 || string.Compare(data, "/EXIT") == 0)
                {
                    Console.WriteLine("Disconnect");
                    disconnect = true;

                    s.Disconnect();
                }
                else
                {
                    ISMTP_Command command;
                    string        error    = null;
                    string        response = null;
                    if (s.waitingForCommand)
                    {
                        command = s.ParseCommand(data, ref error, ref response);
                        if (command == null && error != null)
                        {
                            msg = Encoding.ASCII.GetBytes(error);
                            StreamWrite(stream, msg, 0, msg.Length);
                        }
                        else if (command != null)
                        {
                            SMTP_Message m = command.Launch(s); // Launch command changes state of a session
                            msg = Encoding.ASCII.GetBytes(m.ToString());
                            StreamWrite(stream, msg, 0, msg.Length);
                        }
                        else if (command == null)
                        {
                            if (!(i == 2 && string.Compare(data, "\r\n") == 0) &&
                                !(i == 21 &&
                                  string.Compare(data, "??\u001f?? ??\u0018??'??\u0001??\u0003??\u0003") == 0))
                            {
                                SMTP_Message m = new SMTP_Message((int)States.command_not_implemented, "Bad command");
                                msg = Encoding.ASCII.GetBytes(m.ToString());
                                StreamWrite(stream, msg, 0, msg.Length);
                            }
                        }
                    }
                    else if (s.done)
                    {
                        s.Disconnect();
                    }
                    else
                    {
                        if (i == 1)
                        {
                            if (data[0] == '.')
                            {
                                s.mailData = tempSb.ToString();
                                Console.Write(s.mailData);
                                s.waitingForCommand = true;
                                SMTP_Message m = new SMTP_Message((int)States.action_ok, "Message accepted for delivery.");
                                msg = Encoding.ASCII.GetBytes(m.ToString());
                                StreamWrite(stream, msg, 0, msg.Length);

                                MailTypeResolver mtr = new MailTypeResolver();

                                foreach (string str in s.forwardPath)
                                {
                                    try
                                    {
                                        mail = mtr.GetMailType(str);
                                        mail.Send(s.reversePath, str, s.mailData);
                                    }
                                    catch (NotImplementedException)
                                    {
                                        m   = new SMTP_Message((int)States.command_not_implemented, "Can't send to outside domains");
                                        msg = Encoding.ASCII.GetBytes(m.ToString());
                                        StreamWrite(stream, msg, 0, msg.Length);
                                    }
                                }
                            }
                        }
                        tempSb.Append(data);
                    }
                }
            }
            tempClient.Close();
        }