<commit_before>﻿using ExoMail.Smtp.Authentication;
using ExoMail.Smtp.Enums;
using ExoMail.Smtp.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ExoMail.Smtp.Protocol
{
    public class SmtpRcptCommand : SmtpCommandBase
    {

        public SmtpRcptCommand(string command, List<string> arguments)
        {
            Command = command;
            Arguments = arguments;
        }

        public override bool ArgumentsValid
        {
            get
            {
                return this.Arguments.Count == 1;
            }
        }

        public override async Task<string> GetResponseAsync()
        {
            return await Task.Run(() => GetResponse());
        }

        private string GetResponse()
        {
            string response;

            if (this.ArgumentsValid)
            {
                switch (this.SmtpSession.SessionState)
                {
                    case SessionState.EhloNeeded:
                        response = SmtpResponse.SendHelloFirst;
                        break;
                    case SessionState.StartTlsNeeded:
                        response = SmtpResponse.StartTlsFirst;
                        break;
                    case SessionState.MailNeeded:
                        response = SmtpResponse.SenderFirst;
                        break;
                    case SessionState.RcptNeeded:
                    case SessionState.DataNeeded:
                        response = GetRcptResponse();
                        break;
                    default:
                        response = SmtpResponse.BadCommand;
                        break;
                }

            }
            else
            {
                response = SmtpResponse.ArgumentUnrecognized;
            }

            return response;
        }
        private string GetRcptResponse()
        {
            string response;
            var regex = Regex.Match(this.Arguments[0], @"TO:<(.*)>", RegexOptions.IgnoreCase);
            var validFormat = regex.Success;
            var recipient = regex.Groups[1].Value;
            this.SmtpSession.MessageEnvelope.AddRecipient(recipient);

            if (validFormat)
            {
                if (UserManager.GetUserManager.IsValidRecipient(recipient))
                {
                    response = SetValidRecipient();
                }
                else if (this.SmtpSession.ServerConfig.IsAuthRelayAllowed)
                {
                    if (this.SmtpSession.IsAuthenticated)
                    {
                        response = SetValidRecipient();
                    }
                    else
                    {
                        response = SmtpResponse.UnableToRelay;
                    }
                }
                else
                {
                    response = SmtpResponse.MailboxUnavailable;
                }
            }
            else
            {
                response = SmtpResponse.InvalidRecipient;
            }
            return response;
        }

        private string SetValidRecipient()
        {
            string response;
            this.IsValid = true;
            this.SmtpSession.SessionState = SessionState.DataNeeded;
            this.SmtpSession.SmtpCommands.Add(this);
            response = SmtpResponse.RecipientOK;
            return response;
        }
    }


}<commit_msg>Implement size argument to RCPT command.<commit_after>﻿using ExoMail.Smtp.Authentication;
using ExoMail.Smtp.Enums;
using ExoMail.Smtp.Interfaces;
using ExoMail.Smtp.Utilities;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ExoMail.Smtp.Protocol
{
    public class SmtpRcptCommand : SmtpCommandBase
    {
        private IUserIdentity _user
        {
            get
            {
                return UserManager.GetUserManager.FindByEmailAddress(this._recipient);
            }
        }

        private bool _isMessageSizeOk
        {
            get
            {
                if (this._user == null)
                {
                    return this._messageSize <= this.SmtpSession.ServerConfig.MaxMessageSize;
                }
                else
                {
                    return this._messageSize <= this.SmtpSession.ServerConfig.MaxMessageSize &&
                        this._messageSize <= this._user.MaxMessageSize;
                }
            }
        }

        private int _messageSize { get; set; }
        private string _recipient { get; set; }

        private bool _isValidRecipient
        {
            get
            {
                return UserManager.GetUserManager.IsValidRecipient(_recipient);
            }
        }

        public SmtpRcptCommand(string command, List<string> arguments)
        {
            Command = command;
            Arguments = arguments;
        }

        public override bool ArgumentsValid
        {
            get
            {
                return this.Arguments.Count >= 1 && this.Arguments.Count <= 2;
            }
        }

        public override async Task<string> GetResponseAsync()
        {
            return await Task.Run(() => GetResponse());
        }

        private string GetResponse()
        {
            string response;

            if (this.ArgumentsValid)
            {
                switch (this.SmtpSession.SessionState)
                {
                    case SessionState.EhloNeeded:
                        response = SmtpResponse.SendHelloFirst;
                        break;

                    case SessionState.StartTlsNeeded:
                        response = SmtpResponse.StartTlsFirst;
                        break;

                    case SessionState.MailNeeded:
                        response = SmtpResponse.SenderFirst;
                        break;

                    case SessionState.RcptNeeded:
                    case SessionState.DataNeeded:
                        response = GetRcptResponse();
                        break;

                    default:
                        response = SmtpResponse.BadCommand;
                        break;
                }
            }
            else
            {
                response = SmtpResponse.ArgumentUnrecognized;
            }

            return response;
        }

        private string GetRcptResponse()
        {
            // Regex to capture the recipient Argument
            var emailRegex = Regex.Match(this.Arguments[0], @"TO:<(.*)>", RegexOptions.IgnoreCase);
            var validRecipientFormat = emailRegex.Success;
            int messageSize = 0;

            if (validRecipientFormat)
            {
                this._recipient = emailRegex.Groups[1].Value;

                if (this.Arguments.Count == 2)
                {
                    if (TryParseMessageSize(out messageSize))
                    {
                        this._messageSize = messageSize;
                    }
                    else
                    {
                        return SmtpResponse.ArgumentUnrecognized;
                    }
                }

                if (this._isValidRecipient)
                {
                    if (this._isMessageSizeOk)
                    {
                        return SetValidRecipient();
                    }
                    else
                    {
                        return SmtpResponse.MessageSizeExceeded;
                    }
                }
                else if (this.SmtpSession.ServerConfig.IsAuthRelayAllowed)
                {
                    return this.SmtpSession.IsAuthenticated ?
                         SetValidRecipient() : SmtpResponse.UnableToRelay;
                }
                else
                {
                    return SmtpResponse.MailboxUnavailable;
                }
            }
            else
            {
                return SmtpResponse.InvalidRecipient;
            }
        }

        private bool TryParseMessageSize(out int messageSize)
        {
            var sizeRegex = Regex.Match(this.Arguments[1], @"(SIZE)=(\d+)", RegexOptions.IgnoreCase);
            var isValidSizeArg = sizeRegex.Success;
            return int.TryParse(sizeRegex.Groups[2].Value, out messageSize) && isValidSizeArg;
        }

        private string SetValidRecipient()
        {
            string response;
            this.IsValid = true;
            this.SmtpSession.SessionState = SessionState.DataNeeded;
            this.SmtpSession.SmtpCommands.Add(this);
            this.SmtpSession.MessageEnvelope.AddRecipient(this._recipient);
            response = SmtpResponse.RecipientOK;
            return response;
        }
    }
}
{'fraction_non_alphanumeric': 0.067838910947249, 'fraction_numerical': 0.0012478729438457174, 'mean_word_length': 1.1507684801171016, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6599876', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2005, 'n_words': 364}