001    /**
002     * Copyright (c) 2000-present 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.process.ProcessUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.util.PropsUtil;
025    
026    import java.util.List;
027    import java.util.concurrent.Future;
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[] {SHELL_SCRIPT, "deleteUser", String.valueOf(userId)}
092                    );
093            }
094    
095            @Override
096            public void updateBlocked(
097                    long companyId, long userId, List<String> blocked) {
098    
099                    execute(
100                            new String[] {
101                                    SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
102                                    StringUtil.merge(blocked)
103                            }
104                    );
105            }
106    
107            @Override
108            public void updateEmailAddress(
109                    long companyId, long userId, String emailAddress) {
110    
111                    execute(
112                            new String[] {
113                                    SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
114                                    emailAddress
115                            }
116                    );
117            }
118    
119            @Override
120            public void updatePassword(long companyId, 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                            Future<?> future = ProcessUtil.execute(
137                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, cmdLine);
138    
139                            future.get();
140                    }
141                    catch (Exception e) {
142                            _log.error(e);
143                    }
144            }
145    
146            private static final Log _log = LogFactoryUtil.getLog(ShellHook.class);
147    
148    }