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