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