Esempio n. 1
0
        public override object Execute(Expression expression)
        {
            // reduce expressions to constants where possible
            expression = Evaluator.PartialEval(expression);

            var crawler          = new ExpressionCrawler();
            var searchExpression = crawler.FindSearchLambda(expression);
            var fetchExpression  = crawler.FindFetchLamda(expression);

            // the search string sequence
            string sequence;

            var searchTranslationResult = new ImapQueryTranslator().Translate(searchExpression);

            var isSearchRequired = searchTranslationResult.ValidateSearchNecessity();

            if (isSearchRequired)
            {
                var searchCommand = new ImapCommand(searchTranslationResult.SearchCommand);
                var response      = _client.SendAndReceive(searchCommand);

                var success = TryExtractIds(response, out sequence);
                if (!success)
                {
                    // no messages match the search criteria therefor we don't need to fetch anything
                    // we return an empty list
                    return(Activator.CreateInstance(typeof(List <>).MakeGenericType(fetchExpression.ReturnType), null));
                }
            }
            else
            {
                sequence = searchTranslationResult.ConvertCommandToSequence();
            }


            // There are no id's if no message matches the search criterias.
            var fetchTranslator = new ImapFetchQueryTranslator {
                IsUid = searchTranslationResult.IsUid, UsePeek = _client.UsePeek
            };

            ResponseProcessor processor;
            var fetchString   = fetchTranslator.Translate(fetchExpression, sequence, out processor);
            var fetchCommand  = new ImapCommand(fetchString);
            var fetchResponse = _client.SendAndReceive(fetchCommand);

            return(Activator.CreateInstance(
                       typeof(ImapQueryResponseReader <>).MakeGenericType(new[] { fetchExpression.ReturnType }),
                       BindingFlags.Instance | BindingFlags.Public,
                       null,
                       new object[] { fetchResponse, processor },
                       CultureInfo.CurrentCulture));
        }
        public void TranslateFetchRecursiveBodyPartsTest()
        {
            using (var client = new ImapClient()) {
                var query = client.Messages.Where(x => x.SequenceNumber == 1).Select(x => new SampleContainer {MyText = (string) x.Parts("1.Text"), MyPart = (string) x.Parts("1.2")});

                ResponseProcessor projector;
                var translator = new ImapFetchQueryTranslator();
                var expression = new ExpressionCrawler().FindFetchLamda(query.Expression);
                var actual = translator.Translate(expression, "1:10", out projector);
                const string expected = "FETCH 1:10 (BODY[1.TEXT] BODY[1.2])";
                Assert.AreEqual(expected, actual);
            }
        }
        public override object Execute(Expression expression)
        {
            // reduce expressions to constants where possible
            expression = Evaluator.PartialEval(expression);

            var crawler = new ExpressionCrawler();
            var searchExpression = crawler.FindSearchLambda(expression);
            var fetchExpression = crawler.FindFetchLamda(expression);

            // the search string sequence
            string sequence;

            var searchTranslationResult = new ImapQueryTranslator().Translate(searchExpression);

            var isSearchRequired = searchTranslationResult.ValidateSearchNecessity();
            if (isSearchRequired) {
                var searchCommand = new ImapCommand(searchTranslationResult.SearchCommand);
                var response = _client.SendAndReceive(searchCommand);

                var success = TryExtractIds(response, out sequence);
                if (!success) {
                    // no messages match the search criteria therefor we don't need to fetch anything
                    // we return an empty list
                    return Activator.CreateInstance(typeof (List<>).MakeGenericType(fetchExpression.ReturnType), null);
                }
            } else {
                sequence = searchTranslationResult.ConvertCommandToSequence();
            }

            // There are no id's if no message matches the search criterias.
            var fetchTranslator = new ImapFetchQueryTranslator {IsUid = searchTranslationResult.IsUid, UsePeek = _client.UsePeek};

            ResponseProcessor processor;
            var fetchString = fetchTranslator.Translate(fetchExpression, sequence, out processor);
            var fetchCommand = new ImapCommand(fetchString);
            var fetchResponse = _client.SendAndReceive(fetchCommand);

            return Activator.CreateInstance(
                typeof (ImapQueryResponseReader<>).MakeGenericType(new[] {fetchExpression.ReturnType}),
                BindingFlags.Instance | BindingFlags.Public,
                null,
                new object[] {fetchResponse, processor},
                CultureInfo.CurrentCulture);
        }
 public void TranslateFetchSelectTest()
 {
     using (var client = new ImapClient()) {
         var query = client.Messages.Where(x => x.SequenceNumber == 1).Select(x => new SampleContainer {MySubject = x.Subject});
         ResponseProcessor projector;
         var translator = new ImapFetchQueryTranslator();
         var expression = new ExpressionCrawler().FindFetchLamda(query.Expression);
         var actual = translator.Translate(expression, "1:10", out projector);
         const string expected = "FETCH 1:10 (BODY[HEADER.FIELDS (Subject)])";
         Assert.AreEqual(expected, actual);
     }
 }