initial signature calculator
authoradash <adash>
Fri, 30 Oct 2009 20:05:49 +0000 (20:05 +0000)
committeradash <adash>
Fri, 30 Oct 2009 20:05:49 +0000 (20:05 +0000)
Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/SpamFilter/SpamFilter.java

diff --git a/Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java b/Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java
new file mode 100644 (file)
index 0000000..0761117
--- /dev/null
@@ -0,0 +1,158 @@
+public class SignatureComputer {
+       public EphemeralSignature sig4; //signature engines
+       public WhiplashSignature sig8; //signature engines
+
+       int[] enginesToUseForCheck;
+
+       public SignatureComputer() {
+               sig4 = new EphemeralSignature(); //default values
+               sig8 = new WhiplashSignature();
+               createEnginesToCheck();
+       }
+
+       /**
+        * constructor to be used when some parsing has already taken place with the
+        * server-provides value <code>randomNumberSeed</code>.
+        * 
+        * @param randomNumberSeed
+        *        a non-negative number used for seeding the random number generator
+        *        before starting to hash values.
+        * @param separator
+        *        how the mail-text should be splitted into lines. (== what chars
+        *        separate 2 lines)
+        */
+       public SignatureComputer(int randomNumberSeed, String separator) {
+               sig4 = new EphemeralSignature(randomNumberSeed,separator);
+               sig8 = new WhiplashSignature();
+               createEnginesToCheck();
+       }
+
+       /**
+        * the constructor to be used most of the time. you can hand over the
+        * seed-string exactly as it is provided by the razor-server.
+        * 
+        * @param seedAndSeparator
+        *        a string containing the seed value for the RNG and a separator list
+        *        (separated by ' <b>- </b>'). default value is
+        *        <code>"7542-10"</code> which means server-seed 7542 and only one
+        *        separator 10 (which is ascii '\n').
+        */
+       public SignatureComputer(String seedAndSeparator) {
+               sig4 = new EphemeralSignature(seedAndSeparator);
+               sig8 = new WhiplashSignature();
+               createEnginesToCheck();
+       }
+
+       /**
+        * 
+        */
+       public void createEnginesToCheck() {
+               enginesToUseForCheck = new int[2];
+        enginesToUseForCheck[0] = 4; //Ephemeral engine
+        enginesToUseForCheck[1] = 8;//Whiplash engine
+       }
+
+       public boolean isSigSupported(int sig) {
+               boolean found = false;
+               for (int i = 0; i < enginesToUseForCheck.length && !found; i++) {
+                       if (enginesToUseForCheck[i] == sig) {
+                               found = true;
+                       }
+               }
+               return found;
+       }
+
+       public boolean isSigSupported(String sig) {
+               return (sig != null && isSigSupported(Integer.parseInt(sig)));
+       }
+
+       public String getDefaultEngine() {
+               return "4";
+       }
+
+       public Vector computeSigs(StringBuffer[] Mails) {
+               if (Mails == null) return null;
+
+               Vector printableSigs = new Vector();
+               for (int mailIndex = 0; mailIndex < Mails.length; mailIndex++) {
+                       StringBuffer mail = Mails[mailIndex];
+
+                       if (mail == null) continue;
+
+            /*
+             * Compute Sig for bodyparts that are cleaned.
+             */
+            for (int engineIndex = 0; engineIndex < enginesToUseForCheck.length; engineIndex++) {
+              int engineNo = enginesToUseForCheck[engineIndex];
+              String[] sig = null;
+
+              switch (engineNo) {
+                case 4:
+                  sig = computeSignature(engineNo,curPart.getCleaned());
+                  break;
+                case 8:
+                  sig = computeSignature(engineNo,curPart.getBody());
+                  break;
+                default:
+                  /*
+                   * for nilsimsa and sha1 wich are no longer supported by
+                   * the server and might be removed someday
+                   */
+                  sig = computeSignature(engineNo,curPart.getCleaned());
+                  break;
+              }//switch engineNo
+
+              if (sig != null && sig.length > 0) {
+                for (int curSigIndex = 0; curSigIndex < sig.length; curSigIndex++) {
+                  String hash = engineNo + ":" + sig[curSigIndex];
+                  curPart.addHash(hash);
+                  printableSigs.add(hash);
+                }
+
+              } else {
+                /* we didn't produce a signature for the mail. */
+              }
+            }//engine
+        }//mails
+        return printableSigs;
+    }//computeSigs
+
+       /**
+        * @param engineNo
+        * @param cleaned
+        * @return
+        */
+       private String[] computeSignature(int engineNo, String mail) {
+               switch (engineNo) {
+                       case 4:
+                               return new String[] { this.sig4.computeSignature(mail) };
+                       case 8:
+                               String cleanedButKeepHTML = Preprocessor.preprocess(mail,Preprocessor.ConfigParams.NO_DEHTML);
+                               return this.sig8.computeSignature(cleanedButKeepHTML);
+                       default:
+                               return null;
+               }
+       }
+
+       public static String[] getCommonSupportedEngines(int serverSupportedEngines) {
+               Vector<String> commonSupported = new Vector<String>();
+               int engineMask = 1;
+               int engineIndex = 1;
+               while (engineIndex < 32) {
+                       boolean serverSupported = (serverSupportedEngines & engineMask) > 0;
+                       boolean clientSupported = isSigSupported(engineIndex);
+                       if (serverSupported && clientSupported) {
+                               commonSupported.add(String.valueOf(engineIndex));
+                       }
+                       //switch to next
+                       engineMask <<= 1; //shift one to left
+                       engineIndex++;
+               }
+               if (commonSupported.size() == 0) {
+                       return null;
+               }
+               String[] result = new String[commonSupported.size()];
+               commonSupported.toArray(result);
+               return result;
+       }
+}
index f65ba8badbe0d7769fbf7995f3e48e4982c7f060..7a9b62b4b7d11121d73cb2b1d7962bef77a0c9dd 100644 (file)
@@ -110,13 +110,16 @@ public class SpamFilter extends Thread {
    **/
   public FilterResult[] checkMail(Mail mail) {
     //Preprocess emails
-      //StringBuffer[] mailStrings = createMailStrings();
+      //StringBuffer[] partsOfMailStrings = createMailStrings();
+      //RazorMail[] razorMails = 
     //Compute signatures
-      //CommunicationEngine checkEngine = getCheckEngine();
-      //SignatureComputer sigComp = new SignatureComputer();
+    SignatureComputer sigComp = new SignatureComputer();
+    Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
+          
     //check with global data structure
+    check(signatures);
 
-    //return results
+    //---- create and  return results --------
     FilterResult[] filterResults = new FilterResult[mailStrings.length];
 
     return filterResults;