1
22
23 package com.liferay.mail.util;
24
25 import com.liferay.portal.kernel.util.ProcessUtil;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.util.PropsUtil;
29 import com.liferay.util.FileUtil;
30
31 import java.io.BufferedReader;
32 import java.io.File;
33 import java.io.FileReader;
34
35 import java.util.List;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40
46 public class SendmailHook implements Hook {
47
48 public void addForward(
49 long userId, List filters, List emailAddresses, boolean leaveCopy) {
50
51 try {
52 if (emailAddresses != null) {
53 String home = PropsUtil.get(PropsUtil.MAIL_HOOK_SENDMAIL_HOME);
54
55 File file = new File(home + "/" + userId + "/.forward");
56
57 if (emailAddresses.size() > 0) {
58 StringMaker sm = new StringMaker();
59
60 for (int i = 0; i < emailAddresses.size(); i++) {
61 String emailAddress = (String)emailAddresses.get(i);
62
63 sm.append(emailAddress);
64 sm.append("\n");
65 }
66
67 FileUtil.write(file, sm.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 userId, String password, String firstName, String middleName,
81 String lastName, String emailAddress) {
82
83
85 String addUserCmd =
86 PropsUtil.get(PropsUtil.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(userId, password);
105 updateEmailAddress(userId, emailAddress);
106 }
107
108 public void addVacationMessage(
109 long userId, String emailAddress, String vacationMessage) {
110 }
111
112 public void deleteEmailAddress(long userId) {
113 updateEmailAddress(userId, "");
114 }
115
116 public void deleteUser(long userId) {
117 deleteEmailAddress(userId);
118
119
121 String deleteUserCmd =
122 PropsUtil.get(PropsUtil.MAIL_HOOK_SENDMAIL_DELETE_USER);
123
124
126 deleteUserCmd = StringUtil.replace(
127 deleteUserCmd, "%1%", String.valueOf(userId));
128
129 Runtime rt = Runtime.getRuntime();
130
131 try {
132 Process p = rt.exec(deleteUserCmd);
133
134 ProcessUtil.close(p);
135 }
136 catch (Exception e) {
137 _log.error(e, e);
138 }
139 }
140
141 public void updateBlocked(long userId, List blocked) {
142 String home = PropsUtil.get(PropsUtil.MAIL_HOOK_SENDMAIL_HOME);
143
144 File file = new File(home + "/" + userId + "/.procmailrc");
145
146 if ((blocked == null) || (blocked.size() == 0)) {
147 file.delete();
148
149 return;
150 }
151
152 StringMaker sm = new StringMaker();
153
154 sm.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
155 sm.append("MAILDIR $HOME/\n");
156 sm.append("SENDMAIL /usr/smin/sendmail\n");
157
158 for (int i = 0; i < blocked.size(); i++) {
159 String emailAddress = (String)blocked.get(i);
160
161 sm.append("\n");
162 sm.append(":0\n");
163 sm.append("* ^From.*");
164 sm.append(emailAddress);
165 sm.append("\n");
166 sm.append("{\n");
167 sm.append(":0\n");
168 sm.append("/dev/null\n");
169 sm.append("}\n");
170 }
171
172 try {
173 FileUtil.write(file, sm.toString());
174 }
175 catch (Exception e) {
176 _log.error(e, e);
177 }
178 }
179
180 public void updateEmailAddress(long userId, String emailAddress) {
181 try {
182 String virtusertable =
183 PropsUtil.get(PropsUtil.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
184
185 FileReader fr = new FileReader(virtusertable);
186 BufferedReader br = new BufferedReader(fr);
187
188 StringMaker sm = new StringMaker();
189
190 for (String s = br.readLine(); s != null; s = br.readLine()) {
191 if (!s.endsWith(" " + userId)) {
192 sm.append(s);
193 sm.append('\n');
194 }
195 }
196
197 if ((emailAddress != null) && (!emailAddress.equals(""))) {
198 sm.append(emailAddress);
199 sm.append(" ");
200 sm.append(userId);
201 sm.append('\n');
202 }
203
204 br.close();
205 fr.close();
206
207 FileUtil.write(virtusertable, sm.toString());
208
209 String virtusertableRefreshCmd =
210 PropsUtil.get(
211 PropsUtil.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 userId, String password) {
225
226
228 String changePasswordCmd =
229 PropsUtil.get(PropsUtil.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
230
231
233 changePasswordCmd = StringUtil.replace(
234 changePasswordCmd, "%1%", String.valueOf(userId));
235
236
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 = LogFactory.getLog(SendmailHook.class);
254
255 }