1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
42  public class ShellHook implements Hook {
43  
44      public static String SHELL_SCRIPT =
45          PropsUtil.get(PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
46  
47      public void addFilters(long companyId, long userId, List<String> filters) {
48      }
49  
50      public void addForward(
51          long companyId, long userId, List<Filter> filters,
52          List<String> emailAddresses, boolean leaveCopy) {
53  
54          execute(
55              new String[] {
56                  SHELL_SCRIPT, "addForward", String.valueOf(userId),
57                  StringUtil.merge(emailAddresses)
58              }
59          );
60      }
61  
62      public void addUser(
63          long companyId, long userId, String password, String firstName,
64          String middleName, String lastName, String emailAddress) {
65  
66          execute(
67              new String[] {
68                  SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
69                  firstName, middleName, lastName, emailAddress
70              }
71          );
72      }
73  
74      public void addVacationMessage(
75          long companyId, long userId, String emailAddress,
76          String vacationMessage) {
77  
78          execute(
79              new String[] {
80                  SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
81                  emailAddress, vacationMessage
82              }
83          );
84      }
85  
86      public void deleteEmailAddress(long companyId, long userId) {
87          execute(
88              new String[] {
89                  SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
90              }
91          );
92      }
93  
94      public void deleteUser(long companyId, long userId) {
95          execute(
96              new String[] {
97                  SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
98              }
99          );
100     }
101 
102     public void updateBlocked(
103         long companyId, long userId, List<String> blocked) {
104 
105         execute(
106             new String[] {
107                 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
108                 StringUtil.merge(blocked)
109             }
110         );
111     }
112 
113     public void updateEmailAddress(
114         long companyId, long userId, String emailAddress) {
115 
116         execute(
117             new String[] {
118                 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
119                 emailAddress
120             }
121         );
122     }
123 
124     public void updatePassword(long companyId, long userId, String password) {
125         execute(
126             new String[] {
127                 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
128             }
129         );
130     }
131 
132     protected void execute(String cmdLine[]) {
133         for (int i = 0; i < cmdLine.length; i++) {
134             if (cmdLine[i].trim().length() == 0) {
135                 cmdLine[i] = StringPool.UNDERLINE;
136             }
137         }
138 
139         try {
140             Runtime rt = Runtime.getRuntime();
141 
142             Process p = rt.exec(cmdLine);
143 
144             ProcessUtil.close(p);
145 
146             int exitValue = p.exitValue();
147 
148             if (exitValue != 0) {
149                 StringBuilder sb = new StringBuilder();
150 
151                 for (int i = 0; i < cmdLine.length; i++) {
152                     sb.append(cmdLine[i]);
153                     sb.append(StringPool.SPACE);
154                 }
155 
156                 throw new IllegalArgumentException(sb.toString());
157             }
158         }
159         catch (Exception e) {
160             _log.error(e);
161         }
162     }
163 
164     private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
165 
166 }