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.model.Filter;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ProcessUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsUtil;
33  
34  import java.util.List;
35  
36  /**
37   * <a href="ShellHook.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Michael Lawrence
40   */
41  public class ShellHook implements Hook {
42  
43      public static String SHELL_SCRIPT =
44          PropsUtil.get(PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
45  
46      public void addFilters(long companyId, long userId, List<String> filters) {
47      }
48  
49      public void addForward(
50          long companyId, long userId, List<Filter> filters,
51          List<String> emailAddresses, boolean leaveCopy) {
52  
53          execute(
54              new String[] {
55                  SHELL_SCRIPT, "addForward", String.valueOf(userId),
56                  StringUtil.merge(emailAddresses)
57              }
58          );
59      }
60  
61      public void addUser(
62          long companyId, long userId, String password, String firstName,
63          String middleName, String lastName, String emailAddress) {
64  
65          execute(
66              new String[] {
67                  SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
68                  firstName, middleName, lastName, emailAddress
69              }
70          );
71      }
72  
73      public void addVacationMessage(
74          long companyId, long userId, String emailAddress,
75          String vacationMessage) {
76  
77          execute(
78              new String[] {
79                  SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
80                  emailAddress, vacationMessage
81              }
82          );
83      }
84  
85      public void deleteEmailAddress(long companyId, long userId) {
86          execute(
87              new String[] {
88                  SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
89              }
90          );
91      }
92  
93      public void deleteUser(long companyId, long userId) {
94          execute(
95              new String[] {
96                  SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
97              }
98          );
99      }
100 
101     public void updateBlocked(
102         long companyId, long userId, List<String> blocked) {
103 
104         execute(
105             new String[] {
106                 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
107                 StringUtil.merge(blocked)
108             }
109         );
110     }
111 
112     public void updateEmailAddress(
113         long companyId, long userId, String emailAddress) {
114 
115         execute(
116             new String[] {
117                 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
118                 emailAddress
119             }
120         );
121     }
122 
123     public void updatePassword(long companyId, long userId, String password) {
124         execute(
125             new String[] {
126                 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
127             }
128         );
129     }
130 
131     protected void execute(String cmdLine[]) {
132         for (int i = 0; i < cmdLine.length; i++) {
133             if (cmdLine[i].trim().length() == 0) {
134                 cmdLine[i] = StringPool.UNDERLINE;
135             }
136         }
137 
138         try {
139             Runtime rt = Runtime.getRuntime();
140 
141             Process p = rt.exec(cmdLine);
142 
143             ProcessUtil.close(p);
144 
145             int exitValue = p.exitValue();
146 
147             if (exitValue != 0) {
148                 StringBuilder sb = new StringBuilder();
149 
150                 for (int i = 0; i < cmdLine.length; i++) {
151                     sb.append(cmdLine[i]);
152                     sb.append(StringPool.SPACE);
153                 }
154 
155                 throw new IllegalArgumentException(sb.toString());
156             }
157         }
158         catch (Exception e) {
159             _log.error(e);
160         }
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
164 
165 }