Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#
Article

SpamAssassin Rule Analyzer

Rate me:
Please Sign up or sign in to vote.
1.30/5 (4 votes)
2 Aug 20064 min read 34.4K   588   11   8
Imports SpamAssassin rule files to xml format and simulates the rules.

Introduction

I work for a company that among other things, have a solicited email sender product. We send emails for the biggest e-commerce, internet portals and banks companies in Brazil. We started to have a lot of problems with our clients emails campaigns, because of their limited or non sense technique on making those campaigns as they were beeing marked as spams.

So, this project started up as a tool for this product on behalf to help and/or educate our clients to create better campaigns.

The original idea was to integrate SpamAssassin service (spamd) with our product to make this analysis, but after all the implementation dificulties to integrate it to our system and the fact that we were only going to use few of those rules, We decided to create our own analyzer using SpamAssassin rules.
 

Using the code

First of all, I would like to make an observation conserning the parser. The parser is based on the file br_rules.cf which is in the package and I know it sucks so far. By next week I will make a more generic one, or if anyone could help improving it, I'll appreciated. The important thing is that the application and library are based on the xml format (and file), not actually the parser.

OK... This project contains two VS projects:

1) UnearSpamParser library.

2) Windows application to test the library.

About the UnearSpamParser library:

Classes:

BaseCollection.cs - Base class for the SpamRuleList and SpamMetaLineList.

SpamRuleMetaLineList - List of Spam Rule Meta Line Objects.

SpamRuleList - List of Spam Rule Objects.

SpamMetaLine - class used when parsing the file. Each object represents a line in the br_rules.cf file, that will be used to build the SpamRule object later.
Properties:
Type - Area that the rule is applied or property definition of the rule (i.e. rawbody, uri, score, describe, etc...)
RuleName - Name of the rule.
MetaText - Is the value of the property of the rule. (i.e. at the "describe" definition, the meta text would be the description string. At the rawbody definition would be the regular expression.)

SpamRule - Represents the SpamAssassin Rule object.
Properties:
Type - Area that the rule is applied or property definition of the rule (i.e. rawbody, uri, score, describe, etc...)
Name - Name of the rule.
Description - description of the rule.
RegExExpression - Regular expression of the rule.
Score - the score of the rule.
Result - Returns the 0 or the score after the execution of the rule.
Locale - Locale that the rules applies to (Not beeing used so far).
Source - Origin of the rule (SpamAssassin, etc...). Also, it's not being used so far.
IsMeta< - Returns whether  the rule is a Meta rule.
IsSubRule - Returns whether  the rule is a subrule of a meta rule.
IsRegExValid - Returns whether the Regular Expression is valid.
Scored - Returns whether the rule scored.

Method:
ExecuteRule - overloaded method that executes the rule and sets the Result property if the rule scored.

SpamFileReader
Properties:
FileName - Name of the file to read.

Methods
ReadFromFile - reads and parses the .cf file. As written before, it needs to be improved since it's only parsing the br_rules.cf file.

ReadFromXml - reads the xml file

SaveSpamRuleToXML - Saves Spam Rules to a XML file.

SaveSpamRuleMetaLineXML - Saves Spam Meta Line list to a XML file.

GetLine - Gets the rule line from the .cf file.

GetType - Gets the rule type from the .cf file.

GetRuleName - Gets the rule name.

GetRuleMetaText - Gets the rule meta text from the .cf file.

About the windows application:

First of all, I'm sorry to tell you all, but the interface comunication is in Portuguese language but I'll write down the translation. Portuguese is not that hard though :-)

  • Geral (tab) - General
  • Simular Regras - Simulate Rules
  • Importação das Regras - Rule Import
  • Importar... - Import...
  • Salvar XML - Save XML
  • Testar Mensagens - Test Messages
  • Executar Regras - Execute Rules
  • Resultados - Results
  • Regra - Rule
  • Nome - Name
  • Descrição - Description
  • Simular - Simulate

Image 1

The first step is to import a file (there's a sample of the XML and the .cf files within the zip)files)

Image 2

After the file has been imported, just paste the email message and execute it. It will appear a list of the rules scored for message if it applies to any.

Image 3

To test a specific rule, you need to go to the second tab ("Simular Regras").

After the file has been imported, the list of rules should be displayed at the list box on the right.

Image 4

When you select a rule, it will display all the properties as seen bellow

Image 5

Just put some test to simulate the rule and try to adjust it with the RegEx textbox.

Image 6

Image 7

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoes this work with the existing SpamAssassin rules? Pin
matply20-Aug-09 21:30
matply20-Aug-09 21:30 
AnswerRe: Does this work with the existing SpamAssassin rules? Pin
RafaMiranda30-Mar-10 8:57
RafaMiranda30-Mar-10 8:57 
GeneralRe: Does this work with the existing SpamAssassin rules? Pin
saadallali16-Aug-12 1:39
saadallali16-Aug-12 1:39 
GeneralEnglish Pin
Sixcode10-Aug-06 7:45
Sixcode10-Aug-06 7:45 
GeneralRe: English Pin
RafaMiranda10-Aug-06 8:14
RafaMiranda10-Aug-06 8:14 
GeneralRe: English Pin
Sixcode17-Aug-06 12:30
Sixcode17-Aug-06 12:30 
QuestionLinks? Pin
fwsouthern2-Aug-06 18:21
fwsouthern2-Aug-06 18:21 
AnswerRe: Links? Pin
RafaMiranda3-Aug-06 8:40
RafaMiranda3-Aug-06 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.