001    /**
002     * Copyright (c) 2000-2010 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 String SHELL_SCRIPT =
035                    PropsUtil.get(PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
036    
037            public void addFilters(long companyId, long userId, List<String> filters) {
038            }
039    
040            public void addForward(
041                    long companyId, long userId, List<Filter> filters,
042                    List<String> emailAddresses, boolean leaveCopy) {
043    
044                    execute(
045                            new String[] {
046                                    SHELL_SCRIPT, "addForward", String.valueOf(userId),
047                                    StringUtil.merge(emailAddresses)
048                            }
049                    );
050            }
051    
052            public void addUser(
053                    long companyId, long userId, String password, String firstName,
054                    String middleName, String lastName, String emailAddress) {
055    
056                    execute(
057                            new String[] {
058                                    SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
059                                    firstName, middleName, lastName, emailAddress
060                            }
061                    );
062            }
063    
064            public void addVacationMessage(
065                    long companyId, long userId, String emailAddress,
066                    String vacationMessage) {
067    
068                    execute(
069                            new String[] {
070                                    SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
071                                    emailAddress, vacationMessage
072                            }
073                    );
074            }
075    
076            public void deleteEmailAddress(long companyId, long userId) {
077                    execute(
078                            new String[] {
079                                    SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
080                            }
081                    );
082            }
083    
084            public void deleteUser(long companyId, long userId) {
085                    execute(
086                            new String[] {
087                                    SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
088                            }
089                    );
090            }
091    
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    
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    
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    
122            protected void execute(String cmdLine[]) {
123                    for (int i = 0; i < cmdLine.length; i++) {
124                            if (cmdLine[i].trim().length() == 0) {
125                                    cmdLine[i] = StringPool.UNDERLINE;
126                            }
127                    }
128    
129                    try {
130                            Runtime rt = Runtime.getRuntime();
131    
132                            Process p = rt.exec(cmdLine);
133    
134                            ProcessUtil.close(p);
135    
136                            int exitValue = p.exitValue();
137    
138                            if (exitValue != 0) {
139                                    StringBundler sb = new StringBundler(cmdLine.length * 2);
140    
141                                    for (int i = 0; i < cmdLine.length; i++) {
142                                            sb.append(cmdLine[i]);
143                                            sb.append(StringPool.SPACE);
144                                    }
145    
146                                    throw new IllegalArgumentException(sb.toString());
147                            }
148                    }
149                    catch (Exception e) {
150                            _log.error(e);
151                    }
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
155    
156    }