I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering and Programming.
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:
Note that I could use better Event Logging mechanism for my spam rule but ignored it to have a simpler code.
Dave Burke
Sep 15, 2006 11:16 AM
#
Keyvan Nayyeri
Sep 15, 2006 11:38 AM
#
Community Server Daily News
Sep 15, 2006 2:04 PM
#
Announcements
Sep 15, 2006 2:43 PM
#
J-O Eriksson
Sep 15, 2006 5:27 PM
#
Keyvan Nayyeri
Sep 15, 2006 11:31 PM
#
J-O Eriksson
Sep 16, 2006 1:39 AM
#
Thomas Freudenberg
Sep 17, 2007 1:37 PM
#
Keyvan Nayyeri
Oct 03, 2007 9:36 AM
#
How to Write a Spam Rule for Community Server 2008
May 13, 2008 1:43 PM
#
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