View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * DavGateway main class
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       * Start the gateway, listen on specified smtp and pop3 ports
56       *
57       * @param args command line parameter config file path
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              // force shutdown on Linux
90              System.exit(0);
91          } else {
92  
93              if (GraphicsEnvironment.isHeadless()) {
94                  // force server mode
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             // server mode: all threads are daemon threads, do not let main stop
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      * Start DavMail listeners.
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         // check for new version in a separate thread
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      * Stop all listeners, shutdown connection pool and clear session cache.
206      */
207     public static void stop() {
208         DavGateway.stopServers();
209         // close pooled connections
210         ExchangeSessionFactory.shutdown();
211         DavGatewayTray.info(new BundleMessage("LOG_GATEWAY_STOP"));
212         DavGatewayTray.dispose();
213     }
214 
215     /**
216      * Stop all listeners and clear session cache.
217      */
218     public static void restart() {
219         DavGateway.stopServers();
220         // clear session cache
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      * Get current DavMail version.
239      *
240      * @return current version
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      * Get latest released version from SourceForge.
253      *
254      * @return latest version
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 }