2006-12-12 18:57:24 -05:00
|
|
|
package davmail;
|
|
|
|
|
|
|
|
import davmail.exchange.ExchangeSession;
|
2008-12-04 06:50:31 -05:00
|
|
|
import davmail.exchange.ExchangeSessionFactory;
|
2008-12-03 06:38:35 -05:00
|
|
|
import davmail.tray.DavGatewayTray;
|
|
|
|
import org.apache.commons.httpclient.util.Base64;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
2008-12-03 06:38:35 -05:00
|
|
|
import java.io.*;
|
2006-12-12 18:57:24 -05:00
|
|
|
import java.net.Socket;
|
|
|
|
|
2009-01-29 17:18:53 -05:00
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
|
|
|
* Generic connection common to pop3 and smtp implementations
|
|
|
|
*/
|
|
|
|
public class AbstractConnection extends Thread {
|
2009-01-29 17:18:53 -05:00
|
|
|
|
|
|
|
protected enum State {INITIAL, LOGIN, USER, PASSWORD, AUTHENTICATED, STARTMAIL, RECIPIENT, MAILDATA}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read password state
|
|
|
|
*/
|
|
|
|
public static final int PASSWORD = 1;
|
|
|
|
|
2007-05-15 05:37:55 -04:00
|
|
|
protected final Socket client;
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
protected BufferedReader in;
|
|
|
|
protected OutputStream os;
|
|
|
|
// user name and password initialized through connection
|
|
|
|
protected String userName = null;
|
|
|
|
protected String password = null;
|
|
|
|
// connection state
|
2009-01-29 17:18:53 -05:00
|
|
|
protected State state = State.INITIAL;
|
2006-12-12 18:57:24 -05:00
|
|
|
// Exchange session proxy
|
|
|
|
protected ExchangeSession session;
|
|
|
|
|
2008-12-03 06:38:35 -05:00
|
|
|
// only set the thread name and socket
|
|
|
|
public AbstractConnection(String name, Socket clientSocket) {
|
|
|
|
super(name);
|
|
|
|
this.client = clientSocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the streams and set thread name
|
2008-11-29 09:24:12 -05:00
|
|
|
public AbstractConnection(String name, Socket clientSocket, String encoding) {
|
|
|
|
super(name + "-" + clientSocket.getPort());
|
2007-05-09 19:37:24 -04:00
|
|
|
this.client = clientSocket;
|
2006-12-12 18:57:24 -05:00
|
|
|
try {
|
2008-11-29 09:24:12 -05:00
|
|
|
if (encoding == null) {
|
|
|
|
//noinspection IOResourceOpenedButNotSafelyClosed
|
|
|
|
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
|
|
} else {
|
|
|
|
in = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
|
|
|
|
}
|
2008-12-03 06:38:35 -05:00
|
|
|
os = new BufferedOutputStream(client.getOutputStream());
|
2006-12-12 18:57:24 -05:00
|
|
|
} catch (IOException e) {
|
2007-05-15 05:37:55 -04:00
|
|
|
close();
|
2006-12-12 18:57:24 -05:00
|
|
|
DavGatewayTray.error("Exception while getting socket streams", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
/**
|
|
|
|
* Send message to client followed by CRLF.
|
2008-11-29 09:24:12 -05:00
|
|
|
*
|
2008-11-26 19:56:28 -05:00
|
|
|
* @param message message
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2006-12-12 18:57:24 -05:00
|
|
|
public void sendClient(String message) throws IOException {
|
|
|
|
sendClient(null, message);
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
/**
|
|
|
|
* Send prefix and message to client followed by CRLF.
|
2008-11-29 09:24:12 -05:00
|
|
|
*
|
|
|
|
* @param prefix prefix
|
2008-11-26 19:56:28 -05:00
|
|
|
* @param message message
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
2006-12-12 18:57:24 -05:00
|
|
|
public void sendClient(String prefix, String message) throws IOException {
|
|
|
|
StringBuffer logBuffer = new StringBuffer("> ");
|
|
|
|
if (prefix != null) {
|
|
|
|
logBuffer.append(prefix);
|
|
|
|
os.write(prefix.getBytes());
|
|
|
|
}
|
|
|
|
logBuffer.append(message);
|
|
|
|
DavGatewayTray.debug(logBuffer.toString());
|
|
|
|
os.write(message.getBytes());
|
2008-11-29 09:24:12 -05:00
|
|
|
os.write((char) 13);
|
|
|
|
os.write((char) 10);
|
2006-12-12 18:57:24 -05:00
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
/**
|
|
|
|
* Send only bytes to client.
|
2008-11-29 09:24:12 -05:00
|
|
|
*
|
2008-11-26 19:56:28 -05:00
|
|
|
* @param messageBytes content
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
public void sendClient(byte[] messageBytes) throws IOException {
|
|
|
|
StringBuffer logBuffer = new StringBuffer("> ");
|
|
|
|
logBuffer.append(new String(messageBytes));
|
|
|
|
os.write(messageBytes);
|
|
|
|
os.flush();
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
|
|
|
* Read a line from the client connection.
|
|
|
|
* Log message to stdout
|
2007-03-14 07:55:37 -04:00
|
|
|
*
|
2006-12-12 18:57:24 -05:00
|
|
|
* @return command line or null
|
2007-03-14 07:55:37 -04:00
|
|
|
* @throws IOException when unable to read line
|
2006-12-12 18:57:24 -05:00
|
|
|
*/
|
|
|
|
public String readClient() throws IOException {
|
|
|
|
String line = in.readLine();
|
2008-11-26 19:56:28 -05:00
|
|
|
if (line != null) {
|
|
|
|
if (line.startsWith("PASS")) {
|
|
|
|
DavGatewayTray.debug("< PASS ********");
|
2009-01-29 17:18:53 -05:00
|
|
|
// IMAP LOGIN
|
|
|
|
} else if (line.startsWith("LOGIN")) {
|
|
|
|
DavGatewayTray.debug("< LOGIN ********");
|
|
|
|
} else if (state == State.PASSWORD) {
|
2008-11-26 19:56:28 -05:00
|
|
|
DavGatewayTray.debug("< ********");
|
2009-01-29 17:18:53 -05:00
|
|
|
// HTTP Basic Authentication
|
2008-11-29 09:24:12 -05:00
|
|
|
} else if (line.startsWith("Authorization:")) {
|
2008-11-26 19:56:28 -05:00
|
|
|
DavGatewayTray.debug("< Authorization: ********");
|
2009-02-11 08:22:38 -05:00
|
|
|
} else if (line.startsWith("AUTH PLAIN")) {
|
|
|
|
DavGatewayTray.debug("< AUTH PLAIN ********");
|
2008-11-26 19:56:28 -05:00
|
|
|
} else {
|
|
|
|
DavGatewayTray.debug("< " + line);
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
DavGatewayTray.switchIcon();
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2007-05-15 05:37:55 -04:00
|
|
|
/**
|
|
|
|
* Close client connection, streams and Exchange session .
|
|
|
|
*/
|
|
|
|
public void close() {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
DavGatewayTray.warn("Exception closing client input stream", e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (os != null) {
|
|
|
|
try {
|
|
|
|
os.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
DavGatewayTray.warn("Exception closing client output stream", e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
client.close();
|
|
|
|
} catch (IOException e2) {
|
|
|
|
DavGatewayTray.warn("Exception closing client socket", e2);
|
|
|
|
}
|
2008-12-04 06:50:31 -05:00
|
|
|
ExchangeSessionFactory.close(session);
|
2007-05-15 05:37:55 -04:00
|
|
|
}
|
|
|
|
|
2008-11-26 19:56:28 -05:00
|
|
|
protected String base64Encode(String value) {
|
|
|
|
return new String(Base64.encode(value.getBytes()));
|
|
|
|
}
|
|
|
|
|
2008-12-17 10:31:08 -05:00
|
|
|
protected String base64Decode(String value) {
|
2008-11-26 19:56:28 -05:00
|
|
|
return new String(Base64.decode(value.getBytes()));
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|