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 userId, List<String> filters) {
48      }
49  
50      public void addForward(
51          long userId, List<Filter> filters, List<String> emailAddresses,
52          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 userId, String password, String firstName, String middleName,
64          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 userId, String emailAddress, 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 userId) {
86          execute(
87              new String[] {
88                  SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
89              }
90          );
91      }
92  
93      public void deleteUser(long userId, String companyMx) {
94          execute(
95              new String[] {
96                  SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
97              }
98          );
99      }
100 
101     public void updateBlocked(long userId, List<String> blocked) {
102         execute(
103             new String[] {
104                 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
105                 StringUtil.merge(blocked)
106             }
107         );
108     }
109 
110     public void updateEmailAddress(long userId, String emailAddress) {
111         execute(
112             new String[] {
113                 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
114                 emailAddress
115             }
116         );
117     }
118 
119     public void updatePassword(long userId, String password) {
120         execute(
121             new String[] {
122                 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
123             }
124         );
125     }
126 
127     protected void execute(String cmdLine[]) {
128         for (int i = 0; i < cmdLine.length; i++) {
129             if (cmdLine[i].trim().length() == 0) {
130                 cmdLine[i] = StringPool.UNDERLINE;
131             }
132         }
133 
134         try {
135             Runtime rt = Runtime.getRuntime();
136 
137             Process p = rt.exec(cmdLine);
138 
139             ProcessUtil.close(p);
140 
141             int exitValue = p.exitValue();
142 
143             if (exitValue != 0) {
144                 StringBuilder sb = new StringBuilder();
145 
146                 for (int i = 0; i < cmdLine.length; i++) {
147                     sb.append(cmdLine[i]);
148                     sb.append(StringPool.SPACE);
149                 }
150 
151                 throw new IllegalArgumentException(sb.toString());
152             }
153         }
154         catch (Exception e) {
155             _log.error(e);
156         }
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
160 
161 }