1   /**
2    * Copyright (c) 2000-2009 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.mail.util;
24  
25  import com.liferay.mail.NoSuchCyrusUserException;
26  import com.liferay.mail.model.CyrusUser;
27  import com.liferay.mail.model.CyrusVirtual;
28  import com.liferay.mail.model.Filter;
29  import com.liferay.mail.service.persistence.CyrusUserUtil;
30  import com.liferay.mail.service.persistence.CyrusVirtualUtil;
31  import com.liferay.portal.kernel.log.Log;
32  import com.liferay.portal.kernel.log.LogFactoryUtil;
33  import com.liferay.portal.kernel.util.FileUtil;
34  import com.liferay.portal.kernel.util.ProcessUtil;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.util.PropsKeys;
38  import com.liferay.portal.util.PropsUtil;
39  
40  import java.io.File;
41  
42  import java.util.List;
43  
44  /**
45   * <a href="CyrusHook.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class CyrusHook implements Hook {
51  
52      public void addForward(
53          long companyId, long userId, List<Filter> filters,
54          List<String> emailAddresses, boolean leaveCopy) {
55  
56          try {
57              if (emailAddresses != null) {
58                  String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
59  
60                  File file = new File(home + "/" + userId + ".procmail.forward");
61  
62                  if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
63                      (leaveCopy)) {
64  
65                      StringBuilder sb = new StringBuilder();
66  
67                      for (int i = 0; i < filters.size(); i++) {
68                          Filter filter = filters.get(i);
69  
70                          sb.append(":0\n");
71                          sb.append("* ^(From|Cc|To).*");
72                          sb.append(filter.getEmailAddress());
73                          sb.append("\n");
74                          sb.append("| $DELIVER -e -a $USER -m user.$USER.");
75                          sb.append(filter.getFolder());
76                          sb.append("\n\n");
77                      }
78  
79                      if (leaveCopy) {
80                          sb.append(":0 c\n");
81                          sb.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
82                      }
83  
84                      if (emailAddresses.size() > 0) {
85                          sb.append(":0\n");
86                          sb.append("!");
87  
88                          for (String emailAddress : emailAddresses) {
89                              sb.append(" ");
90                              sb.append(emailAddress);
91                          }
92                      }
93  
94                      String content = sb.toString();
95  
96                      while (content.endsWith("\n")) {
97                          content = content.substring(0, content.length() - 1);
98                      }
99  
100                     FileUtil.write(file, content);
101                 }
102                 else {
103                     file.delete();
104                 }
105             }
106         }
107         catch (Exception e) {
108             _log.error(e, e);
109         }
110     }
111 
112     public void addUser(
113         long companyId, long userId, String password, String firstName,
114         String middleName, String lastName, String emailAddress) {
115 
116         try {
117 
118             // User
119 
120             CyrusUser user = new CyrusUser(userId, password);
121 
122             CyrusUserUtil.update(user);
123 
124             // Virtual
125 
126             CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
127 
128             CyrusVirtualUtil.update(virtual);
129 
130             // Expect
131 
132             String addUserCmd =
133                 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
134 
135             addUserCmd = StringUtil.replace(
136                 addUserCmd, "%1%", String.valueOf(userId));
137 
138             Runtime rt = Runtime.getRuntime();
139 
140             Process p = rt.exec(addUserCmd);
141 
142             ProcessUtil.close(p);
143         }
144         catch (Exception e) {
145             _log.error(e, e);
146         }
147     }
148 
149     public void addVacationMessage(
150         long companyId, long userId, String emailAddress,
151         String vacationMessage) {
152 
153         try {
154             String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
155 
156             // Remove vacation cache
157 
158             new File(home + "/" + userId + ".vacation.cache").delete();
159 
160             // Update vacation message
161 
162             File vacation = new File(home + "/" + userId + ".vacation");
163 
164             if (Validator.isNull(vacationMessage)) {
165                 vacation.delete();
166             }
167             else {
168                 FileUtil.write(vacation, emailAddress + "\n" + vacationMessage);
169             }
170         }
171         catch (Exception e) {
172             _log.error(e, e);
173         }
174     }
175 
176     public void deleteEmailAddress(long companyId, long userId) {
177         try {
178             CyrusVirtualUtil.removeByUserId(userId);
179         }
180         catch (Exception e) {
181             _log.error(e, e);
182         }
183     }
184 
185     public void deleteUser(long companyId, long userId) {
186         try {
187 
188             // User
189 
190             try {
191                 CyrusUserUtil.remove(userId);
192             }
193             catch (NoSuchCyrusUserException nscue) {
194             }
195 
196             // Virtual
197 
198             CyrusVirtualUtil.removeByUserId(userId);
199 
200             // Expect
201 
202             String deleteUserCmd =
203                 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_DELETE_USER);
204 
205             deleteUserCmd = StringUtil.replace(
206                 deleteUserCmd, "%1%", String.valueOf(userId));
207 
208             Runtime rt = Runtime.getRuntime();
209 
210             Process p = rt.exec(deleteUserCmd);
211 
212             ProcessUtil.close(p);
213 
214             // Procmail
215 
216             String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
217 
218             File file = new File(home + "/" + userId + ".procmail.blocked");
219 
220             if (file.exists()) {
221                 file.delete();
222             }
223 
224             file = new File(home + "/" + userId + ".procmail.forward");
225 
226             if (file.exists()) {
227                 file.delete();
228             }
229 
230             file = new File(home + "/" + userId + ".vacation");
231 
232             if (file.exists()) {
233                 file.delete();
234             }
235 
236             file = new File(home + "/" + userId + ".vacation.cache");
237 
238             if (file.exists()) {
239                 file.delete();
240             }
241         }
242         catch (Exception e) {
243             _log.error(e, e);
244         }
245     }
246 
247     public void updateBlocked(
248         long companyId, long userId, List<String> blocked) {
249 
250         String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
251 
252         File file = new File(home + "/" + userId + ".procmail.blocked");
253 
254         if ((blocked == null) || (blocked.size() == 0)) {
255             file.delete();
256 
257             return;
258         }
259 
260         StringBuilder sb = new StringBuilder();
261 
262         for (int i = 0; i < blocked.size(); i++) {
263             String emailAddress = blocked.get(i);
264 
265             sb.append("\n");
266             sb.append(":0\n");
267             sb.append("* ^From.*").append(emailAddress).append("\n");
268             sb.append("{\n");
269             sb.append(":0\n");
270             sb.append("/dev/null\n");
271             sb.append("}\n");
272         }
273 
274         try {
275             FileUtil.write(file, sb.toString());
276         }
277         catch (Exception e) {
278             _log.error(e, e);
279         }
280     }
281 
282     public void updateEmailAddress(
283         long companyId, long userId, String emailAddress) {
284 
285         try {
286             CyrusVirtualUtil.removeByUserId(userId);
287 
288             CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
289 
290             CyrusVirtualUtil.update(virtual);
291         }
292         catch (Exception e) {
293             _log.error(e, e);
294         }
295     }
296 
297     public void updatePassword(long companyId, long userId, String password) {
298         CyrusUser user = null;
299 
300         try {
301             user = CyrusUserUtil.findByPrimaryKey(userId);
302         }
303         catch (NoSuchCyrusUserException nscue) {
304             user = new CyrusUser(userId, password);
305         }
306         catch (Exception e) {
307             _log.error(e, e);
308         }
309 
310         try {
311             user.setPassword(password);
312 
313             CyrusUserUtil.update(user);
314         }
315         catch (Exception e) {
316             _log.error(e, e);
317         }
318     }
319 
320     private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
321 
322 }