custom_thumb_1.png

Spam comments are annoying and sometimes looking at comment allowed by Akismet or some other anti-spam service you think – I’m sure I would do better. It is just so hard to get the plumbing in or I would made my very own solution. With BlogEngine.Net 1.6 it is not, you can easily implement your own anti-spam filter or add existing if you like it more than built-in Akismet. Here is how.

First, open BE 1.6 solution in VS or VWD or whichever tool you are using to work with source code in .NET. Create new class in the App_Code folder, I called mine SpamBlocker. This class should implement ICustomFilter interface, it has just 4 members and looks like this:

namespace BlogEngine.Core
{
    public interface ICustomFilter
    {
        bool Initialize();
        bool Check(Comment comment);
        void Report(Comment comment);
        bool FallThrough { get; }
    }
}

Below is very simple implementation. It based on idea that most reliable way to identify spam is to check if comment contains link to site selling products or services and known for hiring spammers to push their ads all over the internet.

using System.Collections.Specialized;
using BlogEngine.Core;

public class SpamBlocker : ICustomFilter
{
    private static bool _passThrough = true;
    static StringCollection MockService();
    StringCollection spammers = new StringCollection();
    spammers.Add("conference-call-providers.co.uk");
    spammers.Add("telebisyonserye.info");
    spammers.Add("qmw.com.au");
    return spammers;    

    public bool Initialize()    
    {
        if(MockService().Count > 0) return true;
        return false;
    }
    public bool Check(Comment comment)
    {
        bool spam = false;
        foreach (string s in MockService())
        {
            if(comment.Content.Contains(s))
            {
                spam = true;
                break;
            }
        }
        _passThrough = (spam) ? false : true;
        return spam;
    }
    public void Report(Comment comment)
    {
        if(comment.IsApproved)
        {
            foreach (string s in MockService())
            {
                if(comment.Content.Contains(s))
                {
                    MockService().Remove(s);
                }
            }
        }
    }
    public bool FallThrough
    {
        get { return _passThrough; }
    }
}

Mock service has few sites that spammers promote. Obviously, you would have to put some more work here for real deal, but for now it will do. All we need is a way to identify spam. For the sake of this example, if comment mentions any of these sites – it is spam.

Now lets go over each method and explore what it does.

Initialize() – here you can verify that service is running, do authentication or any other kind of required verification. If return true, that means you are ready to handle comments. We just check if we have any spam sites in the list, if we do – good to go.

Check(Comment comment) – this is where you do your custom thing checking if comment is spam. BlogEngine will pass comment to this method, so you can examine all it’s properties. For simplicity, we check if comment has reference to any of spam sites in the list. If it has, we return “true” meaning that comment is spam and set fall through to false (more on it later).

Report(Comment comment) – this is a way to provide feedback to the service, so that you can make it smarter and identify spam better. If blogger restores comment marked by SpamBlocker as spam, we remove it from the list of spam sites. In the real world, here you can examine comment and try to figure out why you did a mistake marking it as spam or verifying that it is not spam and try to put more smarts in your service/filter.

FallThrough – blog might run several custom filters, and we need a way for them to play well together. When fall through set to true, you saying “I think it is spam/not spam, but I’m ok with others to check on it and provide second opinion”. In this case, last judge wins – so the order in which you set filters to run (priority) is important.

filters_thumb.png spamfolder_thumb_1.png

Here we pretty sure that when comment has spam site in it – it is spam. So we set fall through to false, telling BlogEngine that, in this case, we want to be the ultimate judge and don’t pass comment to other custom services down the list.

If you go and make a comment with one of those sites in the comment body, it will be rejected by SpamBlocker. So here you have it – your very own custom anti-spam filter in 60 lines of code. Ok, it’ll be much more lines when you done with custom logic to identify spam, and you might even implement web service and make it available to other BlogEngine users for this. But handle it on BlogEngien side is a snap.

One more thing, if you want blogger to be able turn your service on and off, you can easily do so by implementing it as extension, just the way Akismet is implemented in BlogEngine. Then, blogger can turn it off without removing it from the web site all together. I hope you’ll try it for yourself, and good luck fighting those nasty spam comments.