/* Polarbar Java spam filter. The getResult method returns true if the From: header line contains any of the spammer email addresses found in the file named as a parameter in the filter setup. This class can be used for more than one filter in a filter list. Just create filters with different names and refer them to the "spam" class. Presumably you would also want to specify different file names for the spammer lists for each different filter. Warning: Before Polarbar 1.22, the spam() constructor is called only once the first time the filter class is used after a filter using it is installed in the filter list. For Polarbar 1.22 or later, the constructor is called at the start of each filter session, allowing the file to be re-read if the file has changed or the file name has changed without having to shut down Polarbar. Author: J S Worthington fe31@jsw.gen.nz Version: 5-Feb-2002 Copyright (c) 2002 J S Worthington May be freely used by anyone, including borrowing or modifying the source code, but I would appreciate it if you acknowledged my authorship of any borrowed bits. */ import innoval.mailer.FilterMessage; import java.io.*; public class spam { /* Set by the constructor when it is called, cleared by getResult when it has done the work required after a constructor call. */ private boolean constructorCalled = true; /* The name of a text file containing strings (one per line) which are used to match against From: lines to see if the email is a spam. This file name is entered in the "Optional method call string argument" field of the "Mail Filter Detail" page when setting up the Java filter. */ private String spammersFileName = ""; /* The list of spam strings that identify From: address lines as belonging to a spammer. */ private String [] spammers; /* The timestamp giving the time the file containing the spam strings (spammersFileName) was last modified. */ private long spammersLastModified = 0; /* In the event of a problem creating the spammers list, set up a safe null list that will not cause the filter to keep generating exceptions. */ private void provideSafeNullSpammersList() { spammers = new String [1]; spammers[0] = ""; } /* Method called by Polarbar for each email that is to be filtered. */ public boolean getResult( /* Parameter provided by Polarbar giving the email message to be checked by this filter. See the Sample.java sample filter file provided with Polarbar for documentation of this. */ FilterMessage msg ) { if (constructorCalled) { constructorCalled = false; if (msg.parameterValues.length != 1) { if (msg.parameterValues.length > 1) { System.out.println( "Java spam filter error: Too many parameters" ); } else { System.out.println( "Java spam filter error: Spammer list file name parameter" + " required" ); } provideSafeNullSpammersList(); return false; } if (spammersFileName != msg.parameterValues[0]) { /* New file specified as parameter to the filter - ensure the new file is read. */ spammersLastModified = 0; } spammersFileName = msg.parameterValues[0]; try { /* If spammersFileName does not specify a path, then the file should be in the default directory that Polarbar is run from. */ File spammersFile = new File(spammersFileName); if (!spammersFile.isFile()) { System.out.println( "Java spam filter error: File " + spammersFile.getAbsolutePath() + " not found" ); provideSafeNullSpammersList(); return false; } /* Local copy of the last modified timestamp for spammersFile. Used so the timestamp does not have to be fetched more than once, as operating system file operations always take quite a while. */ long lastModified = spammersFile.lastModified(); if (spammersLastModified != lastModified) { /* Line input capable filtered input file for spammersFile. */ BufferedReader in = new BufferedReader(new FileReader(spammersFile)); /* Count of the number of lines in the "spammersFile" file. */ int lineCount = 0; /* The current line read from spammersFile. */ String line; spammersLastModified = lastModified; while ((line = in.readLine()) != null) { if (line.length() != 0) { ++lineCount; } } in.close(); spammers = new String [lineCount]; in = new BufferedReader(new FileReader(spammersFileName)); for (int index = 0; index < lineCount; ) { if ((line = in.readLine()) == null) { /* The file has changed since it was read above! */ break; } if (line.length() != 0) { spammers[index++] = line; } } in.close(); } } catch (IOException e) { provideSafeNullSpammersList(); e.printStackTrace(); return false; } } for (int index = 0; index < spammers.length; ++index) { if (msg.FROM.indexOf(spammers[index]) >= 0) { return true; } } return false; } /* Constructor. */ public spam() { constructorCalled = true; } }