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.io.unsync.UnsyncBufferedReader;
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.FileUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.util.PropsUtil;
027    
028    import java.io.File;
029    import java.io.FileReader;
030    
031    import java.util.List;
032    import java.util.concurrent.Future;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class SendmailHook implements Hook {
038    
039            @Override
040            public void addForward(
041                    long companyId, long userId, List<Filter> filters,
042                    List<String> emailAddresses, boolean leaveCopy) {
043    
044                    try {
045                            if (emailAddresses != null) {
046                                    String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
047    
048                                    File file = new File(home + "/" + userId + "/.forward");
049    
050                                    if (emailAddresses.size() > 0) {
051                                            StringBundler sb = new StringBundler(
052                                                    emailAddresses.size() * 2);
053    
054                                            for (int i = 0; i < emailAddresses.size(); i++) {
055                                                    String emailAddress = emailAddresses.get(i);
056    
057                                                    sb.append(emailAddress);
058                                                    sb.append("\n");
059                                            }
060    
061                                            FileUtil.write(file, sb.toString());
062                                    }
063                                    else {
064                                            file.delete();
065                                    }
066                            }
067                    }
068                    catch (Exception e) {
069                            _log.error(e, e);
070                    }
071            }
072    
073            @Override
074            public void addUser(
075                    long companyId, long userId, String password, String firstName,
076                    String middleName, String lastName, String emailAddress) {
077    
078                    // Get add user command
079    
080                    String addUserCmd = PropsUtil.get(
081                            PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
082    
083                    // Replace userId
084    
085                    addUserCmd = StringUtil.replace(
086                            addUserCmd, "%1%", String.valueOf(userId));
087    
088                    try {
089                            Future<?> future = ProcessUtil.execute(
090                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, addUserCmd);
091    
092                            future.get();
093                    }
094                    catch (Exception e) {
095                            _log.error(e, e);
096                    }
097    
098                    updatePassword(companyId, userId, password);
099                    updateEmailAddress(companyId, userId, emailAddress);
100            }
101    
102            @Override
103            public void addVacationMessage(
104                    long companyId, long userId, String emailAddress,
105                    String vacationMessage) {
106            }
107    
108            @Override
109            public void deleteEmailAddress(long companyId, long userId) {
110                    updateEmailAddress(companyId, userId, "");
111            }
112    
113            @Override
114            public void deleteUser(long companyId, long userId) {
115                    deleteEmailAddress(companyId, userId);
116    
117                    // Get delete user command
118    
119                    String deleteUserCmd = PropsUtil.get(
120                            PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
121    
122                    // Replace userId
123    
124                    deleteUserCmd = StringUtil.replace(
125                            deleteUserCmd, "%1%", String.valueOf(userId));
126    
127                    try {
128                            Future<?> future = ProcessUtil.execute(
129                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, deleteUserCmd);
130    
131                            future.get();
132                    }
133                    catch (Exception e) {
134                            _log.error(e, e);
135                    }
136            }
137    
138            @Override
139            public void updateBlocked(
140                    long companyId, long userId, List<String> blocked) {
141    
142                    String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
143    
144                    File file = new File(home + "/" + userId + "/.procmailrc");
145    
146                    if ((blocked == null) || (blocked.size() == 0)) {
147                            file.delete();
148    
149                            return;
150                    }
151    
152                    StringBundler sb = new StringBundler(blocked.size() * 9 + 3);
153    
154                    sb.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
155                    sb.append("MAILDIR $HOME/\n");
156                    sb.append("SENDMAIL /usr/smin/sendmail\n");
157    
158                    for (int i = 0; i < blocked.size(); i++) {
159                            String emailAddress = blocked.get(i);
160    
161                            sb.append("\n");
162                            sb.append(":0\n");
163                            sb.append("* ^From.*");
164                            sb.append(emailAddress);
165                            sb.append("\n");
166                            sb.append("{\n");
167                            sb.append(":0\n");
168                            sb.append("/dev/null\n");
169                            sb.append("}\n");
170                    }
171    
172                    try {
173                            FileUtil.write(file, sb.toString());
174                    }
175                    catch (Exception e) {
176                            _log.error(e, e);
177                    }
178            }
179    
180            @Override
181            public void updateEmailAddress(
182                    long companyId, long userId, String emailAddress) {
183    
184                    try {
185                            String virtusertable = PropsUtil.get(
186                                    PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
187    
188                            FileReader fileReader = new FileReader(virtusertable);
189                            UnsyncBufferedReader unsyncBufferedReader =
190                                    new UnsyncBufferedReader(fileReader);
191    
192                            StringBundler sb = new StringBundler();
193    
194                            for (String s = unsyncBufferedReader.readLine(); s != null;
195                                            s = unsyncBufferedReader.readLine()) {
196    
197                                    if (!s.endsWith(" " + userId)) {
198                                            sb.append(s);
199                                            sb.append('\n');
200                                    }
201                            }
202    
203                            if ((emailAddress != null) && !emailAddress.equals("")) {
204                                    sb.append(emailAddress);
205                                    sb.append(" ");
206                                    sb.append(userId);
207                                    sb.append('\n');
208                            }
209    
210                            unsyncBufferedReader.close();
211                            fileReader.close();
212    
213                            FileUtil.write(virtusertable, sb.toString());
214    
215                            String virtusertableRefreshCmd = PropsUtil.get(
216                                    PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
217    
218                            Future<?> future = ProcessUtil.execute(
219                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, virtusertableRefreshCmd);
220    
221                            future.get();
222                    }
223                    catch (Exception e) {
224                            _log.error(e, e);
225                    }
226            }
227    
228            @Override
229            public void updatePassword(long companyId, long userId, String password) {
230    
231                    // Get change password command
232    
233                    String changePasswordCmd = PropsUtil.get(
234                            PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
235    
236                    // Replace userId
237    
238                    changePasswordCmd = StringUtil.replace(
239                            changePasswordCmd, "%1%", String.valueOf(userId));
240    
241                    // Replace password
242    
243                    changePasswordCmd = StringUtil.replace(
244                            changePasswordCmd, "%2%", password);
245    
246                    try {
247                            Future<?> future = ProcessUtil.execute(
248                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, changePasswordCmd);
249    
250                            future.get();
251                    }
252                    catch (Exception e) {
253                            _log.error(e, e);
254                    }
255            }
256    
257            private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
258    
259    }