예제 #1
0
		public static MatchRule Parse (string text)
		{
			if (text.Length > ProtocolInformation.MaxMatchRuleLength)
				throw new Exception ("Match rule length exceeds maximum " + ProtocolInformation.MaxMatchRuleLength + " characters");

			MatchRule r = new MatchRule ();

			// TODO: Stricter validation. Tighten up the regex.
			// It currently succeeds and silently drops malformed test parts.

			for (Match m = matchRuleRegex.Match (text) ; m.Success ; m = m.NextMatch ()) {
				string key = m.Groups[1].Value;
				string value = m.Groups[2].Value;
				// This unescaping may not be perfect..
				value = value.Replace (@"\\", @"\");
				value = value.Replace (@"\'", @"'");

				if (key.StartsWith ("arg")) {
					Match mArg = argNRegex.Match (key);
					if (!mArg.Success)
						return null;
					int argNum = (int)UInt32.Parse (mArg.Groups[1].Value);

					if (argNum < 0 || argNum >= ProtocolInformation.MaxMatchRuleArgs)
						throw new Exception ("arg match must be between 0 and " + (ProtocolInformation.MaxMatchRuleArgs - 1) + " inclusive");

					//if (r.Args.ContainsKey (argNum))
					//	return null;

					string argType = mArg.Groups[2].Value;

					if (argType == "path")
						r.Args.Add (new ArgMatchTest (argNum, new ObjectPath (value)));
					else
						r.Args.Add (new ArgMatchTest (argNum, value));

					continue;
				}

				//TODO: more consistent error handling
				switch (key) {
					case "type":
						if (r.MessageType != MessageType.All)
							return null;
						r.MessageType = MessageFilter.StringToMessageType (value);
						break;
					case "interface":
						r.Fields[FieldCode.Interface] = new MatchTest (value);
						break;
					case "member":
						r.Fields[FieldCode.Member] = new MatchTest (value);
						break;
					case "path":
						r.Fields[FieldCode.Path] = new MatchTest (new ObjectPath (value));
						break;
					case "sender":
						r.Fields[FieldCode.Sender] = new MatchTest (value);
						break;
					case "destination":
						r.Fields[FieldCode.Destination] = new MatchTest (value);
						break;
					default:
						if (ProtocolInformation.Verbose)
							Console.Error.WriteLine ("Warning: Unrecognized match rule key: " + key);
						break;
				}
			}

			return r;
		}
예제 #2
0
파일: MatchRule.cs 프로젝트: t0fuu/disco
        public static MatchRule Parse(string text)
        {
            if (text.Length > ProtocolInformation.MaxMatchRuleLength)
            {
                throw new Exception("Match rule length exceeds maximum " + ProtocolInformation.MaxMatchRuleLength + " characters");
            }

            MatchRule r = new MatchRule();

            // TODO: Stricter validation. Tighten up the regex.
            // It currently succeeds and silently drops malformed test parts.

            for (Match m = matchRuleRegex.Match(text); m.Success; m = m.NextMatch())
            {
                string key   = m.Groups[1].Value;
                string value = m.Groups[2].Value;
                // This unescaping may not be perfect..
                value = value.Replace(@"\\", @"\");
                value = value.Replace(@"\'", @"'");

                if (key.StartsWith("arg"))
                {
                    Match mArg = argNRegex.Match(key);
                    if (!mArg.Success)
                    {
                        return(null);
                    }
                    int argNum = (int)UInt32.Parse(mArg.Groups[1].Value);

                    if (argNum < 0 || argNum >= ProtocolInformation.MaxMatchRuleArgs)
                    {
                        throw new Exception("arg match must be between 0 and " + (ProtocolInformation.MaxMatchRuleArgs - 1) + " inclusive");
                    }

                    //if (r.Args.ContainsKey (argNum))
                    //	return null;

                    string argType = mArg.Groups[2].Value;

                    if (argType == "path")
                    {
                        r.Args.Add(new ArgMatchTest(argNum, new ObjectPath(value)));
                    }
                    else
                    {
                        r.Args.Add(new ArgMatchTest(argNum, value));
                    }

                    continue;
                }

                //TODO: more consistent error handling
                switch (key)
                {
                case "type":
                    if (r.MessageType != MessageType.All)
                    {
                        return(null);
                    }
                    r.MessageType = MessageFilter.StringToMessageType(value);
                    break;

                case "interface":
                    r.Fields[FieldCode.Interface] = new MatchTest(value);
                    break;

                case "member":
                    r.Fields[FieldCode.Member] = new MatchTest(value);
                    break;

                case "path":
                    r.Fields[FieldCode.Path] = new MatchTest(new ObjectPath(value));
                    break;

                case "sender":
                    r.Fields[FieldCode.Sender] = new MatchTest(value);
                    break;

                case "destination":
                    r.Fields[FieldCode.Destination] = new MatchTest(value);
                    break;

                default:
                    if (ProtocolInformation.Verbose)
                    {
                        Console.Error.WriteLine("Warning: Unrecognized match rule key: " + key);
                    }
                    break;
                }
            }

            return(r);
        }