public string RuleProcessor(Display display, Rule rule, String scriptContents)
{
string modifiedContents = "";
if (rule.Type == "replace")
{
display.updateMessage("Running Replace Rule '" + rule.Name + "'");
Regex regex = new Regex(rule.Trigger, RegexOptions.IgnoreCase);
string replacementText = rule.Action;
if (rule.Action.Contains("#RANDOM"))
{
replacementText = rule.Action.Replace("#RANDOM", PSABTUtils.RandomString(16, this.rand));
}
display.updateSecondaryMessage("Replacing " + rule.Trigger + " with " + replacementText);
modifiedContents = regex.Replace(scriptContents, replacementText);
}
if (rule.Type == "ReplaceList")
{
display.updateMessage("Running ReplaceList Rule '" + rule.Name + "'");
this.VariableKey = new Dictionary<string, string>();
Regex regex = new Regex(rule.Trigger, RegexOptions.IgnoreCase);
List<string> safeVars = new List<string>(new string[] {"true", "false", "null", "error" });
string replacementText = rule.Action;
Match hit = regex.Match(scriptContents);
modifiedContents = scriptContents;
while (hit.Success)
{
if (safeVars.Contains(hit.Value.Replace("$","").ToLower()))
{
replacementText = null;
}
else if (this.VariableKey.ContainsKey(hit.Value))
{
display.updateSecondaryMessage("Found hit for key:" + hit.Value);
replacementText = this.VariableKey[hit.Value];
}
else
{
display.updateSecondaryMessage("Creating new string for key:" + hit.Value);
replacementText = rule.Action.Replace("#RANDOM", PSABTUtils.RandomString(32, this.rand));
this.VariableKey.Add(hit.Value, replacementText);
}
if (replacementText != null)
{
display.updateSecondaryMessage("Replacing " + hit.Value + " with " + replacementText);
string variable_match = @"(\$)" + hit.Value.Replace("$", "");
Regex regex_step2 = new Regex(variable_match, RegexOptions.IgnoreCase);
modifiedContents = regex_step2.Replace(modifiedContents, replacementText);
}
else {
display.updateSecondaryMessage("Safe variable " + hit.Value + " found. Not replacing.");
}
hit = hit.NextMatch();
}
}
return modifiedContents;
}