1   /**
2    * Copyright (c) 2000-2008 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.NoSuchCyrusUserException;
26  import com.liferay.mail.model.CyrusUser;
27  import com.liferay.mail.model.CyrusVirtual;
28  import com.liferay.mail.model.Filter;
29  import com.liferay.mail.service.persistence.CyrusUserUtil;
30  import com.liferay.mail.service.persistence.CyrusVirtualUtil;
31  import com.liferay.portal.kernel.util.ProcessUtil;
32  import com.liferay.portal.kernel.util.StringMaker;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.util.FileUtil;
37  
38  import java.io.File;
39  
40  import java.util.List;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="CyrusHook.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class CyrusHook implements Hook {
52  
53      public void addForward(
54          long userId, List<Filter> filters, List<String> emailAddresses,
55          boolean leaveCopy) {
56  
57          try {
58              if (emailAddresses != null) {
59                  String home = PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
60  
61                  File file = new File(home + "/" + userId + ".procmail.forward");
62  
63                  if ((filters.size() > 0) || (emailAddresses.size() > 0) ||
64                      (leaveCopy)) {
65  
66                      StringMaker sm = new StringMaker();
67  
68                      for (int i = 0; i < filters.size(); i++) {
69                          Filter filter = filters.get(i);
70  
71                          sm.append(":0\n");
72                          sm.append("* ^(From|Cc|To).*");
73                          sm.append(filter.getEmailAddress());
74                          sm.append("\n");
75                          sm.append("| $DELIVER -e -a $USER -m user.$USER.");
76                          sm.append(filter.getFolder());
77                          sm.append("\n\n");
78                      }
79  
80                      if (leaveCopy) {
81                          sm.append(":0 c\n");
82                          sm.append("| $DELIVER -e -a $USER -m user.$USER\n\n");
83                      }
84  
85                      if (emailAddresses.size() > 0) {
86                          sm.append(":0\n");
87                          sm.append("!");
88  
89                          for (String emailAddress : emailAddresses) {
90                              sm.append(" ");
91                              sm.append(emailAddress);
92                          }
93                      }
94  
95                      String content = sm.toString();
96  
97                      while (content.endsWith("\n")) {
98                          content = content.substring(0, content.length() - 1);
99                      }
100 
101                     FileUtil.write(file, content);
102                 }
103                 else {
104                     file.delete();
105                 }
106             }
107         }
108         catch (Exception e) {
109             _log.error(e, e);
110         }
111     }
112 
113     public void addUser(
114         long userId, String password, String firstName, String middleName,
115         String lastName, String emailAddress) {
116 
117         try {
118 
119             // User
120 
121             CyrusUser user = new CyrusUser(userId, password);
122 
123             CyrusUserUtil.update(user);
124 
125             // Virtual
126 
127             CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
128 
129             CyrusVirtualUtil.update(virtual);
130 
131             // Expect
132 
133             String addUserCmd =
134                 PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_ADD_USER);
135 
136             addUserCmd = StringUtil.replace(
137                 addUserCmd, "%1%", String.valueOf(userId));
138 
139             Runtime rt = Runtime.getRuntime();
140 
141             Process p = rt.exec(addUserCmd);
142 
143             ProcessUtil.close(p);
144         }
145         catch (Exception e) {
146             _log.error(e, e);
147         }
148     }
149 
150     public void addVacationMessage(
151         long userId, String emailAddress, String vacationMessage) {
152 
153         try {
154             String home = PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
155 
156             // Remove vacation cache
157 
158             new File(home + "/" + userId + ".vacation.cache").delete();
159 
160             // Update vacation message
161 
162             File vacation = new File(home + "/" + userId + ".vacation");
163 
164             if (Validator.isNull(vacationMessage)) {
165                 vacation.delete();
166             }
167             else {
168                 FileUtil.write(vacation, emailAddress + "\n" + vacationMessage);
169             }
170         }
171         catch (Exception e) {
172             _log.error(e, e);
173         }
174     }
175 
176     public void deleteEmailAddress(long userId) {
177         try {
178             CyrusVirtualUtil.removeByUserId(userId);
179         }
180         catch (Exception e) {
181             _log.error(e, e);
182         }
183     }
184 
185     public void deleteUser(long userId) {
186         try {
187 
188             // User
189 
190             try {
191                 CyrusUserUtil.remove(userId);
192             }
193             catch (NoSuchCyrusUserException nscue) {
194             }
195 
196             // Virtual
197 
198             CyrusVirtualUtil.removeByUserId(userId);
199 
200             // Expect
201 
202             String deleteUserCmd =
203                 PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_DELETE_USER);
204 
205             deleteUserCmd = StringUtil.replace(
206                 deleteUserCmd, "%1%", String.valueOf(userId));
207 
208             Runtime rt = Runtime.getRuntime();
209 
210             Process p = rt.exec(deleteUserCmd);
211 
212             ProcessUtil.close(p);
213 
214             // Procmail
215 
216             String home = PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
217 
218             File file = new File(home + "/" + userId + ".procmail.blocked");
219 
220             if (file.exists()) {
221                 file.delete();
222             }
223 
224             file = new File(home + "/" + userId + ".procmail.forward");
225 
226             if (file.exists()) {
227                 file.delete();
228             }
229 
230             file = new File(home + "/" + userId + ".vacation");
231 
232             if (file.exists()) {
233                 file.delete();
234             }
235 
236             file = new File(home + "/" + userId + ".vacation.cache");
237 
238             if (file.exists()) {
239                 file.delete();
240             }
241         }
242         catch (Exception e) {
243             _log.error(e, e);
244         }
245     }
246 
247     public void updateBlocked(long userId, List<String> blocked) {
248         String home = PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
249 
250         File file = new File(home + "/" + userId + ".procmail.blocked");
251 
252         if ((blocked == null) || (blocked.size() == 0)) {
253             file.delete();
254 
255             return;
256         }
257 
258         StringMaker sm = new StringMaker();
259 
260         for (int i = 0; i < blocked.size(); i++) {
261             String emailAddress = blocked.get(i);
262 
263             sm.append("\n");
264             sm.append(":0\n");
265             sm.append("* ^From.*").append(emailAddress).append("\n");
266             sm.append("{\n");
267             sm.append(":0\n");
268             sm.append("/dev/null\n");
269             sm.append("}\n");
270         }
271 
272         try {
273             FileUtil.write(file, sm.toString());
274         }
275         catch (Exception e) {
276             _log.error(e, e);
277         }
278     }
279 
280     public void updateEmailAddress(long userId, String emailAddress) {
281         try {
282             CyrusVirtualUtil.removeByUserId(userId);
283 
284             CyrusVirtual virtual = new CyrusVirtual(emailAddress, userId);
285 
286             CyrusVirtualUtil.update(virtual);
287         }
288         catch (Exception e) {
289             _log.error(e, e);
290         }
291     }
292 
293     public void updatePassword(long userId, String password) {
294         CyrusUser user = null;
295 
296         try {
297             user = CyrusUserUtil.findByPrimaryKey(userId);
298         }
299         catch (NoSuchCyrusUserException nscue) {
300             user = new CyrusUser(userId, password);
301         }
302         catch (Exception e) {
303             _log.error(e, e);
304         }
305 
306         try {
307             user.setPassword(password);
308 
309             CyrusUserUtil.update(user);
310         }
311         catch (Exception e) {
312             _log.error(e, e);
313         }
314     }
315 
316     private static Log _log = LogFactory.getLog(CyrusHook.class);
317 
318 }