1
19
20 package com.liferay.mail.util;
21
22 import com.liferay.mail.NoSuchCyrusUserException;
23 import com.liferay.mail.model.CyrusUser;
24 import com.liferay.mail.model.CyrusVirtual;
25 import com.liferay.mail.model.Filter;
26 import com.liferay.mail.service.persistence.CyrusUserUtil;
27 import com.liferay.mail.service.persistence.CyrusVirtualUtil;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.FileUtil;
31 import com.liferay.portal.kernel.util.ProcessUtil;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.util.PropsKeys;
35 import com.liferay.portal.util.PropsUtil;
36
37 import java.io.File;
38
39 import java.util.List;
40
41
47 public class CyrusHook implements Hook {
48
49 public void addForward(
50 long companyId, long userId, List<Filter> filters,
51 List<String> emailAddresses, boolean leaveCopy) {
52
53 try {
54 if (emailAddresses != null) {
55 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
56
57 File file = new File(home + "/" + userId + ".procmail.forward");
58
59 if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
60 (leaveCopy)) {
61
62 StringBuilder sb = new StringBuilder();
63
64 for (int i = 0; i < filters.size(); i++) {
65 Filter filter = filters.get(i);
66
67 sb.append(":0\n");
68 sb.append("* ^(From|Cc|To).*");
69 sb.append(filter.getEmailAddress());
70 sb.append("\n");
71 sb.append("| $DELIVER -e -a $USER -m user.$USER.");
72 sb.append(filter.getFolder());
73 sb.append("\n\n");
74 }
75
76 if (leaveCopy) {
77 sb.append(":0 c\n");
78 sb.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
79 }
80
81 if (emailAddresses.size() > 0) {
82 sb.append(":0\n");
83 sb.append("!");
84
85 for (String emailAddress : emailAddresses) {
86 sb.append(" ");
87 sb.append(emailAddress);
88 }
89 }
90
91 String content = sb.toString();
92
93 while (content.endsWith("\n")) {
94 content = content.substring(0, content.length() - 1);
95 }
96
97 FileUtil.write(file, content);
98 }
99 else {
100 file.delete();
101 }
102 }
103 }
104 catch (Exception e) {
105 _log.error(e, e);
106 }
107 }
108
109 public void addUser(
110 long companyId, long userId, String password, String firstName,
111 String middleName, String lastName, String emailAddress) {
112
113 try {
114
115
117 CyrusUser user = new CyrusUser(userId, password);
118
119 CyrusUserUtil.update(user);
120
121
123 CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
124
125 CyrusVirtualUtil.update(virtual);
126
127
129 String addUserCmd =
130 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
131
132 addUserCmd = StringUtil.replace(
133 addUserCmd, "%1%", String.valueOf(userId));
134
135 Runtime rt = Runtime.getRuntime();
136
137 Process p = rt.exec(addUserCmd);
138
139 ProcessUtil.close(p);
140 }
141 catch (Exception e) {
142 _log.error(e, e);
143 }
144 }
145
146 public void addVacationMessage(
147 long companyId, long userId, String emailAddress,
148 String vacationMessage) {
149
150 try {
151 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
152
153
155 new File(home + "/" + userId + ".vacation.cache").delete();
156
157
159 File vacation = new File(home + "/" + userId + ".vacation");
160
161 if (Validator.isNull(vacationMessage)) {
162 vacation.delete();
163 }
164 else {
165 FileUtil.write(vacation, emailAddress + "\n" + vacationMessage);
166 }
167 }
168 catch (Exception e) {
169 _log.error(e, e);
170 }
171 }
172
173 public void deleteEmailAddress(long companyId, long userId) {
174 try {
175 CyrusVirtualUtil.removeByUserId(userId);
176 }
177 catch (Exception e) {
178 _log.error(e, e);
179 }
180 }
181
182 public void deleteUser(long companyId, long userId) {
183 try {
184
185
187 try {
188 CyrusUserUtil.remove(userId);
189 }
190 catch (NoSuchCyrusUserException nscue) {
191 }
192
193
195 CyrusVirtualUtil.removeByUserId(userId);
196
197
199 String deleteUserCmd =
200 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_DELETE_USER);
201
202 deleteUserCmd = StringUtil.replace(
203 deleteUserCmd, "%1%", String.valueOf(userId));
204
205 Runtime rt = Runtime.getRuntime();
206
207 Process p = rt.exec(deleteUserCmd);
208
209 ProcessUtil.close(p);
210
211
213 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
214
215 File file = new File(home + "/" + userId + ".procmail.blocked");
216
217 if (file.exists()) {
218 file.delete();
219 }
220
221 file = new File(home + "/" + userId + ".procmail.forward");
222
223 if (file.exists()) {
224 file.delete();
225 }
226
227 file = new File(home + "/" + userId + ".vacation");
228
229 if (file.exists()) {
230 file.delete();
231 }
232
233 file = new File(home + "/" + userId + ".vacation.cache");
234
235 if (file.exists()) {
236 file.delete();
237 }
238 }
239 catch (Exception e) {
240 _log.error(e, e);
241 }
242 }
243
244 public void updateBlocked(
245 long companyId, long userId, List<String> blocked) {
246
247 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
248
249 File file = new File(home + "/" + userId + ".procmail.blocked");
250
251 if ((blocked == null) || (blocked.size() == 0)) {
252 file.delete();
253
254 return;
255 }
256
257 StringBuilder sb = new StringBuilder();
258
259 for (int i = 0; i < blocked.size(); i++) {
260 String emailAddress = blocked.get(i);
261
262 sb.append("\n");
263 sb.append(":0\n");
264 sb.append("* ^From.*").append(emailAddress).append("\n");
265 sb.append("{\n");
266 sb.append(":0\n");
267 sb.append("/dev/null\n");
268 sb.append("}\n");
269 }
270
271 try {
272 FileUtil.write(file, sb.toString());
273 }
274 catch (Exception e) {
275 _log.error(e, e);
276 }
277 }
278
279 public void updateEmailAddress(
280 long companyId, long userId, String emailAddress) {
281
282 try {
283 CyrusVirtualUtil.removeByUserId(userId);
284
285 CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
286
287 CyrusVirtualUtil.update(virtual);
288 }
289 catch (Exception e) {
290 _log.error(e, e);
291 }
292 }
293
294 public void updatePassword(long companyId, long userId, String password) {
295 CyrusUser user = null;
296
297 try {
298 user = CyrusUserUtil.findByPrimaryKey(userId);
299 }
300 catch (NoSuchCyrusUserException nscue) {
301 user = new CyrusUser(userId, password);
302 }
303 catch (Exception e) {
304 _log.error(e, e);
305 }
306
307 try {
308 user.setPassword(password);
309
310 CyrusUserUtil.update(user);
311 }
312 catch (Exception e) {
313 _log.error(e, e);
314 }
315 }
316
317 private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
318
319 }