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.kernel.model.Filter;
018    import com.liferay.mail.kernel.util.Hook;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.process.ProcessUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
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    import java.util.concurrent.Future;
029    
030    /**
031     * @author Michael Lawrence
032     */
033    public class ShellHook implements Hook {
034    
035            public static final String SHELL_SCRIPT = PropsUtil.get(
036                    PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
037    
038            public void addFilters(long companyId, long userId, List<String> filters) {
039            }
040    
041            @Override
042            public void addForward(
043                    long companyId, long userId, List<Filter> filters,
044                    List<String> emailAddresses, boolean leaveCopy) {
045    
046                    execute(
047                            new String[] {
048                                    SHELL_SCRIPT, "addForward", String.valueOf(userId),
049                                    StringUtil.merge(emailAddresses)
050                            }
051                    );
052            }
053    
054            @Override
055            public void addUser(
056                    long companyId, long userId, String password, String firstName,
057                    String middleName, String lastName, String emailAddress) {
058    
059                    execute(
060                            new String[] {
061                                    SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
062                                    firstName, middleName, lastName, emailAddress
063                            }
064                    );
065            }
066    
067            @Override
068            public void addVacationMessage(
069                    long companyId, long userId, String emailAddress,
070                    String vacationMessage) {
071    
072                    execute(
073                            new String[] {
074                                    SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
075                                    emailAddress, vacationMessage
076                            }
077                    );
078            }
079    
080            @Override
081            public void deleteEmailAddress(long companyId, long userId) {
082                    execute(
083                            new String[] {
084                                    SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
085                            }
086                    );
087            }
088    
089            @Override
090            public void deleteUser(long companyId, long userId) {
091                    execute(
092                            new String[] {SHELL_SCRIPT, "deleteUser", String.valueOf(userId)}
093                    );
094            }
095    
096            @Override
097            public void updateBlocked(
098                    long companyId, long userId, List<String> blocked) {
099    
100                    execute(
101                            new String[] {
102                                    SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
103                                    StringUtil.merge(blocked)
104                            }
105                    );
106            }
107    
108            @Override
109            public void updateEmailAddress(
110                    long companyId, long userId, String emailAddress) {
111    
112                    execute(
113                            new String[] {
114                                    SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
115                                    emailAddress
116                            }
117                    );
118            }
119    
120            @Override
121            public void updatePassword(long companyId, long userId, String password) {
122                    execute(
123                            new String[] {
124                                    SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
125                            }
126                    );
127            }
128    
129            protected void execute(String[] cmdLine) {
130                    for (int i = 0; i < cmdLine.length; i++) {
131                            if (cmdLine[i].trim().length() == 0) {
132                                    cmdLine[i] = StringPool.UNDERLINE;
133                            }
134                    }
135    
136                    try {
137                            Future<?> future = ProcessUtil.execute(
138                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, cmdLine);
139    
140                            future.get();
141                    }
142                    catch (Exception e) {
143                            _log.error(e);
144                    }
145            }
146    
147            private static final Log _log = LogFactoryUtil.getLog(ShellHook.class);
148    
149    }