Keyvan Nayyeri

God breathing through me

CS Dev Guide: How to Write a Spam Rule

This week we haven't had CS Dev Guide show but let's finish it with a useful show for the Community Server world to kill spams and help our news man to feed his Daily News.

Community Server 2.1 came with great features for spam blocking and this functionality is absolutely better than 2.0.  Nice point about Community Server 2.1 is you can write your own spam rules to fight with spammers.

I first highlight the steps to write a spam rule:

  • Create a Class Library project.
  • Implement SpamRule abstract class or one of its children such as BlogSpamRule.
  • Compile your class into a DLL file.
  • Deploy this DLL file to your /bin folder.
  • Go to Spam Blocker page at http://[YourAddress]/ControlPanel/Tools/ManageSpamRules.aspx and configure your spam rule.

I give the details of these steps by writing a sample blog spam rule that checks the blog commenter's IP and gives it some points if he's in a country black list.  This black list is given by user in spam rule configurations as a semicolon delimited list of country abbreviations.  I use a free WebService by HostIP.Info located at here to get my client's country.

First step to write a spam rule is implementing an abstract class.  For my blog spam rule, I implement CommunityServer.Spam.BlogSpamRule abstract class.

using System;

using CommunityServer.Blogs.Components;

using CommunityServer.Components;

using CommunityServer.Spam;

using System.Collections;

using System.Xml;

 

namespace Keyvan.SpamRules

{

    public class CountryIPSpamRule : BlogSpamRule

    {

This abstract class needs one method and three properties to be implemented:

  • Name (string property): The name of spam rule that will be shown in Control Panel.
  • Description (string property): Description of spam rule that will be shown in Control Panel.
  • RuleID (Guid property): A static Guid as an identifier for spam rule.
  • GetAvailableSettings (ArrayList function): ArrayList of RuleSettings objects to provide the necessary value in popup window for spam rule configuration in Control Panel.

By implementing these methods and properties you can see your spam rule in Control Panel but it won't work until you override CalculateSpamRule() integer function from base class.  This method is the heart of your spam rule and returns the number of points that will be assigned to a post.

Below I override those above properties and methods except CalculateSpamRule() method.  GetAvailableSettings() must return an ArrayList of RuleSetting objects.  These objects are the information that will be displayed on a popup window when you click on Configure Button for your spam rule in Control Panel.

private static Guid ruleGuid =

    new Guid("A909A1E9-18E8-4759-B7A3-EF7278F1510F");

 

public override string Description

{

    get { return "Type something here!"; }

}

 

public override ArrayList GetAvailableSettings()

{

    ArrayList ruleList = new ArrayList();

 

    ruleList.Add(new RuleSetting

        (ruleGuid, "countries",

        "Enter the list of country abbreviations", ""));

    ruleList.Add(new RuleSetting

        (ruleGuid, "points",

        "Number of points to assign to post", "3"));

 

    return ruleList;

}

 

public override string Name

{

    get { return "Country IP Spam Rule"; }

}

 

public override Guid RuleID

{

    get { return ruleGuid; }

}

Here is my code for CalculateSpamScore() method and IsListed() private function that I added to my code.  GetSettingValue() is a function that gets a key value for one of spam rule parameters as named by GetAvailableSettings() method and specified by end user in Control Panel and returns a string value of what user has put there.

public override int CalculateSpamScore

    (WeblogPost blogPost, CSPostEventArgs e)

{

    base.CalculateSpamScore(blogPost, e);

 

    if (blogPost.BlogPostType == (BlogPostType.Comment |

        BlogPostType.Trackback))

    {

        if (IsListed(CSContext.Current.HostPath))

        {

            EventLogs.Info

                ("Country IP Spam Rule proceed on a feedback.",

                "Country IP Spam Rule",

                5698);

 

            return Convert.ToInt32

                (base.GetSettingValue("points"));

        }

    }

    return 0;

}

 

private bool IsListed(string IP)

{

    XmlDocument doc = new XmlDocument();

    string Query = "http://api.hostip.info/?ip=" + IP;

 

    doc.Load(Query);

 

    XmlNamespaceManager nsManager =

        new XmlNamespaceManager(doc.NameTable);

    nsManager.AddNamespace

        ("hostip", "http://www.hostip.info/api");

 

    string query = "//hostip:countryAbbrev";

    XmlNode node = doc.SelectSingleNode(query, nsManager);

 

    string country = null;

    if (node != null)

    {

        country = node.InnerText;

    }

 

    string[] countries = GetSettingValue("countries").Split(';');

 

    if (Array.BinarySearch(countries, IP) >= 0)

    {

        return true;

    }

    return false;

}

Now I compile my Class and deploy its DLL file to /bin folder and navigate to my Spam Blocker page in Control Panel to see my new spam rule:

Spam Rule

Spam Rule

Note that I could use better Event Logging mechanism for my spam rule but ignored it to have a simpler code.

10 Comments

Dave Burke
Sep 15, 2006 11:16 AM
#
Your CS Dev Guides keep getting better and more detailed than ever! I don't know where you find the time. Another great guide. Thanks!

Keyvan Nayyeri
Sep 15, 2006 11:38 AM
#
We should find time in our life. This is our main job ;-) Thanks my man :-)

Community Server Daily News
Sep 15, 2006 2:04 PM
#
news of the day a grab bag for what's happening in Community Server Because Keyvan Nayyeri made a special

Announcements
Sep 15, 2006 2:43 PM
#
This week... Dave Stokes releases a 100-page Word document detailing the installation of Community Server

J-O Eriksson
Sep 15, 2006 5:27 PM
#
Keyvan rules!! ;-) Where would we be without the CS Dev Guide?

Keyvan Nayyeri
Sep 15, 2006 11:31 PM
#
Somewhere better than here ;-)

J-O Eriksson
Sep 16, 2006 1:39 AM
#
Many of us would be lost in CS Dev Land, wihtout any docs to lead the way.

Thomas Freudenberg
Sep 17, 2007 1:37 PM
#
A few days ago Phil Haack wrote about Honeypot Captcha : At the same time, spam bots tend to ignore CSS

Keyvan Nayyeri
Oct 03, 2007 9:36 AM
#
While spammers are eating our mailboxes, blogs and sites, we (developers) are working to fight against

Back in 2006 and when I was writing my CS Dev Guide post series, I wrote a part about writing spam rules

Leave a Comment





Ads Powered by Lake Quincy Media Network