Exemple #1
0
        /// <summary>
        /// Handels the STAT command.
        /// </summary>
        private void Stat(Pop3Context context)
        {
            // TODO: wrong implementation, should only return unread messages
            if (context.LastCommand >= COMMAND_PASS)
            {
                Pop3MessageInfo[] list = new Pop3MessageInfo[0];

                if (_storage != null)
                {
                    list = _storage.GetMessageOverview(context.Username);
                }

                long mailboxSize = 0;

                foreach (Pop3MessageInfo l in list)
                {
                    mailboxSize += l.Size;
                }

                context.WriteLine(String.Format(MESSAGE_STAT, list.Length, mailboxSize));
                context.LastCommand = COMMAND_STAT;
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #2
0
        /// <summary>
        /// Handels the PASS command.
        /// </summary>
        private void Pass(Pop3Context context, String[] inputs)
        {
            if (context.LastCommand == COMMAND_USER)
            {
                if (inputs.Length == 2)
                {
                    IMailStorage mail = (_storage != null ? _storage as IMailStorage : null);

                    if (mail == null || mail.Login(context.Username, inputs[1]) == true)
                    {
                        context.Password    = inputs[1];
                        context.LastCommand = COMMAND_PASS;
                        context.WriteLine(MESSAGE_PASSWORD_OK);
                    }
                    else
                    {
                        context.Username    = null;
                        context.Password    = null;
                        context.LastCommand = -1;
                        context.WriteLine(MESSAGE_PASSWORD_INVALID);
                    }
                }
                else
                {
                    context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #3
0
        /// <summary>
        /// Reset the connection state.
        /// </summary>
        private void Rset(Pop3Context context)
        {
            if (context.LastCommand != -1)
            {
                if (context != null && !String.IsNullOrEmpty(context.Username))
                {
                    IMailStorage mail = (_storage != null ? _storage as IMailStorage : null);

                    if (mail != null)
                    {
                        mail.Logout(context.Username);
                    }
                }

                context.Reset();
                context.Username    = null;
                context.Password    = null;
                context.LastCommand = -1;

                context.WriteLine(MESSAGE_OK);
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #4
0
 /// <summary>
 /// Handels the CAPA command.
 /// </summary>
 private void Capa(Pop3Context context)
 {
     context.WriteLine(MESSAGE_OK);
     context.WriteLine("TOP");
     context.WriteLine("UIDL");
     context.WriteLine("USER");
     context.WriteLine(MESSAGE_EOF);
 }
Exemple #5
0
        public void ProcessConnection()
        {
            Stream stream;

            if (_server.IsSecure && _server.Certificate != null)
            {
                SslStream ssl = null;

                try
                {
#if (!MF)
                    ssl = new SslStream(new NetworkStream(_socket));
                    ssl.AuthenticateAsServer(_server.Certificate, false, SslProtocols.Default, false);
#else
                    ssl = new SslStream(_socket);
                    ssl.AuthenticateAsServer(_server.Certificate, SslVerification.NoVerification, SslProtocols.Default);
#endif
                    stream = ssl;
                }
                catch (Exception)
                {
                    //Close();
                    return;
                }
            }
            else
            {
                stream = new NetworkStream(_socket);
            }

            Pop3Context context = new Pop3Context(stream, (IPEndPoint)_socket.LocalEndPoint, (IPEndPoint)_socket.RemoteEndPoint);

            try
            {
                if (_storage != null && !_storage.AcceptClient(context.RemoteEndPoint))
                {
                    throw new Exception("Client not accepted!");
                }

                SendWelcomeMessage(context);

                ProcessCommands(context);
            }
            catch (Exception)
            {
                if (_socket.Connected == true)
                {
                    context.WriteLine("-ERR service not available");
                }
            }
            finally
            {
                _socket.Close();

                _socket = null;
                context = null;

                System.GC.Collect();
            }
        }
Exemple #6
0
        private void SendWelcomeMessage(Pop3Context context)
        {
#if (LOG && !MF && !WindowsCE)
            Console.WriteLine("*** Remote IP: {0} ***", context.RemoteEndPoint);
#endif


            context.WriteLine(MESSAGE_DEFAULT_WELCOME);
        }
Exemple #7
0
 /// <summary>
 /// Handels the USER command.
 /// </summary>
 private void User(Pop3Context context, String[] inputs)
 {
     if (context.LastCommand == -1)
     {
         if (inputs.Length == 2)
         {
             context.Username    = inputs[1];
             context.LastCommand = COMMAND_USER;
             context.WriteLine(MESSAGE_USER_OK);
         }
         else
         {
             context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
         }
     }
     else
     {
         context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
     }
 }
Exemple #8
0
        /// <summary>
        /// Handels the DELE command.
        /// </summary>
        private void Dele(Pop3Context context, String[] inputs)
        {
            if (context.LastCommand >= COMMAND_STAT)
            {
                if (inputs.Length == 2)
                {
                    int idx = Convert.ToInt32(inputs[1]);

                    _storage.DeleteMessage(context.Username, idx);

                    context.WriteLine(String.Format(MESSAGE_DELE, idx));
                    context.LastCommand = COMMAND_DELE;
                }
                else
                {
                    context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #9
0
        /// <summary>
        /// Handels the RETR command.
        /// </summary>
        private void Retr(Pop3Context context, String[] inputs)
        {
            if (context.LastCommand >= COMMAND_STAT)
            {
                if (inputs.Length == 2)
                {
                    int idx = Convert.ToInt32(inputs[1]);

                    context.WriteLine(MESSAGE_RETR);
                    context.WriteLine(_storage.ReadMessage(context.Username, idx));
                    context.WriteLine(MESSAGE_EOF);
                    context.LastCommand = COMMAND_RETR;
                }
                else
                {
                    context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #10
0
        /// <summary>
        /// Handels the TOP command.
        /// </summary>
        private void Top(Pop3Context context, String[] inputs)
        {
            if (context.LastCommand >= COMMAND_STAT)
            {
                if (inputs.Length == 3)
                {
                    int idx         = Convert.ToInt32(inputs[1]);
                    int linesToRead = Convert.ToInt32(inputs[2]);

                    context.WriteLine(MESSAGE_RETR);

                    string message = _storage.ReadMessage(context.Username, idx);

                    bool foundBody = false;
                    int  lines     = 0;

                    foreach (string line in message.Split(new string[] { "\r\n" }, 100, StringSplitOptions.None))
                    {
                        if (line.Length == 0)
                        {
                            foundBody = true;
                        }

                        if (!foundBody)
                        {
                            context.WriteLine(line);
                        }
                        else
                        {
                            if (lines < linesToRead)
                            {
                                context.WriteLine(line);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    context.WriteLine(MESSAGE_EOF);
                    context.LastCommand = COMMAND_RETR;
                }
                else
                {
                    context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #11
0
        /// <summary>
        /// Handels the UIDL command.
        /// </summary>
        private void Uidl(Pop3Context context, String[] inputs)
        {
            if (context.LastCommand >= COMMAND_PASS)
            {
                if (inputs.Length > 2)
                {
                    context.WriteLine(MESSAGE_INVALID_ARGUMENT_COUNT);
                }
                else
                {
                    int idx = 0;

                    if (inputs.Length == 2)
                    {
                        idx = Convert.ToInt32(inputs[1]);
                    }

                    Pop3MessageInfo[] list = new Pop3MessageInfo[0];

                    if (_storage != null)
                    {
                        list = _storage.GetMessageOverview(context.Username);
                    }

                    if (inputs.Length == 1)
                    {
                        context.WriteLine(String.Format(MESSAGE_LIST, list.Length));

                        int id = 1;
                        foreach (Pop3MessageInfo l in list)
                        {
                            context.WriteLine(id + " " + l.UniqueIdentifier);
                            id++;
                        }

                        context.WriteLine(MESSAGE_EOF);
                    }
                    else
                    {
                        context.WriteLine(string.Format(MESSAGE_STAT, idx, list[idx - 1].UniqueIdentifier));
                    }

                    context.LastCommand = COMMAND_LIST;
                }
            }
            else
            {
                context.WriteLine(MESSAGE_INVALID_COMMAND_ORDER);
            }
        }
Exemple #12
0
        private void ProcessCommands(Pop3Context context)
        {
            bool   isRunning = true;
            String inputLine;

            while (isRunning)
            {
                try
                {
                    inputLine = context.ReadLine();

                    String[] inputs = inputLine.Split(' ');

                    switch (inputs[0].ToLower())
                    {
                    case "user":
                        User(context, inputs);
                        break;

                    case "pass":
                        Pass(context, inputs);
                        break;

                    case "rset":
                        Rset(context);
                        break;

                    case "stat":
                        Stat(context);
                        break;

                    case "list":
                        List(context, inputs);
                        break;

                    case "uidl":
                        Uidl(context, inputs);
                        break;

                    case "retr":
                        Retr(context, inputs);
                        break;

                    case "top":
                        Top(context, inputs);
                        break;

                    case "dele":
                        Dele(context, inputs);
                        break;

                    case "quit":
                        isRunning = false;

                        context.WriteLine(MESSAGE_GOODBYE);
                        context.Close();
                        break;

                    case "capa":
                        Capa(context);
                        break;

                    case "auth":
                    default:
                        context.WriteLine(MESSAGE_UNKNOWN_COMMAND);
                        break;
                    }
                }
                catch (Exception)
                {
                    isRunning = false;

                    context.WriteLine(MESSAGE_SYSTEM_ERROR);
                    context.Close();
                }
            }
        }