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

using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;
using CommunityServer.Galleries.Components;
using CommunityServer.Files.Components;

namespace CommunityServer.Spam.Rules
{
	/// <summary>
	/// Provides methods available for spam detection.
	/// </summary>
	public class SpamRuleHelper
	{
		/// <summary>
		/// Hide default constructor
		/// </summary>
		private SpamRuleHelper()
		{
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="word"></param>
		/// <param name="post"></param>
		/// <returns></returns>
		public static int CountBadWord(string word, Post post)
		{
            int count = 0;
            if (word.Trim().Length > 0)
            {
                try
                {


                    Regex r = new Regex(word.Trim(), RegexOptions.IgnoreCase);

                    count = r.Matches(post.Body).Count;
                    count += r.Matches(post.Subject).Count;

                    // Check for Blog specific properties
                    WeblogPost wp = post as WeblogPost;
                    if (wp != null)
                    {
                        if (wp.HasSubmittedUserName)
                            count += r.Matches(wp.SubmittedUserName).Count;

                        if (!Globals.IsNullorEmpty(wp.TrackBackName))
                            count += r.Matches(wp.TrackBackName).Count;

                        if (wp.HasTitleUrl)
                            count += r.Matches(wp.TitleUrl).Count;
                    }

                    // Check for Photo Gallery specific properties
                    GalleryPost gp = post as GalleryPost;
                    if (gp != null)
                    {
                        if (gp.HasSubmittedUserName)
                            count += r.Matches(gp.SubmittedUserName).Count;

                        if (!Globals.IsNullorEmpty(gp.TrackBackName))
                            count += r.Matches(gp.TrackBackName).Count;

                        if (gp.HasTitleUrl)
                            count += r.Matches(gp.TitleUrl).Count;
                    }
			
                    // Check for File Gallery specific properties
                    EntryComment ec = post as EntryComment;
                    if (ec != null)
                    {
				
                        if (ec.HasSubmittedUserName)
                            count += r.Matches(ec.SubmittedUserName).Count;

                        if (ec.HasTitleUrl)
                            count += r.Matches(ec.TitleUrl).Count;
                    }
                }
                catch(Exception ex)
                {
		            EventLogs.Warn("The following term could not be processed by a spam rule " + word,"Spam Rules",444, ex,CSContext.Current.SettingsID);
                }
            }


			return count;
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="content"></param>
		/// <returns></returns>
		public static int CountLinks(string content)
		{
			string pattern = @"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])";

			return Regex.Matches(content, pattern, RegexOptions.IgnoreCase).Count;
		}


		/// <summary>
		/// Determine the number of posts that have been created from a specified IP address
		/// during a specified duration.
		/// </summary>
		/// <param name="ipAddress">The IP address filter used to search for posts</param>
		/// <param name="duration">The number of seconds to scan for posts</param>
		/// <returns>The number of recent posts from the ip address</returns>
		public static int GetPostCountFromIPAddress(string ipAddress, int duration)
		{
			// Pass in criteria
			SqlParameter[] parameters = new SqlParameter[3];
			parameters[0] = SqlHelper.CreateParameter("@IPAddress", SqlDbType.NVarChar, 255, ipAddress);
			parameters[1] = SqlHelper.CreateParameter("@Duration", SqlDbType.Int, duration);
			parameters[2] = SqlHelper.CreateParameter("@SettingsID", SqlDbType.Int, CSContext.Current.SettingsID);

			// Calculate points
			int postCount = (int)SqlHelper.ExecuteScalar("spam_GetRecentPostCountFromIP", parameters);

			return postCount;
		}
	}
}
