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