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