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