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