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