adding a test case
[IRC.git] / Robust / src / Benchmarks / Distributed / SpamFilter / Mail.java
1 /**
2  * This class is a container for all data contained in an Email Message.
3  **/
4 public class Mail {
5
6         String header; // the full header
7         //String sentOn; // time the message was sent
8         //String receivedOn; // time when the message arrived
9         String from; // the "from" field
10         String to; // the "to" field
11         String cc;  
12         String subject;
13         String body;
14     String noURLBody;
15         String sourceCode;
16     String spam;
17         boolean hasAttachement;
18         String encoding; //rich text, plain, html
19
20         String messageID; // cached message ID for reuse (takes a lot of memory and is used all over the place)
21                       //same as hashcode of a class
22     boolean isSpam;
23
24     /** 
25      * this is a really simple implementation of a tokenizer
26      * used to build tokens from an email and divide email into parts
27      **/
28     int MAX_TOKEN_SIZE;
29
30   public Mail() {
31       messageID=null;
32   }
33
34   public Mail(String fileName)  // read a mail from file
35   {
36     //System.out.println("DEBUG: fileName= " + fileName);
37
38     BufferedReader fileinput = new BufferedReader(new FileInputStream(fileName));
39     String line;
40     boolean chk = false;
41
42     while((line = fileinput.readLine()) != null)
43     {
44       chk = true;
45
46       Vector splittedLine = line.split();
47       if(((String)(splittedLine.elementAt(0))).equals("Spam:"))
48       {
49         spam = (String)(splittedLine.elementAt(1));
50       }
51       else if(((String)(splittedLine.elementAt(0))).equals("Header:"))  // message id
52       {
53         header = (String)splittedLine.elementAt(1);
54       }
55       else if(((String)(splittedLine.elementAt(0))).equals("To:")) // receiver
56       {
57         to = (String)splittedLine.elementAt(1);
58       }
59       else if(((String)(splittedLine.elementAt(0))).equals("From:")) // sender
60       {
61         from = (String)splittedLine.elementAt(1);
62       }
63       else if(((String)(splittedLine.elementAt(0))).equals("Cc:")) // cc
64       {
65         cc = (String)splittedLine.elementAt(1);
66       }
67       else if(((String)(splittedLine.elementAt(0))).equals("Subject:")) // Subject
68       {
69         subject = (String)splittedLine.elementAt(1);
70         break;
71       }
72     } // parsed messageID, To, from, cc, Title
73
74     /** 
75      * error checking
76      **/
77     if(!chk)
78       System.out.println("no line read");
79
80
81     body = new String();
82     byte[] readBody = new byte[256];
83
84     while((fileinput.read(readBody)>0))
85     {
86       body += new String(readBody);
87       readBody = new byte[256];
88     }
89
90     fileinput.close();
91
92     MAX_TOKEN_SIZE = 1024;
93   }
94
95         // -------------------------------------------------------
96
97         public void setHeader(String header) {
98                 this.header = header;
99         }
100
101         public String getHeader() {
102                 return header;
103         }
104
105    
106     /*
107         public void setSentOn(String sentOn) {
108                 this.sentOn = sentOn;
109         }
110
111         public String getSentOn() {
112                 return sentOn;
113         }
114
115         public Date getSentOnAsDate() {
116                 String sentOn = getSentOn();
117                 return parseDate(sentOn);
118         }
119
120         public void setReceivedOn(String receivedOn) {
121                 this.receivedOn = receivedOn;
122         }
123
124         public String getReceivedOn() {
125                 return receivedOn;
126         }
127
128         public Date getReceivedOnAsDate() {
129                 String receivedOn = getReceivedOn();
130                 return parseDate(receivedOn);
131         }
132     */
133     
134
135         /**
136          * Parses a given Date-String in into a real Date-Object
137          * 
138          * @param stringDate the string in format dd.mm.yyyy hh:mm
139          * @return a Date containing the info of the string or the actual date and time if something fails.
140          */
141     /*
142         public Date parseDate(String stringDate) {
143                 // date is in this format: dd.mm.yyyy hh:mm
144                 if (stringDate == null || "N/A".equals(stringDate)) {
145                         return new Date();
146                 }
147                 try {
148                         synchronized (MAIL_TIME_FORMAT) {
149                                 return MAIL_TIME_FORMAT.parse(stringDate);
150                         }
151                 } catch (Throwable e) {
152                         return new Date();
153                 }
154         }
155     */
156
157         public void setFrom(String from) {
158                 this.from = from;
159         }
160
161         public String getFrom() {
162                 return from;
163         }
164
165         public void setTo(String to) {
166                 this.to = to;
167         }
168
169         public String getTo() {
170                 return to;
171         }
172
173         public void setCc(String cc) {
174                 this.cc = cc;
175         }
176
177         public String getCc() {
178                 return cc;
179         }
180
181         public void setSubject(String subject) {
182                 this.subject = subject;
183         }
184
185         public String getSubject() {
186                 return subject;
187         }
188
189         public void setBody(String body) {
190                 this.body = body;
191         }
192
193         public String getBody() {
194                 return body;
195         }
196
197         public void setSourceCode(String sourceCode) {
198                 this.sourceCode = sourceCode;
199         }
200
201         public String getSourceCode() {
202                 return sourceCode;
203         }
204
205         public void setHasAttachement(boolean hasAttachement) {
206                 this.hasAttachement = hasAttachement;
207         }
208
209         public boolean getHasAttachement() {
210                 return hasAttachement;
211         }
212
213         public void setEncoding(String encoding) {
214                 this.encoding = encoding;
215         }
216
217         public String getEncoding() {
218                 return encoding;
219         }
220
221         public boolean isTextEncoding() {
222                 return getEncoding().toLowerCase().indexOf("plain") >= 0;
223         }
224
225         public boolean isHTMLEncoding() {
226                 return getEncoding().toLowerCase().indexOf("html") >= 0;
227         }
228
229     /*
230         public String toString() {
231                 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
232         }
233     */
234
235         public String toString() {
236                 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
237     }
238
239     /*
240         public String getID() {
241                 if (messageID == null) { // no cached version
242                         // Take the message-ID header as ID (if present)
243                         String[] messageIDs = getHeaderField("Message-ID");
244                         if ((messageIDs != null) && (messageIDs.length > 0)) {
245                                 messageID = messageIDs[0];
246                         } else { // otherwise, hash header and body as ID
247                                 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
248                         }
249                 }
250
251                 return messageID;
252         }
253     */
254
255         public String[] getHeaderField(String fieldName) {
256
257         }
258
259     public String extractEMailAddress() {
260
261         }
262
263     /*
264         public boolean equals(Object o) {
265                 if (o instanceof Mail) {
266                         Mail mail = (Mail)o;
267                         return this.getID().equals(mail.getID());
268                 }
269
270                 return false;
271         }
272     */
273
274   public Vector getCommonPart()
275   {
276     Vector returnStrings = new Vector();
277
278     // add header, sender, and title
279     returnStrings.addElement(header);
280     returnStrings.addElement(from);
281     returnStrings.addElement(subject);
282
283     return returnStrings;
284   }
285
286   public String getBodyString()
287   {
288     return body;
289   }
290
291   public Vector returnEmail() {
292     Vector myemail = new Vector();
293     myemail.addElement(getCommonPart());
294     //System.out.println("DEBUG: getCommonPart.size= " + getCommonPart().size());
295     myemail.addElement(getURLs());
296     //System.out.println("DEBUG: getURLs.size= " + getURLs().size());
297     myemail.addElement(getSplittedBody(MAX_TOKEN_SIZE));
298     //System.out.println("DEBUG: getSplittedBody.size= " + getSplittedBody(MAX_TOKEN_SIZE).size());
299     return myemail;
300   }
301
302   public Vector getURLs()
303   {
304     Vector returnStrings = new Vector();
305     Vector splittedBody = body.split();
306
307     // add URL and email in the body
308     for(int i=0; i<splittedBody.size(); i++) 
309     {
310       String segment = (String)(splittedBody.elementAt(i));
311       if(segment.startsWith("http://"))  // URL
312       {
313         returnStrings.addElement(segment);
314       }
315       else if(isEmailAccount(segment)) // email
316       {
317         returnStrings.addElement(segment);
318       }
319     }
320
321     return returnStrings;
322   }
323
324   // check if it is email account string
325   private boolean isEmailAccount(String str)
326   {
327     if(str.contains("@") && str.contains("."))
328       return true;
329     else
330       return false;
331   }
332
333   public void setNoURLBody()
334   {
335     Vector splittedBody = body.split();
336     int totalsize=0;
337     for(int i=0; i< splittedBody.size();i ++) {
338       String segment = (String)(splittedBody.elementAt(i));
339       if(!(segment.startsWith("http://") || isEmailAccount(segment)))
340         totalsize+=segment.length();
341     }
342
343     StringBuffer sb=new StringBuffer(totalsize);
344     for(int i=0; i< splittedBody.size();i ++) {
345       String segment = (String)(splittedBody.elementAt(i));
346       if(!(segment.startsWith("http://") || isEmailAccount(segment))) {
347         sb.append(segment);
348       }
349     }
350     noURLBody=sb.toString();
351   }
352
353   // setNoURLBody method has to be called before this method
354   // parameter : bytesize to split.
355   public Vector getSplittedBody(int size)
356   {
357     setNoURLBody();
358     Vector returnStrings = new Vector();
359     int end=noURLBody.length();
360
361     for(int i=1; i< end; i+=size)
362     {
363       if((i+size)>=end) {
364         String str=noURLBody.substring(i, end);
365         returnStrings.addElement(str);
366       }
367       else {
368         String str=noURLBody.substring(i, i+size);
369         returnStrings.addElement(str);
370       }
371     }
372     return returnStrings;
373   }
374
375
376   public void setIsSpam(boolean spam) {
377     isSpam = spam;
378   }
379
380   public boolean getIsSpam() {
381     if(spam.equals("yes"))
382       return true;
383     return false;
384   }
385
386   /**
387    *  Returns result to the Spam filter
388    **/
389   public Vector checkMail(int userid) {
390     //Preprocess emails
391
392     //long startGetParts=System.currentTimeMillis();
393     Vector partsOfMailStrings = returnEmail();
394     //long stopGetParts=System.currentTimeMillis();
395     //System.out.println("Time to read email= " + (stopGetParts-startGetParts));
396     
397     //Compute signatures
398     SignatureComputer sigComp = new SignatureComputer();
399     //Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
400     //long startGetsignatures=System.currentTimeMillis();
401     Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of vector of strings
402     //long stopGetsignatures=System.currentTimeMillis();
403     //System.out.println("Time to Getsignatures= " + (stopGetsignatures-startGetsignatures));
404
405     return signatures;
406   }
407
408   /* For tests only */
409   /*
410   public static void main(String[] args)
411   {
412     Mail mail = new Mail("./emails/email1");
413
414     String[] a = mail.createMailStrings();
415
416     for(String b : a)
417     {
418       System.out.println(b);
419     }
420   }
421   */
422 }