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.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.util.PropsUtil;
027
028 import java.io.File;
029 import java.io.FileReader;
030
031 import java.util.List;
032
033
036 public class SendmailHook 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_SENDMAIL_HOME);
045
046 File file = new File(home + "/" + userId + "/.forward");
047
048 if (emailAddresses.size() > 0) {
049 StringBundler sb = new StringBundler(
050 emailAddresses.size() * 2);
051
052 for (int i = 0; i < emailAddresses.size(); i++) {
053 String emailAddress = emailAddresses.get(i);
054
055 sb.append(emailAddress);
056 sb.append("\n");
057 }
058
059 FileUtil.write(file, sb.toString());
060 }
061 else {
062 file.delete();
063 }
064 }
065 }
066 catch (Exception e) {
067 _log.error(e, e);
068 }
069 }
070
071 public void addUser(
072 long companyId, long userId, String password, String firstName,
073 String middleName, String lastName, String emailAddress) {
074
075
076
077 String addUserCmd =
078 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
079
080
081
082 addUserCmd = StringUtil.replace(
083 addUserCmd, "%1%", String.valueOf(userId));
084
085 Runtime rt = Runtime.getRuntime();
086
087 try {
088 Process p = rt.exec(addUserCmd);
089
090 ProcessUtil.close(p);
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 =
115 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
116
117
118
119 deleteUserCmd = StringUtil.replace(
120 deleteUserCmd, "%1%", String.valueOf(userId));
121
122 Runtime rt = Runtime.getRuntime();
123
124 try {
125 Process p = rt.exec(deleteUserCmd);
126
127 ProcessUtil.close(p);
128 }
129 catch (Exception e) {
130 _log.error(e, e);
131 }
132 }
133
134 public void updateBlocked(
135 long companyId, long userId, List<String> blocked) {
136
137 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
138
139 File file = new File(home + "/" + userId + "/.procmailrc");
140
141 if ((blocked == null) || (blocked.size() == 0)) {
142 file.delete();
143
144 return;
145 }
146
147 StringBundler sb = new StringBundler(blocked.size() * 9 + 3);
148
149 sb.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
150 sb.append("MAILDIR $HOME/\n");
151 sb.append("SENDMAIL /usr/smin/sendmail\n");
152
153 for (int i = 0; i < blocked.size(); i++) {
154 String emailAddress = blocked.get(i);
155
156 sb.append("\n");
157 sb.append(":0\n");
158 sb.append("* ^From.*");
159 sb.append(emailAddress);
160 sb.append("\n");
161 sb.append("{\n");
162 sb.append(":0\n");
163 sb.append("/dev/null\n");
164 sb.append("}\n");
165 }
166
167 try {
168 FileUtil.write(file, sb.toString());
169 }
170 catch (Exception e) {
171 _log.error(e, e);
172 }
173 }
174
175 public void updateEmailAddress(
176 long companyId, long userId, String emailAddress) {
177
178 try {
179 String virtusertable =
180 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
181
182 FileReader fileReader = new FileReader(virtusertable);
183 UnsyncBufferedReader unsyncBufferedReader =
184 new UnsyncBufferedReader(fileReader);
185
186 StringBundler sb = new StringBundler();
187
188 for (String s = unsyncBufferedReader.readLine(); s != null;
189 s = unsyncBufferedReader.readLine()) {
190
191 if (!s.endsWith(" " + userId)) {
192 sb.append(s);
193 sb.append('\n');
194 }
195 }
196
197 if ((emailAddress != null) && (!emailAddress.equals(""))) {
198 sb.append(emailAddress);
199 sb.append(" ");
200 sb.append(userId);
201 sb.append('\n');
202 }
203
204 unsyncBufferedReader.close();
205 fileReader.close();
206
207 FileUtil.write(virtusertable, sb.toString());
208
209 String virtusertableRefreshCmd =
210 PropsUtil.get(
211 PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
212
213 Runtime rt = Runtime.getRuntime();
214
215 Process p = rt.exec(virtusertableRefreshCmd);
216
217 ProcessUtil.close(p);
218 }
219 catch (Exception e) {
220 _log.error(e, e);
221 }
222 }
223
224 public void updatePassword(long companyId, long userId, String password) {
225
226
227
228 String changePasswordCmd =
229 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
230
231
232
233 changePasswordCmd = StringUtil.replace(
234 changePasswordCmd, "%1%", String.valueOf(userId));
235
236
237
238 changePasswordCmd = StringUtil.replace(
239 changePasswordCmd, "%2%", password);
240
241 Runtime rt = Runtime.getRuntime();
242
243 try {
244 Process p = rt.exec(changePasswordCmd);
245
246 ProcessUtil.close(p);
247 }
248 catch (Exception e) {
249 _log.error(e, e);
250 }
251 }
252
253 private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
254
255 }