There are no comments yet...Kick things off by filling out the form below.
How to Write a Spam Rule for Community Server 2008
Back in 2006 and when I was writing my CS Dev Guide post series, I wrote a part about writing spam rules for Community Server.
Speaking of Waegis and my recent wrestling with spam rule integrations for Community Server 2007.1 and 2008, I thought it's worthwhile to revisit that post and talk about some changes in a new post.
Since Community Server 2007.1 Telligent applied Dynamic Configuration to spam rules and replaced the traditional configuration. Last week I introduced Dynamic Configurations and here is a good example of them to get in more details about this component in an upcoming post.
The main changes are in the way that you deal with configurations for your spam rules and the rest is almost same as the prior versions. Like the past, you need to inherit from SpamRule abstract base class or one BlogSpamRule, ForumSpamRule or MediaSpamRule classes that are derived from it in order to develop a spam rule.
In Community Server 2008 SpamRule, itself, is derived from ConfigurableAddon class derived from ConfigurationDataBase class located in Telligent.DynamicConfiguration namespace.
Here you need to override three required properties including Name, Description and RuleID. First two properties are string values that are self-explanatory and the third one is a GUID to identify the rule and is used in data manipulations as well.
In Community Server 2008 spam rules are considered like an add-on and override their properties as well (refer to the above discussion about inheritance hierarchy). They also use the same mechanism to store dynamic configuration data into database as add-ons. You don't need to take care about add-on properties and can leave them as are. You also must override a RatePost method in the same way as you were doing this for spam rules in Community Server 2.1.
These were required properties and methods to override but if you want to get settings from end users (which is very common) then you also need to override a new method related to dynamic configurations called GetAvailablePropertyGroups.
Regarding these theories I implement a simple spam rule that counts the number of links in a post and if they exceed a maximum number then it assigns points to each extra link. This is a built-in rule in Community Server and I just redo it as a sample.
public class LinksCountRule : SpamRule
{
private readonly static Guid id = new Guid("D0E79059-4A4B-48d4-AF09-0A19F7FD04DD");
private static readonly int defaultMaxLinks = 2;
private static readonly int defaultPoints = 1;
public LinksCountRule()
{
}
public override string Name
{
get { return "Links Count Spam Rule"; }
}
public override string Description
{
get { return "Identifies spams based on the number of links in the post"; }
}
public override Guid RuleID
{
get { return id; }
}
public override PropertyGroup[] GetAvailablePropertyGroups()
{
PropertyGroup[] propertyGroups = new PropertyGroup[] {
new PropertyGroup("linksCountSettings", "Spam Settings", 1) };
propertyGroups[0].Properties.Add(new Property
("maxlinks", "Max Links Numbers", PropertyType.Int,
0, defaultMaxLinks.ToString()));
propertyGroups[0].Properties.Add(new Property
("points", "Points", PropertyType.Int, 1,
defaultPoints.ToString()));
return propertyGroups;
}
public override int RatePost(Post post, CSPostEventArgs e)
{
int maxLinks = GetIntValue("maxlinks", defaultMaxLinks);
int points = GetIntValue("points", defaultPoints);
if (maxLinks > 0)
{
int linksCount = ParseAndGetLinks(post.Body).Count;
if (linksCount > maxLinks)
{
EventLogs.Info(string.Format("{0} found a spam!",
this.Name), "Spam Rules", 5566);
return ((linksCount - maxLinks) * points);
}
}
return 0;
}
private List<string> ParseAndGetLinks(string text)
{
List<string> result = new List<string>();
if (!string.IsNullOrEmpty(text))
{
string pattern = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);
foreach (Match match in matches)
{
result.Add(match.Value);
}
}
return result;
}
}
The implementation would be familiar to Community Server developers. After overriding some properties, I override RatePost method to rate the incoming post.
GetAvailablePropertyGroups function returns an array of PropertyGroup objects that define the properties and fields that you want to enable for users to set in the control panel. Each PropertyGroup defines a tab in the user interface and you can add Property objects for each property. I defined two properties of integer type for the maximum number of links and the number of points to assign. Each property can get a default value as well.
The last point is about retrieving the data for properties in your spam rules. Here you need to use appropriate methods from ConfigurationDataBase base class like GetStringValue, GetIntValue and others to retrieve data with the ID of properties and a default value.
[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.
No Comments : 05.13.08