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:

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:

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.

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

10 Comments : 09.15.06

Feedbacks

 avatar
#1
Dave Burke
09.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!
admin avatar
#2
Keyvan Nayyeri
09.15.2006 @ 11:38 AM
We should find time in our life. This is our main job ;-) Thanks my man :-)
 avatar
#3
Community Server Daily News
09.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
 avatar
#4
Announcements
09.15.2006 @ 2:43 PM
This week... Dave Stokes releases a 100-page Word document detailing the installation of Community Server
 avatar
#5
J-O Eriksson
09.15.2006 @ 5:27 PM
Keyvan rules!! ;-) Where would we be without the CS Dev Guide?
admin avatar
#6
Keyvan Nayyeri
09.15.2006 @ 11:31 PM
Somewhere better than here ;-)
 avatar
#7
J-O Eriksson
09.16.2006 @ 1:39 AM
Many of us would be lost in CS Dev Land, wihtout any docs to lead the way.
 avatar
#8
Thomas Freudenberg
09.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
 avatar
#9
Keyvan Nayyeri
10.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