1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
41   * <a href="SendmailHook.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SendmailHook implements Hook {
47  
48      public void addForward(
49          long userId, List<Filter> filters, List<String> emailAddresses,
50          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 userId, String password, String firstName, String middleName,
82          String lastName, String emailAddress) {
83  
84          // Get add user command
85  
86          String addUserCmd =
87              PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_ADD_USER);
88  
89          // Replace userId
90  
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(userId, password);
106         updateEmailAddress(userId, emailAddress);
107     }
108 
109     public void addVacationMessage(
110         long userId, String emailAddress, String vacationMessage) {
111     }
112 
113     public void deleteEmailAddress(long userId) {
114         updateEmailAddress(userId, "");
115     }
116 
117     public void deleteUser(long userId, String companyMx) {
118         deleteEmailAddress(userId);
119 
120         // Get delete user command
121 
122         String deleteUserCmd =
123             PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_DELETE_USER);
124 
125         // Replace userId
126 
127         deleteUserCmd = StringUtil.replace(
128             deleteUserCmd, "%1%", String.valueOf(userId));
129 
130         Runtime rt = Runtime.getRuntime();
131 
132         try {
133             Process p = rt.exec(deleteUserCmd);
134 
135             ProcessUtil.close(p);
136         }
137         catch (Exception e) {
138             _log.error(e, e);
139         }
140     }
141 
142     public void updateBlocked(long userId, List<String> blocked) {
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(long userId, String emailAddress) {
182         try {
183             String virtusertable =
184                 PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
185 
186             FileReader fr = new FileReader(virtusertable);
187             BufferedReader br = new BufferedReader(fr);
188 
189             StringBuilder sb = new StringBuilder();
190 
191             for (String s = br.readLine(); s != null; s = br.readLine()) {
192                 if (!s.endsWith(" " + userId)) {
193                     sb.append(s);
194                     sb.append('\n');
195                 }
196             }
197 
198             if ((emailAddress != null) && (!emailAddress.equals(""))) {
199                 sb.append(emailAddress);
200                 sb.append(" ");
201                 sb.append(userId);
202                 sb.append('\n');
203             }
204 
205             br.close();
206             fr.close();
207 
208             FileUtil.write(virtusertable, sb.toString());
209 
210             String virtusertableRefreshCmd =
211                 PropsUtil.get(
212                     PropsKeys.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
213 
214             Runtime rt = Runtime.getRuntime();
215 
216             Process p = rt.exec(virtusertableRefreshCmd);
217 
218             ProcessUtil.close(p);
219         }
220         catch (Exception e) {
221             _log.error(e, e);
222         }
223     }
224 
225     public void updatePassword(long userId, String password) {
226 
227         // Get change password command
228 
229         String changePasswordCmd =
230             PropsUtil.get(PropsKeys.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
231 
232         // Replace userId
233 
234         changePasswordCmd = StringUtil.replace(
235             changePasswordCmd, "%1%", String.valueOf(userId));
236 
237         // Replace password
238 
239         changePasswordCmd = StringUtil.replace(
240             changePasswordCmd, "%2%", password);
241 
242         Runtime rt = Runtime.getRuntime();
243 
244         try {
245             Process p = rt.exec(changePasswordCmd);
246 
247             ProcessUtil.close(p);
248         }
249         catch (Exception e) {
250             _log.error(e, e);
251         }
252     }
253 
254     private static Log _log = LogFactoryUtil.getLog(SendmailHook.class);
255 
256 }