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