1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail;
20
21 import davmail.caldav.CaldavServer;
22 import davmail.exception.DavMailException;
23 import davmail.exchange.ExchangeSessionFactory;
24 import davmail.exchange.auth.ExchangeAuthenticator;
25 import davmail.http.HttpClientAdapter;
26 import davmail.http.request.GetRequest;
27 import davmail.imap.ImapServer;
28 import davmail.ldap.LdapServer;
29 import davmail.pop.PopServer;
30 import davmail.smtp.SmtpServer;
31 import davmail.ui.tray.DavGatewayTray;
32 import org.apache.log4j.Logger;
33
34 import java.awt.*;
35 import java.io.IOException;
36 import java.lang.reflect.InvocationTargetException;
37 import java.util.ArrayList;
38
39
40
41
42 public final class DavGateway {
43 private static final Logger LOGGER = Logger.getLogger(DavGateway.class);
44 private static final String HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT = "https://davmail.sourceforge.net/version.txt";
45
46 private static final Object LOCK = new Object();
47 private static boolean shutdown = false;
48
49 private DavGateway() {
50 }
51
52 private static final ArrayList<AbstractServer> SERVER_LIST = new ArrayList<>();
53
54
55
56
57
58
59 public static void main(String[] args) {
60 boolean notray = false;
61 boolean server = false;
62 boolean token = false;
63 for (String arg : args) {
64 if (arg.startsWith("-")) {
65 if ("-notray".equals(arg)) {
66 notray = true;
67 } else if ("-server".equals(arg)) {
68 server = true;
69 } else if ("-token".equals(arg)) {
70 token = true;
71 }
72 } else {
73 Settings.setConfigFilePath(arg);
74 }
75 }
76
77 Settings.load();
78 if (token) {
79 try {
80 ExchangeAuthenticator authenticator = (ExchangeAuthenticator) Class.forName("davmail.exchange.auth.O365InteractiveAuthenticator")
81 .getDeclaredConstructor().newInstance();
82 authenticator.setUsername("");
83 authenticator.authenticate();
84 System.out.println(authenticator.getToken().getRefreshToken());
85 } catch (IOException | ClassNotFoundException | NoSuchMethodException | InstantiationException |
86 IllegalAccessException | InvocationTargetException e) {
87 System.err.println(e+" "+e.getMessage());
88 }
89
90 System.exit(0);
91 } else {
92
93 if (GraphicsEnvironment.isHeadless()) {
94
95 LOGGER.debug("Headless mode, do not create GUI");
96 server = true;
97 }
98 if (server) {
99 Settings.setProperty("davmail.server", "true");
100 Settings.updateLoggingConfig();
101 }
102
103
104 if (Settings.getBooleanProperty("davmail.server")) {
105 LOGGER.debug("Start DavMail in server mode");
106 } else {
107 LOGGER.debug("Start DavMail in GUI mode");
108 DavGatewayTray.init(notray);
109 }
110
111 start();
112
113
114 if (Settings.getBooleanProperty("davmail.server")) {
115 Runtime.getRuntime().addShutdownHook(new Thread("Shutdown") {
116 @Override
117 public void run() {
118 shutdown = true;
119 DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_INTERRUPTED"));
120 DavGateway.stop();
121 synchronized (LOCK) {
122 LOCK.notifyAll();
123 }
124 }
125 });
126
127 synchronized (LOCK) {
128 try {
129 while (!shutdown) {
130 LOCK.wait();
131 }
132 } catch (InterruptedException e) {
133 DavGatewayTray.debug(new BundleMessage("LOG_GATEWAY_INTERRUPTED"));
134 Thread.currentThread().interrupt();
135 }
136 }
137
138 }
139 }
140 }
141
142
143
144
145 public static void start() {
146 SERVER_LIST.clear();
147
148 int smtpPort = Settings.getIntProperty("davmail.smtpPort");
149 if (smtpPort != 0) {
150 SERVER_LIST.add(new SmtpServer(smtpPort));
151 }
152 int popPort = Settings.getIntProperty("davmail.popPort");
153 if (popPort != 0) {
154 SERVER_LIST.add(new PopServer(popPort));
155 }
156 int imapPort = Settings.getIntProperty("davmail.imapPort");
157 if (imapPort != 0) {
158 SERVER_LIST.add(new ImapServer(imapPort));
159 }
160 int caldavPort = Settings.getIntProperty("davmail.caldavPort");
161 if (caldavPort != 0) {
162 SERVER_LIST.add(new CaldavServer(caldavPort));
163 }
164 int ldapPort = Settings.getIntProperty("davmail.ldapPort");
165 if (ldapPort != 0) {
166 SERVER_LIST.add(new LdapServer(ldapPort));
167 }
168
169 BundleMessage.BundleMessageList messages = new BundleMessage.BundleMessageList();
170 BundleMessage.BundleMessageList errorMessages = new BundleMessage.BundleMessageList();
171 for (AbstractServer server : SERVER_LIST) {
172 try {
173 server.bind();
174 server.start();
175 messages.add(new BundleMessage("LOG_PROTOCOL_PORT", server.getProtocolName(), server.getPort()));
176 } catch (DavMailException e) {
177 errorMessages.add(e.getBundleMessage());
178 }
179 }
180
181 final String currentVersion = getCurrentVersion();
182 boolean showStartupBanner = Settings.getBooleanProperty("davmail.showStartupBanner", true);
183 if (showStartupBanner) {
184 DavGatewayTray.info(new BundleMessage("LOG_DAVMAIL_GATEWAY_LISTENING", currentVersion, messages));
185 }
186 if (!errorMessages.isEmpty()) {
187 DavGatewayTray.error(new BundleMessage("LOG_MESSAGE", errorMessages));
188 }
189
190
191 new Thread("CheckRelease") {
192 @Override
193 public void run() {
194 String releasedVersion = getReleasedVersion();
195 if (!currentVersion.isEmpty() && releasedVersion != null && currentVersion.compareTo(releasedVersion) < 0) {
196 DavGatewayTray.info(new BundleMessage("LOG_NEW_VERSION_AVAILABLE", releasedVersion));
197 }
198
199 }
200 }.start();
201
202 }
203
204
205
206
207 public static void stop() {
208 DavGateway.stopServers();
209
210 ExchangeSessionFactory.shutdown();
211 DavGatewayTray.info(new BundleMessage("LOG_GATEWAY_STOP"));
212 DavGatewayTray.dispose();
213 }
214
215
216
217
218 public static void restart() {
219 DavGateway.stopServers();
220
221 ExchangeSessionFactory.shutdown();
222 DavGateway.start();
223 }
224
225 private static void stopServers() {
226 for (AbstractServer server : SERVER_LIST) {
227 server.close();
228 try {
229 server.join();
230 } catch (InterruptedException e) {
231 DavGatewayTray.warn(new BundleMessage("LOG_EXCEPTION_WAITING_SERVER_THREAD_DIE"), e);
232 Thread.currentThread().interrupt();
233 }
234 }
235 }
236
237
238
239
240
241
242 public static String getCurrentVersion() {
243 Package davmailPackage = DavGateway.class.getPackage();
244 String currentVersion = davmailPackage.getImplementationVersion();
245 if (currentVersion == null) {
246 currentVersion = "";
247 }
248 return currentVersion;
249 }
250
251
252
253
254
255
256 public static String getReleasedVersion() {
257 String version = null;
258 if (!Settings.getBooleanProperty("davmail.disableUpdateCheck")) {
259 try (HttpClientAdapter httpClientAdapter = new HttpClientAdapter(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT)) {
260 GetRequest getRequest = new GetRequest(HTTP_DAVMAIL_SOURCEFORGE_NET_VERSION_TXT);
261 getRequest = httpClientAdapter.executeFollowRedirect(getRequest);
262 version = getRequest.getResponseBodyAsString();
263 LOGGER.debug("DavMail released version: " + version);
264 } catch (IOException e) {
265 DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_GET_RELEASED_VERSION"));
266 }
267 }
268 return version;
269 }
270 }