/// <summary> /// Deletes all. /// </summary> /// <param name="IncidentBoxId">The incident box id.</param> public static void DeleteAll(int IncidentBoxId) { using (DbTransaction tran = DbTransaction.Begin()) { foreach (IncidentBoxRule rule in IncidentBoxRule.List(IncidentBoxId)) { IncidentBoxRule.Delete(rule.IncidentBoxRuleId); } tran.Commit(); } }
/// <summary> /// Evaluates the specified incident box id. /// </summary> /// <param name="IncidentBoxId">The incident box id.</param> /// <returns></returns> public static bool Evaluate(int IncidentBoxId, IncidentInfo incidentInfo) { IncidentBoxRule[] ruleList = IncidentBoxRule.List(IncidentBoxId); if (ruleList.Length == 0) { return(false); } int Index = 0; return(EvaluateBlock(true, string.Empty, incidentInfo, ruleList, ref Index)); }
private static bool EvaluateContains(IncidentInfo incidentInfo, IncidentBoxRule Rule) { string KeyValue = GetStringFromEmailByKey(incidentInfo, Rule.Key); if (KeyValue == null || KeyValue == string.Empty) { return(false); } ArrayList keyValues = GetKeyArray(KeyValue); ArrayList ruleValues = GetKeyArray(Rule.Value); bool bIsNumber = true; foreach (string rule in ruleValues) { if (!IsNumber(rule)) { bIsNumber = false; break; } } foreach (string rule in ruleValues) { bool bFound = false; foreach (string key in keyValues) { if (key == rule) { bFound = true; break; } else if (!bIsNumber && Pattern.Match(key, string.Format("*{0}*", rule))) { bFound = true; break; } } if (!bFound) { return(false); } } return(true); }
/// <summary> /// Creates the new incident. /// </summary> /// <param name="EMailMessageId">The E mail message id.</param> /// <param name="emailBox">The email box.</param> /// <param name="msg">The MSG.</param> /// <returns></returns> public static int CreateNewIncident(int EMailMessageId, EMailRouterPop3Box emailBox, Pop3Message msg) { IEMailIncidentMapping mappingHandler = EMailIncidentMappingHandler.LoadHandler(emailBox.Settings.SelectedHandlerId); // Step. Mapping IncidentInfo incidentInfo = mappingHandler.Create(emailBox, msg); // Set Creator UserLight prevUser = LogOnCreator(incidentInfo.CreatorId, msg); incidentInfo.CreatorId = Security.CurrentUser.UserID; // Step. Evaluate IncidentBox IncidentBox incidentBox = null; if (incidentInfo.IncidentBoxId != -1) { incidentBox = IncidentBox.Load(incidentInfo.IncidentBoxId); } else { incidentBox = IncidentBoxRule.Evaluate(incidentInfo); } // Step. Create Incident int IncidentId = Incident.CreateFromEmail(incidentInfo.Title, incidentInfo.Description, incidentInfo.ProjectId, incidentInfo.TypeId, incidentInfo.PriorityId, incidentInfo.SeverityId, incidentBox.Document.GeneralBlock.TaskTime, incidentBox.Document.GeneralBlock.ExpectedDuration, incidentBox.Document.GeneralBlock.ExpectedResponseTime, incidentBox.Document.GeneralBlock.ExpectedAssignTime, incidentInfo.GeneralCategories, incidentInfo.IncidentCategories, incidentBox.IncidentBoxId, EMailMessageId, incidentInfo.OrgUid, incidentInfo.ContactUid); return(IncidentId); }
private static bool EvaluateIsEqual(IncidentInfo incidentInfo, IncidentBoxRule Rule) { string KeyValue = GetStringFromEmailByKey(incidentInfo, Rule.Key); if (KeyValue == null || KeyValue == string.Empty) { return(false); } ArrayList keyValues = GetKeyArray(KeyValue); ArrayList ruleValues = GetKeyArray(Rule.Value); foreach (string key in keyValues) { bool bFound = false; foreach (string rule in ruleValues) { if (rule.IndexOfAny(new char[] { '*', '?' }) != -1) { if (Pattern.Match(key, rule)) { bFound = true; break; } } else if (key == rule) { bFound = true; break; } } if (!bFound) { return(false); } } return(true); }
private static bool EvaluateFunction(IncidentInfo incidentInfo, IncidentBoxRule Rule) { int FunctionId = int.Parse(Rule.Key); IncidentBoxRuleFunction function = IncidentBoxRuleFunction.Load(FunctionId); ArrayList paramItems = new ArrayList(); foreach (Match match in Regex.Matches(Rule.Value, "\"(?<Param>[^\"]+)\";?", RegexOptions.IgnoreCase | RegexOptions.Singleline)) { string Value = match.Groups["Param"].Value; if (Value.StartsWith("[") && Value.EndsWith("]")) { Value = GetStringFromEmailByKey(incidentInfo, Value.Trim('[', ']')); } paramItems.Add(Value); } return(function.Invoke((string[])paramItems.ToArray(typeof(string)))); }
private static bool EvaluateContains(IncidentInfo incidentInfo, IncidentBoxRule Rule) { string KeyValue = GetStringFromEmailByKey(incidentInfo, Rule.Key); if (KeyValue == null || KeyValue == string.Empty) return false; ArrayList keyValues = GetKeyArray(KeyValue); ArrayList ruleValues = GetKeyArray(Rule.Value); bool bIsNumber = true; foreach (string rule in ruleValues) { if (!IsNumber(rule)) { bIsNumber = false; break; } } foreach (string rule in ruleValues) { bool bFound = false; foreach (string key in keyValues) { if (key == rule) { bFound = true; break; } else if (!bIsNumber && Pattern.Match(key, string.Format("*{0}*", rule))) { bFound = true; break; } } if (!bFound) return false; } return true; }
/// <summary> /// Updates the specified rule. /// </summary> /// <param name="Rule">The rule.</param> /// <param name="NewOutlineIndex">New index of the outline.</param> public static void Update(IncidentBoxRule Rule, int NewOutlineIndex) { Rule._srcRow.OutlineIndex = NewOutlineIndex; Rule._srcRow.Update(); }
/// <summary> /// Updates the specified Rule. /// </summary> /// <param name="Rule">The Rule.</param> public static void Update(IncidentBoxRule Rule) { Rule._srcRow.Update(); }
private int Reccurence(IncidentBoxRule[] ibList, out string ss, string thisLevel, int i) { ss = ""; while (++i < ibList.Length && ibList[i].OutlineLevel.StartsWith(thisLevel)) { IncidentBoxRule ibr = ibList[i]; if (ibr.RuleType == IncidentBoxRuleType.OrBlock || ibr.RuleType == IncidentBoxRuleType.AndBlock) { string thisLevel1 = ibr.OutlineLevel + ibr.IncidentBoxRuleId + "."; ss += GetLabel(ibr, false) + " <b>(</b>"; string ss1 = ""; i = Reccurence(ibList, out ss1, thisLevel1, i); ss += ss1 + "<b>)</b>"; } else ss += GetLabel(ibr, false) + " <b>(</b> " + GetInnerText(ibr) + "<b>) </b>"; } return --i; }
private static void GoToBlockEnd(string BlockOutlineLevel, IncidentBoxRule[] ruleList, ref int Index) { for (; Index < ruleList.Length; Index++) { IncidentBoxRule Rule = ruleList[Index]; if (Rule.OutlineLevel.Length <= BlockOutlineLevel.Length) { Index--; return; } } }
private static bool EvaluateRegexMatch(IncidentInfo incidentInfo, IncidentBoxRule Rule) { string KeyValue = GetStringFromEmailByKey(incidentInfo, Rule.Key); Match match = Regex.Match(KeyValue, Rule.Value, RegexOptions.IgnoreCase); if ((match.Success && (match.Index == 0)) && (match.Length == KeyValue.Length)) return true; return false; }
private string GetLabel(IncidentBoxRule ibr, bool IsFirst) { string retVal = ""; if (IsFirst) { int iLevel = Util.CommonHelper.CountOf(ibr.OutlineLevel, ".") - 1; if (iLevel > 0) { iLevel = iLevel * 40; retVal += String.Format("<img alt='' src='{1}' width='{0}px' height='1px' border='0' />", iLevel, this.Page.ResolveUrl("~/layouts/images/blank.gif")); } } string sAnd_OR = ""; if (ibr.OutlineLevel == ".") { if (ibr.OutlineIndex > 0) sAnd_OR = LocRM.GetString("tAND"); } else { string sLevel = ibr.OutlineLevel; if (sLevel.EndsWith(".")) sLevel = sLevel.Substring(0, sLevel.Length - 1); sLevel = sLevel.Substring(sLevel.LastIndexOf(".") + 1); int iNum = int.Parse(sLevel); IncidentBoxRule ibr1 = IncidentBoxRule.Load(iNum); if (ibr1.RuleType == IncidentBoxRuleType.OrBlock && ibr.OutlineIndex > ibr1.OutlineIndex + 1) sAnd_OR = LocRM.GetString("tOR"); if (ibr1.RuleType == IncidentBoxRuleType.AndBlock && ibr.OutlineIndex > ibr1.OutlineIndex + 1) sAnd_OR = LocRM.GetString("tAND"); } if (IsFirst || sAnd_OR != "") retVal += "<font color='green'><b>" + sAnd_OR + "</b></font>"; return retVal; }
/// <summary> /// Checks the specified email box. /// </summary> /// <param name="emailBox">The email box.</param> /// <param name="message">The message.</param> /// <returns></returns> public static EMailMessageAntiSpamRuleRusult Check(EMailRouterPop3Box emailBox, Pop3Message message) { // 2007-01-09: Check Empty From and Sender if (message.Sender == null && message.From == null || message.To == null) { return(EMailMessageAntiSpamRuleRusult.Deny); } EMailMessageAntiSpamRule[] antiSpamRules = EMailMessageAntiSpamRule.List(); // Load Rules //EMailMessageAntiSpamRule[] antiSpamRules = _antiSpamRules; // Check Rules foreach (EMailMessageAntiSpamRule rule in antiSpamRules) { if (((EMailMessageAntiSpamRuleType)rule.RuleType) != EMailMessageAntiSpamRuleType.Service) { string KeyValue = GetStringFromEmailByKey(message, rule.Key); if (KeyValue != null) { switch ((EMailMessageAntiSpamRuleType)rule.RuleType) { case EMailMessageAntiSpamRuleType.Contains: foreach (string ContainsSubStr in rule.Value.Split(';')) { string TrimContainsSubStr = ContainsSubStr.Trim(); if (TrimContainsSubStr != string.Empty) { if (TrimContainsSubStr.IndexOfAny(new char[] { '*', '?' }) != -1) { if (Pattern.Match(KeyValue, TrimContainsSubStr)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } } else { if (KeyValue.IndexOf(TrimContainsSubStr) != -1) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } } } } break; case EMailMessageAntiSpamRuleType.IsEqual: if (string.Compare(KeyValue, rule.Value, true) == 0) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case EMailMessageAntiSpamRuleType.RegexMatch: Match match = Regex.Match(KeyValue, rule.Value); if ((match.Success && (match.Index == 0)) && (match.Length == KeyValue.Length)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; } } } else { string FromEmail = EMailMessage.GetSenderEmail(message); switch (rule.Key) { case BlackListServiceName: if (BlackListItem.Contains(FromEmail)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case WhiteListServiceName: if (WhiteListItem.Contains(FromEmail)) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case TicketServiceName: if (message.Subject != null && TicketUidUtil.LoadFromString(message.Subject) != string.Empty) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; case IncidentBoxRulesServiceName: // Step 1. Get Incident Info IEMailIncidentMapping mappingHandler = EMailIncidentMappingHandler.LoadHandler(emailBox.Settings.SelectedHandlerId); IncidentInfo incidentInfo = mappingHandler.Create(emailBox, message); // Step 2. Evaluate IncidentBoxRules IncidentBox box = IncidentBoxRule.Evaluate(incidentInfo, false); if (box != null) { return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny); } break; } } } return(EMailMessageAntiSpamRuleRusult.Pending); }
private static bool EvaluateBlock(bool AndBlock, string BlockOutlineLevel, IncidentInfo incidentInfo, IncidentBoxRule[] ruleList, ref int Index) { if (Index >= ruleList.Length) return false; // Empty block is false ... bool bResult = false; for (; Index < ruleList.Length; Index++) { IncidentBoxRule Rule = ruleList[Index]; if (Rule.OutlineLevel.Length <= BlockOutlineLevel.Length) { Index--; return bResult; } bResult = AndBlock ? true : false; switch (Rule.RuleType) { case IncidentBoxRuleType.AndBlock: #region AndBlock Index++; // Increase Index to move next element bResult = EvaluateBlock(true, Rule.OutlineLevel, incidentInfo, ruleList, ref Index); #endregion break; case IncidentBoxRuleType.OrBlock: #region OrBlock Index++; // Increase Index to move next element bResult = EvaluateBlock(false, Rule.OutlineLevel, incidentInfo, ruleList, ref Index); #endregion break; case IncidentBoxRuleType.Contains: bResult = EvaluateContains(incidentInfo, Rule); break; case IncidentBoxRuleType.FromEMailBox: //bResult = EvaluateFromEMailBox(EMailRouterPop3BoxId, Rule); break; case IncidentBoxRuleType.IsEqual: bResult = EvaluateIsEqual(incidentInfo, Rule); break; case IncidentBoxRuleType.RegexMatch: bResult = EvaluateRegexMatch(incidentInfo, Rule); break; case IncidentBoxRuleType.Function: bResult = EvaluateFunction(incidentInfo, Rule); break; case IncidentBoxRuleType.NotContains: bResult = !EvaluateContains(incidentInfo, Rule); break; case IncidentBoxRuleType.NotIsEqual: bResult = !EvaluateIsEqual(incidentInfo, Rule); break; } if (AndBlock) { if (!bResult) { GoToBlockEnd(BlockOutlineLevel, ruleList, ref Index); return false; } } else { if (bResult) { GoToBlockEnd(BlockOutlineLevel, ruleList, ref Index); return true; } } } return bResult; }
private static bool EvaluateFunction(IncidentInfo incidentInfo, IncidentBoxRule Rule) { int FunctionId = int.Parse(Rule.Key); IncidentBoxRuleFunction function = IncidentBoxRuleFunction.Load(FunctionId); ArrayList paramItems = new ArrayList(); foreach (Match match in Regex.Matches(Rule.Value, "\"(?<Param>[^\"]+)\";?", RegexOptions.IgnoreCase | RegexOptions.Singleline)) { string Value = match.Groups["Param"].Value; if (Value.StartsWith("[") && Value.EndsWith("]")) { Value = GetStringFromEmailByKey(incidentInfo, Value.Trim('[', ']')); } paramItems.Add(Value); } return function.Invoke((string[])paramItems.ToArray(typeof(string))); }
private static bool EvaluateIsEqual(IncidentInfo incidentInfo, IncidentBoxRule Rule) { string KeyValue = GetStringFromEmailByKey(incidentInfo, Rule.Key); if (KeyValue == null || KeyValue == string.Empty) return false; ArrayList keyValues = GetKeyArray(KeyValue); ArrayList ruleValues = GetKeyArray(Rule.Value); foreach (string key in keyValues) { bool bFound = false; foreach (string rule in ruleValues) { if (rule.IndexOfAny(new char[] { '*', '?' }) != -1) { if (Pattern.Match(key, rule)) { bFound = true; break; } } else if (key == rule) { bFound = true; break; } } if (!bFound) return false; } return true; }
private static bool EvaluateBlock(bool AndBlock, string BlockOutlineLevel, IncidentInfo incidentInfo, IncidentBoxRule[] ruleList, ref int Index) { if (Index >= ruleList.Length) { return(false); } // Empty block is false ... bool bResult = false; for (; Index < ruleList.Length; Index++) { IncidentBoxRule Rule = ruleList[Index]; if (Rule.OutlineLevel.Length <= BlockOutlineLevel.Length) { Index--; return(bResult); } bResult = AndBlock ? true : false; switch (Rule.RuleType) { case IncidentBoxRuleType.AndBlock: #region AndBlock Index++; // Increase Index to move next element bResult = EvaluateBlock(true, Rule.OutlineLevel, incidentInfo, ruleList, ref Index); #endregion break; case IncidentBoxRuleType.OrBlock: #region OrBlock Index++; // Increase Index to move next element bResult = EvaluateBlock(false, Rule.OutlineLevel, incidentInfo, ruleList, ref Index); #endregion break; case IncidentBoxRuleType.Contains: bResult = EvaluateContains(incidentInfo, Rule); break; case IncidentBoxRuleType.FromEMailBox: //bResult = EvaluateFromEMailBox(EMailRouterPop3BoxId, Rule); break; case IncidentBoxRuleType.IsEqual: bResult = EvaluateIsEqual(incidentInfo, Rule); break; case IncidentBoxRuleType.RegexMatch: bResult = EvaluateRegexMatch(incidentInfo, Rule); break; case IncidentBoxRuleType.Function: bResult = EvaluateFunction(incidentInfo, Rule); break; case IncidentBoxRuleType.NotContains: bResult = !EvaluateContains(incidentInfo, Rule); break; case IncidentBoxRuleType.NotIsEqual: bResult = !EvaluateIsEqual(incidentInfo, Rule); break; } if (AndBlock) { if (!bResult) { GoToBlockEnd(BlockOutlineLevel, ruleList, ref Index); return(false); } } else { if (bResult) { GoToBlockEnd(BlockOutlineLevel, ruleList, ref Index); return(true); } } } return(bResult); }
private string GetInnerText(IncidentBoxRule ibr) { string retVal = ""; switch (ibr.RuleType) { case IncidentBoxRuleType.Contains: case IncidentBoxRuleType.IsEqual: case IncidentBoxRuleType.RegexMatch: case IncidentBoxRuleType.NotContains: case IncidentBoxRuleType.NotIsEqual: string sKey = ibr.Key; sKey = (sKey == "TypeId") ? "Type" : sKey; sKey = (sKey == "PriorityId") ? "Priority" : sKey; sKey = (sKey == "SeverityId") ? "Severity" : sKey; sKey = (sKey == "TypeId") ? "Type" : sKey; sKey = (sKey == "CreatorId") ? "Creator" : sKey; sKey = (sKey == "ProjectId") ? "Project" : sKey; sKey = (sKey == "EMailBox") ? "E-mail Box" : sKey; sKey = (sKey == "TitleOrDescriptionOrEMailBody") ? "Title or Description or EMailBody" : sKey; retVal += "<font color='#0000ff'>[" + sKey + "]</font> "; retVal += "<b>" + GetRuleType(ibr.RuleType) + "</b>"; if (ibr.Value.Length > 0) { if (sKey == "Type") retVal += " <font color='#ff0000'>'" + Mediachase.IBN.Business.Common.GetIncidentType(int.Parse(ibr.Value)) + "'</font>"; else if (sKey == "E-mail Box") retVal += " <font color='#ff0000'>'" + Mediachase.IBN.Business.EMail.EMailRouterPop3Box.Load(int.Parse(ibr.Value)).Name + "'</font>"; else if (sKey == "Creator") retVal += " <font color='#ff0000'>'" + Util.CommonHelper.GetUserStatusPureName(int.Parse(ibr.Value)) + "'</font>"; else if (sKey == "Project") retVal += " <font color='#ff0000'>'" + Task.GetProjectTitle(int.Parse(ibr.Value)) + "'</font>"; else if (sKey == "Priority") retVal += " <font color='#ff0000'>'" + Mediachase.IBN.Business.Common.GetPriority(int.Parse(ibr.Value)) + "'</font>"; else if (sKey == "Severity") retVal += " <font color='#ff0000'>'" + Mediachase.IBN.Business.Common.GetIncidentSeverity(int.Parse(ibr.Value)) + "'</font>"; else if (sKey == "GeneralCategories" && ibr.Value.Length > 0) { retVal += " <font color='red'>"; string[] _values = ibr.Value.Split(';'); for (int i = 0; i < _values.Length; i++) { string sId = _values[i]; if (i > 0) { if (ibr.RuleType == IncidentBoxRuleType.Contains || ibr.RuleType == IncidentBoxRuleType.NotContains) retVal += " " + LocRM.GetString("tAND") + " "; if (ibr.RuleType == IncidentBoxRuleType.IsEqual || ibr.RuleType == IncidentBoxRuleType.NotIsEqual) retVal += " " + LocRM.GetString("tOR") + " "; } retVal += "'" + Mediachase.IBN.Business.Common.GetGeneralCategory(int.Parse(sId)) + "' "; } retVal += "</font>"; } else if (sKey == "IncidentCategories" && ibr.Value.Length > 0) { retVal += " <font color='red'>"; string[] _values = ibr.Value.Split(';'); for (int i = 0; i < _values.Length; i++) { string sId = _values[i]; if (i > 0) { if (ibr.RuleType == IncidentBoxRuleType.Contains || ibr.RuleType == IncidentBoxRuleType.NotContains) retVal += " " + LocRM.GetString("tAND") + " "; if (ibr.RuleType == IncidentBoxRuleType.IsEqual || ibr.RuleType == IncidentBoxRuleType.NotIsEqual) retVal += " " + LocRM.GetString("tOR") + " "; } retVal += "'" + Mediachase.IBN.Business.Common.GetIncidentCategory(int.Parse(sId)) + "' "; } retVal += "</font>"; } else { if (ibr.RuleType == IncidentBoxRuleType.Contains || ibr.RuleType == IncidentBoxRuleType.IsEqual || ibr.RuleType == IncidentBoxRuleType.NotContains || ibr.RuleType == IncidentBoxRuleType.NotIsEqual) { retVal += " <font color='red'>"; string[] _values = ibr.Value.Split(';'); for (int i = 0; i < _values.Length; i++) { string sId = _values[i]; if (i > 0) { if (ibr.RuleType == IncidentBoxRuleType.Contains || ibr.RuleType == IncidentBoxRuleType.NotContains) retVal += " " + LocRM.GetString("tAND") + " "; if (ibr.RuleType == IncidentBoxRuleType.IsEqual || ibr.RuleType == IncidentBoxRuleType.NotIsEqual) retVal += " " + LocRM.GetString("tOR") + " "; } retVal += "'" + sId + "' "; } retVal += "</font>"; } else retVal += " <font color='#ff0000'>'" + ibr.Value + "'</font>"; } } break; case IncidentBoxRuleType.Function: retVal += "<b>" + GetRuleType(ibr.RuleType) + "</b>"; IncidentBoxRuleFunction brf = IncidentBoxRuleFunction.Load(int.Parse(ibr.Key)); retVal += " <font color='#0000ff'>" + brf.Name + "( </font>"; retVal += "<font color='#ff0000'>" + ibr.Value + "</font><font color='#0000ff'> )</font>"; break; default: break; } return retVal; }