/*
* Created on 4/01/2005
*
*/
package org.jasen.plugins;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import org.jasen.core.MultiPointTestResult;
import org.jasen.core.PointTestResult;
import org.jasen.core.calculators.CompoundCalculator;
import org.jasen.core.engine.Jasen;
import org.jasen.error.JasenException;
import org.jasen.interfaces.JasenMessage;
import org.jasen.interfaces.JasenPlugin;
import org.jasen.interfaces.JasenTestResult;
import org.jasen.interfaces.ParserData;
import org.jasen.interfaces.ProbabilityCalculator;
import org.jasen.interfaces.ReceivedHeaderParser;
import org.jasen.util.TextUtils;
/**
* <p>
* Looks for excessive numbers of anomalous characters like '!' and '|'.
* </p>
* @author Jason Polites
*/
public class AnomalousCharacterScanner implements JasenPlugin {
private float max = 0.9f;
private float min = 0.5f;
private int[] thresholds = null;
private char[] chars = null;
private ProbabilityCalculator calculator;
/**
*
*/
public AnomalousCharacterScanner() {
super();
}
/* (non-Javadoc)
* @see org.jasen.interfaces.JasenPlugin#init(java.util.Properties)
*/
public void init(Properties properties) throws JasenException {
if(properties != null) {
String strMin = properties.getProperty("min");
String strMax = properties.getProperty("max");
String strThresh = properties.getProperty("thresholds");
String strChars = properties.getProperty("chars");
String strCalc = properties.getProperty("calculator");
if(strMin != null) {
min = Float.parseFloat(strMin);
}
if(strMax != null) {
max = Float.parseFloat(strMax);
}
if(strThresh != null) {
String[] arrThresholds = strThresh.split(",\\s*");
if(arrThresholds != null) {
thresholds = new int[arrThresholds.length];
for (int i = 0; i < arrThresholds.length; i++) {
thresholds[i] = Integer.parseInt(arrThresholds[i]);
}
}
}
if(strChars != null) {
String[] arrChars = strChars.split(",\\s*");
if(arrChars != null) {
chars = new char[arrChars.length];
for (int i = 0; i < arrChars.length; i++) {
chars[i] = arrChars[i].charAt(0);
}
}
}
if(strCalc != null) {
try {
calculator = (ProbabilityCalculator)Class.forName(strCalc).newInstance();
} catch (InstantiationException e) {
throw new JasenException(e);
} catch (IllegalAccessException e) {
throw new JasenException(e);
} catch (ClassNotFoundException e) {
throw new JasenException(e);
}
}
else
{
calculator = new CompoundCalculator();
}
}
}
/*
* (non-Javadoc)
* @see org.jasen.interfaces.JasenPlugin#destroy()
*/
public void destroy() throws JasenException {}
/* (non-Javadoc)
* @see org.jasen.interfaces.JasenPlugin#test(org.jasen.core.Jasen, javax.mail.internet.MimeMessage, org.jasen.interfaces.JasenMessage, org.jasen.interfaces.ParserData, org.jasen.interfaces.ReceivedHeaderParser)
*/
public JasenTestResult test(Jasen engine, MimeMessage rawMessage,
JasenMessage parsedMessage, ParserData data,
ReceivedHeaderParser parser) throws JasenException {
// Look into the text part
char[] charData = null;
if(chars != null && thresholds != null && chars.length == thresholds.length) {
PointTestResult result = null;
MultiPointTestResult multiResult = new MultiPointTestResult(calculator, min);
int count = 0;
for (int i = 0; i < chars.length; i++) {
result = new PointTestResult(min, max, thresholds[i]);
count += TextUtils.countChars(parsedMessage.getTextPart(), chars[i]);
count += TextUtils.countChars(data.getHtmlAsText(), chars[i]);
result.setPoints(count);
multiResult.addPointResult(result);
count = 0;
}
return multiResult;
}
else
{
return new PointTestResult();
}
}
}