001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.mail.util;
016    
017    import com.liferay.mail.model.Filter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ProcessUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.util.PropsUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Michael Lawrence
031     */
032    public class ShellHook implements Hook {
033    
034            public static final String SHELL_SCRIPT = PropsUtil.get(
035                    PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
036    
037            public void addFilters(long companyId, long userId, List<String> filters) {
038            }
039    
040            @Override
041            public void addForward(
042                    long companyId, long userId, List<Filter> filters,
043                    List<String> emailAddresses, boolean leaveCopy) {
044    
045                    execute(
046                            new String[] {
047                                    SHELL_SCRIPT, "addForward", String.valueOf(userId),
048                                    StringUtil.merge(emailAddresses)
049                            }
050                    );
051            }
052    
053            @Override
054            public void addUser(
055                    long companyId, long userId, String password, String firstName,
056                    String middleName, String lastName, String emailAddress) {
057    
058                    execute(
059                            new String[] {
060                                    SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
061                                    firstName, middleName, lastName, emailAddress
062                            }
063                    );
064            }
065    
066            @Override
067            public void addVacationMessage(
068                    long companyId, long userId, String emailAddress,
069                    String vacationMessage) {
070    
071                    execute(
072                            new String[] {
073                                    SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
074                                    emailAddress, vacationMessage
075                            }
076                    );
077            }
078    
079            @Override
080            public void deleteEmailAddress(long companyId, long userId) {
081                    execute(
082                            new String[] {
083                                    SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
084                            }
085                    );
086            }
087    
088            @Override
089            public void deleteUser(long companyId, long userId) {
090                    execute(
091                            new String[] {
092                                    SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
093                            }
094                    );
095            }
096    
097            @Override
098            public void updateBlocked(
099                    long companyId, long userId, List<String> blocked) {
100    
101                    execute(
102                            new String[] {
103                                    SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
104                                    StringUtil.merge(blocked)
105                            }
106                    );
107            }
108    
109            @Override
110            public void updateEmailAddress(
111                    long companyId, long userId, String emailAddress) {
112    
113                    execute(
114                            new String[] {
115                                    SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
116                                    emailAddress
117                            }
118                    );
119            }
120    
121            @Override
122            public void updatePassword(long companyId, long userId, String password) {
123                    execute(
124                            new String[] {
125                                    SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
126                            }
127                    );
128            }
129    
130            protected void execute(String[] cmdLine) {
131                    for (int i = 0; i < cmdLine.length; i++) {
132                            if (cmdLine[i].trim().length() == 0) {
133                                    cmdLine[i] = StringPool.UNDERLINE;
134                            }
135                    }
136    
137                    try {
138                            Runtime rt = Runtime.getRuntime();
139    
140                            Process p = rt.exec(cmdLine);
141    
142                            ProcessUtil.close(p);
143    
144                            int exitValue = p.exitValue();
145    
146                            if (exitValue != 0) {
147                                    StringBundler sb = new StringBundler(cmdLine.length * 2);
148    
149                                    for (int i = 0; i < cmdLine.length; i++) {
150                                            sb.append(cmdLine[i]);
151                                            sb.append(StringPool.SPACE);
152                                    }
153    
154                                    throw new IllegalArgumentException(sb.toString());
155                            }
156                    }
157                    catch (Exception e) {
158                            _log.error(e);
159                    }
160            }
161    
162            private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
163    
164    }