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