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.mail.service.CyrusServiceUtil;
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.kernel.util.Validator;
027    import com.liferay.portal.util.PropsUtil;
028    
029    import java.io.File;
030    
031    import java.util.List;
032    import java.util.concurrent.Future;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class CyrusHook 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_CYRUS_HOME);
047    
048                                    File file = new File(home + "/" + userId + ".procmail.forward");
049    
050                                    if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
051                                            leaveCopy) {
052    
053                                            StringBundler sb = new StringBundler();
054    
055                                            for (int i = 0; i < filters.size(); i++) {
056                                                    Filter filter = filters.get(i);
057    
058                                                    sb.append(":0\n");
059                                                    sb.append("* ^(From|Cc|To).*");
060                                                    sb.append(filter.getEmailAddress());
061                                                    sb.append("\n");
062                                                    sb.append("| $DELIVER -e -a $USER -m user.$USER.");
063                                                    sb.append(filter.getFolder());
064                                                    sb.append("\n\n");
065                                            }
066    
067                                            if (leaveCopy) {
068                                                    sb.append(":0 c\n");
069                                                    sb.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
070                                            }
071    
072                                            if (emailAddresses.size() > 0) {
073                                                    sb.append(":0\n");
074                                                    sb.append("!");
075    
076                                                    for (String emailAddress : emailAddresses) {
077                                                            sb.append(" ");
078                                                            sb.append(emailAddress);
079                                                    }
080                                            }
081    
082                                            String content = sb.toString();
083    
084                                            while (content.endsWith("\n")) {
085                                                    content = content.substring(0, content.length() - 1);
086                                            }
087    
088                                            FileUtil.write(file, content);
089                                    }
090                                    else {
091                                            file.delete();
092                                    }
093                            }
094                    }
095                    catch (Exception e) {
096                            _log.error(e, e);
097                    }
098            }
099    
100            @Override
101            public void addUser(
102                    long companyId, long userId, String password, String firstName,
103                    String middleName, String lastName, String emailAddress) {
104    
105                    try {
106                            CyrusServiceUtil.addUser(userId, emailAddress, password);
107    
108                            // Expect
109    
110                            String addUserCmd = PropsUtil.get(
111                                    PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
112    
113                            addUserCmd = StringUtil.replace(
114                                    addUserCmd, "%1%", String.valueOf(userId));
115    
116                            Future<?> future = ProcessUtil.execute(
117                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, addUserCmd);
118    
119                            future.get();
120                    }
121                    catch (Exception e) {
122                            _log.error(e, e);
123                    }
124            }
125    
126            @Override
127            public void addVacationMessage(
128                    long companyId, long userId, String emailAddress,
129                    String vacationMessage) {
130    
131                    try {
132                            String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
133    
134                            // Remove vacation cache
135    
136                            new File(home + "/" + userId + ".vacation.cache").delete();
137    
138                            // Update vacation message
139    
140                            File vacation = new File(home + "/" + userId + ".vacation");
141    
142                            if (Validator.isNull(vacationMessage)) {
143                                    vacation.delete();
144                            }
145                            else {
146                                    FileUtil.write(vacation, emailAddress + "\n" + vacationMessage);
147                            }
148                    }
149                    catch (Exception e) {
150                            _log.error(e, e);
151                    }
152            }
153    
154            @Override
155            public void deleteEmailAddress(long companyId, long userId) {
156                    try {
157                            CyrusServiceUtil.deleteEmailAddress(companyId, userId);
158                    }
159                    catch (Exception e) {
160                            _log.error(e, e);
161                    }
162            }
163    
164            @Override
165            public void deleteUser(long companyId, long userId) {
166                    try {
167                            CyrusServiceUtil.deleteUser(userId);
168    
169                            // Expect
170    
171                            String deleteUserCmd = PropsUtil.get(
172                                    PropsKeys.MAIL_HOOK_CYRUS_DELETE_USER);
173    
174                            deleteUserCmd = StringUtil.replace(
175                                    deleteUserCmd, "%1%", String.valueOf(userId));
176    
177                            Future<?> future = ProcessUtil.execute(
178                                    ProcessUtil.LOGGING_OUTPUT_PROCESSOR, deleteUserCmd);
179    
180                            future.get();
181    
182                            // Procmail
183    
184                            String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
185    
186                            File file = new File(home + "/" + userId + ".procmail.blocked");
187    
188                            if (file.exists()) {
189                                    file.delete();
190                            }
191    
192                            file = new File(home + "/" + userId + ".procmail.forward");
193    
194                            if (file.exists()) {
195                                    file.delete();
196                            }
197    
198                            file = new File(home + "/" + userId + ".vacation");
199    
200                            if (file.exists()) {
201                                    file.delete();
202                            }
203    
204                            file = new File(home + "/" + userId + ".vacation.cache");
205    
206                            if (file.exists()) {
207                                    file.delete();
208                            }
209                    }
210                    catch (Exception e) {
211                            _log.error(e, e);
212                    }
213            }
214    
215            @Override
216            public void updateBlocked(
217                    long companyId, long userId, List<String> blocked) {
218    
219                    String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
220    
221                    File file = new File(home + "/" + userId + ".procmail.blocked");
222    
223                    if ((blocked == null) || (blocked.size() == 0)) {
224                            file.delete();
225    
226                            return;
227                    }
228    
229                    StringBundler sb = new StringBundler(blocked.size() * 9);
230    
231                    for (int i = 0; i < blocked.size(); i++) {
232                            String emailAddress = blocked.get(i);
233    
234                            sb.append("\n");
235                            sb.append(":0\n");
236                            sb.append("* ^From.*");
237                            sb.append(emailAddress);
238                            sb.append("\n");
239                            sb.append("{\n");
240                            sb.append(":0\n");
241                            sb.append("/dev/null\n");
242                            sb.append("}\n");
243                    }
244    
245                    try {
246                            FileUtil.write(file, sb.toString());
247                    }
248                    catch (Exception e) {
249                            _log.error(e, e);
250                    }
251            }
252    
253            @Override
254            public void updateEmailAddress(
255                    long companyId, long userId, String emailAddress) {
256    
257                    try {
258                            CyrusServiceUtil.updateEmailAddress(
259                                    companyId, userId, emailAddress);
260                    }
261                    catch (Exception e) {
262                            _log.error(e, e);
263                    }
264            }
265    
266            @Override
267            public void updatePassword(long companyId, long userId, String password) {
268                    try {
269                            CyrusServiceUtil.updatePassword(companyId, userId, password);
270                    }
271                    catch (Exception e) {
272                            _log.error(e, e);
273                    }
274            }
275    
276            private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
277    
278    }