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