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