001
014
015 package com.liferay.mail.util;
016
017 import com.liferay.mail.NoSuchCyrusUserException;
018 import com.liferay.mail.model.CyrusUser;
019 import com.liferay.mail.model.CyrusVirtual;
020 import com.liferay.mail.model.Filter;
021 import com.liferay.mail.service.persistence.CyrusUserUtil;
022 import com.liferay.mail.service.persistence.CyrusVirtualUtil;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.util.FileUtil;
026 import com.liferay.portal.kernel.util.ProcessUtil;
027 import com.liferay.portal.kernel.util.PropsKeys;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringUtil;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.util.PropsUtil;
032
033 import java.io.File;
034
035 import java.util.List;
036
037
040 public class CyrusHook implements Hook {
041
042 public void addForward(
043 long companyId, long userId, List<Filter> filters,
044 List<String> emailAddresses, boolean leaveCopy) {
045
046 try {
047 if (emailAddresses != null) {
048 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
049
050 File file = new File(home + "/" + userId + ".procmail.forward");
051
052 if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
053 (leaveCopy)) {
054
055 StringBundler sb = new StringBundler();
056
057 for (int i = 0; i < filters.size(); i++) {
058 Filter filter = filters.get(i);
059
060 sb.append(":0\n");
061 sb.append("* ^(From|Cc|To).*");
062 sb.append(filter.getEmailAddress());
063 sb.append("\n");
064 sb.append("| $DELIVER -e -a $USER -m user.$USER.");
065 sb.append(filter.getFolder());
066 sb.append("\n\n");
067 }
068
069 if (leaveCopy) {
070 sb.append(":0 c\n");
071 sb.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
072 }
073
074 if (emailAddresses.size() > 0) {
075 sb.append(":0\n");
076 sb.append("!");
077
078 for (String emailAddress : emailAddresses) {
079 sb.append(" ");
080 sb.append(emailAddress);
081 }
082 }
083
084 String content = sb.toString();
085
086 while (content.endsWith("\n")) {
087 content = content.substring(0, content.length() - 1);
088 }
089
090 FileUtil.write(file, content);
091 }
092 else {
093 file.delete();
094 }
095 }
096 }
097 catch (Exception e) {
098 _log.error(e, e);
099 }
100 }
101
102 public void addUser(
103 long companyId, long userId, String password, String firstName,
104 String middleName, String lastName, String emailAddress) {
105
106 try {
107
108
109
110 CyrusUser user = new CyrusUser(userId, password);
111
112 CyrusUserUtil.update(user);
113
114
115
116 CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
117
118 CyrusVirtualUtil.update(virtual);
119
120
121
122 String addUserCmd =
123 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
124
125 addUserCmd = StringUtil.replace(
126 addUserCmd, "%1%", String.valueOf(userId));
127
128 Runtime rt = Runtime.getRuntime();
129
130 Process p = rt.exec(addUserCmd);
131
132 ProcessUtil.close(p);
133 }
134 catch (Exception e) {
135 _log.error(e, e);
136 }
137 }
138
139 public void addVacationMessage(
140 long companyId, long userId, String emailAddress,
141 String vacationMessage) {
142
143 try {
144 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
145
146
147
148 new File(home + "/" + userId + ".vacation.cache").delete();
149
150
151
152 File vacation = new File(home + "/" + userId + ".vacation");
153
154 if (Validator.isNull(vacationMessage)) {
155 vacation.delete();
156 }
157 else {
158 FileUtil.write(vacation, emailAddress + "\n" + vacationMessage);
159 }
160 }
161 catch (Exception e) {
162 _log.error(e, e);
163 }
164 }
165
166 public void deleteEmailAddress(long companyId, long userId) {
167 try {
168 CyrusVirtualUtil.removeByUserId(userId);
169 }
170 catch (Exception e) {
171 _log.error(e, e);
172 }
173 }
174
175 public void deleteUser(long companyId, long userId) {
176 try {
177
178
179
180 try {
181 CyrusUserUtil.remove(userId);
182 }
183 catch (NoSuchCyrusUserException nscue) {
184 }
185
186
187
188 CyrusVirtualUtil.removeByUserId(userId);
189
190
191
192 String deleteUserCmd =
193 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_DELETE_USER);
194
195 deleteUserCmd = StringUtil.replace(
196 deleteUserCmd, "%1%", String.valueOf(userId));
197
198 Runtime rt = Runtime.getRuntime();
199
200 Process p = rt.exec(deleteUserCmd);
201
202 ProcessUtil.close(p);
203
204
205
206 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
207
208 File file = new File(home + "/" + userId + ".procmail.blocked");
209
210 if (file.exists()) {
211 file.delete();
212 }
213
214 file = new File(home + "/" + userId + ".procmail.forward");
215
216 if (file.exists()) {
217 file.delete();
218 }
219
220 file = new File(home + "/" + userId + ".vacation");
221
222 if (file.exists()) {
223 file.delete();
224 }
225
226 file = new File(home + "/" + userId + ".vacation.cache");
227
228 if (file.exists()) {
229 file.delete();
230 }
231 }
232 catch (Exception e) {
233 _log.error(e, e);
234 }
235 }
236
237 public void updateBlocked(
238 long companyId, long userId, List<String> blocked) {
239
240 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
241
242 File file = new File(home + "/" + userId + ".procmail.blocked");
243
244 if ((blocked == null) || (blocked.size() == 0)) {
245 file.delete();
246
247 return;
248 }
249
250 StringBundler sb = new StringBundler(blocked.size() * 9);
251
252 for (int i = 0; i < blocked.size(); i++) {
253 String emailAddress = blocked.get(i);
254
255 sb.append("\n");
256 sb.append(":0\n");
257 sb.append("* ^From.*");
258 sb.append(emailAddress);
259 sb.append("\n");
260 sb.append("{\n");
261 sb.append(":0\n");
262 sb.append("/dev/null\n");
263 sb.append("}\n");
264 }
265
266 try {
267 FileUtil.write(file, sb.toString());
268 }
269 catch (Exception e) {
270 _log.error(e, e);
271 }
272 }
273
274 public void updateEmailAddress(
275 long companyId, long userId, String emailAddress) {
276
277 try {
278 CyrusVirtualUtil.removeByUserId(userId);
279
280 CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
281
282 CyrusVirtualUtil.update(virtual);
283 }
284 catch (Exception e) {
285 _log.error(e, e);
286 }
287 }
288
289 public void updatePassword(long companyId, long userId, String password) {
290 CyrusUser user = null;
291
292 try {
293 user = CyrusUserUtil.findByPrimaryKey(userId);
294 }
295 catch (NoSuchCyrusUserException nscue) {
296 user = new CyrusUser(userId, password);
297 }
298 catch (Exception e) {
299 _log.error(e, e);
300 }
301
302 try {
303 user.setPassword(password);
304
305 CyrusUserUtil.update(user);
306 }
307 catch (Exception e) {
308 _log.error(e, e);
309 }
310 }
311
312 private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
313
314 }