001
014
015 package com.liferay.mail.util;
016
017 import com.liferay.mail.model.Filter;
018 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.process.ProcessUtil;
022 import com.liferay.portal.kernel.util.FileUtil;
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.util.PropsUtil;
027
028 import java.io.File;
029 import java.io.FileReader;
030
031 import java.util.List;
032 import java.util.concurrent.Future;
033
034
037 public class SendmailHook implements Hook {
038
039 public void addForward(
040 long companyId, long userId, List<Filter> filters,
041 List<String> emailAddresses, boolean leaveCopy) {
042
043 try {
044 if (emailAddresses != null) {
045 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
046
047 File file = new File(home + "/" + userId + "/.forward");
048
049 if (emailAddresses.size() > 0) {
050 StringBundler sb = new StringBundler(
051 emailAddresses.size() * 2);
052
053 for (int i = 0; i < emailAddresses.size(); i++) {
054 String emailAddress = emailAddresses.get(i);
055
056 sb.append(emailAddress);
057 sb.append("\n");
058 }
059
060 FileUtil.write(file, sb.toString());
061 }
062 else {
063 file.delete();
064 }
065 }
066 }
067 catch (Exception e) {
068 _log.error(e, e);
069 }
070 }
071
072 public void addUser(
073 long companyId, long userId, String password, String firstName,
074 String middleName, String lastName, String emailAddress) {
075
076
077
078 String addUserCmd = PropsUtil.get(
079 PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
080
081
082
083 addUserCmd = StringUtil.replace(
084 addUserCmd, "%1%", String.valueOf(userId));
085
086 try {
087 Future<?> future = ProcessUtil.execute(
088 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, addUserCmd);
089
090 future.get();
091 }
092 catch (Exception e) {
093 _log.error(e, e);
094 }
095
096 updatePassword(companyId, userId, password);
097 updateEmailAddress(companyId, userId, emailAddress);
098 }
099
100 public void addVacationMessage(
101 long companyId, long userId, String emailAddress,
102 String vacationMessage) {
103 }
104
105 public void deleteEmailAddress(long companyId, long userId) {
106 updateEmailAddress(companyId, userId, "");
107 }
108
109 public void deleteUser(long companyId, long userId) {
110 deleteEmailAddress(companyId, userId);
111
112
113
114 String deleteUserCmd = PropsUtil.get(
115 PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
116
117
118
119 deleteUserCmd = StringUtil.replace(
120 deleteUserCmd, "%1%", String.valueOf(userId));
121
122 try {
123 Future<?> future = ProcessUtil.execute(
124 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, deleteUserCmd);
125
126 future.get();
127 }
128 catch (Exception e) {
129 _log.error(e, e);
130 }
131 }
132
133 public void updateBlocked(
134 long companyId, long userId, List<String> blocked) {
135
136 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
137
138 File file = new File(home + "/" + userId + "/.procmailrc");
139
140 if ((blocked == null) || (blocked.size() == 0)) {
141 file.delete();
142
143 return;
144 }
145
146 StringBundler sb = new StringBundler(blocked.size() * 9 + 3);
147
148 sb.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
149 sb.append("MAILDIR $HOME/\n");
150 sb.append("SENDMAIL /usr/smin/sendmail\n");
151
152 for (int i = 0; i < blocked.size(); i++) {
153 String emailAddress = blocked.get(i);
154
155 sb.append("\n");
156 sb.append(":0\n");
157 sb.append("* ^From.*");
158 sb.append(emailAddress);
159 sb.append("\n");
160 sb.append("{\n");
161 sb.append(":0\n");
162 sb.append("/dev/null\n");
163 sb.append("}\n");
164 }
165
166 try {
167 FileUtil.write(file, sb.toString());
168 }
169 catch (Exception e) {
170 _log.error(e, e);
171 }
172 }
173
174 public void updateEmailAddress(
175 long companyId, long userId, String emailAddress) {
176
177 try {
178 String virtusertable = PropsUtil.get(
179 PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
180
181 FileReader fileReader = new FileReader(virtusertable);
182 UnsyncBufferedReader unsyncBufferedReader =
183 new UnsyncBufferedReader(fileReader);
184
185 StringBundler sb = new StringBundler();
186
187 for (String s = unsyncBufferedReader.readLine(); s != null;
188 s = unsyncBufferedReader.readLine()) {
189
190 if (!s.endsWith(" " + userId)) {
191 sb.append(s);
192 sb.append('\n');
193 }
194 }
195
196 if ((emailAddress != null) && !emailAddress.equals("")) {
197 sb.append(emailAddress);
198 sb.append(" ");
199 sb.append(userId);
200 sb.append('\n');
201 }
202
203 unsyncBufferedReader.close();
204 fileReader.close();
205
206 FileUtil.write(virtusertable, sb.toString());
207
208 String virtusertableRefreshCmd = PropsUtil.get(
209 PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
210
211 Future<?> future = ProcessUtil.execute(
212 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, virtusertableRefreshCmd);
213
214 future.get();
215 }
216 catch (Exception e) {
217 _log.error(e, e);
218 }
219 }
220
221 public void updatePassword(long companyId, long userId, String password) {
222
223
224
225 String changePasswordCmd = PropsUtil.get(
226 PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
227
228
229
230 changePasswordCmd = StringUtil.replace(
231 changePasswordCmd, "%1%", String.valueOf(userId));
232
233
234
235 changePasswordCmd = StringUtil.replace(
236 changePasswordCmd, "%2%", password);
237
238 try {
239 Future<?> future = ProcessUtil.execute(
240 ProcessUtil.LOGGING_OUTPUT_PROCESSOR, changePasswordCmd);
241
242 future.get();
243 }
244 catch (Exception e) {
245 _log.error(e, e);
246 }
247 }
248
249 private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
250
251 }