001
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
037 public class CyrusHook implements Hook {
038
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 public void addUser(
100 long companyId, long userId, String password, String firstName,
101 String middleName, String lastName, String emailAddress) {
102
103 try {
104 CyrusServiceUtil.addUser(userId, emailAddress, password);
105
106
107
108 String addUserCmd = PropsUtil.get(
109 PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
110
111 addUserCmd = StringUtil.replace(
112 addUserCmd, "%1%", String.valueOf(userId));
113
114 Future<?> future = ProcessUtil.execute(
115 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, addUserCmd);
116
117 future.get();
118 }
119 catch (Exception e) {
120 _log.error(e, e);
121 }
122 }
123
124 public void addVacationMessage(
125 long companyId, long userId, String emailAddress,
126 String vacationMessage) {
127
128 try {
129 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
130
131
132
133 new File(home + "/" + userId + ".vacation.cache").delete();
134
135
136
137 File vacation = new File(home + "/" + userId + ".vacation");
138
139 if (Validator.isNull(vacationMessage)) {
140 vacation.delete();
141 }
142 else {
143 FileUtil.write(vacation, emailAddress + "\n" + vacationMessage);
144 }
145 }
146 catch (Exception e) {
147 _log.error(e, e);
148 }
149 }
150
151 public void deleteEmailAddress(long companyId, long userId) {
152 try {
153 CyrusServiceUtil.deleteEmailAddress(companyId, userId);
154 }
155 catch (Exception e) {
156 _log.error(e, e);
157 }
158 }
159
160 public void deleteUser(long companyId, long userId) {
161 try {
162 CyrusServiceUtil.deleteUser(userId);
163
164
165
166 String deleteUserCmd = PropsUtil.get(
167 PropsKeys.MAIL_HOOK_CYRUS_DELETE_USER);
168
169 deleteUserCmd = StringUtil.replace(
170 deleteUserCmd, "%1%", String.valueOf(userId));
171
172 Future<?> future = ProcessUtil.execute(
173 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, deleteUserCmd);
174
175 future.get();
176
177
178
179 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
180
181 File file = new File(home + "/" + userId + ".procmail.blocked");
182
183 if (file.exists()) {
184 file.delete();
185 }
186
187 file = new File(home + "/" + userId + ".procmail.forward");
188
189 if (file.exists()) {
190 file.delete();
191 }
192
193 file = new File(home + "/" + userId + ".vacation");
194
195 if (file.exists()) {
196 file.delete();
197 }
198
199 file = new File(home + "/" + userId + ".vacation.cache");
200
201 if (file.exists()) {
202 file.delete();
203 }
204 }
205 catch (Exception e) {
206 _log.error(e, e);
207 }
208 }
209
210 public void updateBlocked(
211 long companyId, long userId, List<String> blocked) {
212
213 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
214
215 File file = new File(home + "/" + userId + ".procmail.blocked");
216
217 if ((blocked == null) || (blocked.size() == 0)) {
218 file.delete();
219
220 return;
221 }
222
223 StringBundler sb = new StringBundler(blocked.size() * 9);
224
225 for (int i = 0; i < blocked.size(); i++) {
226 String emailAddress = blocked.get(i);
227
228 sb.append("\n");
229 sb.append(":0\n");
230 sb.append("* ^From.*");
231 sb.append(emailAddress);
232 sb.append("\n");
233 sb.append("{\n");
234 sb.append(":0\n");
235 sb.append("/dev/null\n");
236 sb.append("}\n");
237 }
238
239 try {
240 FileUtil.write(file, sb.toString());
241 }
242 catch (Exception e) {
243 _log.error(e, e);
244 }
245 }
246
247 public void updateEmailAddress(
248 long companyId, long userId, String emailAddress) {
249
250 try {
251 CyrusServiceUtil.updateEmailAddress(
252 companyId, userId, emailAddress);
253 }
254 catch (Exception e) {
255 _log.error(e, e);
256 }
257 }
258
259 public void updatePassword(long companyId, long userId, String password) {
260 try {
261 CyrusServiceUtil.updatePassword(companyId, userId, password);
262 }
263 catch (Exception e) {
264 _log.error(e, e);
265 }
266 }
267
268 private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
269
270 }