//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Spam.Rules
{
	/// <summary>
	/// Rates spam factor based on number of occurences of configured bad words
	/// </summary>
	public class BadWordSpamRule : SpamRule
	{
		private static readonly Guid ruleID = new Guid("dbddf6c8-edc6-4b06-8f50-5cf15a3406e6");
		private static readonly string resourceFile = "ControlPanelResources.xml";
		private static readonly int defaultPoints = 2;
		private static readonly int defaultThreshold = 1;
		private static readonly string defaultBadWords = "-online;4u;adipex;advicer;ambien;art works;baccarrat;blackjack;bllogspot;booker;byob;car-rental-;carisoprodol;cartwish;casino;chatroom;china;cialis;coolhu;credit-;cwas;cyclen;cyclobenzaprine;dating-;day-trading;debt-consolidation;download;drug;discreetordering;duty-free;dutyfree;edit1;equityloans;fioricet;flowers-;free;freenet;gambling;health-insurancedeals-;holdem;homeequityloans;homefinance;hotel-deals;hotele-site;hotelse-site;incest;insurance-quotesdeals-;insurancedeals-;jrcreations;levitra;loan;macinstruct;mortgage-4;mortgagequotes;online-gambling;onlinegambling-;ottawavalleyag;ownsthis;paxil;pen1s;penis;pharmacy;phentermine;poker;porn;poze;rental-car-;right-top;ripoffs;roulette;search-browse;sex;shemale;shoes;slot;soma;sysworkds;taboo;teen;texas;thorcarlson;top-site;top-e-;tramadol;trim-spa;ultram;valeofglamorganconservatives;viagra;vioxx;xanax;zolus";

		public BadWordSpamRule()
		{
		}

		public override string Name
		{
			get { return ResourceManager.GetString("spam_WordCountRule_Name", resourceFile); }
		}

		public override string Description
		{
			get { return ResourceManager.GetString("spam_WordCountRule_Description", resourceFile); }
		}

		public override Guid RuleID
		{
			get { return ruleID; }
		}

		public override ArrayList GetAvailableSettings()
		{
			ArrayList settings = new ArrayList();
			settings.Add(new RuleSetting(ruleID, "points", ResourceManager.GetString("spam_WordCountRule_PointsDescription", resourceFile), defaultPoints.ToString()));
			settings.Add(new RuleSetting(ruleID, "threshold", ResourceManager.GetString("spam_WordCountRule_ThresholdDescription", resourceFile), defaultThreshold.ToString()));
			settings.Add(new RuleSetting(ruleID, "badwords", ResourceManager.GetString("spam_WordCountRule_BadWordsDescription", resourceFile), defaultBadWords, 8000) );
			return settings;
		}

		public override int RatePost(Post post, CSPostEventArgs e)
		{
			// Verify that this spam rule should process this post
			if (IsExemptFromSpamRating(post, e))
				return 0;

			// Retrieve setting values
			int points = Globals.SafeInt(this.GetSettingValue("points"), defaultPoints);
			int threshold = Globals.SafeInt(this.GetSettingValue("threshold"), defaultThreshold);

			string badWordString = GetSettingValue("badwords");
			if (badWordString == null)
				badWordString = defaultBadWords;

			// If no bad words are selected, exit method
			if (badWordString.Trim().Length <= 0)
				return 0;

			string[] badWords = badWordString.Split(';');

			// Count bad words
			if (badWords != null && badWords.Length > 0)
			{
				int counter = 0;
				foreach (string word in badWords)
					if (word.Trim().Length > 0)
						counter += SpamRuleHelper.CountBadWord(word.Trim(), post);

				// Score xxx points for each badword above the threshold
				if (counter > threshold)
					return (points * (counter - threshold));
			}

			return 0;
		}
	}
}
