1
22
23 package com.liferay.mail.util;
24
25 import com.liferay.mail.model.Filter;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.FileUtil;
29 import com.liferay.portal.kernel.util.ProcessUtil;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.util.PropsKeys;
32 import com.liferay.portal.util.PropsUtil;
33
34 import java.io.BufferedReader;
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 fr = new FileReader(virtusertable);
191 BufferedReader br = new BufferedReader(fr);
192
193 StringBuilder sb = new StringBuilder();
194
195 for (String s = br.readLine(); s != null; s = br.readLine()) {
196 if (!s.endsWith(" " + userId)) {
197 sb.append(s);
198 sb.append('\n');
199 }
200 }
201
202 if ((emailAddress != null) && (!emailAddress.equals(""))) {
203 sb.append(emailAddress);
204 sb.append(" ");
205 sb.append(userId);
206 sb.append('\n');
207 }
208
209 br.close();
210 fr.close();
211
212 FileUtil.write(virtusertable, sb.toString());
213
214 String virtusertableRefreshCmd =
215 PropsUtil.get(
216 PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
217
218 Runtime rt = Runtime.getRuntime();
219
220 Process p = rt.exec(virtusertableRefreshCmd);
221
222 ProcessUtil.close(p);
223 }
224 catch (Exception e) {
225 _log.error(e, e);
226 }
227 }
228
229 public void updatePassword(long companyId, long userId, String password) {
230
231
233 String changePasswordCmd =
234 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
235
236
238 changePasswordCmd = StringUtil.replace(
239 changePasswordCmd, "%1%", String.valueOf(userId));
240
241
243 changePasswordCmd = StringUtil.replace(
244 changePasswordCmd, "%2%", password);
245
246 Runtime rt = Runtime.getRuntime();
247
248 try {
249 Process p = rt.exec(changePasswordCmd);
250
251 ProcessUtil.close(p);
252 }
253 catch (Exception e) {
254 _log.error(e, e);
255 }
256 }
257
258 private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
259
260 }