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.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.kernel.util.Validator;
027    import com.liferay.portal.util.PropsUtil;
028    
029    import java.io.File;
030    
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class CyrusHook 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_CYRUS_HOME);
046    
047                                    File file = new File(home + "/" + userId + ".procmail.forward");
048    
049                                    if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
050                                            leaveCopy) {
051    
052                                            StringBundler sb = new StringBundler();
053    
054                                            for (int i = 0; i < filters.size(); i++) {
055                                                    Filter filter = filters.get(i);
056    
057                                                    sb.append(":0\n");
058                                                    sb.append("* ^(From|Cc|To).*");
059                                                    sb.append(filter.getEmailAddress());
060                                                    sb.append("\n");
061                                                    sb.append("| $DELIVER -e -a $USER -m user.$USER.");
062                                                    sb.append(filter.getFolder());
063                                                    sb.append("\n\n");
064                                            }
065    
066                                            if (leaveCopy) {
067                                                    sb.append(":0 c\n");
068                                                    sb.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
069                                            }
070    
071                                            if (emailAddresses.size() > 0) {
072                                                    sb.append(":0\n");
073                                                    sb.append("!");
074    
075                                                    for (String emailAddress : emailAddresses) {
076                                                            sb.append(" ");
077                                                            sb.append(emailAddress);
078                                                    }
079                                            }
080    
081                                            String content = sb.toString();
082    
083                                            while (content.endsWith("\n")) {
084                                                    content = content.substring(0, content.length() - 1);
085                                            }
086    
087                                            FileUtil.write(file, content);
088                                    }
089                                    else {
090                                            file.delete();
091                                    }
092                            }
093                    }
094                    catch (Exception e) {
095                            _log.error(e, e);
096                    }
097            }
098    
099            @Override
100            public void addUser(
101                    long companyId, long userId, String password, String firstName,
102                    String middleName, String lastName, String emailAddress) {
103    
104                    try {
105                            CyrusServiceUtil.addUser(userId, emailAddress, password);
106    
107                            // Expect
108    
109                            String addUserCmd = PropsUtil.get(
110                                    PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
111    
112                            addUserCmd = StringUtil.replace(
113                                    addUserCmd, "%1%", String.valueOf(userId));
114    
115                            Runtime rt = Runtime.getRuntime();
116    
117                            Process p = rt.exec(addUserCmd);
118    
119                            ProcessUtil.close(p);
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                            Runtime rt = Runtime.getRuntime();
178    
179                            Process p = rt.exec(deleteUserCmd);
180    
181                            ProcessUtil.close(p);
182    
183                            // Procmail
184    
185                            String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
186    
187                            File file = new File(home + "/" + userId + ".procmail.blocked");
188    
189                            if (file.exists()) {
190                                    file.delete();
191                            }
192    
193                            file = new File(home + "/" + userId + ".procmail.forward");
194    
195                            if (file.exists()) {
196                                    file.delete();
197                            }
198    
199                            file = new File(home + "/" + userId + ".vacation");
200    
201                            if (file.exists()) {
202                                    file.delete();
203                            }
204    
205                            file = new File(home + "/" + userId + ".vacation.cache");
206    
207                            if (file.exists()) {
208                                    file.delete();
209                            }
210                    }
211                    catch (Exception e) {
212                            _log.error(e, e);
213                    }
214            }
215    
216            @Override
217            public void updateBlocked(
218                    long companyId, long userId, List<String> blocked) {
219    
220                    String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
221    
222                    File file = new File(home + "/" + userId + ".procmail.blocked");
223    
224                    if ((blocked == null) || (blocked.size() == 0)) {
225                            file.delete();
226    
227                            return;
228                    }
229    
230                    StringBundler sb = new StringBundler(blocked.size() * 9);
231    
232                    for (int i = 0; i < blocked.size(); i++) {
233                            String emailAddress = blocked.get(i);
234    
235                            sb.append("\n");
236                            sb.append(":0\n");
237                            sb.append("* ^From.*");
238                            sb.append(emailAddress);
239                            sb.append("\n");
240                            sb.append("{\n");
241                            sb.append(":0\n");
242                            sb.append("/dev/null\n");
243                            sb.append("}\n");
244                    }
245    
246                    try {
247                            FileUtil.write(file, sb.toString());
248                    }
249                    catch (Exception e) {
250                            _log.error(e, e);
251                    }
252            }
253    
254            @Override
255            public void updateEmailAddress(
256                    long companyId, long userId, String emailAddress) {
257    
258                    try {
259                            CyrusServiceUtil.updateEmailAddress(
260                                    companyId, userId, emailAddress);
261                    }
262                    catch (Exception e) {
263                            _log.error(e, e);
264                    }
265            }
266    
267            @Override
268            public void updatePassword(long companyId, long userId, String password) {
269                    try {
270                            CyrusServiceUtil.updatePassword(companyId, userId, password);
271                    }
272                    catch (Exception e) {
273                            _log.error(e, e);
274                    }
275            }
276    
277            private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
278    
279    }