more changes
[IRC.git] / Robust / src / Benchmarks / Distributed / SpamFilter / SignatureComputer.java
1 public class SignatureComputer {
2   public EphemeralSignature sig4; //signature engines
3   public WhiplashSignature sig8; //signature engines
4
5   int[] enginesToUseForCheck;
6
7   public SignatureComputer() {
8     sig4 = new EphemeralSignature(); //default values
9     sig8 = new WhiplashSignature();
10     createEnginesToCheck();
11   }
12
13   /**
14    * constructor to be used when some parsing has already taken place with the
15    * server-provides value <code>randomNumberSeed</code>.
16    * 
17    * @param randomNumberSeed
18    *        a non-negative number used for seeding the random number generator
19    *        before starting to hash values.
20    * @param separator
21    *        how the mail-text should be splitted into lines. (== what chars
22    *        separate 2 lines)
23    */
24   public SignatureComputer(int randomNumberSeed, String separator) {
25     sig4 = new EphemeralSignature(randomNumberSeed,separator);
26     sig8 = new WhiplashSignature();
27     createEnginesToCheck();
28   }
29
30   /**
31    * the constructor to be used most of the time. you can hand over the
32    * seed-string exactly as it is provided by the razor-server.
33    * 
34    * @param seedAndSeparator
35    *        a string containing the seed value for the RNG and a separator list
36    *        (separated by ' <b>- </b>'). default value is
37    *        <code>"7542-10"</code> which means server-seed 7542 and only one
38    *        separator 10 (which is ascii '\n').
39    */
40   public SignatureComputer(String seedAndSeparator) {
41     sig4 = new EphemeralSignature(seedAndSeparator);
42     sig8 = new WhiplashSignature();
43     createEnginesToCheck();
44   }
45
46   /**
47    * 
48    */
49   public void createEnginesToCheck() {
50     enginesToUseForCheck = new int[2];
51     enginesToUseForCheck[0] = 4; //Ephemeral engine
52     enginesToUseForCheck[1] = 8;//Whiplash engine
53   }
54
55   public boolean isSigSupported(int sig) {
56     boolean found = false;
57     for (int i = 0; i < enginesToUseForCheck.length && !found; i++) {
58       if (enginesToUseForCheck[i] == sig) {
59         found = true;
60       }
61     }
62     return found;
63   }
64
65   public boolean isSigSupported(String sig) {
66     return (sig != null && isSigSupported(Integer.parseInt(sig)));
67   }
68
69   public String getDefaultEngine() {
70     return "4";
71   }
72
73   public Vector computeSigs(Vector EmailParts) {
74     if (EmailParts == null) return null;
75
76     Vector printableSigs = new Vector(); // vector of strings
77     for (int mailIndex = 0; mailIndex < EmailParts.size(); mailIndex++) {
78       String mail = EmailParts.elementAt(mailIndex);
79
80       if (mail == null) continue;
81
82       /*
83        * Compute Sig for bodyparts that are cleaned.
84        */
85       for (int engineIndex = 0; engineIndex < enginesToUseForCheck.length; engineIndex++) {
86         int engineNo = enginesToUseForCheck[engineIndex];
87         String sig = null;
88
89         switch (engineNo) {
90           case 4:
91             sig = computeSignature(engineNo,mail);
92             break;
93           case 8:
94             sig = computeSignature(engineNo,mail);
95             break;
96           default:
97             System.out.println("Couldn't find the signature engine\n");
98             //sig = computeSignature(engineNo,curPart.getCleaned());
99             break;
100         }//switch engineNo
101
102         if (sig != null && sig.length > 0) {
103           String hash = engineNo + ":" + sig[curSigIndex];
104           printableSigs.add(hash);
105         } else {
106           /* we didn't produce a signature for the mail. */
107         }
108       }//engine
109     }//each emails part
110     return printableSigs;
111   }//computeSigs
112
113   /**
114    * @param engineNo
115    * @param cleaned
116    * @return
117    */
118   private String computeSignature(int engineNo, String mail) {
119     switch (engineNo) {
120       case 4:
121         return new String { this.sig4.computeSignature(mail) };
122       case 8:
123         //TODO device and equivalent for this
124         //String cleanedButKeepHTML = Preprocessor.preprocess(mail,Preprocessor.ConfigParams.NO_DEHTML);
125         return this.sig8.computeSignature(cleanedButKeepHTML);
126       default:
127         return null;
128     }
129   }
130 }