예제 #1
0
        public async Task <IActionResult> GenerateEmailSentence(string messageId, bool isSpam)
        {
            var(mail, uniqueId) = await _mailKitRepository.GetUnreadEmailByIdAsync(messageId).ConfigureAwait(false);

            var headerEmailSentence = new StringBuilder();

            var authorizationResultsHeaders       = mail.Headers.Where(header => header.Field == "Authentication-Results");
            var authorizationResultsHeadersValues = authorizationResultsHeaders.Select(authorizationResultsHeader => authorizationResultsHeader.Value).ToList();

            var senderPolicyFrameworkProtocolsResults    = _informationExtractorService.GetSenderPolicyFrameworkResults(authorizationResultsHeadersValues);
            var domainKeysIdentifiedMailProtocolsResults = _informationExtractorService.GetDomainKeysIdentifiedMailResults(authorizationResultsHeadersValues);
            var domainBasedMessageAuthenticationReportingConformanceProtocolsResults = _informationExtractorService.GetDomainBasedMessageAuthenticationReportingConformanceResults(authorizationResultsHeadersValues);

            //Add attachments info
            foreach (var attachment in mail.Attachments)
            {
                if (attachment.ContentDisposition.FileName != null)
                {
                    var file          = attachment.ContentDisposition.FileName;
                    var fileSize      = attachment.ContentDisposition.Size;
                    var fileName      = Path.GetFileName(file).ToLower();
                    var fileExtension = Path.GetExtension(file).ToLower();
                    headerEmailSentence.Append($"attachment filename {fileName} extension {fileExtension} size {fileSize} ");
                }
            }

            //Add protocol result info
            foreach (var protocolResult in senderPolicyFrameworkProtocolsResults)
            {
                headerEmailSentence.Append($"spfProtocol result {protocolResult} ");
            }

            foreach (var protocolResult in domainKeysIdentifiedMailProtocolsResults)
            {
                headerEmailSentence.Append($"dkimProtocol result {protocolResult} ");
            }

            foreach (var protocolResult in domainBasedMessageAuthenticationReportingConformanceProtocolsResults)
            {
                headerEmailSentence.Append($"dmarcProtocol result {protocolResult} ");
            }

            //Add body info
            var bodyHtmlText = mail.HtmlBody;

            var uris = Regex
                       .Matches(bodyHtmlText, @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)", RegexOptions.IgnoreCase)
                       .Select(match => match.Value)
                       .ToList();

            var subjectSentence = _textPreparerService.Prepare(mail.Subject).ToList();
            var subjectLabel    = Convert.ToInt32(isSpam);

            var headerSentence = _textPreparerService.Prepare(headerEmailSentence.ToString()).ToList();
            var headerLabel    = Convert.ToInt32(isSpam);

            var bodySentence = _textPreparerService.Prepare(GetBodyText(bodyHtmlText)).ToList();
            var bodyLabel    = Convert.ToInt32(isSpam);

            await _grannyRepository.AddSubjectArray(subjectSentence, subjectLabel).ConfigureAwait(false);

            await _grannyRepository.AddHeaderArray(headerSentence, headerLabel).ConfigureAwait(false);

            await _grannyRepository.AddBodyArray(bodySentence, bodyLabel).ConfigureAwait(false);

            foreach (var uri in uris)
            {
                await _grannyRepository.AddUri(uri, (int)UriEvaluationCategory.NotEvaluated).ConfigureAwait(false);
            }

            await _mailKitRepository.MarkEmailAsSeen(uniqueId).ConfigureAwait(false);

            var trainedEmailAddedResultViewModel = new TrainedEmailAddedResultViewModel()
            {
                SubjectSentence = subjectSentence,
                HeaderSentence  = headerSentence,
                BodySentence    = bodySentence,
                Uris            = uris
            };

            return(View(trainedEmailAddedResultViewModel));
        }