Ejemplo 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 TranslateFromAndToQuery()
        {
            using (var client = new ImapClient()) {
                var translator = new ImapQueryTranslator();
                var expression = client.Messages.Where(x => x.From.Contains("Foo") && x.To.Contains("Bar")).Expression;

                const string expected = "SEARCH FROM \"Foo\" TO \"Bar\"";

                var result = translator.Translate(expression);
                Assert.IsFalse(result.IsUid);
                Assert.AreEqual(expected, result.SearchCommand);
            }
        }
Ejemplo n.º 3
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 TranslateHasHeaderField()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => x.Headers.Any(y => y.Name.Contains("Foo") && y.Value.Contains("Bar"))).Expression;
            }

            const string expected = "SEARCH HEADER \"Foo\" \"Bar\"";

            var result = target.Translate(expression);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchUnseenTextKeyword()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => !x.Flags.HasFlag(MessageFlags.Seen)
                                                        && x.Text.Contains("Peter File")
                                                        && x.Keywords.Contains("Cheers")
                                                        && !x.Keywords.Contains("Cheerio")).Expression;
            }

            const string expected = "SEARCH NOT SEEN TEXT \"Peter File\" KEYWORD Cheers NOT KEYWORD Cheerio";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchUnseen()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => !x.Flags.HasFlag(MessageFlags.Seen)).Expression;
            }

            const string expected = "SEARCH NOT SEEN";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchSeenOperator()
        {
            try {
                using (var client = new ImapClient()) {
                    var target = new ImapQueryTranslator();
                    var expression = client.Messages.Where(x => x.Flags == MessageFlags.Seen).Expression;

                    target.Translate(expression);
                }
                Assert.Fail();
            }
            catch (NotSupportedException) {
                Assert.IsTrue(true);
            }
        }
        public void TranslateTestSearchIsOldAndNotIsNew()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => x.IsNew && !x.IsOld).Expression;
            }

            const string expected = "SEARCH (RECENT UNSEEN) NOT NOT RECENT";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateTestSearchDateSinceSeenOrAnswered()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                var date = DateTime.Parse("1-Feb-1994");
                target = new ImapQueryTranslator();

                expression = client.Messages.Where(x => x.Date < date && (x.Flags.HasFlag(MessageFlags.Seen) || x.Flags.HasFlag(MessageFlags.Answered))).Expression;

                // we need to evaluate the field date into a constant.
                expression = Evaluator.PartialEval(expression);
            }

            const string expected = "SEARCH SENTBEFORE 1-Feb-1994 OR SEEN ANSWERED";

            var result = target.Translate(expression);
            Assert.IsFalse(result.IsUid);
            Assert.AreEqual(expected, result.SearchCommand);
        }
        public void TranslateSingleUidQuery()
        {
            using (var client = new ImapClient()) {
                var translator = new ImapQueryTranslator();
                var expression = client.Messages.Where(x => x.Uid == 5).Expression;

                const string expected = "UID SEARCH UID 5";

                var result = translator.Translate(expression);
                Assert.IsTrue(result.IsUid);
                Assert.AreEqual(expected, result.SearchCommand);
            }
        }
        public void TranslateSingleSequenceNumberQuery()
        {
            using (var client = new ImapClient()) {
                var translator = new ImapQueryTranslator();
                var expression = client.Messages.Where(x => x.SequenceNumber == 5).Expression;

                const string expected = "SEARCH 5";

                var result = translator.Translate(expression);
                Assert.IsFalse(result.IsUid);
                Assert.AreEqual(expected, result.SearchCommand);
            }
        }
        public void TranslateSeperatedUidQueriesOpenEnd()
        {
            ImapQueryTranslator target;
            Expression expression;

            using (var client = new ImapClient()) {
                target = new ImapQueryTranslator();
                expression = client.Messages.Where(x => x.Uid >= 1).Expression;
            }

            const string expected = "UID SEARCH UID 1:*";

            var resuult = target.Translate(expression);
            Assert.AreEqual(expected, resuult.SearchCommand);
        }
 public void TranslateNegativeOperatorSeenFlags()
 {
     using (var client = new ImapClient()) {
         var translator = new ImapQueryTranslator();
         var expression = client.Messages.Where(x => x.Flags == MessageFlags.Seen).Expression;
         SearchTranslationResult result = null;
         try {
             result = translator.Translate(expression);
             Assert.Fail("This call must throw a NotSupportedException.");
         }
         catch (NotSupportedException) {
             Assert.IsTrue(true);
             if (result != null) {
                 Assert.IsFalse(result.IsUid);
             }
         }
     }
 }