1
22
23 package com.liferay.mail.util;
24
25 import com.liferay.mail.model.Filter;
26 import com.liferay.portal.kernel.util.ProcessUtil;
27 import com.liferay.portal.kernel.util.StringMaker;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.util.PropsUtil;
31
32 import java.util.List;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
43 public class ShellHook implements Hook {
44
45 public static String SHELL_SCRIPT =
46 PropsUtil.get(PropsUtil.MAIL_HOOK_SHELL_SCRIPT);
47
48 public void addFilters(long userId, List<String> filters) {
49 }
50
51 public void addForward(
52 long userId, List<Filter> filters, List<String> emailAddresses,
53 boolean leaveCopy) {
54
55 execute(
56 new String[] {
57 SHELL_SCRIPT, "addForward", String.valueOf(userId),
58 StringUtil.merge(emailAddresses)
59 }
60 );
61 }
62
63 public void addUser(
64 long userId, String password, String firstName, String middleName,
65 String lastName, String emailAddress) {
66
67 execute(
68 new String[] {
69 SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
70 firstName, middleName, lastName, emailAddress
71 }
72 );
73 }
74
75 public void addVacationMessage(
76 long userId, String emailAddress, String vacationMessage) {
77
78 execute(
79 new String[] {
80 SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
81 emailAddress, vacationMessage
82 }
83 );
84 }
85
86 public void deleteEmailAddress(long userId) {
87 execute(
88 new String[] {
89 SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
90 }
91 );
92 }
93
94 public void deleteUser(long userId) {
95 execute(
96 new String[] {
97 SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
98 }
99 );
100 }
101
102 public void updateBlocked(long userId, List<String> blocked) {
103 execute(
104 new String[] {
105 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
106 StringUtil.merge(blocked)
107 }
108 );
109 }
110
111 public void updateEmailAddress(long userId, String emailAddress) {
112 execute(
113 new String[] {
114 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
115 emailAddress
116 }
117 );
118 }
119
120 public void updatePassword(long userId, String password) {
121 execute(
122 new String[] {
123 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
124 });
125 }
126
127 protected void execute(String cmdLine[]) {
128 for (int i = 0; i < cmdLine.length; i++) {
129 if (cmdLine[i].trim().length() == 0) {
130 cmdLine[i] = StringPool.UNDERLINE;
131 }
132 }
133
134 try {
135 Runtime rt = Runtime.getRuntime();
136
137 Process p = rt.exec(cmdLine);
138
139 ProcessUtil.close(p);
140
141 int exitValue = p.exitValue();
142
143 if (exitValue != 0) {
144 StringMaker cmd = new StringMaker();
145
146 for (int i = 0; i < cmdLine.length; i++) {
147 cmd.append(cmdLine[i]);
148 cmd.append(StringPool.SPACE);
149 }
150
151 throw new IllegalArgumentException(cmd.toString());
152 }
153 }
154 catch (Exception e) {
155 _log.error(e);
156 }
157 }
158
159 private static Log _log = LogFactory.getLog(ShellHook.class);
160
161 }