mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 11:12:22 -05:00
Sitch to file based settings
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@10 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
7ddb77af7a
commit
4626c66e04
@ -16,8 +16,6 @@ public class AbstractConnection extends Thread {
|
||||
protected Socket client;
|
||||
protected BufferedReader in;
|
||||
protected OutputStream os;
|
||||
// exchange server url
|
||||
protected String url;
|
||||
// user name and password initialized through connection
|
||||
protected String userName = null;
|
||||
protected String password = null;
|
||||
@ -27,8 +25,7 @@ public class AbstractConnection extends Thread {
|
||||
protected ExchangeSession session;
|
||||
|
||||
// Initialize the streams and start the thread
|
||||
public AbstractConnection(String url, Socket clientSocket) {
|
||||
this.url = url;
|
||||
public AbstractConnection(Socket clientSocket) {
|
||||
client = clientSocket;
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
@ -72,7 +69,11 @@ public class AbstractConnection extends Thread {
|
||||
*/
|
||||
public String readClient() throws IOException {
|
||||
String line = in.readLine();
|
||||
if (line != null && !line.startsWith("PASS")) {
|
||||
DavGatewayTray.debug("< "+line);
|
||||
} else {
|
||||
DavGatewayTray.debug("< PASS ********");
|
||||
}
|
||||
DavGatewayTray.switchIcon();
|
||||
return line;
|
||||
}
|
||||
|
@ -9,20 +9,18 @@ import java.io.IOException;
|
||||
*/
|
||||
public abstract class AbstractServer extends Thread {
|
||||
protected int port;
|
||||
protected String url;
|
||||
protected ServerSocket serverSocket;
|
||||
|
||||
/**
|
||||
* Create a ServerSocket to listen for connections.
|
||||
* Start the thread.
|
||||
*/
|
||||
public AbstractServer(String url, int port) {
|
||||
public AbstractServer(int port) {
|
||||
this.port = port;
|
||||
this.url = url;
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
} catch (IOException e) {
|
||||
fail(e, "Exception creating server socket");
|
||||
DavGatewayTray.error("Exception creating server socket", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +44,7 @@ public abstract class AbstractServer extends Thread {
|
||||
DavGatewayTray.debug("Connection from " + clientSocket.getInetAddress() + " on port " + port);
|
||||
// only accept localhost connections for security reasons
|
||||
if (clientSocket.getInetAddress().toString().indexOf("127.0.0.1") > 0) {
|
||||
createConnectionHandler(url, clientSocket);
|
||||
createConnectionHandler(clientSocket);
|
||||
} else {
|
||||
clientSocket.close();
|
||||
DavGatewayTray.warn("Connection from external client refused");
|
||||
@ -54,10 +52,18 @@ public abstract class AbstractServer extends Thread {
|
||||
System.gc();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
fail(e, "Exception while listening for connections");
|
||||
DavGatewayTray.warn("Exception while listening for connections", e);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void createConnectionHandler(String url, Socket clientSocket);
|
||||
public abstract void createConnectionHandler(Socket clientSocket);
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
DavGatewayTray.warn("Exception closing server socket", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package davmail;
|
||||
|
||||
import davmail.imap.ImapServer;
|
||||
import davmail.pop.PopServer;
|
||||
import davmail.smtp.SmtpServer;
|
||||
|
||||
@ -8,45 +7,59 @@ import davmail.smtp.SmtpServer;
|
||||
* DavGateway main class
|
||||
*/
|
||||
public class DavGateway {
|
||||
protected static final String USAGE_MESSAGE = "Usage : java davmail.DavGateway url [smtplistenport] [pop3listenport] [imaplistenport]";
|
||||
protected static SmtpServer smtpServer;
|
||||
protected static PopServer popServer;
|
||||
|
||||
/**
|
||||
* Start the gateway, listen on spécified smtp and pop3 ports
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
int smtpPort = SmtpServer.DEFAULT_PORT;
|
||||
int popPort = PopServer.DEFAULT_PORT;
|
||||
int imapPort = ImapServer.DEFAULT_PORT;
|
||||
String url;
|
||||
|
||||
String configFilePath = System.getProperty("user.home") + "/.davmail.properties";
|
||||
if (args.length >= 1) {
|
||||
url = args[0];
|
||||
try {
|
||||
if (args.length >= 2) {
|
||||
smtpPort = Integer.parseInt(args[1]);
|
||||
}
|
||||
if (args.length >= 3) {
|
||||
popPort = Integer.parseInt(args[2]);
|
||||
}
|
||||
if (args.length >= 4) {
|
||||
imapPort = Integer.parseInt(args[3]);
|
||||
configFilePath = args[0];
|
||||
}
|
||||
Settings.setConfigFilePath(configFilePath);
|
||||
Settings.load();
|
||||
DavGatewayTray.init();
|
||||
|
||||
SmtpServer smtpServer = new SmtpServer(url, smtpPort);
|
||||
PopServer popServer = new PopServer(url, popPort);
|
||||
ImapServer imapServer = new ImapServer(url, imapPort);
|
||||
start();
|
||||
}
|
||||
|
||||
public static void start() {
|
||||
// first stop existing servers
|
||||
if (smtpServer != null) {
|
||||
smtpServer.close();
|
||||
try {
|
||||
smtpServer.join();
|
||||
} catch (InterruptedException e) {
|
||||
DavGatewayTray.warn("Exception waiting for listener to die", e);
|
||||
}
|
||||
}
|
||||
if (popServer != null) {
|
||||
popServer.close();
|
||||
try {
|
||||
popServer.join();
|
||||
} catch (InterruptedException e) {
|
||||
DavGatewayTray.warn("Exception waiting for listener to die", e);
|
||||
}
|
||||
}
|
||||
int smtpPort = Settings.getIntProperty("davmail.smtpPort");
|
||||
if (smtpPort == 0) {
|
||||
smtpPort = SmtpServer.DEFAULT_PORT;
|
||||
}
|
||||
int popPort = Settings.getIntProperty("davmail.popPort");
|
||||
if (popPort == 0) {
|
||||
popPort = PopServer.DEFAULT_PORT;
|
||||
}
|
||||
smtpServer = new SmtpServer(smtpPort);
|
||||
popServer = new PopServer(popPort);
|
||||
smtpServer.start();
|
||||
popServer.start();
|
||||
imapServer.start();
|
||||
DavGatewayTray.info("Listening on ports " + smtpPort + " "+popPort+" "+imapPort);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println(DavGateway.USAGE_MESSAGE);
|
||||
}
|
||||
} else {
|
||||
System.out.println(DavGateway.USAGE_MESSAGE);
|
||||
}
|
||||
|
||||
DavGatewayTray.info("DavMail gateway listening on SMTP port " + smtpPort +
|
||||
" and POP port " + popPort);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
70
src/java/davmail/Settings.java
Normal file
70
src/java/davmail/Settings.java
Normal file
@ -0,0 +1,70 @@
|
||||
package davmail;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Settings facade
|
||||
*/
|
||||
public class Settings {
|
||||
protected static final Properties settings = new Properties();
|
||||
protected static String configFilePath;
|
||||
|
||||
public static synchronized void setConfigFilePath(String value) {
|
||||
configFilePath = value;
|
||||
}
|
||||
|
||||
public static synchronized void load() {
|
||||
try {
|
||||
File configFile = new File(configFilePath);
|
||||
if (configFile.exists()) {
|
||||
settings.load(new FileReader(configFile));
|
||||
} else {
|
||||
settings.put("davmail.url", "http://exchangeServer");
|
||||
settings.put("davmail.popPort", "110");
|
||||
settings.put("davmail.smtpPort", "25");
|
||||
settings.put("davmail.enableProxy", "false");
|
||||
settings.put("davmail.proxyHost", "");
|
||||
settings.put("davmail.proxyPort", "");
|
||||
settings.put("davmail.proxyUser", "");
|
||||
settings.put("davmail.proxyPassword", "");
|
||||
save();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
DavGatewayTray.error("Unable to load settings: ", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static synchronized void save() {
|
||||
try {
|
||||
settings.store(new FileWriter(configFilePath), "DavMail settings");
|
||||
} catch (IOException e) {
|
||||
DavGatewayTray.error("Unable to store settings: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized String getProperty(String property) {
|
||||
return settings.getProperty(property);
|
||||
}
|
||||
|
||||
public static synchronized void setProperty(String property, String value) {
|
||||
settings.setProperty(property, value);
|
||||
}
|
||||
|
||||
public static synchronized int getIntProperty(String property) {
|
||||
int value = 0;
|
||||
try {
|
||||
String propertyValue = settings.getProperty(property);
|
||||
if (propertyValue != null && propertyValue.length() > 0) {
|
||||
value = Integer.valueOf(propertyValue);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
DavGatewayTray.error("Invalid setting value in " + property, e);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
@ -22,9 +22,9 @@ public class SettingsFrame extends JFrame {
|
||||
JPanel panel = new JPanel(new GridLayout(3, 2));
|
||||
panel.setBorder(BorderFactory.createTitledBorder("Gateway settings"));
|
||||
|
||||
final JTextField urlField = new JTextField("", 15);
|
||||
final JTextField popPortField = new JTextField(4);
|
||||
final JTextField smtpPortField = new JTextField(4);
|
||||
final JTextField urlField = new JTextField(Settings.getProperty("davmail.url"), 15);
|
||||
final JTextField popPortField = new JTextField(Settings.getProperty("davmail.popPort"), 4);
|
||||
final JTextField smtpPortField = new JTextField(Settings.getProperty("davmail.smtpPort"), 4);
|
||||
|
||||
addSettingComponent(panel, "OWA url: ", urlField);
|
||||
addSettingComponent(panel, "Local POP port: ", popPortField);
|
||||
@ -35,13 +35,14 @@ public class SettingsFrame extends JFrame {
|
||||
panel = new JPanel(new GridLayout(5, 2));
|
||||
panel.setBorder(BorderFactory.createTitledBorder("Proxy settings"));
|
||||
|
||||
boolean enableProxy = "true".equals(Settings.getProperty("davmail.enableProxy"));
|
||||
final JCheckBox enableProxyField = new JCheckBox();
|
||||
final JTextField httpProxyField = new JTextField(System.getProperty("http.proxyHost"), 15);
|
||||
final JTextField httpProxyPortField = new JTextField(System.getProperty("http.proxyPort"), 4);
|
||||
final JTextField httpProxyUserField = new JTextField(System.getProperty("http.proxyUser"), 4);
|
||||
final JTextField httpProxyPasswordField = new JPasswordField (System.getProperty("http.proxyPassword"), 4);
|
||||
enableProxyField.setSelected(enableProxy);
|
||||
final JTextField httpProxyField = new JTextField(Settings.getProperty("davmail.proxyHost"), 15);
|
||||
final JTextField httpProxyPortField = new JTextField(Settings.getProperty("davmail.proxyPort"), 4);
|
||||
final JTextField httpProxyUserField = new JTextField(Settings.getProperty("davmail.proxyUser"), 4);
|
||||
final JTextField httpProxyPasswordField = new JPasswordField (Settings.getProperty("davmail.proxyPassword"), 4);
|
||||
|
||||
boolean enableProxy = enableProxyField.isSelected();
|
||||
httpProxyField.setEnabled(enableProxy);
|
||||
httpProxyPortField.setEnabled(enableProxy);
|
||||
httpProxyUserField.setEnabled(enableProxy);
|
||||
@ -70,8 +71,19 @@ public class SettingsFrame extends JFrame {
|
||||
JButton ok = new JButton("Save");
|
||||
ActionListener save = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent evt) {
|
||||
// TODO : save options
|
||||
// save options
|
||||
Settings.setProperty("davmail.url", urlField.getText());
|
||||
Settings.setProperty("davmail.popPort", popPortField.getText());
|
||||
Settings.setProperty("davmail.smtpPort", smtpPortField.getText());
|
||||
Settings.setProperty("davmail.enableProxy", String.valueOf(enableProxyField.isSelected()));
|
||||
Settings.setProperty("davmail.proxyHost", httpProxyField.getText());
|
||||
Settings.setProperty("davmail.proxyPort", httpProxyPortField.getText());
|
||||
Settings.setProperty("davmail.proxyUser", httpProxyUserField.getText());
|
||||
Settings.setProperty("davmail.proxyPassword", httpProxyPasswordField.getText());
|
||||
Settings.save();
|
||||
setVisible(false);
|
||||
// restart listeners with new config
|
||||
DavGateway.start();
|
||||
}
|
||||
};
|
||||
ok.addActionListener(save);
|
||||
|
@ -23,6 +23,8 @@ import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import davmail.Settings;
|
||||
|
||||
/**
|
||||
* Exchange session through Outlook Web Access (DAV)
|
||||
*/
|
||||
@ -96,8 +98,6 @@ public class ExchangeSession {
|
||||
*/
|
||||
public final SimpleDateFormat dateFormatter;
|
||||
|
||||
protected String url;
|
||||
|
||||
/**
|
||||
* Various standard mail boxes Urls
|
||||
*/
|
||||
@ -119,8 +119,7 @@ public class ExchangeSession {
|
||||
*
|
||||
* @param url Outlook Web Access URL
|
||||
*/
|
||||
public ExchangeSession(String url) {
|
||||
this.url = url;
|
||||
public ExchangeSession() {
|
||||
// SimpleDateFormat are not thread safe, need to create one instance for
|
||||
// each session
|
||||
dateParser = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
|
||||
@ -130,7 +129,22 @@ public class ExchangeSession {
|
||||
|
||||
public void login(String userName, String password) throws Exception {
|
||||
try {
|
||||
// TODO : support different ports
|
||||
String url = Settings.getProperty("davmail.url");
|
||||
String enableProxy = Settings.getProperty("davmail.enableProxy");
|
||||
String proxyHost = null;
|
||||
String proxyPort = null;
|
||||
String proxyUser = null;
|
||||
String proxyPassword = null;
|
||||
|
||||
if ("true".equals(enableProxy)) {
|
||||
proxyHost = Settings.getProperty("davmail.proxyHost");
|
||||
proxyPort = Settings.getProperty("davmail.proxyPort");
|
||||
proxyUser = Settings.getProperty("davmail.proxyUser");
|
||||
proxyPassword = Settings.getProperty("davmail.proxyPassword");
|
||||
}
|
||||
|
||||
// get proxy configuration from setttings properties
|
||||
|
||||
URL urlObject = new URL(url);
|
||||
// webdavresource is unable to create the correct url type
|
||||
HttpURL httpURL;
|
||||
@ -148,12 +162,6 @@ public class ExchangeSession {
|
||||
// set httpclient timeout to 30 seconds
|
||||
//wdr.retrieveSessionInstance().setTimeout(30000);
|
||||
|
||||
// get proxy configuration from system properties
|
||||
String proxyHost = System.getProperty("http.proxyHost");
|
||||
String proxyPort = System.getProperty("http.proxyPort");
|
||||
String proxyUser = System.getProperty("http.proxyUser");
|
||||
String proxyPassword = System.getProperty("http.proxyPassword");
|
||||
|
||||
// get the internal HttpClient instance
|
||||
HttpClient httpClient = wdr.retrieveSessionInstance();
|
||||
|
||||
@ -166,9 +174,9 @@ public class ExchangeSession {
|
||||
httpClient.getState().setAuthenticationPreemptive(false);
|
||||
|
||||
// configure proxy
|
||||
if (proxyHost != null) {
|
||||
if (proxyHost != null && proxyHost.length() > 0) {
|
||||
httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
|
||||
if (proxyUser != null) {
|
||||
if (proxyUser != null && proxyUser.length() > 0) {
|
||||
// detect ntlm authentication (windows domain name in user name)
|
||||
int backslashindex = proxyUser.indexOf("\\");
|
||||
if (backslashindex > 0) {
|
||||
|
@ -18,8 +18,8 @@ public class ImapConnection extends AbstractConnection {
|
||||
protected static final int AUTHENTICATED = 1;
|
||||
|
||||
// Initialize the streams and start the thread
|
||||
public ImapConnection(String url, Socket clientSocket) {
|
||||
super(url, clientSocket);
|
||||
public ImapConnection(Socket clientSocket) {
|
||||
super(clientSocket);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@ -50,7 +50,7 @@ public class ImapConnection extends AbstractConnection {
|
||||
sendClient(commandId + " OK CAPABILITY completed");
|
||||
} else if ("login".equalsIgnoreCase(command)) {
|
||||
parseCredentials(tokens);
|
||||
session = new ExchangeSession(url);
|
||||
session = new ExchangeSession();
|
||||
try {
|
||||
session.login(userName, password);
|
||||
sendClient(commandId + " OK Authenticated");
|
||||
|
@ -15,12 +15,12 @@ public class ImapServer extends AbstractServer {
|
||||
* Create a ServerSocket to listen for connections.
|
||||
* Start the thread.
|
||||
*/
|
||||
public ImapServer(String url, int port) {
|
||||
super(url, (port == 0) ? ImapServer.DEFAULT_PORT : port);
|
||||
public ImapServer(int port) {
|
||||
super((port == 0) ? ImapServer.DEFAULT_PORT : port);
|
||||
}
|
||||
|
||||
public void createConnectionHandler(String url, Socket clientSocket) {
|
||||
new ImapConnection(url, clientSocket);
|
||||
public void createConnectionHandler(Socket clientSocket) {
|
||||
new ImapConnection(clientSocket);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,8 +19,8 @@ public class PopConnection extends AbstractConnection {
|
||||
private List<ExchangeSession.Message> messages;
|
||||
|
||||
// Initialize the streams and start the thread
|
||||
public PopConnection(String url, Socket clientSocket) {
|
||||
super(url, clientSocket);
|
||||
public PopConnection(Socket clientSocket) {
|
||||
super(clientSocket);
|
||||
}
|
||||
|
||||
public long getTotalMessagesLength() {
|
||||
@ -91,7 +91,7 @@ public class PopConnection extends AbstractConnection {
|
||||
} else {
|
||||
password = tokens.nextToken();
|
||||
try {
|
||||
session = new ExchangeSession(url);
|
||||
session = new ExchangeSession();
|
||||
session.login(userName, password);
|
||||
messages = session.getAllMessages();
|
||||
sendOK("PASS");
|
||||
|
@ -15,12 +15,12 @@ public class PopServer extends AbstractServer {
|
||||
* Create a ServerSocket to listen for connections.
|
||||
* Start the thread.
|
||||
*/
|
||||
public PopServer(String url, int port) {
|
||||
super(url, (port == 0) ? PopServer.DEFAULT_PORT : port);
|
||||
public PopServer(int port) {
|
||||
super((port == 0) ? PopServer.DEFAULT_PORT : port);
|
||||
}
|
||||
|
||||
public void createConnectionHandler(String url, Socket clientSocket) {
|
||||
new PopConnection(url, clientSocket);
|
||||
public void createConnectionHandler(Socket clientSocket) {
|
||||
new PopConnection(clientSocket);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ public class SmtpConnection extends AbstractConnection {
|
||||
protected static final int MAILDATA = 4;
|
||||
|
||||
// Initialize the streams and start the thread
|
||||
public SmtpConnection(String url, Socket clientSocket) {
|
||||
super(url, clientSocket);
|
||||
public SmtpConnection(Socket clientSocket) {
|
||||
super(clientSocket);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@ -58,7 +58,7 @@ public class SmtpConnection extends AbstractConnection {
|
||||
String authType = tokens.nextToken();
|
||||
if ("PLAIN".equals(authType) && tokens.hasMoreElements()) {
|
||||
decodeCredentials(tokens.nextToken());
|
||||
session = new ExchangeSession(url);
|
||||
session = new ExchangeSession();
|
||||
try {
|
||||
session.login(userName, password);
|
||||
sendClient("235 OK Authenticated");
|
||||
|
@ -3,6 +3,7 @@ package davmail.smtp;
|
||||
import java.net.Socket;
|
||||
|
||||
import davmail.AbstractServer;
|
||||
import davmail.Settings;
|
||||
|
||||
public class SmtpServer extends AbstractServer {
|
||||
public final static int DEFAULT_PORT = 25;
|
||||
@ -11,12 +12,12 @@ public class SmtpServer extends AbstractServer {
|
||||
* Create a ServerSocket to listen for connections.
|
||||
* Start the thread.
|
||||
*/
|
||||
public SmtpServer(String url, int port) {
|
||||
super(url, (port == 0) ? SmtpServer.DEFAULT_PORT : port);
|
||||
public SmtpServer(int port) {
|
||||
super((port == 0) ? SmtpServer.DEFAULT_PORT : port);
|
||||
}
|
||||
|
||||
public void createConnectionHandler(String url, Socket clientSocket) {
|
||||
new SmtpConnection(url, clientSocket);
|
||||
public void createConnectionHandler(Socket clientSocket) {
|
||||
new SmtpConnection(clientSocket);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user