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.portal.kernel.util.ProcessUtil;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.util.PropsUtil;
027: import com.liferay.util.FileUtil;
028:
029: import java.io.BufferedReader;
030: import java.io.File;
031: import java.io.FileReader;
032:
033: import java.util.List;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * <a href="SendmailHook.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class SendmailHook implements Hook {
045:
046: public void addForward(long userId, List filters,
047: List emailAddresses, boolean leaveCopy) {
048:
049: try {
050: if (emailAddresses != null) {
051: String home = PropsUtil
052: .get(PropsUtil.MAIL_HOOK_SENDMAIL_HOME);
053:
054: File file = new File(home + "/" + userId + "/.forward");
055:
056: if (emailAddresses.size() > 0) {
057: StringMaker sm = new StringMaker();
058:
059: for (int i = 0; i < emailAddresses.size(); i++) {
060: String emailAddress = (String) emailAddresses
061: .get(i);
062:
063: sm.append(emailAddress);
064: sm.append("\n");
065: }
066:
067: FileUtil.write(file, sm.toString());
068: } else {
069: file.delete();
070: }
071: }
072: } catch (Exception e) {
073: _log.error(e, e);
074: }
075: }
076:
077: public void addUser(long userId, String password, String firstName,
078: String middleName, String lastName, String emailAddress) {
079:
080: // Get add user command
081:
082: String addUserCmd = PropsUtil
083: .get(PropsUtil.MAIL_HOOK_SENDMAIL_ADD_USER);
084:
085: // Replace userId
086:
087: addUserCmd = StringUtil.replace(addUserCmd, "%1%", String
088: .valueOf(userId));
089:
090: Runtime rt = Runtime.getRuntime();
091:
092: try {
093: Process p = rt.exec(addUserCmd);
094:
095: ProcessUtil.close(p);
096: } catch (Exception e) {
097: _log.error(e, e);
098: }
099:
100: updatePassword(userId, password);
101: updateEmailAddress(userId, emailAddress);
102: }
103:
104: public void addVacationMessage(long userId, String emailAddress,
105: String vacationMessage) {
106: }
107:
108: public void deleteEmailAddress(long userId) {
109: updateEmailAddress(userId, "");
110: }
111:
112: public void deleteUser(long userId) {
113: deleteEmailAddress(userId);
114:
115: // Get delete user command
116:
117: String deleteUserCmd = PropsUtil
118: .get(PropsUtil.MAIL_HOOK_SENDMAIL_DELETE_USER);
119:
120: // Replace userId
121:
122: deleteUserCmd = StringUtil.replace(deleteUserCmd, "%1%", String
123: .valueOf(userId));
124:
125: Runtime rt = Runtime.getRuntime();
126:
127: try {
128: Process p = rt.exec(deleteUserCmd);
129:
130: ProcessUtil.close(p);
131: } catch (Exception e) {
132: _log.error(e, e);
133: }
134: }
135:
136: public void updateBlocked(long userId, List blocked) {
137: String home = PropsUtil.get(PropsUtil.MAIL_HOOK_SENDMAIL_HOME);
138:
139: File file = new File(home + "/" + userId + "/.procmailrc");
140:
141: if ((blocked == null) || (blocked.size() == 0)) {
142: file.delete();
143:
144: return;
145: }
146:
147: StringMaker sm = new StringMaker();
148:
149: sm.append("ORGMAIL /var/spool/mail/$LOGNAME\n");
150: sm.append("MAILDIR $HOME/\n");
151: sm.append("SENDMAIL /usr/smin/sendmail\n");
152:
153: for (int i = 0; i < blocked.size(); i++) {
154: String emailAddress = (String) blocked.get(i);
155:
156: sm.append("\n");
157: sm.append(":0\n");
158: sm.append("* ^From.*");
159: sm.append(emailAddress);
160: sm.append("\n");
161: sm.append("{\n");
162: sm.append(":0\n");
163: sm.append("/dev/null\n");
164: sm.append("}\n");
165: }
166:
167: try {
168: FileUtil.write(file, sm.toString());
169: } catch (Exception e) {
170: _log.error(e, e);
171: }
172: }
173:
174: public void updateEmailAddress(long userId, String emailAddress) {
175: try {
176: String virtusertable = PropsUtil
177: .get(PropsUtil.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE);
178:
179: FileReader fr = new FileReader(virtusertable);
180: BufferedReader br = new BufferedReader(fr);
181:
182: StringMaker sm = new StringMaker();
183:
184: for (String s = br.readLine(); s != null; s = br.readLine()) {
185: if (!s.endsWith(" " + userId)) {
186: sm.append(s);
187: sm.append('\n');
188: }
189: }
190:
191: if ((emailAddress != null) && (!emailAddress.equals(""))) {
192: sm.append(emailAddress);
193: sm.append(" ");
194: sm.append(userId);
195: sm.append('\n');
196: }
197:
198: br.close();
199: fr.close();
200:
201: FileUtil.write(virtusertable, sm.toString());
202:
203: String virtusertableRefreshCmd = PropsUtil
204: .get(PropsUtil.MAIL_HOOK_SENDMAIL_VIRTUSERTABLE_REFRESH);
205:
206: Runtime rt = Runtime.getRuntime();
207:
208: Process p = rt.exec(virtusertableRefreshCmd);
209:
210: ProcessUtil.close(p);
211: } catch (Exception e) {
212: _log.error(e, e);
213: }
214: }
215:
216: public void updatePassword(long userId, String password) {
217:
218: // Get change password command
219:
220: String changePasswordCmd = PropsUtil
221: .get(PropsUtil.MAIL_HOOK_SENDMAIL_CHANGE_PASSWORD);
222:
223: // Replace userId
224:
225: changePasswordCmd = StringUtil.replace(changePasswordCmd,
226: "%1%", String.valueOf(userId));
227:
228: // Replace password
229:
230: changePasswordCmd = StringUtil.replace(changePasswordCmd,
231: "%2%", password);
232:
233: Runtime rt = Runtime.getRuntime();
234:
235: try {
236: Process p = rt.exec(changePasswordCmd);
237:
238: ProcessUtil.close(p);
239: } catch (Exception e) {
240: _log.error(e, e);
241: }
242: }
243:
244: private static Log _log = LogFactory.getLog(SendmailHook.class);
245:
246: }
|