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