2006-12-12 18:57:24 -05:00
|
|
|
package davmail.imap;
|
|
|
|
|
|
|
|
import java.net.Socket;
|
2009-01-22 19:59:41 -05:00
|
|
|
import java.net.SocketTimeoutException;
|
|
|
|
import java.net.SocketException;
|
2006-12-12 18:57:24 -05:00
|
|
|
import java.util.StringTokenizer;
|
|
|
|
import java.util.List;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import davmail.AbstractConnection;
|
2007-10-09 08:01:58 -04:00
|
|
|
import davmail.tray.DavGatewayTray;
|
2006-12-12 18:57:24 -05:00
|
|
|
import davmail.exchange.ExchangeSession;
|
2008-10-31 13:12:30 -04:00
|
|
|
import davmail.exchange.ExchangeSessionFactory;
|
2009-01-22 19:59:41 -05:00
|
|
|
import com.sun.mail.imap.protocol.BASE64MailboxEncoder;
|
|
|
|
import com.sun.mail.imap.protocol.BASE64MailboxDecoder;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dav Gateway smtp connection implementation.
|
|
|
|
* Still alpha code : need to find a way to handle message ids
|
|
|
|
*/
|
|
|
|
public class ImapConnection extends AbstractConnection {
|
|
|
|
protected static final int INITIAL = 0;
|
|
|
|
protected static final int AUTHENTICATED = 1;
|
|
|
|
|
2009-01-22 19:59:41 -05:00
|
|
|
ExchangeSession.Folder currentFolder;
|
|
|
|
List<ExchangeSession.Message> messages;
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
// Initialize the streams and start the thread
|
2006-12-14 10:14:18 -05:00
|
|
|
public ImapConnection(Socket clientSocket) {
|
2008-11-29 09:24:12 -05:00
|
|
|
super("ImapConnection", clientSocket, null);
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
String line;
|
|
|
|
StringTokenizer tokens;
|
|
|
|
try {
|
2009-01-22 19:59:41 -05:00
|
|
|
sendClient("* OK [CAPABILITY IMAP4REV1 AUTH=LOGIN] IMAP4rev1 DavMail server ready");
|
2006-12-12 18:57:24 -05:00
|
|
|
for (; ;) {
|
|
|
|
line = readClient();
|
|
|
|
// unable to read line, connection closed ?
|
|
|
|
if (line == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-22 19:59:41 -05:00
|
|
|
tokens = new StringTokenizer(line) {
|
|
|
|
public String nextToken() {
|
|
|
|
StringBuilder nextToken = new StringBuilder();
|
|
|
|
nextToken.append(super.nextToken());
|
|
|
|
while (hasMoreTokens() && nextToken.length() > 0 && nextToken.charAt(0) == '"'
|
|
|
|
&& nextToken.charAt(nextToken.length() - 1) != '"') {
|
|
|
|
nextToken.append(' ').append(super.nextToken());
|
|
|
|
}
|
|
|
|
return nextToken.toString();
|
|
|
|
}
|
|
|
|
};
|
2006-12-12 18:57:24 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String commandId = tokens.nextToken();
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String command = tokens.nextToken();
|
|
|
|
|
|
|
|
if ("LOGOUT".equalsIgnoreCase(command)) {
|
|
|
|
sendClient("* BYE Closing connection");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ("capability".equalsIgnoreCase(command)) {
|
2009-01-22 19:59:41 -05:00
|
|
|
sendClient("* CAPABILITY IMAP4REV1 AUTH=LOGIN");
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient(commandId + " OK CAPABILITY completed");
|
|
|
|
} else if ("login".equalsIgnoreCase(command)) {
|
|
|
|
parseCredentials(tokens);
|
|
|
|
try {
|
2008-10-31 13:12:30 -04:00
|
|
|
session = ExchangeSessionFactory.getInstance(userName, password);
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient(commandId + " OK Authenticated");
|
|
|
|
state = AUTHENTICATED;
|
|
|
|
} catch (Exception e) {
|
2008-11-24 07:35:58 -05:00
|
|
|
DavGatewayTray.error(e);
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient(commandId + " NO LOGIN failed");
|
|
|
|
state = INITIAL;
|
|
|
|
}
|
2009-01-22 19:59:41 -05:00
|
|
|
} else if ("AUTHENTICATE".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String authenticationMethod = tokens.nextToken();
|
|
|
|
if ("LOGIN".equalsIgnoreCase(authenticationMethod)) {
|
|
|
|
sendClient("+ " + base64Encode("Username:"));
|
|
|
|
userName = base64Decode(readClient());
|
|
|
|
sendClient("+ " + base64Encode("Password:"));
|
|
|
|
password = base64Decode(readClient());
|
|
|
|
try {
|
|
|
|
session = ExchangeSessionFactory.getInstance(userName, password);
|
|
|
|
sendClient(commandId + " OK Authenticated");
|
|
|
|
state = AUTHENTICATED;
|
|
|
|
} catch (Exception e) {
|
|
|
|
DavGatewayTray.error(e);
|
|
|
|
sendClient(commandId + " NO LOGIN failed");
|
|
|
|
state = INITIAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " NO unsupported authentication method");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD authentication method required");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
if (state != AUTHENTICATED) {
|
|
|
|
sendClient(commandId + " BAD command authentication required");
|
|
|
|
} else {
|
|
|
|
if ("lsub".equalsIgnoreCase(command)) {
|
|
|
|
sendClient(commandId + " OK LSUB completed");
|
|
|
|
} else if ("list".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-01-22 19:59:41 -05:00
|
|
|
String folderContext = BASE64MailboxDecoder.decode(removeQuotes(tokens.nextToken()));
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String folderQuery = folderContext + BASE64MailboxDecoder.decode(removeQuotes(tokens.nextToken()));
|
|
|
|
if (folderQuery.endsWith("%")) {
|
|
|
|
List<ExchangeSession.Folder> folders = session.getSubFolders(folderQuery.substring(0, folderQuery.length() - 1));
|
|
|
|
for (ExchangeSession.Folder folder : folders) {
|
|
|
|
sendClient("* LIST (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderUrl) + "\"");
|
|
|
|
}
|
|
|
|
sendClient(commandId + " OK LIST completed");
|
|
|
|
} else {
|
|
|
|
ExchangeSession.Folder folder = session.getFolder(folderQuery);
|
|
|
|
if (folder != null) {
|
|
|
|
sendClient("* LIST (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderUrl) + "\"");
|
|
|
|
sendClient(commandId + " OK LIST completed");
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " NO Folder not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD missing folder argument");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD missing folder argument");
|
|
|
|
}
|
|
|
|
} else if ("select".equalsIgnoreCase(command) || "examine".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String folderName = BASE64MailboxDecoder.decode(removeQuotes(tokens.nextToken()));
|
|
|
|
currentFolder = session.getFolder(folderName);
|
|
|
|
messages = session.getAllMessages(currentFolder.folderUrl);
|
|
|
|
sendClient("* " + currentFolder.objectCount + " EXISTS");
|
|
|
|
sendClient("* " + currentFolder.objectCount + " RECENT");
|
|
|
|
sendClient("* OK [UIDVALIDITY " + System.currentTimeMillis() + "]");
|
|
|
|
sendClient("* OK [UIDNEXT " + (currentFolder.objectCount + 1) + "]");
|
|
|
|
sendClient("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen)");
|
|
|
|
sendClient("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen)]");
|
|
|
|
sendClient("* [UNSEEN 1] first unseen message in inbox");
|
|
|
|
sendClient(commandId + " OK [READ-WRITE] " + command + " completed");
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
2009-01-22 19:59:41 -05:00
|
|
|
} else if ("close".equalsIgnoreCase(command)) {
|
|
|
|
currentFolder = null;
|
|
|
|
messages = null;
|
|
|
|
sendClient(commandId + " OK CLOSE unrecognized");
|
|
|
|
} else if ("create".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String folderName = BASE64MailboxDecoder.decode(removeQuotes(tokens.nextToken()));
|
|
|
|
if (session.getFolder(folderName) != null) {
|
|
|
|
sendClient(commandId + " OK folder already exists");
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
sendClient(commandId + " NO unsupported");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD missing create argument");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else if ("uid".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens() && "fetch".equalsIgnoreCase(tokens.nextToken())) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String parameter = tokens.nextToken();
|
2009-01-22 19:59:41 -05:00
|
|
|
if (currentFolder == null) {
|
|
|
|
sendClient(commandId + " NO no folder selected");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
if ("1:*".equals(parameter)) {
|
2009-01-22 19:59:41 -05:00
|
|
|
for (int i = 0; i < currentFolder.objectCount; i++) {
|
|
|
|
sendClient("* FETCH (UID " + (i + 1) + " FLAGS (\\Recent))");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
sendClient(commandId + " OK UID FETCH completed");
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
2009-01-22 19:59:41 -05:00
|
|
|
} else if ("fetch".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
int messageIndex = Integer.parseInt(tokens.nextToken());
|
|
|
|
ExchangeSession.Message message = messages.get(messageIndex - 1);
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String parameters = tokens.nextToken();
|
|
|
|
if ("(BODYSTRUCTURE)".equals(parameters)) {
|
|
|
|
sendClient("* "+messageIndex+" FETCH (BODYSTRUCTURE (\"TEXT\" \"PLAIN\" (\"CHARSET\" \"windows-1252\") NIL NIL \"QUOTED-PRINTABLE\" "+message.size+" 50 NIL NIL NIL NIL))");
|
|
|
|
sendClient(commandId + " OK FETCH completed");
|
|
|
|
} else {
|
|
|
|
sendClient("* "+messageIndex+" 1 FETCH (BODY[TEXT]<0> {" + message.size + "}");
|
|
|
|
message.write(os);
|
|
|
|
sendClient(commandId + " OK FETCH completed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD missing command");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient("BAD Null command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
os.flush();
|
2009-01-22 19:59:41 -05:00
|
|
|
} catch (SocketTimeoutException e) {
|
|
|
|
DavGatewayTray.debug("Closing connection on timeout");
|
|
|
|
try {
|
|
|
|
sendClient("* BYE Closing connection");
|
|
|
|
} catch (IOException e1) {
|
|
|
|
DavGatewayTray.debug("Exception closing connection on timeout");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} catch (IOException e) {
|
2008-10-31 13:12:30 -04:00
|
|
|
DavGatewayTray.error("Exception handling client", e);
|
2006-12-12 18:57:24 -05:00
|
|
|
} finally {
|
2007-05-15 05:37:55 -04:00
|
|
|
close();
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
DavGatewayTray.resetIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-09 17:41:10 -05:00
|
|
|
* Decode IMAP credentials
|
2009-01-22 19:59:41 -05:00
|
|
|
*
|
2009-01-09 17:41:10 -05:00
|
|
|
* @param tokens tokens
|
|
|
|
* @throws java.io.IOException on error
|
2006-12-12 18:57:24 -05:00
|
|
|
*/
|
|
|
|
protected void parseCredentials(StringTokenizer tokens) throws IOException {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
userName = removeQuotes(tokens.nextToken());
|
|
|
|
} else {
|
|
|
|
throw new IOException("Invalid credentials");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
password = removeQuotes(tokens.nextToken());
|
|
|
|
} else {
|
|
|
|
throw new IOException("Invalid credentials");
|
|
|
|
}
|
|
|
|
int backslashindex = userName.indexOf("\\");
|
|
|
|
if (backslashindex > 0) {
|
|
|
|
userName = userName.substring(0, backslashindex) + userName.substring(backslashindex + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String removeQuotes(String value) {
|
|
|
|
String result = value;
|
|
|
|
if (result.startsWith("\"")) {
|
|
|
|
result = result.substring(1);
|
|
|
|
}
|
|
|
|
if (result.endsWith("\"")) {
|
|
|
|
result = result.substring(0, result.length() - 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|