1   /**
2    * Copyright (c) 2000-2007 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.portal.kernel.util.ProcessUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.util.PropsUtil;
30  
31  import java.util.List;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
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(PropsUtil.MAIL_HOOK_SHELL_SCRIPT);
46  
47      public void addFilters(long userId, List filters) {
48      }
49  
50      public void addForward(
51          long userId, List filters, List 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 userId, String password, String firstName, String middleName,
63          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 userId, String emailAddress, String vacationMessage) {
75  
76          _execute(
77              new String[] {
78                  SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
79                  emailAddress, vacationMessage
80              }
81          );
82      }
83  
84      public void deleteEmailAddress(long userId) {
85          _execute(
86              new String[] {
87                  SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
88              }
89          );
90      }
91  
92      public void deleteUser(long userId) {
93          _execute(
94              new String[] {
95                  SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
96              }
97          );
98      }
99  
100     public void updateBlocked(long userId, List blocked) {
101         _execute(
102             new String[] {
103                 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
104                 StringUtil.merge(blocked)
105             }
106         );
107     }
108 
109     public void updateEmailAddress(long userId, String emailAddress) {
110         _execute(
111             new String[] {
112                 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
113                 emailAddress
114             }
115         );
116     }
117 
118     public void updatePassword(long userId, String password) {
119         _execute(
120             new String[] {
121                 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
122         });
123     }
124 
125     private void _execute(String cmdLine[]) {
126         for (int i = 0; i < cmdLine.length; i++) {
127             if (cmdLine[i].trim().length() == 0) {
128                 cmdLine[i] = StringPool.UNDERLINE;
129             }
130         }
131 
132         try {
133             Runtime rt = Runtime.getRuntime();
134 
135             Process p = rt.exec(cmdLine);
136 
137             ProcessUtil.close(p);
138 
139             int exitValue = p.exitValue();
140 
141             if (exitValue != 0) {
142                 StringMaker cmd = new StringMaker();
143 
144                 for (int i = 0; i < cmdLine.length; i++) {
145                     cmd.append(cmdLine[i]);
146                     cmd.append(StringPool.SPACE);
147                 }
148 
149                 throw new IllegalArgumentException(cmd.toString());
150             }
151         }
152         catch (Exception e) {
153             _log.error(e);
154         }
155     }
156 
157     private static Log _log = LogFactory.getLog(ShellHook.class);
158 
159 }