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.util.FileUtil;
022 import com.liferay.portal.kernel.util.ProcessUtil;
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
033
036 public class CyrusHook implements Hook {
037
038 public void addForward(
039 long companyId, long userId, List<Filter> filters,
040 List<String> emailAddresses, boolean leaveCopy) {
041
042 try {
043 if (emailAddresses != null) {
044 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
045
046 File file = new File(home + "/" + userId + ".procmail.forward");
047
048 if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
049 (leaveCopy)) {
050
051 StringBundler sb = new StringBundler();
052
053 for (int i = 0; i < filters.size(); i++) {
054 Filter filter = filters.get(i);
055
056 sb.append(":0\n");
057 sb.append("* ^(From|Cc|To).*");
058 sb.append(filter.getEmailAddress());
059 sb.append("\n");
060 sb.append("| $DELIVER -e -a $USER -m user.$USER.");
061 sb.append(filter.getFolder());
062 sb.append("\n\n");
063 }
064
065 if (leaveCopy) {
066 sb.append(":0 c\n");
067 sb.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
068 }
069
070 if (emailAddresses.size() > 0) {
071 sb.append(":0\n");
072 sb.append("!");
073
074 for (String emailAddress : emailAddresses) {
075 sb.append(" ");
076 sb.append(emailAddress);
077 }
078 }
079
080 String content = sb.toString();
081
082 while (content.endsWith("\n")) {
083 content = content.substring(0, content.length() - 1);
084 }
085
086 FileUtil.write(file, content);
087 }
088 else {
089 file.delete();
090 }
091 }
092 }
093 catch (Exception e) {
094 _log.error(e, e);
095 }
096 }
097
098 public void addUser(
099 long companyId, long userId, String password, String firstName,
100 String middleName, String lastName, String emailAddress) {
101
102 try {
103 CyrusServiceUtil.addUser(userId, emailAddress, password);
104
105
106
107 String addUserCmd =
108 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_ADD_USER);
109
110 addUserCmd = StringUtil.replace(
111 addUserCmd, "%1%", String.valueOf(userId));
112
113 Runtime rt = Runtime.getRuntime();
114
115 Process p = rt.exec(addUserCmd);
116
117 ProcessUtil.close(p);
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 =
167 PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_DELETE_USER);
168
169 deleteUserCmd = StringUtil.replace(
170 deleteUserCmd, "%1%", String.valueOf(userId));
171
172 Runtime rt = Runtime.getRuntime();
173
174 Process p = rt.exec(deleteUserCmd);
175
176 ProcessUtil.close(p);
177
178
179
180 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
181
182 File file = new File(home + "/" + userId + ".procmail.blocked");
183
184 if (file.exists()) {
185 file.delete();
186 }
187
188 file = new File(home + "/" + userId + ".procmail.forward");
189
190 if (file.exists()) {
191 file.delete();
192 }
193
194 file = new File(home + "/" + userId + ".vacation");
195
196 if (file.exists()) {
197 file.delete();
198 }
199
200 file = new File(home + "/" + userId + ".vacation.cache");
201
202 if (file.exists()) {
203 file.delete();
204 }
205 }
206 catch (Exception e) {
207 _log.error(e, e);
208 }
209 }
210
211 public void updateBlocked(
212 long companyId, long userId, List<String> blocked) {
213
214 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_CYRUS_HOME);
215
216 File file = new File(home + "/" + userId + ".procmail.blocked");
217
218 if ((blocked == null) || (blocked.size() == 0)) {
219 file.delete();
220
221 return;
222 }
223
224 StringBundler sb = new StringBundler(blocked.size() * 9);
225
226 for (int i = 0; i < blocked.size(); i++) {
227 String emailAddress = blocked.get(i);
228
229 sb.append("\n");
230 sb.append(":0\n");
231 sb.append("* ^From.*");
232 sb.append(emailAddress);
233 sb.append("\n");
234 sb.append("{\n");
235 sb.append(":0\n");
236 sb.append("/dev/null\n");
237 sb.append("}\n");
238 }
239
240 try {
241 FileUtil.write(file, sb.toString());
242 }
243 catch (Exception e) {
244 _log.error(e, e);
245 }
246 }
247
248 public void updateEmailAddress(
249 long companyId, long userId, String emailAddress) {
250
251 try {
252 CyrusServiceUtil.updateEmailAddress(
253 companyId, userId, emailAddress);
254 }
255 catch (Exception e) {
256 _log.error(e, e);
257 }
258 }
259
260 public void updatePassword(long companyId, long userId, String password) {
261 try {
262 CyrusServiceUtil.updatePassword(companyId, userId, password);
263 }
264 catch (Exception e) {
265 _log.error(e, e);
266 }
267 }
268
269 private static Log _log = LogFactoryUtil.getLog(CyrusHook.class);
270
271 }