1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.mail.model;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  
27  import java.util.ArrayList;
28  import java.util.Date;
29  import java.util.List;
30  import java.util.regex.Pattern;
31  
32  import javax.mail.Address;
33  import javax.mail.internet.AddressException;
34  import javax.mail.internet.InternetAddress;
35  
36  /**
37   * <a href="MailMessage.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Alexander Chow
40   *
41   */
42  public class MailMessage {
43  
44      public long getMessageId() {
45          return _messageId;
46      }
47  
48      public void setMessageId(long messageId) {
49          _messageId = messageId;
50      }
51  
52      public Address getFrom() {
53          return _from;
54      }
55  
56      public void setFrom(Address from) {
57          _from = from;
58      }
59  
60      public Address[] getTo() {
61          return _to;
62      }
63  
64      public void setTo(String to) throws AddressException {
65          _to = InternetAddress.parse(to);
66      }
67  
68      public void setTo(Address[] to) {
69          _to = to;
70      }
71  
72      public Address[] getCc() {
73          return _cc;
74      }
75  
76      public void setCc(Address[] cc) {
77          _cc = cc;
78      }
79  
80      public void setCc(String ccs) throws AddressException {
81          _cc = InternetAddress.parse(ccs);
82      }
83  
84      public Address[] getBcc() {
85          return _bcc;
86      }
87  
88      public void setBcc(Address[] bcc) {
89          _bcc = bcc;
90      }
91  
92      public void setBcc(String bccs) throws AddressException {
93          _bcc = InternetAddress.parse(bccs);
94      }
95  
96      public String getInReplyTo() {
97          return _inReplyTo;
98      }
99  
100     public void setInReplyTo(String inReplyTo) {
101         _inReplyTo = inReplyTo;
102     }
103 
104     public Address[] getReplyTo() {
105         return _replyTo;
106     }
107 
108     public void setReplyTo(String replyTos) throws AddressException {
109         _replyTo = InternetAddress.parse(replyTos);
110     }
111 
112     public void setReplyTo(Address[] replyTo) {
113         _replyTo = replyTo;
114     }
115 
116     public String getReferences() {
117         return _references;
118     }
119 
120     public void setReferences(String references) {
121         _references = references;
122     }
123 
124     public String getSubject() {
125         return GetterUtil.getString(_subject);
126     }
127 
128     public void setSubject(String subject) {
129         _subject = subject;
130     }
131 
132     public Date getSentDate() {
133         return _sentDate;
134     }
135 
136     public void setSentDate(Date sentDate) {
137         _sentDate = sentDate;
138     }
139 
140     public String getBody() {
141         return getBody(false, false);
142     }
143 
144     public String getBody(boolean replaceLinks, boolean popup) {
145         String body = _content.toString();
146 
147         if (replaceLinks) {
148             for (int i = 0; i < _LINK_REGEXP.length; i++) {
149                 body = Pattern.compile(
150                     _LINK_REGEXP[i], Pattern.CASE_INSENSITIVE).
151                         matcher(body).replaceAll(_LINK_REPLACEMENT[i]);
152             }
153 
154             String mailtoReplacement = "<a href=\"javascript: ";
155 
156             if (popup) {
157                 mailtoReplacement += "opener.compose('$2')";
158             }
159             else {
160                 mailtoReplacement += "parent.compose('$2')";
161             }
162 
163             body = Pattern.compile(
164                 _MAILTO_REGEXP, Pattern.CASE_INSENSITIVE).
165                     matcher(body).replaceAll(mailtoReplacement);
166         }
167 
168         return body;
169     }
170 
171     public void setBody(String body) {
172         MailContent content = new MailContent(body);
173 
174         _content = content;
175     }
176 
177     public MailContent getMailContent() {
178         return _content;
179     }
180 
181     public void setMailContent(MailContent mc) {
182         _content = mc;
183     }
184 
185     public List getAttachments() {
186         return _attachments;
187     }
188 
189     public void appendAttachment(MailAttachment ma) {
190         _attachments.add(ma);
191     }
192 
193     public List getRemoteAttachments() {
194         return _remoteAttachments;
195     }
196 
197     public void setRemoteAttachments(List remoteAttachments) {
198         _remoteAttachments = remoteAttachments;
199     }
200 
201     public void appendRemoteAttachment(RemoteMailAttachment rma) {
202         _remoteAttachments.add(rma);
203     }
204 
205     public void purgeDirtyRemoteAttachments() {
206         for (int i = 0; i < _remoteAttachments.size(); i++) {
207             RemoteMailAttachment rma =
208                 (RemoteMailAttachment)_remoteAttachments.get(i);
209 
210             if (rma.isDirty()) {
211                 _remoteAttachments.remove(i);
212 
213                 i--;
214             }
215         }
216     }
217 
218     private static final String[] _LINK_REGEXP = {
219         "([^]_a-z0-9-=\"'/])" +
220             "((https?|ftp|gopher|news|telnet)://|www\\.)" +
221             "([^\\s\\(\\)\\*\\^\\$!`\"'\\|\\[\\]\\{\\};<>\\.]*)" +
222             "((\\.[^\\s\\(\\)\\*\\^\\$!`\"'\\|\\[\\]\\{\\};<>\\.]+)*)",
223         "<a\\s+href=\"www\\."
224     };
225 
226     private static String[] _LINK_REPLACEMENT = {
227         "$1<a href=\"$2$4$5\" target=\"_blank\">$2$4$5</a>",
228         "<a href=\"http://www."
229     };
230 
231     private static String _MAILTO_REGEXP =
232         "(<a\\s+href=\"mailto:\\s*)([\\w.-_]*@[\\w.-_]*)";
233 
234     private long _messageId;
235     private Address _from;
236     private Address[] _to;
237     private Address[] _cc;
238     private Address[] _bcc;
239     private String _inReplyTo;
240     private Address[] _replyTo;
241     private String _references;
242     private String _subject;
243     private Date _sentDate;
244     private MailContent _content;
245     private List _attachments = new ArrayList();
246     private List _remoteAttachments = new ArrayList();
247 
248 }