001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.mail.util;
022:
023: import com.liferay.mail.NoSuchCyrusUserException;
024: import com.liferay.mail.model.CyrusUser;
025: import com.liferay.mail.model.CyrusVirtual;
026: import com.liferay.mail.model.Filter;
027: import com.liferay.mail.service.persistence.CyrusUserUtil;
028: import com.liferay.mail.service.persistence.CyrusVirtualUtil;
029: import com.liferay.portal.kernel.util.ProcessUtil;
030: import com.liferay.portal.kernel.util.StringMaker;
031: import com.liferay.portal.kernel.util.StringUtil;
032: import com.liferay.portal.kernel.util.Validator;
033: import com.liferay.portal.util.PropsUtil;
034: import com.liferay.util.FileUtil;
035:
036: import java.io.File;
037:
038: import java.util.List;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: /**
044: * <a href="CyrusHook.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class CyrusHook implements Hook {
050:
051: public void addForward(long userId, List filters,
052: List emailAddresses, boolean leaveCopy) {
053:
054: try {
055: if (emailAddresses != null) {
056: String home = PropsUtil
057: .get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
058:
059: File file = new File(home + "/" + userId
060: + ".procmail.forward");
061:
062: if ((filters.size() > 0) || (emailAddresses.size() > 0)
063: || (leaveCopy)) {
064:
065: StringMaker sm = new StringMaker();
066:
067: for (int i = 0; i < filters.size(); i++) {
068: Filter filter = (Filter) filters.get(i);
069:
070: sm.append(":0\n");
071: sm.append("* ^(From|Cc|To).*");
072: sm.append(filter.getEmailAddress());
073: sm.append("\n");
074: sm
075: .append("| $DELIVER -e -a $USER -m user.$USER.");
076: sm.append(filter.getFolder());
077: sm.append("\n\n");
078: }
079:
080: if (leaveCopy) {
081: sm.append(":0 c\n");
082: sm
083: .append("| $DELIVER -e -a $USER -m user.$USER\n\n");
084: }
085:
086: if (emailAddresses.size() > 0) {
087: sm.append(":0\n");
088: sm.append("!");
089:
090: for (int i = 0; i < emailAddresses.size(); i++) {
091: String emailAddress = (String) emailAddresses
092: .get(i);
093: sm.append(" ").append(emailAddress);
094: }
095: }
096:
097: String content = sm.toString();
098:
099: while (content.endsWith("\n")) {
100: content = content.substring(0,
101: content.length() - 1);
102: }
103:
104: FileUtil.write(file, content);
105: } else {
106: file.delete();
107: }
108: }
109: } catch (Exception e) {
110: _log.error(e, e);
111: }
112: }
113:
114: public void addUser(long userId, String password, String firstName,
115: String middleName, 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,
128: userId);
129:
130: CyrusVirtualUtil.update(virtual);
131:
132: // Expect
133:
134: String addUserCmd = PropsUtil
135: .get(PropsUtil.MAIL_HOOK_CYRUS_ADD_USER);
136:
137: addUserCmd = StringUtil.replace(addUserCmd, "%1%", String
138: .valueOf(userId));
139:
140: Runtime rt = Runtime.getRuntime();
141:
142: Process p = rt.exec(addUserCmd);
143:
144: ProcessUtil.close(p);
145: } catch (Exception e) {
146: _log.error(e, e);
147: }
148: }
149:
150: public void addVacationMessage(long userId, String emailAddress,
151: 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: } else {
167: FileUtil.write(vacation, emailAddress + "\n"
168: + vacationMessage);
169: }
170: } catch (Exception e) {
171: _log.error(e, e);
172: }
173: }
174:
175: public void deleteEmailAddress(long userId) {
176: try {
177: CyrusVirtualUtil.removeByUserId(userId);
178: } catch (Exception e) {
179: _log.error(e, e);
180: }
181: }
182:
183: public void deleteUser(long userId) {
184: try {
185:
186: // User
187:
188: try {
189: CyrusUserUtil.remove(userId);
190: } catch (NoSuchCyrusUserException nscue) {
191: }
192:
193: // Virtual
194:
195: CyrusVirtualUtil.removeByUserId(userId);
196:
197: // Expect
198:
199: String deleteUserCmd = PropsUtil
200: .get(PropsUtil.MAIL_HOOK_CYRUS_DELETE_USER);
201:
202: deleteUserCmd = StringUtil.replace(deleteUserCmd, "%1%",
203: String.valueOf(userId));
204:
205: Runtime rt = Runtime.getRuntime();
206:
207: Process p = rt.exec(deleteUserCmd);
208:
209: ProcessUtil.close(p);
210:
211: // Procmail
212:
213: String home = PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
214:
215: File file = new File(home + "/" + userId
216: + ".procmail.blocked");
217:
218: if (file.exists()) {
219: file.delete();
220: }
221:
222: file = new File(home + "/" + userId + ".procmail.forward");
223:
224: if (file.exists()) {
225: file.delete();
226: }
227:
228: file = new File(home + "/" + userId + ".vacation");
229:
230: if (file.exists()) {
231: file.delete();
232: }
233:
234: file = new File(home + "/" + userId + ".vacation.cache");
235:
236: if (file.exists()) {
237: file.delete();
238: }
239: } catch (Exception e) {
240: _log.error(e, e);
241: }
242: }
243:
244: public void updateBlocked(long userId, List blocked) {
245: String home = PropsUtil.get(PropsUtil.MAIL_HOOK_CYRUS_HOME);
246:
247: File file = new File(home + "/" + userId + ".procmail.blocked");
248:
249: if ((blocked == null) || (blocked.size() == 0)) {
250: file.delete();
251:
252: return;
253: }
254:
255: StringMaker sm = new StringMaker();
256:
257: for (int i = 0; i < blocked.size(); i++) {
258: String emailAddress = (String) blocked.get(i);
259:
260: sm.append("\n");
261: sm.append(":0\n");
262: sm.append("* ^From.*").append(emailAddress).append("\n");
263: sm.append("{\n");
264: sm.append(":0\n");
265: sm.append("/dev/null\n");
266: sm.append("}\n");
267: }
268:
269: try {
270: FileUtil.write(file, sm.toString());
271: } catch (Exception e) {
272: _log.error(e, e);
273: }
274: }
275:
276: public void updateEmailAddress(long userId, String emailAddress) {
277: try {
278: CyrusVirtualUtil.removeByUserId(userId);
279:
280: CyrusVirtual virtual = new CyrusVirtual(emailAddress,
281: userId);
282:
283: CyrusVirtualUtil.update(virtual);
284: } catch (Exception e) {
285: _log.error(e, e);
286: }
287: }
288:
289: public void updatePassword(long userId, String password) {
290: CyrusUser user = null;
291:
292: try {
293: user = CyrusUserUtil.findByPrimaryKey(userId);
294: } catch (NoSuchCyrusUserException nscue) {
295: user = new CyrusUser(userId, password);
296: } catch (Exception e) {
297: _log.error(e, e);
298: }
299:
300: try {
301: user.setPassword(password);
302:
303: CyrusUserUtil.update(user);
304: } catch (Exception e) {
305: _log.error(e, e);
306: }
307: }
308:
309: private static Log _log = LogFactory.getLog(CyrusHook.class);
310:
311: }
|