1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.mail.util;
16  
17  import com.liferay.mail.model.Filter;
18  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.ProcessUtil;
23  import com.liferay.portal.kernel.util.PropsKeys;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.util.PropsUtil;
27  
28  import java.io.File;
29  import java.io.FileReader;
30  
31  import java.util.List;
32  
33  /**
34   * <a href="SendmailHook.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class SendmailHook implements Hook {
39  
40      public void addForward(
41          long companyId, long userId, List<Filter> filters,
42          List<String> emailAddresses, boolean leaveCopy) {
43  
44          try {
45              if (emailAddresses != null) {
46                  String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
47  
48                  File file = new File(home + "/" + userId + "/.forward");
49  
50                  if (emailAddresses.size() > 0) {
51                      StringBundler sb = new StringBundler(
52                          emailAddresses.size() * 2);
53  
54                      for (int i = 0; i < emailAddresses.size(); i++) {
55                          String emailAddress = emailAddresses.get(i);
56  
57                          sb.append(emailAddress);
58                          sb.append("\n");
59                      }
60  
61                      FileUtil.write(file, sb.toString());
62                  }
63                  else {
64                      file.delete();
65                  }
66              }
67          }
68          catch (Exception e) {
69              _log.error(e, e);
70          }
71      }
72  
73      public void addUser(
74          long companyId, long userId, String password, String firstName,
75          String middleName, String lastName, String emailAddress) {
76  
77          // Get add user command
78  
79          String addUserCmd =
80              PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
81  
82          // Replace userId
83  
84          addUserCmd = StringUtil.replace(
85              addUserCmd, "%1%", String.valueOf(userId));
86  
87          Runtime rt = Runtime.getRuntime();
88  
89          try {
90              Process p = rt.exec(addUserCmd);
91  
92              ProcessUtil.close(p);
93          }
94          catch (Exception e) {
95              _log.error(e, e);
96          }
97  
98          updatePassword(companyId, userId, password);
99          updateEmailAddress(companyId, userId, emailAddress);
100     }
101 
102     public void addVacationMessage(
103         long companyId, long userId, String emailAddress,
104         String vacationMessage) {
105     }
106 
107     public void deleteEmailAddress(long companyId, long userId) {
108         updateEmailAddress(companyId, userId, "");
109     }
110 
111     public void deleteUser(long companyId, long userId) {
112         deleteEmailAddress(companyId, userId);
113 
114         // Get delete user command
115 
116         String deleteUserCmd =
117             PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
118 
119         // Replace userId
120 
121         deleteUserCmd = StringUtil.replace(
122             deleteUserCmd, "%1%", String.valueOf(userId));
123 
124         Runtime rt = Runtime.getRuntime();
125 
126         try {
127             Process p = rt.exec(deleteUserCmd);
128 
129             ProcessUtil.close(p);
130         }
131         catch (Exception e) {
132             _log.error(e, e);
133         }
134     }
135 
136     public void updateBlocked(
137         long companyId, long userId, List<String> blocked) {
138 
139         String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
140 
141         File file = new File(home + "/" + userId + "/.procmailrc");
142 
143         if ((blocked == null) || (blocked.size() == 0)) {
144             file.delete();
145 
146             return;
147         }
148 
149         StringBundler sb = new StringBundler(blocked.size() * 9 + 3);
150 
151         sb.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
152         sb.append("MAILDIR $HOME/\n");
153         sb.append("SENDMAIL /usr/smin/sendmail\n");
154 
155         for (int i = 0; i < blocked.size(); i++) {
156             String emailAddress = blocked.get(i);
157 
158             sb.append("\n");
159             sb.append(":0\n");
160             sb.append("* ^From.*");
161             sb.append(emailAddress);
162             sb.append("\n");
163             sb.append("{\n");
164             sb.append(":0\n");
165             sb.append("/dev/null\n");
166             sb.append("}\n");
167         }
168 
169         try {
170             FileUtil.write(file, sb.toString());
171         }
172         catch (Exception e) {
173             _log.error(e, e);
174         }
175     }
176 
177     public void updateEmailAddress(
178         long companyId, long userId, String emailAddress) {
179 
180         try {
181             String virtusertable =
182                 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
183 
184             FileReader fileReader = new FileReader(virtusertable);
185             UnsyncBufferedReader unsyncBufferedReader =
186                 new UnsyncBufferedReader(fileReader);
187 
188             StringBundler sb = new StringBundler();
189 
190             for (String s = unsyncBufferedReader.readLine(); s != null;
191                     s = unsyncBufferedReader.readLine()) {
192 
193                 if (!s.endsWith(" " + userId)) {
194                     sb.append(s);
195                     sb.append('\n');
196                 }
197             }
198 
199             if ((emailAddress != null) && (!emailAddress.equals(""))) {
200                 sb.append(emailAddress);
201                 sb.append(" ");
202                 sb.append(userId);
203                 sb.append('\n');
204             }
205 
206             unsyncBufferedReader.close();
207             fileReader.close();
208 
209             FileUtil.write(virtusertable, sb.toString());
210 
211             String virtusertableRefreshCmd =
212                 PropsUtil.get(
213                     PropsKeys.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 companyId, long userId, String password) {
227 
228         // Get change password command
229 
230         String changePasswordCmd =
231             PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
232 
233         // Replace userId
234 
235         changePasswordCmd = StringUtil.replace(
236             changePasswordCmd, "%1%", String.valueOf(userId));
237 
238         // Replace password
239 
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 = LogFactoryUtil.getLog(SendmailHook.class);
256 
257 }