bug fixes with string split and other changes to make a little more sophisticated
[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("fileName= " + fileName);
37
38     FileInputStream fileinput = 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("Title:")) // 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     }
88
89     fileinput.close();
90
91     MAX_TOKEN_SIZE = 1024;
92   }
93
94         // -------------------------------------------------------
95
96         public void setHeader(String header) {
97                 this.header = header;
98         }
99
100         public String getHeader() {
101                 return header;
102         }
103
104    
105     /*
106         public void setSentOn(String sentOn) {
107                 this.sentOn = sentOn;
108         }
109
110         public String getSentOn() {
111                 return sentOn;
112         }
113
114         public Date getSentOnAsDate() {
115                 String sentOn = getSentOn();
116                 return parseDate(sentOn);
117         }
118
119         public void setReceivedOn(String receivedOn) {
120                 this.receivedOn = receivedOn;
121         }
122
123         public String getReceivedOn() {
124                 return receivedOn;
125         }
126
127         public Date getReceivedOnAsDate() {
128                 String receivedOn = getReceivedOn();
129                 return parseDate(receivedOn);
130         }
131     */
132     
133
134         /**
135          * Parses a given Date-String in into a real Date-Object
136          * 
137          * @param stringDate the string in format dd.mm.yyyy hh:mm
138          * @return a Date containing the info of the string or the actual date and time if something fails.
139          */
140     /*
141         public Date parseDate(String stringDate) {
142                 // date is in this format: dd.mm.yyyy hh:mm
143                 if (stringDate == null || "N/A".equals(stringDate)) {
144                         return new Date();
145                 }
146                 try {
147                         synchronized (MAIL_TIME_FORMAT) {
148                                 return MAIL_TIME_FORMAT.parse(stringDate);
149                         }
150                 } catch (Throwable e) {
151                         return new Date();
152                 }
153         }
154     */
155
156         public void setFrom(String from) {
157                 this.from = from;
158         }
159
160         public String getFrom() {
161                 return from;
162         }
163
164         public void setTo(String to) {
165                 this.to = to;
166         }
167
168         public String getTo() {
169                 return to;
170         }
171
172         public void setCc(String cc) {
173                 this.cc = cc;
174         }
175
176         public String getCc() {
177                 return cc;
178         }
179
180         public void setSubject(String subject) {
181                 this.subject = subject;
182         }
183
184         public String getSubject() {
185                 return subject;
186         }
187
188         public void setBody(String body) {
189                 this.body = body;
190         }
191
192         public String getBody() {
193                 return body;
194         }
195
196         public void setSourceCode(String sourceCode) {
197                 this.sourceCode = sourceCode;
198         }
199
200         public String getSourceCode() {
201                 return sourceCode;
202         }
203
204         public void setHasAttachement(boolean hasAttachement) {
205                 this.hasAttachement = hasAttachement;
206         }
207
208         public boolean getHasAttachement() {
209                 return hasAttachement;
210         }
211
212         public void setEncoding(String encoding) {
213                 this.encoding = encoding;
214         }
215
216         public String getEncoding() {
217                 return encoding;
218         }
219
220         public boolean isTextEncoding() {
221                 return getEncoding().toLowerCase().indexOf("plain") >= 0;
222         }
223
224         public boolean isHTMLEncoding() {
225                 return getEncoding().toLowerCase().indexOf("html") >= 0;
226         }
227
228     /*
229         public String toString() {
230                 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getReceivedOn() + "," + getSentOn() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
231         }
232     */
233
234         public String toString() {
235                 return getBody() + "," + getCc() + "," + getEncoding() + "," + getFrom() + "," + getHasAttachement() + "," + getHeader() + "," + getSourceCode() + "," + getSubject() + "," + getTo();
236     }
237
238     /*
239         public String getID() {
240                 if (messageID == null) { // no cached version
241                         // Take the message-ID header as ID (if present)
242                         String[] messageIDs = getHeaderField("Message-ID");
243                         if ((messageIDs != null) && (messageIDs.length > 0)) {
244                                 messageID = messageIDs[0];
245                         } else { // otherwise, hash header and body as ID
246                                 return String.valueOf(getHeader().hashCode() + getBody().hashCode());
247                         }
248                 }
249
250                 return messageID;
251         }
252     */
253
254         public String[] getHeaderField(String fieldName) {
255
256         }
257
258     public String extractEMailAddress() {
259
260         }
261
262     /*
263         public boolean equals(Object o) {
264                 if (o instanceof Mail) {
265                         Mail mail = (Mail)o;
266                         return this.getID().equals(mail.getID());
267                 }
268
269                 return false;
270         }
271     */
272
273   public Vector getCommonPart()
274   {
275     Vector returnStrings = new Vector();
276
277     // add header, sender, and title
278     returnStrings.addElement(header);
279     returnStrings.addElement(from);
280     returnStrings.addElement(subject);
281
282     return returnStrings;
283   }
284
285   public String getBodyString()
286   {
287     return body;
288   }
289
290   public Vector returnEmail() {
291     Vector myemail = new Vector();
292     myemail.addElement(getCommonPart());
293     //System.out.println("DEBUG: getCommonPart.size= " + getCommonPart().size());
294     myemail.addElement(getURLs());
295     //System.out.println("DEBUG: getURLs.size= " + getURLs().size());
296     myemail.addElement(getSplittedBody(MAX_TOKEN_SIZE));
297     //System.out.println("DEBUG: getSplittedBody.size= " + getSplittedBody(MAX_TOKEN_SIZE).size());
298     return myemail;
299   }
300
301   public Vector getURLs()
302   {
303     Vector returnStrings = new Vector();
304     Vector splittedBody = body.split();
305
306     // add URL and email in the body
307     for(int i=0; i<splittedBody.size(); i++) 
308     {
309       String segment = (String)(splittedBody.elementAt(i));
310       if(segment.startsWith("http://"))  // URL
311       {
312         returnStrings.addElement(segment);
313       }
314       else if(isEmailAccount(segment)) // email
315       {
316         returnStrings.addElement(segment);
317       }
318     }
319
320     return returnStrings;
321   }
322
323   // check if it is email account string
324   private boolean isEmailAccount(String str)
325   {
326     if(str.contains("@") && str.contains("."))
327       return true;
328     else
329       return false;
330   }
331
332   public void setNoURLBody()
333   {
334     noURLBody = new String();
335
336     Vector splittedBody = body.split();
337
338     //System.out.println("DEBUG: splittedBody.size()= " + splittedBody.size());
339     for(int i=0; i< splittedBody.size();i ++)
340     {
341       String segment = (String)(splittedBody.elementAt(i));
342       //System.out.println("DEBUG: segment= " + segment);
343       
344       if(!(segment.startsWith("http://") || isEmailAccount(segment)))
345         noURLBody += segment;
346     }
347   }
348
349   // setNoURLBody method has to be called before this method
350   // parameter : bytesize to split.
351   public Vector getSplittedBody(int size)
352   {
353     setNoURLBody();
354     Vector returnStrings = new Vector();
355
356     char[] charArray = noURLBody.toCharArray();
357
358     String tmpStr = new String();
359     tmpStr += charArray[0];
360
361     //System.out.println("tmpStr= " + tmpStr);
362
363     for(int i=1; i< noURLBody.length(); i++)
364     {
365       if((i % size) == 0) {
366         returnStrings.addElement(tmpStr);
367         tmpStr = new String();
368       }
369       else {
370         tmpStr += charArray[i];
371       }
372     }
373
374     returnStrings.addElement(tmpStr);
375
376     return returnStrings;
377   }
378
379
380   public void setIsSpam(boolean spam) {
381     isSpam = spam;
382   }
383
384   public boolean getIsSpam() {
385     if(spam.equals("yes"))
386       return true;
387     return false;
388   }
389
390   /**
391    *  Returns result to the Spam filter
392    **/
393   public Vector checkMail(int userid) {
394     //Preprocess emails
395     //Vector partsOfMailStrings = mail.createMailStringsWithURL();
396     //Vector partsOfMailStrings = getCommonPart();
397     //partsOfMailStrings.addElement(getBodyString());
398     Vector partsOfMailStrings = returnEmail();
399     
400
401     //Compute signatures
402     SignatureComputer sigComp = new SignatureComputer();
403     //Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings
404     Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of vector of strings
405
406     return signatures;
407   }
408
409   /* For tests only */
410   /*
411   public static void main(String[] args)
412   {
413     Mail mail = new Mail("./emails/email1");
414
415     String[] a = mail.createMailStrings();
416
417     for(String b : a)
418     {
419       System.out.println(b);
420     }
421   }
422   */
423 }