1
22
23 package com.liferay.mail.util;
24
25 import com.liferay.mail.model.Filter;
26 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.util.FileUtil;
30 import com.liferay.portal.kernel.util.ProcessUtil;
31 import com.liferay.portal.kernel.util.PropsKeys;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.util.PropsUtil;
34
35 import java.io.File;
36 import java.io.FileReader;
37
38 import java.util.List;
39
40
45 public class SendmailHook implements Hook {
46
47 public void addForward(
48 long companyId, long userId, List<Filter> filters,
49 List<String> emailAddresses, boolean leaveCopy) {
50
51 try {
52 if (emailAddresses != null) {
53 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
54
55 File file = new File(home + "/" + userId + "/.forward");
56
57 if (emailAddresses.size() > 0) {
58 StringBuilder sb = new StringBuilder();
59
60 for (int i = 0; i < emailAddresses.size(); i++) {
61 String emailAddress = emailAddresses.get(i);
62
63 sb.append(emailAddress);
64 sb.append("\n");
65 }
66
67 FileUtil.write(file, sb.toString());
68 }
69 else {
70 file.delete();
71 }
72 }
73 }
74 catch (Exception e) {
75 _log.error(e, e);
76 }
77 }
78
79 public void addUser(
80 long companyId, long userId, String password, String firstName,
81 String middleName, String lastName, String emailAddress) {
82
83
85 String addUserCmd =
86 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
87
88
90 addUserCmd = StringUtil.replace(
91 addUserCmd, "%1%", String.valueOf(userId));
92
93 Runtime rt = Runtime.getRuntime();
94
95 try {
96 Process p = rt.exec(addUserCmd);
97
98 ProcessUtil.close(p);
99 }
100 catch (Exception e) {
101 _log.error(e, e);
102 }
103
104 updatePassword(companyId, userId, password);
105 updateEmailAddress(companyId, userId, emailAddress);
106 }
107
108 public void addVacationMessage(
109 long companyId, long userId, String emailAddress,
110 String vacationMessage) {
111 }
112
113 public void deleteEmailAddress(long companyId, long userId) {
114 updateEmailAddress(companyId, userId, "");
115 }
116
117 public void deleteUser(long companyId, long userId) {
118 deleteEmailAddress(companyId, userId);
119
120
122 String deleteUserCmd =
123 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
124
125
127 deleteUserCmd = StringUtil.replace(
128 deleteUserCmd, "%1%", String.valueOf(userId));
129
130 Runtime rt = Runtime.getRuntime();
131
132 try {
133 Process p = rt.exec(deleteUserCmd);
134
135 ProcessUtil.close(p);
136 }
137 catch (Exception e) {
138 _log.error(e, e);
139 }
140 }
141
142 public void updateBlocked(
143 long companyId, long userId, List<String> blocked) {
144
145 String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
146
147 File file = new File(home + "/" + userId + "/.procmailrc");
148
149 if ((blocked == null) || (blocked.size() == 0)) {
150 file.delete();
151
152 return;
153 }
154
155 StringBuilder sb = new StringBuilder();
156
157 sb.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
158 sb.append("MAILDIR $HOME/\n");
159 sb.append("SENDMAIL /usr/smin/sendmail\n");
160
161 for (int i = 0; i < blocked.size(); i++) {
162 String emailAddress = blocked.get(i);
163
164 sb.append("\n");
165 sb.append(":0\n");
166 sb.append("* ^From.*");
167 sb.append(emailAddress);
168 sb.append("\n");
169 sb.append("{\n");
170 sb.append(":0\n");
171 sb.append("/dev/null\n");
172 sb.append("}\n");
173 }
174
175 try {
176 FileUtil.write(file, sb.toString());
177 }
178 catch (Exception e) {
179 _log.error(e, e);
180 }
181 }
182
183 public void updateEmailAddress(
184 long companyId, long userId, String emailAddress) {
185
186 try {
187 String virtusertable =
188 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
189
190 FileReader fileReader = new FileReader(virtusertable);
191 UnsyncBufferedReader unsyncBufferedReader =
192 new UnsyncBufferedReader(fileReader);
193
194 StringBuilder sb = new StringBuilder();
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 unsyncBufferedReader.close();
213 fileReader.close();
214
215 FileUtil.write(virtusertable, sb.toString());
216
217 String virtusertableRefreshCmd =
218 PropsUtil.get(
219 PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
220
221 Runtime rt = Runtime.getRuntime();
222
223 Process p = rt.exec(virtusertableRefreshCmd);
224
225 ProcessUtil.close(p);
226 }
227 catch (Exception e) {
228 _log.error(e, e);
229 }
230 }
231
232 public void updatePassword(long companyId, long userId, String password) {
233
234
236 String changePasswordCmd =
237 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
238
239
241 changePasswordCmd = StringUtil.replace(
242 changePasswordCmd, "%1%", String.valueOf(userId));
243
244
246 changePasswordCmd = StringUtil.replace(
247 changePasswordCmd, "%2%", password);
248
249 Runtime rt = Runtime.getRuntime();
250
251 try {
252 Process p = rt.exec(changePasswordCmd);
253
254 ProcessUtil.close(p);
255 }
256 catch (Exception e) {
257 _log.error(e, e);
258 }
259 }
260
261 private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
262
263 }