public override string ToString() { StringBuilder sb = new StringBuilder(); if (MessageType != null) { Append(sb, "type", MessageFilter.MessageTypeToString((MessageType)MessageType)); } if (Interface != null) { Append(sb, "interface", Interface); } if (Member != null) { Append(sb, "member", Member); } if (Path != null) { //Append (sb, "path", Path.ToString ()); Append(sb, "path", Path.Value); } if (Sender != null) { Append(sb, "sender", Sender); } if (Destination != null) { Append(sb, "destination", Destination); } if (Args != null) { foreach (KeyValuePair <int, string> pair in Args) { AppendArg(sb, pair.Key, pair.Value); } } return(sb.ToString()); }
public override string ToString() { StringBuilder sb = new StringBuilder(); if (MessageType != null) { Append(sb, "type", MessageFilter.MessageTypeToString((MessageType)MessageType)); } // Note that fdo D-Bus daemon sorts in a different order. // It shouldn't matter though as long as we're consistent. foreach (KeyValuePair <FieldCode, MatchTest> pair in Fields) { Append(sb, pair.Key.ToString().ToLower(), pair.Value.Value); } // Sorting the list here is not ideal List <ArgMatchTest> tests = new List <ArgMatchTest> (Args); tests.Sort(delegate(ArgMatchTest aa, ArgMatchTest bb) { return(aa.ArgNum - bb.ArgNum); }); if (Args != null) { foreach (ArgMatchTest test in tests) { if (test.Signature == Signature.StringSig) { AppendArg(sb, test.ArgNum, (string)test.Value); } else if (test.Signature == Signature.ObjectPathSig) { AppendPathArg(sb, test.ArgNum, (ObjectPath)test.Value); } } } return(sb.ToString()); }
//this could be made more efficient public static MatchRule Parse(string text) { MatchRule r = new MatchRule(); foreach (string propStr in text.Split(',')) { string[] parts = propStr.Split('='); if (parts.Length < 2) { throw new Exception("No equals sign found"); } if (parts.Length > 2) { throw new Exception("Too many equals signs found"); } string key = parts[0].Trim(); string value = parts[1].Trim(); if (!value.StartsWith("'") || !value.EndsWith("'")) { throw new Exception("Too many equals signs found"); } value = value.Substring(1, value.Length - 2); if (key.StartsWith("arg")) { int argnum = Int32.Parse(key.Remove(0, "arg".Length)); if (argnum < 0 || argnum > 63) { throw new Exception("arg match must be between 0 and 63 inclusive"); } if (r.Args.ContainsKey(argnum)) { return(null); } r.Args[argnum] = value; continue; } //TODO: more consistent error handling switch (key) { case "type": if (r.MessageType != null) { return(null); } r.MessageType = MessageFilter.StringToMessageType(value); break; case "interface": if (r.Interface != null) { return(null); } r.Interface = value; break; case "member": if (r.Member != null) { return(null); } r.Member = value; break; case "path": if (r.Path != null) { return(null); } r.Path = new ObjectPath(value); break; case "sender": if (r.Sender != null) { return(null); } r.Sender = value; break; case "destination": if (r.Destination != null) { return(null); } r.Destination = value; break; default: if (Protocol.Verbose) { Console.Error.WriteLine("Warning: Unrecognized match rule key: " + key); } break; } } return(r); }
public static MatchRule Parse(string text) { if (text.Length > Protocol.MaxMatchRuleLength) { throw new Exception("Match rule length exceeds maximum " + Protocol.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 >= Protocol.MaxMatchRuleArgs) { throw new Exception("arg match must be between 0 and " + (Protocol.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 != null) { 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 (Protocol.Verbose) { Console.Error.WriteLine("Warning: Unrecognized match rule key: " + key); } break; } } return(r); }