1
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.ProcessUtil;
29 import com.liferay.portal.kernel.util.StringPool;
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.util.List;
35
36
42 public class ShellHook implements Hook {
43
44 public static String SHELL_SCRIPT =
45 PropsUtil.get(PropsKeys.MAIL_HOOK_SHELL_SCRIPT);
46
47 public void addFilters(long userId, List<String> filters) {
48 }
49
50 public void addForward(
51 long userId, List<Filter> filters, List<String> emailAddresses,
52 boolean leaveCopy) {
53
54 execute(
55 new String[] {
56 SHELL_SCRIPT, "addForward", String.valueOf(userId),
57 StringUtil.merge(emailAddresses)
58 }
59 );
60 }
61
62 public void addUser(
63 long userId, String password, String firstName, String middleName,
64 String lastName, String emailAddress) {
65
66 execute(
67 new String[] {
68 SHELL_SCRIPT, "addUser", String.valueOf(userId), password,
69 firstName, middleName, lastName, emailAddress
70 }
71 );
72 }
73
74 public void addVacationMessage(
75 long userId, String emailAddress, String vacationMessage) {
76
77 execute(
78 new String[] {
79 SHELL_SCRIPT, "addVacationMessage", String.valueOf(userId),
80 emailAddress, vacationMessage
81 }
82 );
83 }
84
85 public void deleteEmailAddress(long userId) {
86 execute(
87 new String[] {
88 SHELL_SCRIPT, "deleteEmailAddress", String.valueOf(userId)
89 }
90 );
91 }
92
93 public void deleteUser(long userId, String companyMx) {
94 execute(
95 new String[] {
96 SHELL_SCRIPT, "deleteUser", String.valueOf(userId)
97 }
98 );
99 }
100
101 public void updateBlocked(long userId, List<String> blocked) {
102 execute(
103 new String[] {
104 SHELL_SCRIPT, "updateBlocked", String.valueOf(userId),
105 StringUtil.merge(blocked)
106 }
107 );
108 }
109
110 public void updateEmailAddress(long userId, String emailAddress) {
111 execute(
112 new String[] {
113 SHELL_SCRIPT, "updateEmailAddress", String.valueOf(userId),
114 emailAddress
115 }
116 );
117 }
118
119 public void updatePassword(long userId, String password) {
120 execute(
121 new String[] {
122 SHELL_SCRIPT, "updatePassword", String.valueOf(userId), password
123 }
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 StringBuilder sb = new StringBuilder();
145
146 for (int i = 0; i < cmdLine.length; i++) {
147 sb.append(cmdLine[i]);
148 sb.append(StringPool.SPACE);
149 }
150
151 throw new IllegalArgumentException(sb.toString());
152 }
153 }
154 catch (Exception e) {
155 _log.error(e);
156 }
157 }
158
159 private static Log _log = LogFactoryUtil.getLog(ShellHook.class);
160
161 }