1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.mail.util;
21  
22  import com.liferay.mail.model.Filter;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.ProcessUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.util.PropsKeys;
29  import com.liferay.portal.util.PropsUtil;
30  
31  import java.io.BufferedReader;
32  import java.io.File;
33  import java.io.FileReader;
34  
35  import java.util.List;
36  
37  /**
38   * <a href="SendmailHook.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class SendmailHook implements Hook {
44  
45      public void addForward(
46          long companyId, long userId, List<Filter> filters,
47          List<String> emailAddresses, boolean leaveCopy) {
48  
49          try {
50              if (emailAddresses != null) {
51                  String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
52  
53                  File file = new File(home + "/" + userId + "/.forward");
54  
55                  if (emailAddresses.size() > 0) {
56                      StringBuilder sb = new StringBuilder();
57  
58                      for (int i = 0; i < emailAddresses.size(); i++) {
59                          String emailAddress = emailAddresses.get(i);
60  
61                          sb.append(emailAddress);
62                          sb.append("\n");
63                      }
64  
65                      FileUtil.write(file, sb.toString());
66                  }
67                  else {
68                      file.delete();
69                  }
70              }
71          }
72          catch (Exception e) {
73              _log.error(e, e);
74          }
75      }
76  
77      public void addUser(
78          long companyId, long userId, String password, String firstName,
79          String middleName, String lastName, String emailAddress) {
80  
81          // Get add user command
82  
83          String addUserCmd =
84              PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
85  
86          // Replace userId
87  
88          addUserCmd = StringUtil.replace(
89              addUserCmd, "%1%", String.valueOf(userId));
90  
91          Runtime rt = Runtime.getRuntime();
92  
93          try {
94              Process p = rt.exec(addUserCmd);
95  
96              ProcessUtil.close(p);
97          }
98          catch (Exception e) {
99              _log.error(e, e);
100         }
101 
102         updatePassword(companyId, userId, password);
103         updateEmailAddress(companyId, userId, emailAddress);
104     }
105 
106     public void addVacationMessage(
107         long companyId, long userId, String emailAddress,
108         String vacationMessage) {
109     }
110 
111     public void deleteEmailAddress(long companyId, long userId) {
112         updateEmailAddress(companyId, userId, "");
113     }
114 
115     public void deleteUser(long companyId, long userId) {
116         deleteEmailAddress(companyId, userId);
117 
118         // Get delete user command
119 
120         String deleteUserCmd =
121             PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
122 
123         // Replace userId
124 
125         deleteUserCmd = StringUtil.replace(
126             deleteUserCmd, "%1%", String.valueOf(userId));
127 
128         Runtime rt = Runtime.getRuntime();
129 
130         try {
131             Process p = rt.exec(deleteUserCmd);
132 
133             ProcessUtil.close(p);
134         }
135         catch (Exception e) {
136             _log.error(e, e);
137         }
138     }
139 
140     public void updateBlocked(
141         long companyId, long userId, List<String> blocked) {
142 
143         String home = PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_HOME);
144 
145         File file = new File(home + "/" + userId + "/.procmailrc");
146 
147         if ((blocked == null) || (blocked.size() == 0)) {
148             file.delete();
149 
150             return;
151         }
152 
153         StringBuilder sb = new StringBuilder();
154 
155         sb.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
156         sb.append("MAILDIR $HOME/\n");
157         sb.append("SENDMAIL /usr/smin/sendmail\n");
158 
159         for (int i = 0; i < blocked.size(); i++) {
160             String emailAddress = blocked.get(i);
161 
162             sb.append("\n");
163             sb.append(":0\n");
164             sb.append("* ^From.*");
165             sb.append(emailAddress);
166             sb.append("\n");
167             sb.append("{\n");
168             sb.append(":0\n");
169             sb.append("/dev/null\n");
170             sb.append("}\n");
171         }
172 
173         try {
174             FileUtil.write(file, sb.toString());
175         }
176         catch (Exception e) {
177             _log.error(e, e);
178         }
179     }
180 
181     public void updateEmailAddress(
182         long companyId, long userId, String emailAddress) {
183 
184         try {
185             String virtusertable =
186                 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
187 
188             FileReader fr = new FileReader(virtusertable);
189             BufferedReader br = new BufferedReader(fr);
190 
191             StringBuilder sb = new StringBuilder();
192 
193             for (String s = br.readLine(); s != null; s = br.readLine()) {
194                 if (!s.endsWith(" " + userId)) {
195                     sb.append(s);
196                     sb.append('\n');
197                 }
198             }
199 
200             if ((emailAddress != null) && (!emailAddress.equals(""))) {
201                 sb.append(emailAddress);
202                 sb.append(" ");
203                 sb.append(userId);
204                 sb.append('\n');
205             }
206 
207             br.close();
208             fr.close();
209 
210             FileUtil.write(virtusertable, sb.toString());
211 
212             String virtusertableRefreshCmd =
213                 PropsUtil.get(
214                     PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
215 
216             Runtime rt = Runtime.getRuntime();
217 
218             Process p = rt.exec(virtusertableRefreshCmd);
219 
220             ProcessUtil.close(p);
221         }
222         catch (Exception e) {
223             _log.error(e, e);
224         }
225     }
226 
227     public void updatePassword(long companyId, long userId, String password) {
228 
229         // Get change password command
230 
231         String changePasswordCmd =
232             PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
233 
234         // Replace userId
235 
236         changePasswordCmd = StringUtil.replace(
237             changePasswordCmd, "%1%", String.valueOf(userId));
238 
239         // Replace password
240 
241         changePasswordCmd = StringUtil.replace(
242             changePasswordCmd, "%2%", password);
243 
244         Runtime rt = Runtime.getRuntime();
245 
246         try {
247             Process p = rt.exec(changePasswordCmd);
248 
249             ProcessUtil.close(p);
250         }
251         catch (Exception e) {
252             _log.error(e, e);
253         }
254     }
255 
256     private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
257 
258 }