1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.mail.util;
16  
17  import com.liferay.mail.model.Filter;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.ProcessUtil;
21  import com.liferay.portal.kernel.util.PropsKeys;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.util.PropsUtil;
26  
27  import java.util.List;
28  
29  /**
30   * <a href="ShellHook.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Michael Lawrence
33   */
34  public class ShellHook implements Hook {
35  
36      public static String SHELL_SCRIPT =
37          PropsUtil.get(PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
38  
39      public void addFilters(long companyId, long userId, List<String> filters) {
40      }
41  
42      public void addForward(
43          long companyId, long userId, List<Filter> filters,
44          List<String> emailAddresses, boolean leaveCopy) {
45  
46          execute(
47              new String[] {
48                  SHELL_SCRIPT, "addForward", String.valueOf(userId),
49                  StringUtil.merge(emailAddresses)
50              }
51          );
52      }
53  
54      public void addUser(
55          long companyId, long userId, String password, String firstName,
56          String middleName, String lastName, String emailAddress) {
57  
58          execute(
59              new String[] {
60                  SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
61                  firstName, middleName, lastName, emailAddress
62              }
63          );
64      }
65  
66      public void addVacationMessage(
67          long companyId, long userId, String emailAddress,
68          String vacationMessage) {
69  
70          execute(
71              new String[] {
72                  SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
73                  emailAddress, vacationMessage
74              }
75          );
76      }
77  
78      public void deleteEmailAddress(long companyId, long userId) {
79          execute(
80              new String[] {
81                  SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
82              }
83          );
84      }
85  
86      public void deleteUser(long companyId, long userId) {
87          execute(
88              new String[] {
89                  SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
90              }
91          );
92      }
93  
94      public void updateBlocked(
95          long companyId, long userId, List<String> blocked) {
96  
97          execute(
98              new String[] {
99                  SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
100                 StringUtil.merge(blocked)
101             }
102         );
103     }
104 
105     public void updateEmailAddress(
106         long companyId, long userId, String emailAddress) {
107 
108         execute(
109             new String[] {
110                 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
111                 emailAddress
112             }
113         );
114     }
115 
116     public void updatePassword(long companyId, long userId, String password) {
117         execute(
118             new String[] {
119                 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
120             }
121         );
122     }
123 
124     protected void execute(String cmdLine[]) {
125         for (int i = 0; i < cmdLine.length; i++) {
126             if (cmdLine[i].trim().length() == 0) {
127                 cmdLine[i] = StringPool.UNDERLINE;
128             }
129         }
130 
131         try {
132             Runtime rt = Runtime.getRuntime();
133 
134             Process p = rt.exec(cmdLine);
135 
136             ProcessUtil.close(p);
137 
138             int exitValue = p.exitValue();
139 
140             if (exitValue != 0) {
141                 StringBundler sb = new StringBundler(cmdLine.length * 2);
142 
143                 for (int i = 0; i < cmdLine.length; i++) {
144                     sb.append(cmdLine[i]);
145                     sb.append(StringPool.SPACE);
146                 }
147 
148                 throw new IllegalArgumentException(sb.toString());
149             }
150         }
151         catch (Exception e) {
152             _log.error(e);
153         }
154     }
155 
156     private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
157 
158 }