Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C#

SpamAssassin Rule Analyzer

Rate me:
Please Sign up or sign in to vote.
1.30/5 (4 votes)
2 Aug 20064 min read 34.5K   588   11  
Imports SpamAssassin rule files to xml format and simulates the rules.
using System;
using System.Text.RegularExpressions;

namespace UnearSpamParserLib
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[Serializable]
	public class SpamRule
	{
		#region Constructor

		public SpamRule()
		{
			
		}

		#endregion

		#region Public Properties

		public string Type
		{
			get
			{
				return this.type;
			}
			set
			{
				this.type = value;
			}
		}

		public string Name
		{
			get
			{
				return this.name;
			}
			set
			{
				this.name = value;
			}
		}

		public string Description
		{
			get
			{
				return this.description;
			}
			set
			{
				this.description = value;
			}
		}

		public string RegexExpression
		{
			get
			{
				return this.regexExpression;
			}
			set
			{
				this.regexExpression = value;
			}
		}

		public float Score
		{
			get
			{
				return this.score;
			}
			set
			{
				this.score = value;
			}
		}

		public float Result
		{
			get
			{
				return this.result;
			}
			set
			{
				this.result = value;
			}
		}

		public string Locale
		{
			get
			{
				return this.locale;
			}
			set
			{
				this.locale = value;
			}
		}

		public string Source
		{
			get
			{
				return this.source;
			}
			set
			{
				this.source = value;
			}
		}

		public bool IsMeta
		{
			get
			{
				if(this.RegexExpression != null && !this.RegexExpression.Equals(string.Empty))
					return (this.RegexExpression.IndexOf("BR_") > 0);
				else
					return false;
			}
		}

		public bool IsSubRule
		{
			get
			{
				if(this.Name != null && !this.Name.Equals(string.Empty))
					return (this.Name.IndexOf("__BR") > 0);
				else
					return false;
			}
		}

		public bool IsRegExValid
		{
			get
			{
				return(this.RegexExpression != null && !string.Empty.Equals(this.RegexExpression));
			}
		}

		public bool Scored
		{
			get
			{
				return this.scored;
			}
		}

		#endregion

		#region Public Methods

		public override string ToString()
		{
			return this.Name;
		}

		/// <summary>
		/// Executes the rule.
		/// </summary>
		/// <param name="text">Text to apply the rule.</param>
		public void ExecuteRule(string text)
		{
			try
			{
				if(!this.IsSubRule && !this.IsMeta)
				{
					if(this.IsRegExValid)
					{
						Regex regex = new Regex( this.RegexExpression, RegexOptions.IgnoreCase);
						if(regex.IsMatch(text))
						{
							this.Result = this.Score;
							this.scored = true;
						}
						else
						{
							this.Result = 0;
							this.scored = false;
						}
					}
				}
			}
			catch(Exception ex)
			{
				string message = ex.Message;
			}
		}

		/// <summary>
		/// Executes the rule.
		/// </summary>
		/// <param name="text">Text to apply the rule.</param>
		/// <param name="ruleRegEx">Regular expression of the rule.</param>
		public void ExecuteRule(string text, string ruleRegEx)
		{
			try
			{
				if(!this.IsSubRule && !this.IsMeta)
				{
					if(ruleRegEx != null && !ruleRegEx.Equals(string.Empty))
					{
						Regex regex = new Regex( ruleRegEx, RegexOptions.IgnoreCase);
						if(regex.IsMatch(text))
						{
							this.Result = this.Score;
							this.scored = true;
						}
						else
						{
							this.Result = 0;
							this.scored = false;
						}
					}
				}
			}
			catch(Exception ex)
			{
				string message = ex.Message;
			}
		}

		#endregion

		#region Fields

		private string type;
		private string name;
		private string description;
		private string regexExpression;
		private float score;
		private string locale;
		private string source;
		private bool scored;
		private float result;

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions