1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.mail.util;
21  
22  import com.liferay.mail.model.Filter;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ProcessUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.util.PropsKeys;
29  import com.liferay.portal.util.PropsUtil;
30  
31  import java.util.List;
32  
33  /**
34   * <a href="ShellHook.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Michael Lawrence
37   *
38   */
39  public class ShellHook implements Hook {
40  
41      public static String SHELL_SCRIPT =
42          PropsUtil.get(PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
43  
44      public void addFilters(long companyId, long userId, List<String> filters) {
45      }
46  
47      public void addForward(
48          long companyId, long userId, List<Filter> filters,
49          List<String> emailAddresses, boolean leaveCopy) {
50  
51          execute(
52              new String[] {
53                  SHELL_SCRIPT, "addForward", String.valueOf(userId),
54                  StringUtil.merge(emailAddresses)
55              }
56          );
57      }
58  
59      public void addUser(
60          long companyId, long userId, String password, String firstName,
61          String middleName, String lastName, String emailAddress) {
62  
63          execute(
64              new String[] {
65                  SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
66                  firstName, middleName, lastName, emailAddress
67              }
68          );
69      }
70  
71      public void addVacationMessage(
72          long companyId, long userId, String emailAddress,
73          String vacationMessage) {
74  
75          execute(
76              new String[] {
77                  SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
78                  emailAddress, vacationMessage
79              }
80          );
81      }
82  
83      public void deleteEmailAddress(long companyId, long userId) {
84          execute(
85              new String[] {
86                  SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
87              }
88          );
89      }
90  
91      public void deleteUser(long companyId, long userId) {
92          execute(
93              new String[] {
94                  SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
95              }
96          );
97      }
98  
99      public void updateBlocked(
100         long companyId, long userId, List<String> blocked) {
101 
102         execute(
103             new String[] {
104                 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
105                 StringUtil.merge(blocked)
106             }
107         );
108     }
109 
110     public void updateEmailAddress(
111         long companyId, long userId, String emailAddress) {
112 
113         execute(
114             new String[] {
115                 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
116                 emailAddress
117             }
118         );
119     }
120 
121     public void updatePassword(long companyId, long userId, String password) {
122         execute(
123             new String[] {
124                 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
125             }
126         );
127     }
128 
129     protected void execute(String cmdLine[]) {
130         for (int i = 0; i < cmdLine.length; i++) {
131             if (cmdLine[i].trim().length() == 0) {
132                 cmdLine[i] = StringPool.UNDERLINE;
133             }
134         }
135 
136         try {
137             Runtime rt = Runtime.getRuntime();
138 
139             Process p = rt.exec(cmdLine);
140 
141             ProcessUtil.close(p);
142 
143             int exitValue = p.exitValue();
144 
145             if (exitValue != 0) {
146                 StringBuilder sb = new StringBuilder();
147 
148                 for (int i = 0; i < cmdLine.length; i++) {
149                     sb.append(cmdLine[i]);
150                     sb.append(StringPool.SPACE);
151                 }
152 
153                 throw new IllegalArgumentException(sb.toString());
154             }
155         }
156         catch (Exception e) {
157             _log.error(e);
158         }
159     }
160 
161     private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
162 
163 }