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            @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            @Override
066            public void addVacationMessage(
067                    long companyId, long userId, String emailAddress,
068                    String vacationMessage) {
069    
070                    execute(
071                            new String[] {
072                                    SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
073                                    emailAddress, vacationMessage
074                            });
075            }
076    
077            @Override
078            public void deleteEmailAddress(long companyId, long userId) {
079                    execute(
080                            new String[] {
081                                    SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
082                            });
083            }
084    
085            @Override
086            public void deleteUser(long companyId, long userId) {
087                    execute(
088                            new String[] {SHELL_SCRIPT, "deleteUser", String.valueOf(userId)});
089            }
090    
091            @Override
092            public void updateBlocked(
093                    long companyId, long userId, List<String> blocked) {
094    
095                    execute(
096                            new String[] {
097                                    SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
098                                    StringUtil.merge(blocked)
099                            });
100            }
101    
102            @Override
103            public void updateEmailAddress(
104                    long companyId, long userId, String emailAddress) {
105    
106                    execute(
107                            new String[] {
108                                    SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
109                                    emailAddress
110                            });
111            }
112    
113            @Override
114            public void updatePassword(long companyId, long userId, String password) {
115                    execute(
116                            new String[] {
117                                    SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
118                            });
119            }
120    
121            protected void execute(String[] cmdLine) {
122                    for (int i = 0; i < cmdLine.length; i++) {
123                            if (cmdLine[i].trim().length() == 0) {
124                                    cmdLine[i] = StringPool.UNDERLINE;
125                            }
126                    }
127    
128                    try {
129                            Future<?> future = ProcessUtil.execute(
130                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, cmdLine);
131    
132                            future.get();
133                    }
134                    catch (Exception e) {
135                            _log.error(e);
136                    }
137            }
138    
139            private static final Log _log = LogFactoryUtil.getLog(ShellHook.class);
140    
141    }