예제 #1
0
 private static void ValidateSizeRestriction(Restriction.SizeRestriction restriction)
 {
     if (!RestrictionEvaluator.IsOrderOperation(restriction.Op))
     {
         throw new InvalidRuleException(string.Format("Operation {0} is not supported in SizeRestriction", restriction.Op));
     }
 }
예제 #2
0
        private static bool EvaluateAttachmentRestriction(Restriction.AttachmentRestriction restriction, IRuleEvaluationContext context)
        {
            context.TraceFunction("AttachmentRestriction");
            context.NestedLevel++;
            bool flag = false;

            foreach (AttachmentHandle handle in context.Message.AttachmentCollection)
            {
                using (Attachment attachment = context.Message.AttachmentCollection.Open(handle))
                {
                    context.TraceFunction <string>("Attachment {0}", attachment.DisplayName);
                    using (IRuleEvaluationContext attachmentContext = context.GetAttachmentContext(attachment))
                    {
                        if (RestrictionEvaluator.Evaluate(restriction.Restriction, attachmentContext))
                        {
                            flag = true;
                            break;
                        }
                    }
                }
            }
            context.NestedLevel--;
            context.TraceFunction <bool>("AttachmentRestriction evaluates to {0}", flag);
            return(flag);
        }
예제 #3
0
 private static void ValidatePropertyRestriction(Restriction.PropertyRestriction restriction)
 {
     if (!RestrictionEvaluator.IsSupportedOperation(restriction.Op))
     {
         throw new InvalidRuleException(string.Format("Operation {0} is not supported", restriction.Op));
     }
     if (RuleUtil.IsMultiValueTag(restriction.PropValue.PropTag))
     {
         if (restriction.MultiValued)
         {
             throw new InvalidRuleException("At most one tag can be multi-valued");
         }
         if (restriction.Op != Restriction.RelOp.Equal && restriction.Op != Restriction.RelOp.NotEqual)
         {
             throw new InvalidRuleException("Restriction has multi-valued value only support Equal or NotEqual operation");
         }
     }
     if (!RuleUtil.IsSameType(restriction.PropTag, restriction.PropValue.PropTag))
     {
         throw new InvalidRuleException("Tag and value are of different type");
     }
     if (RuleUtil.IsBooleanTag(restriction.PropTag) && restriction.Op != Restriction.RelOp.Equal && restriction.Op != Restriction.RelOp.NotEqual)
     {
         throw new InvalidRuleException("Boolean tag only support Equal or NotEqual operation");
     }
     RestrictionEvaluator.ValidateRestrictionValue(restriction.PropValue);
 }
예제 #4
0
 private static bool MatchMultiValueWithPattern(IRuleEvaluationContext context, PropTag tag, object content, object pattern, ContentFlags flags)
 {
     if (RuleUtil.IsTextProp(tag))
     {
         string[] array    = (string[])content;
         string   pattern2 = (string)pattern;
         foreach (string content2 in array)
         {
             if (RestrictionEvaluator.MatchString(context.LimitChecker, context.CultureInfo, content2, pattern2, flags))
             {
                 return(true);
             }
         }
     }
     else if (RuleUtil.IsBinaryProp(tag))
     {
         byte[][] array3   = (byte[][])content;
         byte[]   pattern3 = pattern as byte[];
         foreach (byte[] content3 in array3)
         {
             if (RestrictionEvaluator.MatchByteArray(context.LimitChecker, content3, pattern3, flags))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
예제 #5
0
        internal static bool MatchString(LimitChecker limitChecker, CultureInfo cultureInfo, string content, string pattern, ContentFlags flags)
        {
            if (!limitChecker.CheckAndIncrementContentRestrictionCount(1, content))
            {
                return(false);
            }
            CompareOptions compareOptions = RestrictionEvaluator.GetCompareOptions(flags);
            ContentFlags   contentFlags   = flags & (ContentFlags.SubString | ContentFlags.Prefix);
            CompareInfo    compareInfo    = cultureInfo.CompareInfo;

            switch (contentFlags)
            {
            case ContentFlags.FullString:
                return(compareInfo.Compare(content, pattern, compareOptions) == 0);

            case ContentFlags.SubString:
                return(compareInfo.IndexOf(content, pattern, compareOptions) != -1);

            case ContentFlags.Prefix:
                return(compareInfo.IsPrefix(content, pattern, compareOptions));

            default:
                throw new InvalidRuleException(string.Format("Not supported content flags {0}", flags));
            }
        }
예제 #6
0
        private static bool EvaluatePropertyRestriction(Restriction.PropertyRestriction restriction, IRuleEvaluationContext context)
        {
            RestrictionEvaluator.ValidatePropertyRestriction(restriction);
            PropTag propTag = restriction.PropTag;

            if (restriction.MultiValued)
            {
                propTag = RuleUtil.GetMultiValuePropTag(propTag);
            }
            context.TraceFunction <PropTag, Restriction.RelOp, object>("PropertyRestriction [{0}] {1} [{2}]", propTag, restriction.Op, restriction.PropValue.Value);
            object obj = context[propTag];
            bool   flag;

            if (obj == null)
            {
                flag = false;
            }
            else if (restriction.MultiValued)
            {
                flag = RestrictionEvaluator.CompareMultiValueWithValue(context, RuleUtil.GetSingleValuePropTag(restriction.PropTag), restriction.Op, obj, restriction.PropValue.Value);
            }
            else if (RuleUtil.IsMultiValueTag(restriction.PropValue.PropTag))
            {
                flag = RestrictionEvaluator.CompareValueWithMultiValue(context, restriction.PropTag, restriction.Op, obj, restriction.PropValue.Value);
            }
            else
            {
                flag = context.CompareSingleValue(restriction.PropTag, restriction.Op, obj, restriction.PropValue.Value);
            }
            context.TraceFunction <bool, object>("PropertyRestriction evaluated to {0} with property value [{1}]", flag, obj);
            return(flag);
        }
예제 #7
0
        private static bool EvaluateNotRestriction(Restriction.NotRestriction restriction, IRuleEvaluationContext context)
        {
            context.NestedLevel++;
            bool flag = !RestrictionEvaluator.Evaluate(restriction.Restriction, context);

            context.NestedLevel--;
            context.TraceFunction <bool>("NotRestriction evaluated to {0}", flag);
            return(flag);
        }
예제 #8
0
        private static bool EvaluateContentRestriction(Restriction.ContentRestriction restriction, IRuleEvaluationContext context)
        {
            RestrictionEvaluator.ValidateContentRestriction(restriction);
            PropTag propTag = restriction.PropTag;

            if (restriction.MultiValued)
            {
                propTag = RuleUtil.GetMultiValuePropTag(propTag);
            }
            context.TraceFunction <PropTag, ContentFlags, object>("ContentRestriction tag [{0}] flags [{1}] value [{2}]", propTag, restriction.Flags, restriction.PropValue.Value);
            object obj = context[propTag];
            bool   flag;

            if (obj == null)
            {
                flag = false;
            }
            else if (restriction.MultiValued)
            {
                flag = RestrictionEvaluator.MatchMultiValueWithPattern(context, restriction.PropTag, obj, restriction.PropValue.Value, restriction.Flags);
            }
            else if (RuleUtil.IsTextProp(restriction.PropTag))
            {
                flag = RestrictionEvaluator.MatchString(context.LimitChecker, context.CultureInfo, (string)obj, (string)restriction.PropValue.Value, restriction.Flags);
            }
            else
            {
                if (!RuleUtil.IsBinaryProp(restriction.PropTag))
                {
                    throw new InvalidRuleException(string.Format("Content restriction can't be used on tag {0}", restriction.PropTag));
                }
                flag = RestrictionEvaluator.MatchByteArray(context.LimitChecker, (byte[])obj, (byte[])restriction.PropValue.Value, restriction.Flags);
            }
            context.TraceFunction <bool, object>("ContentRestriction Evaluated to {0} with property value [{1}]", flag, obj);
            if (!flag && propTag == PropTag.SenderSearchKey)
            {
                object obj2 = context[PropTag.SenderSmtpAddress];
                if (obj2 != null)
                {
                    string       @string      = CTSGlobals.AsciiEncoding.GetString((byte[])restriction.PropValue.Value);
                    ContentFlags contentFlags = ContentFlags.SubString | ContentFlags.IgnoreCase;
                    context.TraceFunction("No match found in SenderSearchKey, searching for string in SenderSmtpAddress...");
                    context.TraceFunction <PropTag, ContentFlags, string>("ContentRestriction tag [{0}] flags [{1}] value [{2}]", PropTag.SenderSmtpAddress, contentFlags, @string);
                    flag = RestrictionEvaluator.MatchString(context.LimitChecker, context.CultureInfo, (string)obj2, @string, contentFlags);
                }
            }
            return(flag);
        }
예제 #9
0
        private static bool EvaluateComparePropertyRestriction(Restriction.ComparePropertyRestriction restriction, IRuleEvaluationContext context)
        {
            RestrictionEvaluator.ValidateComparePropertyRestriction(restriction);
            object obj  = context[restriction.TagLeft];
            object obj2 = context[restriction.TagRight];

            context.TraceFunction <PropTag, Restriction.RelOp, PropTag>("ComparePropertyRestriction [{0}] {1} [{2}]", restriction.TagLeft, restriction.Op, restriction.TagRight);
            bool flag = false;

            if (obj != null && obj2 != null)
            {
                flag = context.CompareSingleValue(restriction.TagLeft, restriction.Op, obj, obj2);
            }
            context.TraceFunction <object, Restriction.RelOp, object, bool>("[{0}] {1} [{2}] evaluated to {3}", obj, restriction.Op, obj2, flag);
            return(flag);
        }
예제 #10
0
        private static bool EvaluateOrRestriction(Restriction.OrRestriction restriction, IRuleEvaluationContext context)
        {
            context.NestedLevel++;
            bool flag = false;

            foreach (Restriction restriction2 in restriction.Restrictions)
            {
                if (RestrictionEvaluator.Evaluate(restriction2, context))
                {
                    flag = true;
                    break;
                }
            }
            context.NestedLevel--;
            context.TraceFunction <bool>("OrRestriction evaluated to {0}", flag);
            return(flag);
        }
 public virtual bool CompareSingleValue(PropTag tag, Restriction.RelOp op, object x, object y)
 {
     if (x == null)
     {
         return(y == null);
     }
     if (y == null)
     {
         return(false);
     }
     if (!RestrictionEvaluator.IsSupportedOperation(op))
     {
         throw new InvalidRuleException(string.Format("Operation {0} is not supported", op));
     }
     if (x.GetType() != y.GetType() && (!(x.GetType() == typeof(ExDateTime)) || !(y.GetType() == typeof(DateTime))))
     {
         throw new InvalidRuleException(string.Format("Can not compare values of type {0} and type {1}", x.GetType(), y.GetType()));
     }
     if (op == Restriction.RelOp.MemberOfDL)
     {
         byte[] array = x as byte[];
         if (array == null)
         {
             this.TraceDebug("CompareSingleValue: MemberOf, recipientEntryId is not a byte array.");
             this.RecordError(ServerStrings.FolderRuleErrorInvalidRecipientEntryId);
             return(false);
         }
         byte[] array2 = y as byte[];
         if (array2 == null)
         {
             this.TraceDebug("CompareSingleValue: MemberOf, groupEntryId is not a byte array, marking rule in error.");
             throw new InvalidRuleException(ServerStrings.FolderRuleErrorInvalidGroup(y.GetType().Name));
         }
         return(RuleUtil.IsMemberOf(this, array, array2));
     }
     else
     {
         int?num = RestrictionEvaluator.CompareValue(this.CultureInfo, tag, x, y);
         if (num == null)
         {
             throw new InvalidRuleException(string.Format("Can't compare value '{0}' and '{1}'", x, y));
         }
         return(RestrictionEvaluator.GetOperationResultFromOrder(op, num.Value));
     }
 }
예제 #12
0
 private static void ValidateComparePropertyRestriction(Restriction.ComparePropertyRestriction restriction)
 {
     if (!RestrictionEvaluator.IsSupportedOperation(restriction.Op))
     {
         throw new InvalidRuleException(string.Format("Operation {0} is not supported", restriction.Op));
     }
     if (!RuleUtil.IsSameType(restriction.TagLeft, restriction.TagRight))
     {
         throw new InvalidRuleException("Tag and value are of different type");
     }
     if (RuleUtil.IsBooleanTag(restriction.TagLeft) && restriction.Op != Restriction.RelOp.Equal && restriction.Op != Restriction.RelOp.NotEqual)
     {
         throw new InvalidRuleException("Boolean tag only support Equal or NotEqual operation");
     }
     if (RuleUtil.IsMultiValueTag(restriction.TagLeft) || RuleUtil.IsMultiValueTag(restriction.TagRight))
     {
         throw new InvalidRuleException("Multi-valued prop or value is not supported in CompareProperty Restriction");
     }
 }
예제 #13
0
        private static bool EvaluateSizeRestriction(Restriction.SizeRestriction restriction, IRuleEvaluationContext context)
        {
            RestrictionEvaluator.ValidateSizeRestriction(restriction);
            context.TraceFunction <PropTag, Restriction.RelOp, int>("SizeRestriction [{0}] {1} [{2}]", restriction.Tag, restriction.Op, restriction.Size);
            PropType type = restriction.Tag.ValueType();
            int      num  = 0;
            bool     flag = false;
            object   obj  = null;

            if (RuleUtil.GetPropTypeSize(type, out num))
            {
                flag = RestrictionEvaluator.GetOperationResultFromOrder(restriction.Op, num.CompareTo(restriction.Size));
            }
            else if (RuleUtil.IsMultiValueTag(restriction.Tag))
            {
                obj = context[restriction.Tag];
                object[] array = obj as object[];
                if (array != null)
                {
                    foreach (object value in array)
                    {
                        int size = RuleUtil.GetSize(value);
                        if (RestrictionEvaluator.GetOperationResultFromOrder(restriction.Op, size.CompareTo(restriction.Size)))
                        {
                            flag = true;
                            break;
                        }
                    }
                }
            }
            else
            {
                obj = context[restriction.Tag];
                int size2 = RuleUtil.GetSize(obj);
                flag = RestrictionEvaluator.GetOperationResultFromOrder(restriction.Op, size2.CompareTo(restriction.Size));
            }
            context.TraceFunction <bool, object>("SizeRestriction evaluated to {0} with property value [{1}]", flag, obj);
            return(flag);
        }
예제 #14
0
        private static bool EvaluateRecipientRestriction(Restriction.RecipientRestriction restriction, IRuleEvaluationContext context)
        {
            context.TraceFunction("RecipientRestriction");
            context.NestedLevel++;
            bool flag = false;

            foreach (Recipient recipient in context.Message.Recipients)
            {
                using (IRuleEvaluationContext recipientContext = context.GetRecipientContext(recipient))
                {
                    context.TraceFunction <string>("Recipient {0}", recipient.Participant.DisplayName);
                    if (RestrictionEvaluator.Evaluate(restriction.Restriction, recipientContext))
                    {
                        flag = true;
                        break;
                    }
                }
            }
            context.NestedLevel--;
            context.TraceFunction <bool>("RecipientRestriction evaluates to {0}", flag);
            return(flag);
        }
예제 #15
0
        private static bool EvaluateBitMaskRestriction(Restriction.BitMaskRestriction restriction, IRuleEvaluationContext context)
        {
            RestrictionEvaluator.ValidateBitMaskRestriction(restriction);
            object obj = context[restriction.Tag];

            if (obj is int)
            {
                int  num = (int)obj;
                bool flag;
                if ((num & restriction.Mask) != 0)
                {
                    flag = (restriction.Bmr == Restriction.RelBmr.NotEqualToZero);
                }
                else
                {
                    flag = (restriction.Bmr == Restriction.RelBmr.EqualToZero);
                }
                context.TraceFunction <object, Restriction.RelBmr, int, bool>("BitMaskRestriction [{0}] {1} [{2}] evaluates to {3}", obj, restriction.Bmr, restriction.Mask, flag);
                return(flag);
            }
            context.TraceError <object>("Invalid BitMaskRestriction value: {0}", obj);
            return(false);
        }
예제 #16
0
 internal static bool IsSupportedOperation(Restriction.RelOp op)
 {
     return(RestrictionEvaluator.IsOrderOperation(op) || op == Restriction.RelOp.MemberOfDL);
 }
예제 #17
0
        private bool EvaluateRule(Rule rule, FolderEvaluationResult result)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }
            if (rule.Actions == null)
            {
                throw new ArgumentException("Rule.Actions must not be null.");
            }
            this.context.CurrentRule = rule;
            this.context.NestedLevel = 0;
            this.context.TraceDebug <string>("Evaluate rule {0}", rule.Name);
            int  i = 0;
            bool result2;

            try
            {
                RuleUtil.FaultInjection((FaultInjectionLid)3102092605U);
                if (!RestrictionEvaluator.Evaluate(rule.Condition, this.context))
                {
                    result2 = false;
                }
                else
                {
                    this.context.TraceDebug <string>("Rule {0} evaluated to true", rule.Name);
                    if (this.context.HasRuleFiredBefore(rule))
                    {
                        this.context.TraceDebug <string>("Rule history loop detected on rule {0}", rule.Name);
                        result2 = false;
                    }
                    else
                    {
                        while (i < rule.Actions.Length)
                        {
                            RuleAction ruleAction = rule.Actions[i];
                            if (ruleAction == null)
                            {
                                this.context.TraceDebug <string, int>("For rule \"{0}\", action at index {1} is null.", rule.Name, i);
                            }
                            else
                            {
                                this.CreateWorkItemForAction(result, ruleAction, i++);
                                if (!result.ShouldContinue)
                                {
                                    this.context.TraceDebug <Type>("Stop evaluation process after action {0}", ruleAction.GetType());
                                    break;
                                }
                            }
                        }
                        result2 = true;
                    }
                }
            }
            catch (InvalidRuleException exception)
            {
                this.context.DisableAndMarkRuleInError(rule, rule.Actions[i].ActionType, i, DeferredError.RuleError.Parsing);
                this.context.RecordError(exception, ServerStrings.FolderRuleStageEvaluation);
                result2 = false;
            }
            return(result2);
        }
예제 #18
0
        public static bool Evaluate(Restriction restriction, IRuleEvaluationContext context)
        {
            if (restriction == null)
            {
                context.TraceDebug <string>("Empty restriction evaluated to true in rule {0}", context.CurrentRule.Name);
                return(true);
            }
            Restriction.ResType type = restriction.Type;
            switch (type)
            {
            case Restriction.ResType.And:
                return(RestrictionEvaluator.EvaluateAndRestriction((Restriction.AndRestriction)restriction, context));

            case Restriction.ResType.Or:
                return(RestrictionEvaluator.EvaluateOrRestriction((Restriction.OrRestriction)restriction, context));

            case Restriction.ResType.Not:
                return(RestrictionEvaluator.EvaluateNotRestriction((Restriction.NotRestriction)restriction, context));

            case Restriction.ResType.Content:
                return(RestrictionEvaluator.EvaluateContentRestriction((Restriction.ContentRestriction)restriction, context));

            case Restriction.ResType.Property:
                return(RestrictionEvaluator.EvaluatePropertyRestriction((Restriction.PropertyRestriction)restriction, context));

            case Restriction.ResType.CompareProps:
                return(RestrictionEvaluator.EvaluateComparePropertyRestriction((Restriction.ComparePropertyRestriction)restriction, context));

            case Restriction.ResType.BitMask:
                return(RestrictionEvaluator.EvaluateBitMaskRestriction((Restriction.BitMaskRestriction)restriction, context));

            case Restriction.ResType.Size:
                return(RestrictionEvaluator.EvaluateSizeRestriction((Restriction.SizeRestriction)restriction, context));

            case Restriction.ResType.Exist:
                return(RestrictionEvaluator.EvaluateExistRestriction((Restriction.ExistRestriction)restriction, context));

            case Restriction.ResType.SubRestriction:
                if (restriction is Restriction.AttachmentRestriction)
                {
                    return(RestrictionEvaluator.EvaluateAttachmentRestriction((Restriction.AttachmentRestriction)restriction, context));
                }
                if (restriction is Restriction.RecipientRestriction)
                {
                    return(RestrictionEvaluator.EvaluateRecipientRestriction((Restriction.RecipientRestriction)restriction, context));
                }
                throw new InvalidRuleException(string.Format("SubRestriction Type {0} is not supported!", restriction.GetType()));

            case Restriction.ResType.Comment:
                return(RestrictionEvaluator.EvaluateCommentRestriction((Restriction.CommentRestriction)restriction, context));

            case Restriction.ResType.Count:
                return(RestrictionEvaluator.EvaluateCountRestriction((Restriction.CountRestriction)restriction, context));

            default:
                switch (type)
                {
                case Restriction.ResType.True:
                    context.TraceFunction("True Restriction");
                    return(true);

                case Restriction.ResType.False:
                    context.TraceFunction("False Restriction");
                    return(false);

                default:
                    throw new InvalidRuleException(string.Format("Restriction Type {0} is not supported!", restriction.Type));
                }
                break;
            }
        }