2006-12-12 18:57:24 -05:00
|
|
|
package davmail.imap;
|
|
|
|
|
2009-01-29 17:18:53 -05:00
|
|
|
import com.sun.mail.imap.protocol.BASE64MailboxDecoder;
|
|
|
|
import com.sun.mail.imap.protocol.BASE64MailboxEncoder;
|
2006-12-12 18:57:24 -05:00
|
|
|
import davmail.AbstractConnection;
|
|
|
|
import davmail.exchange.ExchangeSession;
|
2008-10-31 13:12:30 -04:00
|
|
|
import davmail.exchange.ExchangeSessionFactory;
|
2009-01-29 17:18:53 -05:00
|
|
|
import davmail.tray.DavGatewayTray;
|
2009-01-26 18:51:08 -05:00
|
|
|
import org.apache.commons.httpclient.HttpException;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
2009-01-29 17:18:53 -05:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.FilterOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.SocketTimeoutException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
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;
|
2009-01-29 17:18:53 -05:00
|
|
|
protected static final int AUTHENTICATED = 2;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
2009-01-22 19:59:41 -05:00
|
|
|
ExchangeSession.Folder currentFolder;
|
2009-02-02 02:19:57 -05:00
|
|
|
ExchangeSession.MessageList messages;
|
2009-01-22 19:59:41 -05:00
|
|
|
|
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());
|
|
|
|
}
|
2009-01-23 05:30:48 -05:00
|
|
|
while (hasMoreTokens() && nextToken.length() > 0 && nextToken.charAt(0) == '('
|
|
|
|
&& nextToken.charAt(nextToken.length() - 1) != ')') {
|
|
|
|
nextToken.append(' ').append(super.nextToken());
|
|
|
|
}
|
2009-01-26 18:51:08 -05:00
|
|
|
return removeQuotes(nextToken.toString());
|
2009-01-22 19:59:41 -05:00
|
|
|
}
|
|
|
|
};
|
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");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2006-12-12 18:57:24 -05:00
|
|
|
} 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");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.INITIAL;
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
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");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2009-01-22 19:59:41 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
DavGatewayTray.error(e);
|
|
|
|
sendClient(commandId + " NO LOGIN failed");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.INITIAL;
|
2009-01-22 19:59:41 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " NO unsupported authentication method");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD authentication method required");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
2009-01-29 17:18:53 -05:00
|
|
|
if (state != State.AUTHENTICATED) {
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient(commandId + " BAD command authentication required");
|
|
|
|
} else {
|
2009-01-23 05:30:48 -05:00
|
|
|
if ("lsub".equalsIgnoreCase(command) || "list".equalsIgnoreCase(command)) {
|
2006-12-12 18:57:24 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
String folderContext = BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-01-22 19:59:41 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
String folderQuery = folderContext + BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-01-23 05:30:48 -05:00
|
|
|
if (folderQuery.endsWith("%/%")) {
|
|
|
|
folderQuery = folderQuery.substring(0, folderQuery.length() - 2);
|
|
|
|
}
|
|
|
|
if (folderQuery.endsWith("%") || folderQuery.endsWith("*")) {
|
|
|
|
boolean recursive = folderQuery.endsWith("*");
|
|
|
|
List<ExchangeSession.Folder> folders = session.getSubFolders(folderQuery.substring(0, folderQuery.length() - 1), recursive);
|
2009-01-22 19:59:41 -05:00
|
|
|
for (ExchangeSession.Folder folder : folders) {
|
2009-01-23 05:30:48 -05:00
|
|
|
sendClient("* " + command + " (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderUrl) + "\"");
|
2009-01-22 19:59:41 -05:00
|
|
|
}
|
2009-01-23 05:30:48 -05:00
|
|
|
sendClient(commandId + " OK " + command + " completed");
|
2009-01-22 19:59:41 -05:00
|
|
|
} else {
|
|
|
|
ExchangeSession.Folder folder = session.getFolder(folderQuery);
|
|
|
|
if (folder != null) {
|
2009-01-23 05:30:48 -05:00
|
|
|
sendClient("* " + command + " (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderUrl) + "\"");
|
|
|
|
sendClient(commandId + " OK " + command + " completed");
|
2009-01-22 19:59:41 -05:00
|
|
|
} 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()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
String folderName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-01-22 19:59:41 -05:00
|
|
|
currentFolder = session.getFolder(folderName);
|
|
|
|
messages = session.getAllMessages(currentFolder.folderUrl);
|
|
|
|
sendClient("* " + currentFolder.objectCount + " EXISTS");
|
|
|
|
sendClient("* " + currentFolder.objectCount + " RECENT");
|
2009-02-02 02:19:57 -05:00
|
|
|
sendClient("* OK [UIDVALIDITY 1]");
|
|
|
|
if (messages.size() == 0) {
|
2009-02-02 15:46:15 -05:00
|
|
|
sendClient("* OK [UIDNEXT " + 1 + "]");
|
2009-02-02 02:19:57 -05:00
|
|
|
} else {
|
2009-02-02 15:46:15 -05:00
|
|
|
sendClient("* OK [UIDNEXT " + (messages.get(messages.size() - 1).getUidAsLong() + 1) + "]");
|
2009-02-02 02:19:57 -05:00
|
|
|
}
|
2009-01-23 05:30:48 -05:00
|
|
|
sendClient("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded $MDNSent Forwarded $Junk $NotJunk Junk JunkRecorded NonJunk NotJunk)");
|
|
|
|
sendClient("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded $MDNSent Forwarded \\*)] junk-related flags are not permanent");
|
|
|
|
//sendClient("* [UNSEEN 1] first unseen message in inbox");
|
2009-01-22 19:59:41 -05:00
|
|
|
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;
|
2009-01-26 18:51:08 -05:00
|
|
|
sendClient(commandId + " OK CLOSE completed");
|
2009-01-22 19:59:41 -05:00
|
|
|
} else if ("create".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
String folderName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-01-23 08:01:46 -05:00
|
|
|
session.createFolder(folderName);
|
|
|
|
sendClient(commandId + " OK folder created");
|
2009-01-22 19:59:41 -05:00
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD missing create argument");
|
|
|
|
}
|
2009-01-26 18:51:08 -05:00
|
|
|
} else if ("rename".equalsIgnoreCase(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
String folderName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
|
|
|
String targetName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-01-26 18:51:08 -05:00
|
|
|
try {
|
|
|
|
session.moveFolder(folderName, targetName);
|
|
|
|
sendClient(commandId + " OK rename completed");
|
|
|
|
} catch (HttpException e) {
|
|
|
|
sendClient(commandId + " NO " + e.getReason());
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else if ("uid".equalsIgnoreCase(command)) {
|
2009-01-23 06:20:20 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String subcommand = tokens.nextToken();
|
|
|
|
if ("fetch".equalsIgnoreCase(subcommand)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String messageParameter = tokens.nextToken();
|
|
|
|
if (currentFolder == null) {
|
|
|
|
sendClient(commandId + " NO no folder selected");
|
2009-01-23 05:30:48 -05:00
|
|
|
}
|
2009-02-02 02:19:57 -05:00
|
|
|
long startIndex;
|
|
|
|
long endIndex;
|
|
|
|
int colonIndex = messageParameter.lastIndexOf(":");
|
2009-01-23 06:20:20 -05:00
|
|
|
if (colonIndex < 0) {
|
2009-02-02 02:19:57 -05:00
|
|
|
startIndex = endIndex = Long.parseLong(messageParameter);
|
2009-01-23 06:20:20 -05:00
|
|
|
} else {
|
2009-02-02 02:19:57 -05:00
|
|
|
int commaIndex = messageParameter.indexOf(",");
|
|
|
|
if (commaIndex > 0) {
|
|
|
|
// workaround for multiple scopes : start at first and end at last
|
|
|
|
startIndex = Long.parseLong(messageParameter.substring(0, Math.min(commaIndex, messageParameter.indexOf(":"))));
|
|
|
|
} else {
|
|
|
|
startIndex = Long.parseLong(messageParameter.substring(0, colonIndex));
|
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
if (messageParameter.endsWith("*")) {
|
2009-02-02 02:19:57 -05:00
|
|
|
if (messages.size() > 0) {
|
|
|
|
endIndex = messages.get(messages.size() - 1).getUidAsLong();
|
|
|
|
// fix according to spec
|
2009-02-02 15:46:15 -05:00
|
|
|
//if (startIndex > endIndex) {
|
|
|
|
// startIndex = endIndex;
|
|
|
|
//}
|
2009-02-02 02:19:57 -05:00
|
|
|
} else {
|
|
|
|
endIndex = 1;
|
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
} else {
|
2009-02-02 02:19:57 -05:00
|
|
|
endIndex = Long.parseLong(messageParameter.substring(colonIndex + 1));
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
if ("1:*".equals(messageParameter)) {
|
|
|
|
int count = 0;
|
|
|
|
for (ExchangeSession.Message message : messages) {
|
|
|
|
count++;
|
2009-02-02 02:19:57 -05:00
|
|
|
sendClient("* " + count + " FETCH (UID " + message.getUidAsLong() + " FLAGS (" + (message.read ? "\\Seen" : "") + "))");
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
|
|
|
sendClient(commandId + " OK UID FETCH completed");
|
|
|
|
} else {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String parameters = tokens.nextToken();
|
2009-02-02 02:19:57 -05:00
|
|
|
for (int messageIndex = 1; messageIndex <= messages.size(); messageIndex++) {
|
2009-01-23 06:20:20 -05:00
|
|
|
ExchangeSession.Message message = messages.get(messageIndex - 1);
|
2009-02-02 02:19:57 -05:00
|
|
|
long uid = message.getUidAsLong();
|
|
|
|
if (uid >= startIndex && uid <= endIndex) {
|
2009-01-23 05:30:48 -05:00
|
|
|
|
2009-02-02 02:19:57 -05:00
|
|
|
if ("BODYSTRUCTURE".equals(parameters)) {
|
|
|
|
sendClient("* " + messageIndex + " FETCH (BODYSTRUCTURE (\"TEXT\" \"PLAIN\" (\"CHARSET\" \"windows-1252\") NIL NIL \"QUOTED-PRINTABLE\" " + message.size + " 50 NIL NIL NIL NIL))");
|
|
|
|
} else if (parameters.indexOf("BODY[]") >= 0) {
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
message.write(baos);
|
|
|
|
baos.close();
|
2009-01-23 05:30:48 -05:00
|
|
|
|
2009-02-02 02:19:57 -05:00
|
|
|
DavGatewayTray.debug("Messagee size: " + message.size + " actual size:" + baos.size() + " message+headers: " + (message.size + baos.size()));
|
|
|
|
sendClient("* " + messageIndex + " FETCH (UID " + message.getUidAsLong() + " RFC822.SIZE " + baos.size() + " BODY[]<0>" +
|
|
|
|
" {" + baos.size() + "}");
|
|
|
|
message.write(os);
|
|
|
|
sendClient(")");
|
|
|
|
} else {
|
|
|
|
// write headers to byte array
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
HeaderOutputStream headerOutputStream = new HeaderOutputStream(baos);
|
|
|
|
message.write(headerOutputStream);
|
|
|
|
baos.close();
|
|
|
|
sendClient("* " + messageIndex + " FETCH (UID " + message.getUidAsLong() + " RFC822.SIZE " + headerOutputStream.size() + " BODY[HEADER.FIELDS ()" +
|
|
|
|
"] {" + baos.size() + "}");
|
|
|
|
os.write(baos.toByteArray());
|
|
|
|
os.flush();
|
|
|
|
sendClient(" FLAGS (" + (message.read ? "\\Seen" : "") + "))");
|
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
2009-01-23 05:30:48 -05:00
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
sendClient(commandId + " OK FETCH completed");
|
2009-01-23 05:30:48 -05:00
|
|
|
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
2009-01-23 05:30:48 -05:00
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
} else if ("search".equalsIgnoreCase(subcommand)) {
|
|
|
|
// only create check search
|
|
|
|
String messageId = null;
|
2009-02-02 02:19:57 -05:00
|
|
|
long messageUid = 0;
|
2009-01-23 06:20:20 -05:00
|
|
|
while (tokens.hasMoreTokens()) {
|
|
|
|
messageId = tokens.nextToken();
|
|
|
|
}
|
|
|
|
// reload messages
|
|
|
|
messages = session.getAllMessages(currentFolder.folderName);
|
2009-02-02 02:19:57 -05:00
|
|
|
for (ExchangeSession.Message message : messages) {
|
|
|
|
if (messageId.equals(message.messageId)) {
|
|
|
|
messageUid = message.getUidAsLong();
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
|
|
|
}
|
2009-02-02 02:19:57 -05:00
|
|
|
if (messageUid > 0) {
|
|
|
|
sendClient("* SEARCH " + messageUid);
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
|
|
|
sendClient(commandId + " OK SEARCH completed");
|
|
|
|
|
|
|
|
} else if ("store".equalsIgnoreCase(subcommand)) {
|
2009-02-02 02:19:57 -05:00
|
|
|
long uid = Long.parseLong(tokens.nextToken());
|
2009-02-02 15:46:15 -05:00
|
|
|
ExchangeSession.Message message = messages.getByUid(uid);
|
2009-01-26 18:51:08 -05:00
|
|
|
String action = tokens.nextToken();
|
|
|
|
String flags = tokens.nextToken();
|
|
|
|
HashMap<String, String> properties = new HashMap<String, String>();
|
|
|
|
if ("-Flags".equalsIgnoreCase(action)) {
|
|
|
|
StringTokenizer flagtokenizer = new StringTokenizer(flags);
|
|
|
|
while (flagtokenizer.hasMoreTokens()) {
|
2009-02-02 02:19:57 -05:00
|
|
|
String flag = flagtokenizer.nextToken();
|
2009-01-26 18:51:08 -05:00
|
|
|
if ("\\Seen".equals(flag)) {
|
|
|
|
properties.put("read", "0");
|
2009-02-02 15:46:15 -05:00
|
|
|
message.read = false;
|
2009-01-26 18:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ("+Flags".equalsIgnoreCase(action)) {
|
|
|
|
StringTokenizer flagtokenizer = new StringTokenizer(flags);
|
|
|
|
while (flagtokenizer.hasMoreTokens()) {
|
2009-02-02 02:19:57 -05:00
|
|
|
String flag = flagtokenizer.nextToken();
|
2009-01-26 18:51:08 -05:00
|
|
|
if ("\\Seen".equals(flag)) {
|
|
|
|
properties.put("read", "1");
|
2009-02-02 15:46:15 -05:00
|
|
|
message.read = true;
|
2009-01-26 18:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 02:19:57 -05:00
|
|
|
session.updateMessage(messages.getByUid(uid), properties);
|
2009-02-02 15:46:15 -05:00
|
|
|
int index = 0;
|
|
|
|
for (ExchangeSession.Message currentMessage : messages) {
|
|
|
|
index++;
|
|
|
|
if (currentMessage == message) {
|
|
|
|
sendClient("* " + index + " FETCH (UID " + message.getUidAsLong() + " FLAGS (" + (message.read ? "\\Seen" : "") + "))");
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 08:01:46 -05:00
|
|
|
sendClient(commandId + " OK STORE 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 ("fetch".equalsIgnoreCase(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
// TODO : refactor with uid fetch
|
2009-01-22 19:59:41 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
int messageIndex = Integer.parseInt(tokens.nextToken());
|
|
|
|
ExchangeSession.Message message = messages.get(messageIndex - 1);
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String parameters = tokens.nextToken();
|
2009-01-29 17:18:53 -05:00
|
|
|
if ("BODYSTRUCTURE".equals(parameters)) {
|
2009-01-23 05:30:48 -05:00
|
|
|
sendClient("* " + messageIndex + " FETCH (BODYSTRUCTURE (\"TEXT\" \"PLAIN\" (\"CHARSET\" \"windows-1252\") NIL NIL \"QUOTED-PRINTABLE\" " + message.size + " 50 NIL NIL NIL NIL))");
|
2009-01-22 19:59:41 -05:00
|
|
|
sendClient(commandId + " OK FETCH completed");
|
|
|
|
} else {
|
2009-01-23 05:30:48 -05:00
|
|
|
sendClient("* " + messageIndex + " 1 FETCH (BODY[TEXT]<0> {" + message.size + "}");
|
2009-01-22 19:59:41 -05:00
|
|
|
message.write(os);
|
|
|
|
sendClient(commandId + " OK FETCH completed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 05:50:46 -05:00
|
|
|
} else if ("append".equalsIgnoreCase(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
String folderName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-01-23 05:50:46 -05:00
|
|
|
String parameters = tokens.nextToken();
|
2009-01-29 17:18:53 -05:00
|
|
|
int size = Integer.parseInt(tokens.nextToken());
|
2009-01-23 05:50:46 -05:00
|
|
|
sendClient("+ send literal data");
|
|
|
|
char[] buffer = new char[size];
|
2009-01-23 08:01:46 -05:00
|
|
|
int index = 0;
|
|
|
|
int count = 0;
|
|
|
|
while (count >= 0 && index < size) {
|
|
|
|
count = in.read(buffer, index, size - index);
|
|
|
|
if (count >= 0) {
|
|
|
|
index += count;
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 05:50:46 -05:00
|
|
|
// empty line
|
|
|
|
readClient();
|
2009-01-24 07:53:22 -05:00
|
|
|
String messageBody = new String(buffer);
|
|
|
|
String subject = null;
|
|
|
|
int subjectStartIndex = messageBody.indexOf("Subject: ");
|
|
|
|
if (subjectStartIndex >= 0) {
|
|
|
|
int subjectEndIndex = messageBody.indexOf("\r", subjectStartIndex);
|
|
|
|
if (subjectEndIndex >= 0) {
|
2009-01-26 18:51:08 -05:00
|
|
|
subject = messageBody.substring(subjectStartIndex + "Subject: ".length(), subjectEndIndex);
|
2009-01-24 07:53:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (subject == null) {
|
2009-01-26 18:51:08 -05:00
|
|
|
subject = "mail" + System.currentTimeMillis();
|
2009-01-24 07:53:22 -05:00
|
|
|
}
|
|
|
|
session.createMessage(session.getFolderPath(folderName), subject, null, new String(buffer), true);
|
2009-01-23 05:50:46 -05:00
|
|
|
sendClient(commandId + " OK APPEND completed");
|
2009-02-02 02:19:57 -05:00
|
|
|
} else if ("noop".equalsIgnoreCase(command) || "check".equalsIgnoreCase(command)) {
|
2009-01-23 06:20:20 -05:00
|
|
|
if (currentFolder != null) {
|
|
|
|
currentFolder = session.getFolder(currentFolder.folderName);
|
2009-02-02 02:19:57 -05:00
|
|
|
messages = session.getAllMessages(currentFolder.folderUrl);
|
2009-01-23 06:20:20 -05:00
|
|
|
sendClient("* " + currentFolder.objectCount + " EXISTS");
|
|
|
|
sendClient("* " + currentFolder.objectCount + " RECENT");
|
|
|
|
}
|
2009-02-02 02:19:57 -05:00
|
|
|
sendClient(commandId + " OK " + command + " 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()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
userName = tokens.nextToken();
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
throw new IOException("Invalid credentials");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
password = tokens.nextToken();
|
2006-12-12 18:57:24 -05:00
|
|
|
} 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;
|
2009-01-26 18:51:08 -05:00
|
|
|
if (result.startsWith("\"") || result.startsWith("{") || result.startsWith("(")) {
|
2006-12-12 18:57:24 -05:00
|
|
|
result = result.substring(1);
|
|
|
|
}
|
2009-01-26 18:51:08 -05:00
|
|
|
if (result.endsWith("\"") || result.endsWith("}") || result.endsWith(")")) {
|
2006-12-12 18:57:24 -05:00
|
|
|
result = result.substring(0, result.length() - 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-01-26 18:51:08 -05:00
|
|
|
/**
|
|
|
|
* Filter to limit output lines to max body lines after header
|
|
|
|
*/
|
|
|
|
private static class HeaderOutputStream extends FilterOutputStream {
|
|
|
|
protected static final int START = 0;
|
|
|
|
protected static final int CR = 1;
|
|
|
|
protected static final int CRLF = 2;
|
|
|
|
protected static final int CRLFCR = 3;
|
|
|
|
protected static final int BODY = 4;
|
|
|
|
|
|
|
|
protected int state = START;
|
|
|
|
protected int size = 0;
|
|
|
|
|
|
|
|
public HeaderOutputStream(OutputStream os) {
|
|
|
|
super(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(int b) throws IOException {
|
|
|
|
size++;
|
|
|
|
if (state != BODY) {
|
|
|
|
super.write(b);
|
|
|
|
}
|
|
|
|
if (state == START) {
|
|
|
|
if (b == '\r') {
|
|
|
|
state = CR;
|
|
|
|
}
|
|
|
|
} else if (state == CR) {
|
|
|
|
if (b == '\n') {
|
|
|
|
state = CRLF;
|
|
|
|
} else {
|
|
|
|
state = START;
|
|
|
|
}
|
|
|
|
} else if (state == CRLF) {
|
|
|
|
if (b == '\r') {
|
|
|
|
state = CRLFCR;
|
|
|
|
} else {
|
|
|
|
state = START;
|
|
|
|
}
|
|
|
|
} else if (state == CRLFCR) {
|
|
|
|
if (b == '\n') {
|
|
|
|
state = BODY;
|
|
|
|
} else {
|
|
|
|
state = START;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|