2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
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;
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2009-04-27 19:03:58 -04:00
|
|
|
import davmail.exception.DavMailException;
|
2009-09-25 05:13:02 -04:00
|
|
|
import davmail.exception.HttpForbiddenException;
|
|
|
|
import davmail.exception.HttpNotFoundException;
|
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-04-03 03:38:31 -04:00
|
|
|
import davmail.ui.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-03-02 10:38:02 -05:00
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.internet.MimeMultipart;
|
|
|
|
import javax.mail.internet.MimeBodyPart;
|
|
|
|
import javax.mail.internet.MimePart;
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import java.io.*;
|
2009-01-29 17:18:53 -05:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.SocketTimeoutException;
|
2009-02-03 12:56:38 -05:00
|
|
|
import java.net.SocketException;
|
2009-02-04 16:55:42 -05:00
|
|
|
import java.util.*;
|
2009-02-05 12:15:30 -05:00
|
|
|
import java.text.SimpleDateFormat;
|
2009-02-17 17:59:32 -05:00
|
|
|
import java.text.ParseException;
|
2009-01-29 17:18:53 -05:00
|
|
|
|
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 {
|
|
|
|
|
2009-01-22 19:59:41 -05:00
|
|
|
ExchangeSession.Folder currentFolder;
|
|
|
|
|
2009-08-11 12:01:21 -04:00
|
|
|
/**
|
|
|
|
* Initialize the streams and start the thread.
|
|
|
|
*
|
|
|
|
* @param clientSocket IMAP client socket
|
|
|
|
*/
|
2006-12-14 10:14:18 -05:00
|
|
|
public ImapConnection(Socket clientSocket) {
|
2009-05-04 18:31:06 -04:00
|
|
|
super(ImapConnection.class.getSimpleName(), clientSocket, null);
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
2009-05-15 08:39:15 -04:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2006-12-12 18:57:24 -05:00
|
|
|
String line;
|
2009-02-03 12:56:38 -05:00
|
|
|
String commandId = null;
|
2009-03-16 20:25:10 -04:00
|
|
|
IMAPTokenizer tokens;
|
2006-12-12 18:57:24 -05:00
|
|
|
try {
|
2009-02-04 15:38:07 -05:00
|
|
|
ExchangeSessionFactory.checkConfig();
|
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-02-17 17:59:32 -05:00
|
|
|
tokens = new IMAPTokenizer(line);
|
2006-12-12 18:57:24 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-02-03 12:56:38 -05:00
|
|
|
commandId = tokens.nextToken();
|
2006-12-12 18:57:24 -05:00
|
|
|
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)) {
|
|
|
|
try {
|
2009-02-20 12:06:03 -05:00
|
|
|
sendClient("+ " + base64Encode("Username:"));
|
|
|
|
state = State.LOGIN;
|
|
|
|
userName = base64Decode(readClient());
|
|
|
|
sendClient("+ " + base64Encode("Password:"));
|
|
|
|
state = State.PASSWORD;
|
|
|
|
password = base64Decode(readClient());
|
2009-01-22 19:59:41 -05:00
|
|
|
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-09-22 11:55:31 -04:00
|
|
|
// check for expired session
|
|
|
|
session = ExchangeSessionFactory.getInstance(session, userName, password);
|
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-09-25 05:13:02 -04:00
|
|
|
if (folderQuery.endsWith("%/%") && !"/%/%".equals(folderQuery)) {
|
|
|
|
List<ExchangeSession.Folder> folders = session.getSubFolders(folderQuery.substring(0, folderQuery.length() - 3), false);
|
|
|
|
for (ExchangeSession.Folder folder : folders) {
|
|
|
|
sendClient("* " + command + " (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderPath) + '\"');
|
|
|
|
try {
|
|
|
|
List<ExchangeSession.Folder> subfolders = session.getSubFolders(folder.folderPath, false);
|
|
|
|
for (ExchangeSession.Folder subfolder : subfolders) {
|
|
|
|
sendClient("* " + command + " (" + subfolder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(subfolder.folderPath) + '\"');
|
|
|
|
}
|
|
|
|
} catch (HttpForbiddenException e) {
|
|
|
|
// access forbidden, ignore
|
|
|
|
} catch (HttpNotFoundException e) {
|
|
|
|
// not found, ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sendClient(commandId + " OK " + command + " completed");
|
|
|
|
} else if (folderQuery.endsWith("%") || folderQuery.endsWith("*")) {
|
|
|
|
if ("/*".equals(folderQuery) || "/%".equals(folderQuery) || "/%/%".equals(folderQuery)) {
|
|
|
|
folderQuery = folderQuery.substring(1);
|
|
|
|
if ("%/%".equals(folderQuery)) {
|
|
|
|
folderQuery = folderQuery.substring(0, folderQuery.length() - 2);
|
|
|
|
}
|
|
|
|
sendClient("* " + command + " (\\HasChildren) \"/\" \"/public\"");
|
|
|
|
}
|
|
|
|
boolean recursive = folderQuery.endsWith("*") && !folderQuery.startsWith("/public");
|
2009-01-23 05:30:48 -05:00
|
|
|
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-07-31 02:21:46 -04:00
|
|
|
sendClient("* " + command + " (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderPath) + '\"');
|
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 {
|
2009-09-25 05:13:02 -04:00
|
|
|
ExchangeSession.Folder folder = null;
|
|
|
|
try {
|
|
|
|
session.getFolder(folderQuery);
|
|
|
|
} catch (HttpForbiddenException e) {
|
|
|
|
// access forbidden, ignore
|
|
|
|
} catch (HttpNotFoundException e) {
|
|
|
|
// not found, ignore
|
|
|
|
}
|
2009-01-22 19:59:41 -05:00
|
|
|
if (folder != null) {
|
2009-07-31 02:21:46 -04:00
|
|
|
sendClient("* " + command + " (" + folder.getFlags() + ") \"/\" \"" + BASE64MailboxEncoder.encode(folder.folderPath) + '\"');
|
2009-01-23 05:30:48 -05:00
|
|
|
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-09-25 05:13:02 -04:00
|
|
|
try {
|
|
|
|
currentFolder = session.getFolder(folderName);
|
|
|
|
currentFolder.loadMessages();
|
|
|
|
sendClient("* " + currentFolder.count() + " EXISTS");
|
|
|
|
sendClient("* " + currentFolder.count() + " RECENT");
|
|
|
|
sendClient("* OK [UIDVALIDITY 1]");
|
|
|
|
if (currentFolder.count() == 0) {
|
|
|
|
sendClient("* OK [UIDNEXT " + 1 + ']');
|
|
|
|
} else {
|
|
|
|
sendClient("* OK [UIDNEXT " + currentFolder.getUidNext() + ']');
|
|
|
|
}
|
|
|
|
sendClient("* FLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded Junk)");
|
|
|
|
sendClient("* OK [PERMANENTFLAGS (\\Answered \\Deleted \\Draft \\Flagged \\Seen $Forwarded Junk \\*)]");
|
|
|
|
if ("select".equalsIgnoreCase(command)) {
|
|
|
|
sendClient(commandId + " OK [READ-WRITE] " + command + " completed");
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " OK [READ-ONLY] " + command + " completed");
|
|
|
|
}
|
|
|
|
} catch (HttpForbiddenException e) {
|
|
|
|
sendClient(commandId + " NO Forbidden");
|
2009-03-26 17:54:50 -04:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
2009-02-04 18:06:30 -05:00
|
|
|
} else if ("close".equalsIgnoreCase(command) || "expunge".equalsIgnoreCase(command)) {
|
2009-03-02 10:45:07 -05:00
|
|
|
expunge("close".equalsIgnoreCase(command));
|
2009-02-05 12:50:15 -05:00
|
|
|
sendClient(commandId + " OK " + command + " 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) {
|
2009-04-01 18:58:03 -04:00
|
|
|
sendClient(commandId + " NO " + e.getMessage());
|
2009-01-26 18:51:08 -05:00
|
|
|
}
|
2009-02-02 18:29:44 -05:00
|
|
|
} else if ("delete".equalsIgnoreCase(command)) {
|
|
|
|
String folderName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
|
|
|
try {
|
|
|
|
session.deleteFolder(folderName);
|
|
|
|
sendClient(commandId + " OK delete completed");
|
|
|
|
} catch (HttpException e) {
|
2009-04-01 18:58:03 -04:00
|
|
|
sendClient(commandId + " NO " + e.getMessage());
|
2009-02-02 18:29:44 -05:00
|
|
|
}
|
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)) {
|
2009-02-04 17:17:34 -05:00
|
|
|
if (currentFolder == null) {
|
|
|
|
sendClient(commandId + " NO no folder selected");
|
|
|
|
} else {
|
2009-05-15 08:39:15 -04:00
|
|
|
String ranges = tokens.nextToken();
|
|
|
|
if (ranges == null) {
|
|
|
|
sendClient(commandId + " BAD missing range parameter");
|
|
|
|
} else {
|
|
|
|
UIDRangeIterator uidRangeIterator = new UIDRangeIterator(ranges);
|
|
|
|
String parameters = null;
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
parameters = tokens.nextToken();
|
|
|
|
}
|
|
|
|
if (!uidRangeIterator.hasNext() && ranges.indexOf(':') < 0) {
|
|
|
|
// message not found in current list, maybe deleted in another thread
|
|
|
|
sendClient(commandId + " NO No message found");
|
|
|
|
} else {
|
|
|
|
while (uidRangeIterator.hasNext()) {
|
2009-09-24 05:18:57 -04:00
|
|
|
DavGatewayTray.switchIcon();
|
2009-05-15 08:39:15 -04:00
|
|
|
ExchangeSession.Message message = uidRangeIterator.next();
|
|
|
|
handleFetch(message, uidRangeIterator.currentIndex, parameters);
|
|
|
|
}
|
|
|
|
sendClient(commandId + " OK UID FETCH completed");
|
|
|
|
}
|
2009-01-23 05:30:48 -05:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
2009-02-04 17:17:34 -05:00
|
|
|
|
2009-01-23 06:20:20 -05:00
|
|
|
} else if ("search".equalsIgnoreCase(subcommand)) {
|
2009-02-17 19:09:42 -05:00
|
|
|
SearchConditions conditions = new SearchConditions();
|
2009-02-17 19:58:20 -05:00
|
|
|
conditions.append("AND (");
|
|
|
|
boolean or = false;
|
2009-02-17 17:59:32 -05:00
|
|
|
|
2009-01-23 06:20:20 -05:00
|
|
|
while (tokens.hasMoreTokens()) {
|
2009-08-12 12:31:31 -04:00
|
|
|
String token = tokens.nextToken().toUpperCase();
|
2009-07-17 08:19:08 -04:00
|
|
|
if ("OR".equals(token)) {
|
2009-02-17 19:58:20 -05:00
|
|
|
or = true;
|
2009-02-17 17:59:32 -05:00
|
|
|
} else if (token.startsWith("OR ")) {
|
2009-02-17 19:58:20 -05:00
|
|
|
or = true;
|
2009-02-23 05:06:12 -05:00
|
|
|
appendOrSearchParams(token, conditions);
|
2009-02-17 17:59:32 -05:00
|
|
|
} else {
|
2009-02-17 19:58:20 -05:00
|
|
|
String operator;
|
|
|
|
if (conditions.query.length() == 5) {
|
|
|
|
operator = "";
|
|
|
|
} else if (or) {
|
|
|
|
operator = " OR ";
|
|
|
|
} else {
|
|
|
|
operator = " AND ";
|
|
|
|
}
|
|
|
|
appendSearchParam(operator, tokens, token, conditions);
|
2009-02-17 17:59:32 -05:00
|
|
|
}
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
2009-02-17 19:58:20 -05:00
|
|
|
conditions.append(")");
|
2009-03-09 19:12:08 -04:00
|
|
|
String query = conditions.query.toString();
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_SEARCH_QUERY", conditions.query));
|
2009-03-09 19:12:08 -04:00
|
|
|
if ("AND ()".equals(query)) {
|
|
|
|
query = null;
|
|
|
|
}
|
|
|
|
ExchangeSession.MessageList localMessages = session.searchMessages(currentFolder.folderName, query);
|
2009-07-17 04:45:34 -04:00
|
|
|
int index = 1;
|
2009-02-23 05:06:12 -05:00
|
|
|
for (ExchangeSession.Message message : localMessages) {
|
2009-07-17 08:19:08 -04:00
|
|
|
if ((conditions.deleted == null || message.deleted == conditions.deleted)
|
2009-02-17 19:09:42 -05:00
|
|
|
&& (conditions.flagged == null || message.flagged == conditions.flagged)
|
|
|
|
&& (conditions.answered == null || message.answered == conditions.answered)
|
2009-03-19 08:41:11 -04:00
|
|
|
&& (conditions.startUid == 0 || message.getImapUid() >= conditions.startUid)
|
2009-07-17 04:45:34 -04:00
|
|
|
&& (conditions.startIndex == 0 || (index++ >= conditions.startIndex))
|
2009-02-17 19:09:42 -05:00
|
|
|
) {
|
2009-03-19 08:41:11 -04:00
|
|
|
sendClient("* SEARCH " + message.getImapUid());
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sendClient(commandId + " OK SEARCH completed");
|
|
|
|
|
|
|
|
} else if ("store".equalsIgnoreCase(subcommand)) {
|
2009-09-21 17:34:13 -04:00
|
|
|
UIDRangeIterator uidRangeIterator = new UIDRangeIterator(tokens.nextToken());
|
2009-01-26 18:51:08 -05:00
|
|
|
String action = tokens.nextToken();
|
|
|
|
String flags = tokens.nextToken();
|
2009-09-21 17:34:13 -04:00
|
|
|
while (uidRangeIterator.hasNext()) {
|
2009-09-24 05:18:57 -04:00
|
|
|
DavGatewayTray.switchIcon();
|
2009-09-21 17:34:13 -04:00
|
|
|
ExchangeSession.Message message = uidRangeIterator.next();
|
2009-02-04 16:55:42 -05:00
|
|
|
updateFlags(message, action, flags);
|
2009-09-21 17:34:13 -04:00
|
|
|
sendClient("* " + (uidRangeIterator.currentIndex) + " FETCH (UID " + message.getImapUid() + " FLAGS (" + (message.getImapFlags()) + "))");
|
2009-02-02 15:46:15 -05:00
|
|
|
}
|
2009-01-23 08:01:46 -05:00
|
|
|
sendClient(commandId + " OK STORE completed");
|
2009-02-02 18:29:44 -05:00
|
|
|
} else if ("copy".equalsIgnoreCase(subcommand)) {
|
|
|
|
try {
|
2009-09-21 17:34:13 -04:00
|
|
|
UIDRangeIterator uidRangeIterator = new UIDRangeIterator(tokens.nextToken());
|
2009-02-03 11:18:29 -05:00
|
|
|
String targetName = BASE64MailboxDecoder.decode(tokens.nextToken());
|
2009-09-21 17:34:13 -04:00
|
|
|
while (uidRangeIterator.hasNext()) {
|
2009-09-24 05:18:57 -04:00
|
|
|
DavGatewayTray.switchIcon();
|
2009-09-21 17:34:13 -04:00
|
|
|
ExchangeSession.Message message = uidRangeIterator.next();
|
2009-07-31 02:21:46 -04:00
|
|
|
session.copyMessage(message, targetName);
|
2009-02-03 11:18:29 -05:00
|
|
|
}
|
2009-02-04 16:55:42 -05:00
|
|
|
sendClient(commandId + " OK copy completed");
|
|
|
|
} catch (HttpException e) {
|
2009-04-01 18:58:03 -04:00
|
|
|
sendClient(commandId + " NO " + e.getMessage());
|
2009-02-02 18:29:44 -05:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendClient(commandId + " BAD command unrecognized");
|
|
|
|
}
|
2009-02-20 12:06:03 -05:00
|
|
|
} else if ("fetch".equalsIgnoreCase(command)) {
|
|
|
|
if (currentFolder == null) {
|
|
|
|
sendClient(commandId + " NO no folder selected");
|
|
|
|
} else {
|
|
|
|
RangeIterator rangeIterator = new RangeIterator(tokens.nextToken());
|
|
|
|
String parameters = null;
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
parameters = tokens.nextToken();
|
|
|
|
}
|
|
|
|
while (rangeIterator.hasNext()) {
|
2009-09-24 05:18:57 -04:00
|
|
|
DavGatewayTray.switchIcon();
|
2009-02-20 12:06:03 -05:00
|
|
|
ExchangeSession.Message message = rangeIterator.next();
|
|
|
|
handleFetch(message, rangeIterator.currentIndex, parameters);
|
|
|
|
}
|
|
|
|
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-02-03 18:54:48 -05:00
|
|
|
HashMap<String, String> properties = new HashMap<String, String>();
|
2009-03-16 20:25:10 -04:00
|
|
|
String flags = null;
|
|
|
|
String date = null;
|
|
|
|
// handle optional flags
|
|
|
|
String nextToken = tokens.nextQuotedToken();
|
|
|
|
if (nextToken.startsWith("(")) {
|
|
|
|
flags = removeQuotes(nextToken);
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
nextToken = tokens.nextToken();
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
date = nextToken;
|
|
|
|
nextToken = tokens.nextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (tokens.hasMoreTokens()) {
|
|
|
|
date = removeQuotes(nextToken);
|
|
|
|
nextToken = tokens.nextToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags != null) {
|
|
|
|
StringTokenizer flagtokenizer = new StringTokenizer(flags);
|
|
|
|
while (flagtokenizer.hasMoreTokens()) {
|
|
|
|
String flag = flagtokenizer.nextToken();
|
|
|
|
if ("\\Seen".equals(flag)) {
|
|
|
|
properties.put("read", "1");
|
|
|
|
} else if ("\\Flagged".equals(flag)) {
|
|
|
|
properties.put("flagged", "2");
|
|
|
|
} else if ("\\Answered".equals(flag)) {
|
|
|
|
properties.put("answered", "102");
|
|
|
|
} else if ("$Forwarded".equals(flag)) {
|
|
|
|
properties.put("forwarded", "104");
|
|
|
|
} else if ("\\Draft".equals(flag)) {
|
|
|
|
properties.put("draft", "9");
|
|
|
|
} else if ("Junk".equals(flag)) {
|
|
|
|
properties.put("junk", "1");
|
|
|
|
}
|
2009-02-03 18:54:48 -05:00
|
|
|
}
|
|
|
|
}
|
2009-02-05 12:15:30 -05:00
|
|
|
// handle optional date
|
2009-03-16 20:25:10 -04:00
|
|
|
if (date != null) {
|
2009-02-05 12:15:30 -05:00
|
|
|
SimpleDateFormat dateParser = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z", Locale.ENGLISH);
|
2009-03-16 20:25:10 -04:00
|
|
|
Date dateReceived = dateParser.parse(date);
|
2009-02-05 12:15:30 -05:00
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
|
dateFormatter.setTimeZone(ExchangeSession.GMT_TIMEZONE);
|
|
|
|
|
|
|
|
properties.put("datereceived", dateFormatter.format(dateReceived));
|
2009-02-03 12:56:38 -05:00
|
|
|
}
|
2009-03-16 20:25:10 -04:00
|
|
|
int size = Integer.parseInt(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-02-03 09:38:05 -05:00
|
|
|
|
2009-02-03 11:58:18 -05:00
|
|
|
String messageName = UUID.randomUUID().toString();
|
2009-07-28 02:53:46 -04:00
|
|
|
session.createMessage(folderName, messageName, properties, new String(buffer));
|
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) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_IMAP_COMMAND", command, currentFolder.folderName));
|
2009-01-23 06:20:20 -05:00
|
|
|
currentFolder = session.getFolder(currentFolder.folderName);
|
2009-03-27 06:31:52 -04:00
|
|
|
currentFolder.loadMessages();
|
2009-07-31 02:21:46 -04:00
|
|
|
sendClient("* " + currentFolder.count() + " EXISTS");
|
|
|
|
sendClient("* " + currentFolder.count() + " RECENT");
|
2009-01-23 06:20:20 -05:00
|
|
|
}
|
2009-02-02 02:19:57 -05:00
|
|
|
sendClient(commandId + " OK " + command + " completed");
|
2009-02-05 11:06:34 -05:00
|
|
|
} else if ("subscribe".equalsIgnoreCase(command) || "unsubscribe".equalsIgnoreCase(command)) {
|
|
|
|
sendClient(commandId + " OK " + command + " completed");
|
2009-02-05 12:50:15 -05:00
|
|
|
} else if ("status".equalsIgnoreCase(command)) {
|
|
|
|
try {
|
|
|
|
String encodedFolderName = tokens.nextToken();
|
|
|
|
String folderName = BASE64MailboxDecoder.decode(encodedFolderName);
|
|
|
|
ExchangeSession.Folder folder = session.getFolder(folderName);
|
2009-02-20 12:06:03 -05:00
|
|
|
// must retrieve messages
|
2009-03-27 06:31:52 -04:00
|
|
|
folder.loadMessages();
|
2009-02-05 12:50:15 -05:00
|
|
|
String parameters = tokens.nextToken();
|
|
|
|
StringBuilder answer = new StringBuilder();
|
|
|
|
StringTokenizer parametersTokens = new StringTokenizer(parameters);
|
|
|
|
while (parametersTokens.hasMoreTokens()) {
|
|
|
|
String token = parametersTokens.nextToken();
|
|
|
|
if ("MESSAGES".equalsIgnoreCase(token)) {
|
2009-07-31 02:21:46 -04:00
|
|
|
answer.append("MESSAGES ").append(folder.count()).append(' ');
|
2009-02-05 12:50:15 -05:00
|
|
|
}
|
|
|
|
if ("RECENT".equalsIgnoreCase(token)) {
|
2009-07-31 02:21:46 -04:00
|
|
|
answer.append("RECENT ").append(folder.count()).append(' ');
|
2009-02-05 12:50:15 -05:00
|
|
|
}
|
|
|
|
if ("UIDNEXT".equalsIgnoreCase(token)) {
|
2009-07-31 02:21:46 -04:00
|
|
|
if (folder.count() == 0) {
|
2009-02-05 12:50:15 -05:00
|
|
|
answer.append("UIDNEXT 1 ");
|
|
|
|
} else {
|
2009-07-31 02:21:46 -04:00
|
|
|
if (folder.count() == 0) {
|
2009-02-05 12:50:15 -05:00
|
|
|
answer.append("UIDNEXT 1 ");
|
|
|
|
} else {
|
2009-04-23 16:53:22 -04:00
|
|
|
answer.append("UIDNEXT ").append(folder.getUidNext()).append(' ');
|
2009-02-05 12:50:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if ("UIDVALIDITY".equalsIgnoreCase(token)) {
|
|
|
|
answer.append("UIDVALIDITY 1 ");
|
|
|
|
}
|
|
|
|
if ("UNSEEN".equalsIgnoreCase(token)) {
|
2009-04-23 16:53:22 -04:00
|
|
|
answer.append("UNSEEN ").append(folder.unreadCount).append(' ');
|
2009-02-05 12:50:15 -05:00
|
|
|
}
|
|
|
|
}
|
2009-04-23 16:53:22 -04:00
|
|
|
sendClient("* STATUS " + encodedFolderName + " (" + answer.toString().trim() + ')');
|
2009-02-05 12:50:15 -05:00
|
|
|
sendClient(commandId + " OK " + command + " completed");
|
|
|
|
} catch (HttpException e) {
|
|
|
|
sendClient(commandId + " NO folder not found");
|
|
|
|
}
|
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");
|
|
|
|
}
|
2009-04-13 05:36:27 -04:00
|
|
|
DavGatewayTray.resetIcon();
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
os.flush();
|
2009-01-22 19:59:41 -05:00
|
|
|
} catch (SocketTimeoutException e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_CLOSE_CONNECTION_ON_TIMEOUT"));
|
2009-01-22 19:59:41 -05:00
|
|
|
try {
|
|
|
|
sendClient("* BYE Closing connection");
|
|
|
|
} catch (IOException e1) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_EXCEPTION_CLOSING_CONNECTION_ON_TIMEOUT"));
|
2009-01-22 19:59:41 -05:00
|
|
|
}
|
2009-02-03 12:56:38 -05:00
|
|
|
} catch (SocketException e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_CONNECTION_CLOSED"));
|
2009-02-03 12:56:38 -05:00
|
|
|
} catch (Exception e) {
|
2009-07-15 18:47:31 -04:00
|
|
|
DavGatewayTray.log(e);
|
2009-02-03 12:56:38 -05:00
|
|
|
try {
|
2009-09-22 05:03:32 -04:00
|
|
|
String message = ((e.getMessage() == null) ? e.toString() : e.getMessage()).replaceAll("\\n", " ");
|
2009-02-03 12:56:38 -05:00
|
|
|
if (commandId != null) {
|
2009-03-26 17:54:50 -04:00
|
|
|
sendClient(commandId + " BAD unable to handle request: " + message);
|
2009-02-03 12:56:38 -05:00
|
|
|
} else {
|
2009-03-26 17:54:50 -04:00
|
|
|
sendClient("* BYE unable to handle request: " + message);
|
2009-02-03 12:56:38 -05:00
|
|
|
}
|
|
|
|
} catch (IOException e2) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_EXCEPTION_SENDING_ERROR_TO_CLIENT"), e2);
|
2009-02-03 12:56:38 -05:00
|
|
|
}
|
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-02-20 12:06:03 -05:00
|
|
|
private void handleFetch(ExchangeSession.Message message, int currentIndex, String parameters) throws IOException {
|
2009-02-23 12:42:49 -05:00
|
|
|
StringBuilder buffer = new StringBuilder();
|
2009-03-19 08:41:11 -04:00
|
|
|
buffer.append("* ").append(currentIndex).append(" FETCH (UID ").append(message.getImapUid());
|
2009-03-27 06:31:52 -04:00
|
|
|
if (parameters != null) {
|
|
|
|
boolean bodystructure = false;
|
|
|
|
StringTokenizer paramTokens = new StringTokenizer(parameters);
|
|
|
|
while (paramTokens.hasMoreTokens()) {
|
|
|
|
String param = paramTokens.nextToken();
|
|
|
|
if ("FLAGS".equals(param)) {
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(" FLAGS (").append(message.getImapFlags()).append(')');
|
2009-03-27 06:31:52 -04:00
|
|
|
} else if ("BODYSTRUCTURE".equals(param)) {
|
|
|
|
if (parameters.indexOf("BODY.") >= 0) {
|
|
|
|
// Apple Mail: send structure with body, need exact RFC822.SIZE
|
|
|
|
bodystructure = true;
|
|
|
|
} else {
|
|
|
|
// thunderbird : send BODYSTRUCTURE
|
2009-09-08 18:25:39 -04:00
|
|
|
appendBodyStructure(buffer, message);
|
2009-03-27 06:31:52 -04:00
|
|
|
}
|
|
|
|
} else if ("INTERNALDATE".equals(param) && message.date != null && message.date.length() > 0) {
|
|
|
|
try {
|
|
|
|
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
|
dateParser.setTimeZone(ExchangeSession.GMT_TIMEZONE);
|
|
|
|
Date date = dateParser.parse(message.date);
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z", Locale.ENGLISH);
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(" INTERNALDATE \"").append(dateFormatter.format(date)).append('\"');
|
2009-03-27 06:31:52 -04:00
|
|
|
} catch (ParseException e) {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_DATE", message.date);
|
2009-03-27 06:31:52 -04:00
|
|
|
}
|
|
|
|
} else if ("BODY.PEEK[HEADER]".equals(param) || param.startsWith("BODY.PEEK[HEADER")) {
|
2009-03-02 10:38:02 -05:00
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
2009-03-27 06:31:52 -04:00
|
|
|
PartOutputStream partOutputStream = new PartOutputStream(baos, true, false, 0);
|
|
|
|
message.write(partOutputStream);
|
|
|
|
baos.close();
|
2009-09-02 18:24:53 -04:00
|
|
|
|
|
|
|
if (bodystructure) {
|
|
|
|
bodystructure = false;
|
|
|
|
// Apple Mail: need to build full bodystructure
|
2009-09-08 18:25:39 -04:00
|
|
|
appendBodyStructure(buffer, message);
|
2009-09-02 18:24:53 -04:00
|
|
|
}
|
2009-03-27 06:31:52 -04:00
|
|
|
buffer.append(" RFC822.SIZE ").append(partOutputStream.size);
|
|
|
|
if ("BODY.PEEK[HEADER]".equals(param)) {
|
|
|
|
buffer.append(" BODY[HEADER] {");
|
|
|
|
} else {
|
|
|
|
buffer.append(" BODY[HEADER.FIELDS ()] {");
|
|
|
|
}
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(baos.size()).append('}');
|
2009-03-27 06:31:52 -04:00
|
|
|
sendClient(buffer.toString());
|
|
|
|
os.write(baos.toByteArray());
|
|
|
|
os.flush();
|
|
|
|
buffer.setLength(0);
|
|
|
|
} else if (param.startsWith("BODY[]") || param.startsWith("BODY.PEEK[]") || "BODY.PEEK[TEXT]".equals(param)) {
|
|
|
|
// parse buffer size
|
|
|
|
int startIndex = 0;
|
|
|
|
int ltIndex = param.indexOf('<');
|
|
|
|
if (ltIndex >= 0) {
|
|
|
|
int dotIndex = param.indexOf('.', ltIndex);
|
|
|
|
if (dotIndex >= 0) {
|
|
|
|
startIndex = Integer.parseInt(param.substring(ltIndex + 1, dotIndex));
|
|
|
|
// ignore buffer size
|
|
|
|
}
|
2009-03-09 20:12:52 -04:00
|
|
|
}
|
|
|
|
|
2009-03-27 06:31:52 -04:00
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
boolean writeHeaders = true;
|
|
|
|
int rfc822size;
|
|
|
|
if ("BODY.PEEK[TEXT]".equals(param)) {
|
|
|
|
writeHeaders = false;
|
|
|
|
}
|
|
|
|
PartOutputStream bodyOutputStream = new PartOutputStream(baos, writeHeaders, true, startIndex);
|
|
|
|
message.write(bodyOutputStream);
|
|
|
|
rfc822size = bodyOutputStream.size;
|
|
|
|
baos.close();
|
2009-04-23 10:54:06 -04:00
|
|
|
|
2009-03-27 06:31:52 -04:00
|
|
|
if (bodystructure) {
|
2009-09-02 18:24:53 -04:00
|
|
|
bodystructure = false;
|
2009-03-27 06:31:52 -04:00
|
|
|
// Apple Mail: need to build full bodystructure
|
2009-09-08 18:25:39 -04:00
|
|
|
appendBodyStructure(buffer, message);
|
2009-03-27 06:31:52 -04:00
|
|
|
}
|
2009-09-02 17:57:35 -04:00
|
|
|
buffer.append(" RFC822.SIZE ").append(rfc822size).append(' ');
|
|
|
|
if ("BODY.PEEK[TEXT]".equals(param)) {
|
|
|
|
buffer.append("BODY[TEXT]");
|
|
|
|
} else {
|
|
|
|
buffer.append("BODY[]");
|
|
|
|
}
|
2009-03-27 06:31:52 -04:00
|
|
|
// partial
|
|
|
|
if (startIndex > 0) {
|
|
|
|
buffer.append('<').append(startIndex).append('>');
|
|
|
|
}
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(" {").append(baos.size()).append('}');
|
2009-03-27 06:31:52 -04:00
|
|
|
sendClient(buffer.toString());
|
|
|
|
os.write(baos.toByteArray());
|
|
|
|
os.flush();
|
|
|
|
buffer.setLength(0);
|
2009-03-09 20:12:52 -04:00
|
|
|
}
|
2009-02-23 12:42:49 -05:00
|
|
|
}
|
2009-02-20 12:06:03 -05:00
|
|
|
}
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(')');
|
2009-02-23 12:42:49 -05:00
|
|
|
sendClient(buffer.toString());
|
2009-02-20 12:06:03 -05:00
|
|
|
}
|
|
|
|
|
2009-09-08 18:25:39 -04:00
|
|
|
protected void appendBodyStructure(StringBuilder buffer, ExchangeSession.Message message) throws IOException {
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
message.write(baos);
|
2009-09-21 17:34:13 -04:00
|
|
|
|
2009-03-02 10:38:02 -05:00
|
|
|
buffer.append(" BODYSTRUCTURE ");
|
|
|
|
try {
|
|
|
|
MimeMessage mimeMessage = new MimeMessage(null, new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
Object mimeBody = mimeMessage.getContent();
|
|
|
|
if (mimeBody instanceof MimeMultipart) {
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append('(');
|
2009-03-02 10:38:02 -05:00
|
|
|
MimeMultipart multiPart = (MimeMultipart) mimeBody;
|
|
|
|
|
|
|
|
for (int i = 0; i < multiPart.getCount(); i++) {
|
|
|
|
MimeBodyPart bodyPart = (MimeBodyPart) multiPart.getBodyPart(i);
|
|
|
|
appendBodyStructure(buffer, bodyPart);
|
|
|
|
}
|
|
|
|
int slashIndex = multiPart.getContentType().indexOf('/');
|
|
|
|
if (slashIndex < 0) {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_CONTENT_TYPE", multiPart.getContentType());
|
2009-03-02 10:38:02 -05:00
|
|
|
}
|
|
|
|
int semiColonIndex = multiPart.getContentType().indexOf(';');
|
|
|
|
if (semiColonIndex < 0) {
|
|
|
|
buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1).toUpperCase()).append("\")");
|
|
|
|
} else {
|
|
|
|
buffer.append(" \"").append(multiPart.getContentType().substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append("\")");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no multipart, single body
|
|
|
|
appendBodyStructure(buffer, mimeMessage);
|
|
|
|
}
|
|
|
|
} catch (MessagingException me) {
|
2009-09-04 12:01:24 -04:00
|
|
|
DavGatewayTray.warn(me);
|
|
|
|
// dump message in log
|
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_MESSAGE", new String(baos.toByteArray())));
|
2009-09-07 05:01:07 -04:00
|
|
|
// failover: send default bodystructure
|
|
|
|
buffer.append("(\"TEXT\" \"PLAIN\" (\"CHARSET\" \"US-ASCII\") NIL NIL NIL ").append(baos.size()).append(" NIL)");
|
2009-03-02 10:38:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void appendBodyStructure(StringBuilder buffer, MimePart bodyPart) throws IOException, MessagingException {
|
|
|
|
String contentType = bodyPart.getContentType();
|
|
|
|
int slashIndex = contentType.indexOf('/');
|
|
|
|
if (slashIndex < 0) {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_CONTENT_TYPE", contentType);
|
2009-03-02 10:38:02 -05:00
|
|
|
}
|
|
|
|
buffer.append("(\"").append(contentType.substring(0, slashIndex).toUpperCase()).append("\" \"");
|
|
|
|
int semiColonIndex = contentType.indexOf(';');
|
|
|
|
if (semiColonIndex < 0) {
|
|
|
|
buffer.append(contentType.substring(slashIndex + 1).toUpperCase()).append("\" ()");
|
|
|
|
} else {
|
|
|
|
// extended content type
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(contentType.substring(slashIndex + 1, semiColonIndex).trim().toUpperCase()).append('\"');
|
2009-03-02 10:38:02 -05:00
|
|
|
int charsetindex = contentType.indexOf("charset=");
|
|
|
|
if (charsetindex >= 0) {
|
|
|
|
buffer.append(" (\"CHARSET\" ");
|
|
|
|
int charsetEndIndex = Math.max(contentType.indexOf(' '), contentType.length());
|
|
|
|
String charSet = contentType.substring(charsetindex + "charset=".length(), charsetEndIndex);
|
|
|
|
if (!charSet.startsWith("\"")) {
|
|
|
|
buffer.append('"');
|
|
|
|
}
|
|
|
|
buffer.append(charSet.toUpperCase());
|
|
|
|
if (!charSet.endsWith("\"")) {
|
|
|
|
buffer.append('"');
|
|
|
|
}
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(')');
|
2009-03-02 10:38:02 -05:00
|
|
|
} else {
|
|
|
|
buffer.append(" ()");
|
|
|
|
}
|
|
|
|
}
|
2009-03-27 05:07:03 -04:00
|
|
|
appendBodyStructureValue(buffer, bodyPart.getContentID());
|
|
|
|
appendBodyStructureValue(buffer, bodyPart.getDescription());
|
|
|
|
appendBodyStructureValue(buffer, bodyPart.getEncoding());
|
|
|
|
appendBodyStructureValue(buffer, bodyPart.getSize());
|
|
|
|
appendBodyStructureValue(buffer, bodyPart.getLineCount());
|
|
|
|
buffer.append(')');
|
|
|
|
}
|
|
|
|
|
2009-04-03 03:38:31 -04:00
|
|
|
protected void appendBodyStructureValue(StringBuilder buffer, String value) {
|
2009-03-27 05:07:03 -04:00
|
|
|
if (value == null) {
|
2009-03-02 10:38:02 -05:00
|
|
|
buffer.append(" NIL");
|
|
|
|
} else {
|
2009-04-23 16:53:22 -04:00
|
|
|
buffer.append(" \"").append(value.toUpperCase()).append('\"');
|
2009-03-02 10:38:02 -05:00
|
|
|
}
|
2009-03-27 05:07:03 -04:00
|
|
|
}
|
|
|
|
|
2009-04-03 03:38:31 -04:00
|
|
|
protected void appendBodyStructureValue(StringBuilder buffer, int value) {
|
2009-03-27 05:07:03 -04:00
|
|
|
if (value < 0) {
|
2009-03-02 10:38:02 -05:00
|
|
|
buffer.append(" NIL");
|
|
|
|
} else {
|
2009-03-27 05:07:03 -04:00
|
|
|
buffer.append(' ').append(value);
|
2009-03-02 10:38:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-17 19:09:42 -05:00
|
|
|
static final class SearchConditions {
|
2009-04-16 18:20:30 -04:00
|
|
|
Boolean flagged;
|
|
|
|
Boolean answered;
|
2009-07-17 08:19:08 -04:00
|
|
|
Boolean deleted;
|
2009-04-16 18:20:30 -04:00
|
|
|
long startUid;
|
2009-07-17 04:45:34 -04:00
|
|
|
int startIndex;
|
2009-02-24 05:22:55 -05:00
|
|
|
final StringBuilder query = new StringBuilder();
|
2009-02-17 19:09:42 -05:00
|
|
|
|
|
|
|
public StringBuilder append(String value) {
|
|
|
|
return query.append(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-23 05:06:12 -05:00
|
|
|
protected void appendOrSearchParams(String token, SearchConditions conditions) throws IOException {
|
2009-02-17 19:58:20 -05:00
|
|
|
IMAPTokenizer innerTokens = new IMAPTokenizer(token);
|
|
|
|
innerTokens.nextToken();
|
|
|
|
boolean first = true;
|
|
|
|
while (innerTokens.hasMoreTokens()) {
|
|
|
|
String innerToken = innerTokens.nextToken();
|
|
|
|
String operator = "";
|
|
|
|
if (!first) {
|
|
|
|
operator = " OR ";
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
appendSearchParam(operator, innerTokens, innerToken, conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-17 19:09:42 -05:00
|
|
|
protected void appendSearchParam(String operator, StringTokenizer tokens, String token, SearchConditions conditions) throws IOException {
|
2009-02-17 17:59:32 -05:00
|
|
|
if ("NOT".equals(token)) {
|
2009-07-17 08:19:08 -04:00
|
|
|
appendSearchParam(operator + " NOT ", tokens, tokens.nextToken(), conditions);
|
2009-02-17 19:58:20 -05:00
|
|
|
} else if (token.startsWith("OR ")) {
|
2009-02-23 05:06:12 -05:00
|
|
|
appendOrSearchParams(token, conditions);
|
2009-02-17 17:59:32 -05:00
|
|
|
} else if ("SUBJECT".equals(token)) {
|
2009-02-17 19:09:42 -05:00
|
|
|
conditions.append(operator).append("\"urn:schemas:httpmail:subject\" LIKE '%").append(tokens.nextToken()).append("%'");
|
2009-02-17 17:59:32 -05:00
|
|
|
} else if ("BODY".equals(token)) {
|
2009-02-17 19:09:42 -05:00
|
|
|
conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x01000001E\" LIKE '%").append(tokens.nextToken()).append("%'");
|
2009-02-17 17:59:32 -05:00
|
|
|
} else if ("FROM".equals(token)) {
|
2009-02-17 19:09:42 -05:00
|
|
|
conditions.append(operator).append("\"urn:schemas:mailheader:from\" LIKE '%").append(tokens.nextToken()).append("%'");
|
2009-02-17 19:58:20 -05:00
|
|
|
} else if ("TO".equals(token)) {
|
|
|
|
conditions.append(operator).append("\"urn:schemas:mailheader:to\" LIKE '%").append(tokens.nextToken()).append("%'");
|
|
|
|
} else if ("CC".equals(token)) {
|
|
|
|
conditions.append(operator).append("\"urn:schemas:mailheader:cc\" LIKE '%").append(tokens.nextToken()).append("%'");
|
|
|
|
} else if ("LARGER".equals(token)) {
|
|
|
|
conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x0e080003\" >= ").append(Long.parseLong(tokens.nextToken())).append("");
|
|
|
|
} else if ("SMALLER".equals(token)) {
|
2009-03-19 09:46:45 -04:00
|
|
|
conditions.append(operator).append("\"http://schemas.microsoft.com/mapi/proptag/x0e080003\" < ").append(Long.parseLong(tokens.nextToken())).append("");
|
2009-08-11 12:01:21 -04:00
|
|
|
} else if (token.startsWith("SENT") || "SINCE".equals(token) || "BEFORE".equals(token)) {
|
2009-02-17 19:09:42 -05:00
|
|
|
conditions.append(operator);
|
|
|
|
appendDateSearchParam(tokens, token, conditions);
|
|
|
|
} else if ("SEEN".equals(token)) {
|
|
|
|
conditions.append(operator).append("\"urn:schemas:httpmail:read\" = True");
|
|
|
|
} else if ("UNSEEN".equals(token) || "NEW".equals(token)) {
|
|
|
|
conditions.append(operator).append("\"urn:schemas:httpmail:read\" = False");
|
2009-07-17 08:19:08 -04:00
|
|
|
} else if ("DELETED".equals(token)) {
|
|
|
|
conditions.deleted = !operator.endsWith(" NOT ");
|
|
|
|
} else if ("UNDELETED".equals(token)) {
|
|
|
|
conditions.deleted = Boolean.FALSE;
|
2009-02-17 19:09:42 -05:00
|
|
|
} else if ("FLAGGED".equals(token)) {
|
|
|
|
conditions.flagged = Boolean.TRUE;
|
|
|
|
} else if ("UNFLAGGED".equals(token) || "NEW".equals(token)) {
|
|
|
|
conditions.flagged = Boolean.FALSE;
|
|
|
|
} else if ("ANSWERED".equals(token)) {
|
|
|
|
conditions.answered = Boolean.TRUE;
|
|
|
|
} else if ("UNANSWERED".equals(token)) {
|
|
|
|
conditions.answered = Boolean.FALSE;
|
2009-02-17 17:59:32 -05:00
|
|
|
} else if ("HEADER".equals(token)) {
|
2009-02-17 19:58:20 -05:00
|
|
|
String headerName = tokens.nextToken().toLowerCase();
|
2009-03-19 08:41:11 -04:00
|
|
|
String value = tokens.nextToken();
|
2009-03-27 06:31:52 -04:00
|
|
|
if ("message-id".equals(headerName) && !value.startsWith("<")) {
|
2009-04-23 16:53:22 -04:00
|
|
|
value = '<' + value + '>';
|
2009-03-19 08:41:11 -04:00
|
|
|
}
|
2009-04-23 16:53:22 -04:00
|
|
|
conditions.append(operator).append("\"urn:schemas:mailheader:").append(headerName).append("\"='").append(value).append('\'');
|
2009-02-23 05:06:12 -05:00
|
|
|
} else if ("UID".equals(token)) {
|
|
|
|
String range = tokens.nextToken();
|
|
|
|
if ("1:*".equals(range)) {
|
|
|
|
// ignore: this is a noop filter
|
2009-02-25 12:17:51 -05:00
|
|
|
} else if (range.endsWith(":*")) {
|
2009-03-02 10:38:02 -05:00
|
|
|
conditions.startUid = Long.parseLong(range.substring(0, range.indexOf(':')));
|
2009-02-23 05:06:12 -05:00
|
|
|
} else {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_SEARCH_PARAMETERS", range);
|
2009-02-23 05:06:12 -05:00
|
|
|
}
|
2009-08-11 12:01:21 -04:00
|
|
|
} else if ("OLD".equals(token) || "RECENT".equals(token) || "ALL".equals(token)) {
|
2009-02-17 19:09:42 -05:00
|
|
|
// ignore
|
2009-07-17 04:45:34 -04:00
|
|
|
} else if (token.indexOf(':') >= 0) {
|
|
|
|
// range search
|
|
|
|
try {
|
|
|
|
conditions.startIndex = Integer.parseInt(token.substring(0, token.indexOf(':')));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw new DavMailException("EXCEPTION_INVALID_SEARCH_PARAMETERS", token);
|
|
|
|
}
|
2009-02-17 17:59:32 -05:00
|
|
|
} else {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_SEARCH_PARAMETERS", token);
|
2009-02-17 17:59:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-17 19:09:42 -05:00
|
|
|
protected void appendDateSearchParam(StringTokenizer tokens, String token, SearchConditions conditions) throws IOException {
|
|
|
|
Date startDate;
|
|
|
|
Date endDate;
|
|
|
|
SimpleDateFormat parser = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
|
|
|
|
parser.setTimeZone(ExchangeSession.GMT_TIMEZONE);
|
|
|
|
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
|
|
|
dateFormatter.setTimeZone(ExchangeSession.GMT_TIMEZONE);
|
2009-04-27 19:03:58 -04:00
|
|
|
String dateToken = tokens.nextToken();
|
2009-02-17 19:09:42 -05:00
|
|
|
try {
|
2009-04-27 19:03:58 -04:00
|
|
|
startDate = parser.parse(dateToken);
|
2009-02-17 19:09:42 -05:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(startDate);
|
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
|
endDate = calendar.getTime();
|
|
|
|
} catch (ParseException e) {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_SEARCH_PARAMETERS", dateToken);
|
2009-02-17 19:09:42 -05:00
|
|
|
}
|
2009-08-11 12:01:21 -04:00
|
|
|
String searchAttribute;
|
|
|
|
if (token.startsWith("SENT")) {
|
|
|
|
searchAttribute = "urn:schemas:httpmail:date";
|
|
|
|
} else {
|
2009-09-02 17:57:35 -04:00
|
|
|
searchAttribute = "DAV:getlastmodified";
|
2009-08-11 12:01:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token.endsWith("ON")) {
|
|
|
|
conditions.append("(\"").append(searchAttribute).append("\" > '")
|
2009-02-17 19:09:42 -05:00
|
|
|
.append(dateFormatter.format(startDate))
|
2009-08-11 12:01:21 -04:00
|
|
|
.append("' AND \"").append(searchAttribute).append("\" < '")
|
2009-02-17 19:09:42 -05:00
|
|
|
.append(dateFormatter.format(endDate))
|
|
|
|
.append("')");
|
2009-08-11 12:01:21 -04:00
|
|
|
} else if (token.endsWith("BEFORE")) {
|
|
|
|
conditions.append("\"").append(searchAttribute).append("\" < '")
|
2009-02-17 19:09:42 -05:00
|
|
|
.append(dateFormatter.format(startDate))
|
2009-04-23 16:53:22 -04:00
|
|
|
.append('\'');
|
2009-08-11 12:01:21 -04:00
|
|
|
} else if (token.endsWith("SINCE")) {
|
|
|
|
conditions.append("\"").append(searchAttribute).append("\" >= '")
|
2009-02-17 19:09:42 -05:00
|
|
|
.append(dateFormatter.format(startDate))
|
2009-04-23 16:53:22 -04:00
|
|
|
.append('\'');
|
2009-02-17 19:09:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-02 10:45:07 -05:00
|
|
|
protected void expunge(boolean silent) throws IOException {
|
2009-03-27 06:31:52 -04:00
|
|
|
if (currentFolder.messages != null) {
|
2009-02-02 18:29:44 -05:00
|
|
|
int index = 0;
|
2009-03-27 06:31:52 -04:00
|
|
|
for (ExchangeSession.Message message : currentFolder.messages) {
|
2009-02-02 18:29:44 -05:00
|
|
|
index++;
|
|
|
|
if (message.deleted) {
|
2009-02-03 16:02:33 -05:00
|
|
|
message.delete();
|
2009-03-02 10:45:07 -05:00
|
|
|
if (!silent) {
|
|
|
|
sendClient("* " + index + " EXPUNGE");
|
|
|
|
}
|
2009-02-02 18:29:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 18:54:48 -05:00
|
|
|
protected void updateFlags(ExchangeSession.Message message, String action, String flags) throws IOException {
|
|
|
|
HashMap<String, String> properties = new HashMap<String, String>();
|
2009-02-23 05:06:12 -05:00
|
|
|
if ("-Flags".equalsIgnoreCase(action) || "-FLAGS.SILENT".equalsIgnoreCase(action)) {
|
2009-02-03 18:54:48 -05:00
|
|
|
StringTokenizer flagtokenizer = new StringTokenizer(flags);
|
|
|
|
while (flagtokenizer.hasMoreTokens()) {
|
|
|
|
String flag = flagtokenizer.nextToken();
|
|
|
|
if ("\\Seen".equals(flag)) {
|
|
|
|
properties.put("read", "0");
|
|
|
|
message.read = false;
|
|
|
|
} else if ("\\Flagged".equals(flag)) {
|
|
|
|
properties.put("flagged", "0");
|
|
|
|
message.flagged = false;
|
2009-09-23 18:18:07 -04:00
|
|
|
} else if ("\\Deleted".equals(flag)) {
|
|
|
|
properties.put("deleted", null);
|
|
|
|
message.deleted = false;
|
2009-02-03 18:54:48 -05:00
|
|
|
} else if ("Junk".equals(flag)) {
|
|
|
|
properties.put("junk", "0");
|
|
|
|
message.junk = false;
|
|
|
|
}
|
|
|
|
}
|
2009-02-23 05:06:12 -05:00
|
|
|
} else if ("+Flags".equalsIgnoreCase(action) || "+FLAGS.SILENT".equalsIgnoreCase(action)) {
|
2009-02-03 18:54:48 -05:00
|
|
|
StringTokenizer flagtokenizer = new StringTokenizer(flags);
|
|
|
|
while (flagtokenizer.hasMoreTokens()) {
|
|
|
|
String flag = flagtokenizer.nextToken();
|
|
|
|
if ("\\Seen".equals(flag)) {
|
|
|
|
properties.put("read", "1");
|
|
|
|
message.read = true;
|
|
|
|
} else if ("\\Deleted".equals(flag)) {
|
2009-02-24 09:26:48 -05:00
|
|
|
message.deleted = true;
|
2009-09-23 18:18:07 -04:00
|
|
|
properties.put("deleted", "deleted");
|
2009-02-24 09:26:48 -05:00
|
|
|
} else if ("\\Flagged".equals(flag)) {
|
|
|
|
properties.put("flagged", "2");
|
|
|
|
message.flagged = true;
|
|
|
|
} else if ("\\Answered".equals(flag)) {
|
|
|
|
properties.put("answered", "102");
|
|
|
|
message.answered = true;
|
|
|
|
} else if ("$Forwarded".equals(flag)) {
|
|
|
|
properties.put("forwarded", "104");
|
|
|
|
message.forwarded = true;
|
|
|
|
} else if ("Junk".equals(flag)) {
|
|
|
|
properties.put("junk", "1");
|
|
|
|
message.junk = true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-16 11:08:00 -04:00
|
|
|
} else if ("FLAGS".equalsIgnoreCase(action) || "FLAGS.SILENT".equalsIgnoreCase(action)) {
|
2009-02-24 09:26:48 -05:00
|
|
|
properties.put("read", "0");
|
|
|
|
message.read = false;
|
|
|
|
properties.put("flagged", "0");
|
|
|
|
message.flagged = false;
|
|
|
|
properties.put("junk", "0");
|
|
|
|
message.junk = false;
|
2009-09-23 18:18:07 -04:00
|
|
|
properties.put("deleted", null);
|
|
|
|
message.deleted = false;
|
2009-02-24 09:26:48 -05:00
|
|
|
StringTokenizer flagtokenizer = new StringTokenizer(flags);
|
|
|
|
while (flagtokenizer.hasMoreTokens()) {
|
|
|
|
String flag = flagtokenizer.nextToken();
|
|
|
|
if ("\\Seen".equals(flag)) {
|
|
|
|
properties.put("read", "1");
|
|
|
|
message.read = true;
|
|
|
|
} else if ("\\Deleted".equals(flag)) {
|
2009-02-03 18:54:48 -05:00
|
|
|
message.deleted = true;
|
2009-09-23 18:18:07 -04:00
|
|
|
properties.put("deleted", "deleted");
|
2009-02-03 18:54:48 -05:00
|
|
|
} else if ("\\Flagged".equals(flag)) {
|
|
|
|
properties.put("flagged", "2");
|
|
|
|
message.flagged = true;
|
|
|
|
} else if ("\\Answered".equals(flag)) {
|
2009-02-04 19:18:29 -05:00
|
|
|
properties.put("answered", "102");
|
2009-02-03 18:54:48 -05:00
|
|
|
message.answered = true;
|
2009-02-04 19:18:29 -05:00
|
|
|
} else if ("$Forwarded".equals(flag)) {
|
|
|
|
properties.put("forwarded", "104");
|
|
|
|
message.forwarded = true;
|
2009-02-03 18:54:48 -05:00
|
|
|
} else if ("Junk".equals(flag)) {
|
|
|
|
properties.put("junk", "1");
|
|
|
|
message.junk = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-16 17:16:40 -04:00
|
|
|
if (!properties.isEmpty()) {
|
2009-02-03 18:54:48 -05:00
|
|
|
session.updateMessage(message, properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
/**
|
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
|
2009-04-16 18:20:30 -04:00
|
|
|
* @throws 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 {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_CREDENTIALS");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-01-29 17:18:53 -05:00
|
|
|
password = tokens.nextToken();
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
2009-04-27 19:03:58 -04:00
|
|
|
throw new DavMailException("EXCEPTION_INVALID_CREDENTIALS");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
2009-04-16 17:16:40 -04:00
|
|
|
int backslashindex = userName.indexOf('\\');
|
2006-12-12 18:57:24 -05:00
|
|
|
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
|
|
|
/**
|
2009-02-25 12:12:02 -05:00
|
|
|
* Filter to output only headers, also count full size
|
2009-01-26 18:51:08 -05:00
|
|
|
*/
|
2009-09-21 17:34:13 -04:00
|
|
|
private static final class PartOutputStream extends FilterOutputStream {
|
|
|
|
private static final int START = 0;
|
|
|
|
private static final int CR = 1;
|
|
|
|
private static final int CRLF = 2;
|
|
|
|
private static final int CRLFCR = 3;
|
|
|
|
private static final int BODY = 4;
|
|
|
|
|
|
|
|
private int state = START;
|
|
|
|
private int size;
|
|
|
|
private final boolean writeHeaders;
|
|
|
|
private final boolean writeBody;
|
|
|
|
private final int startIndex;
|
2009-01-26 18:51:08 -05:00
|
|
|
|
2009-04-16 17:52:17 -04:00
|
|
|
private PartOutputStream(OutputStream os, boolean writeHeaders, boolean writeBody,
|
2009-05-15 08:39:15 -04:00
|
|
|
int startIndex) {
|
2009-01-26 18:51:08 -05:00
|
|
|
super(os);
|
2009-02-25 12:12:02 -05:00
|
|
|
this.writeHeaders = writeHeaders;
|
|
|
|
this.writeBody = writeBody;
|
2009-03-09 20:12:52 -04:00
|
|
|
this.startIndex = startIndex;
|
2009-01-26 18:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(int b) throws IOException {
|
|
|
|
size++;
|
2009-03-09 20:12:52 -04:00
|
|
|
if (((state != BODY && writeHeaders) || (state == BODY && writeBody)) &&
|
2009-03-09 20:20:20 -04:00
|
|
|
(size > startIndex)
|
2009-03-16 20:25:10 -04:00
|
|
|
) {
|
2009-01-26 18:51:08 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-20 12:06:03 -05:00
|
|
|
protected class UIDRangeIterator implements Iterator<ExchangeSession.Message> {
|
2009-02-24 05:22:55 -05:00
|
|
|
final String[] ranges;
|
2009-04-16 18:20:30 -04:00
|
|
|
int currentIndex;
|
|
|
|
int currentRangeIndex;
|
2009-02-04 16:55:42 -05:00
|
|
|
long startUid;
|
|
|
|
long endUid;
|
|
|
|
|
2009-02-20 12:06:03 -05:00
|
|
|
protected UIDRangeIterator(String value) {
|
2009-02-04 16:55:42 -05:00
|
|
|
ranges = value.split(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected long convertToLong(String value) {
|
|
|
|
if ("*".equals(value)) {
|
|
|
|
return Long.MAX_VALUE;
|
|
|
|
} else {
|
|
|
|
return Long.parseLong(value);
|
|
|
|
}
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
|
2009-09-07 07:42:07 -04:00
|
|
|
protected void skipToNextRangeStartUid() {
|
2009-02-04 16:55:42 -05:00
|
|
|
if (currentRangeIndex < ranges.length) {
|
|
|
|
String currentRange = ranges[currentRangeIndex++];
|
|
|
|
int colonIndex = currentRange.indexOf(':');
|
|
|
|
if (colonIndex > 0) {
|
|
|
|
startUid = convertToLong(currentRange.substring(0, colonIndex));
|
|
|
|
endUid = convertToLong(currentRange.substring(colonIndex + 1));
|
2009-09-07 07:42:07 -04:00
|
|
|
if (endUid < startUid) {
|
|
|
|
long swap = endUid;
|
|
|
|
endUid = startUid;
|
|
|
|
startUid = swap;
|
|
|
|
}
|
2009-02-04 16:55:42 -05:00
|
|
|
} else {
|
|
|
|
startUid = endUid = convertToLong(currentRange);
|
|
|
|
}
|
2009-07-31 02:21:46 -04:00
|
|
|
while (currentIndex < currentFolder.count() && currentFolder.getImapUid(currentIndex) < startUid) {
|
2009-02-04 16:55:42 -05:00
|
|
|
currentIndex++;
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-31 02:21:46 -04:00
|
|
|
currentIndex = currentFolder.count();
|
2009-02-04 16:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-07 07:42:07 -04:00
|
|
|
protected boolean hasNextInRange() {
|
|
|
|
return hasNextIndex() && currentFolder.getImapUid(currentIndex) <= endUid;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean hasNextIndex() {
|
|
|
|
return currentIndex < currentFolder.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean hasNextRange() {
|
|
|
|
return currentRangeIndex < ranges.length;
|
|
|
|
}
|
|
|
|
|
2009-02-04 16:55:42 -05:00
|
|
|
public boolean hasNext() {
|
2009-09-07 07:42:07 -04:00
|
|
|
boolean hasNextInRange = hasNextInRange();
|
|
|
|
// if has next range and current index after current range end, reset index
|
|
|
|
if (hasNextRange() && !hasNextInRange) {
|
|
|
|
currentIndex = 0;
|
2009-02-04 16:55:42 -05:00
|
|
|
}
|
2009-09-07 07:42:07 -04:00
|
|
|
while (hasNextIndex() && !hasNextInRange) {
|
|
|
|
skipToNextRangeStartUid();
|
|
|
|
hasNextInRange = hasNextInRange();
|
|
|
|
}
|
|
|
|
return hasNextIndex();
|
2009-02-04 16:55:42 -05:00
|
|
|
}
|
|
|
|
|
2009-02-20 12:06:03 -05:00
|
|
|
public ExchangeSession.Message next() {
|
2009-03-27 06:31:52 -04:00
|
|
|
ExchangeSession.Message message = currentFolder.get(currentIndex++);
|
2009-03-19 08:41:11 -04:00
|
|
|
long uid = message.getImapUid();
|
2009-03-03 10:49:42 -05:00
|
|
|
if (uid < startUid || uid > endUid) {
|
2009-04-23 16:53:22 -04:00
|
|
|
throw new RuntimeException("Message uid " + uid + " not in range " + startUid + ':' + endUid);
|
2009-03-03 10:49:42 -05:00
|
|
|
}
|
|
|
|
return message;
|
2009-02-20 12:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class RangeIterator implements Iterator<ExchangeSession.Message> {
|
2009-02-24 05:22:55 -05:00
|
|
|
final String[] ranges;
|
2009-04-16 18:20:30 -04:00
|
|
|
int currentIndex;
|
|
|
|
int currentRangeIndex;
|
2009-02-20 12:06:03 -05:00
|
|
|
long startUid;
|
|
|
|
long endUid;
|
|
|
|
|
|
|
|
protected RangeIterator(String value) {
|
|
|
|
ranges = value.split(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
protected long convertToLong(String value) {
|
|
|
|
if ("*".equals(value)) {
|
|
|
|
return Long.MAX_VALUE;
|
|
|
|
} else {
|
|
|
|
return Long.parseLong(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-07 07:42:07 -04:00
|
|
|
protected void skipToNextRangeStart() {
|
2009-02-20 12:06:03 -05:00
|
|
|
if (currentRangeIndex < ranges.length) {
|
|
|
|
String currentRange = ranges[currentRangeIndex++];
|
|
|
|
int colonIndex = currentRange.indexOf(':');
|
|
|
|
if (colonIndex > 0) {
|
|
|
|
startUid = convertToLong(currentRange.substring(0, colonIndex));
|
|
|
|
endUid = convertToLong(currentRange.substring(colonIndex + 1));
|
2009-09-07 07:42:07 -04:00
|
|
|
if (endUid < startUid) {
|
|
|
|
long swap = endUid;
|
|
|
|
endUid = startUid;
|
|
|
|
startUid = swap;
|
|
|
|
}
|
2009-02-20 12:06:03 -05:00
|
|
|
} else {
|
|
|
|
startUid = endUid = convertToLong(currentRange);
|
|
|
|
}
|
2009-07-31 02:21:46 -04:00
|
|
|
while (currentIndex < currentFolder.count() && (currentIndex + 1) < startUid) {
|
2009-02-20 12:06:03 -05:00
|
|
|
currentIndex++;
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-31 02:21:46 -04:00
|
|
|
currentIndex = currentFolder.count();
|
2009-02-20 12:06:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-07 07:42:07 -04:00
|
|
|
protected boolean hasNextInRange() {
|
|
|
|
return hasNextIndex() && currentIndex < endUid;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean hasNextIndex() {
|
|
|
|
return currentIndex < currentFolder.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean hasNextRange() {
|
|
|
|
return currentRangeIndex < ranges.length;
|
|
|
|
}
|
|
|
|
|
2009-02-20 12:06:03 -05:00
|
|
|
public boolean hasNext() {
|
2009-09-07 07:42:07 -04:00
|
|
|
boolean hasNextInRange = hasNextInRange();
|
|
|
|
// if has next range and current index after current range end, reset index
|
|
|
|
if (hasNextRange() && !hasNextInRange) {
|
|
|
|
currentIndex = 0;
|
2009-02-20 12:06:03 -05:00
|
|
|
}
|
2009-09-07 07:42:07 -04:00
|
|
|
while (hasNextIndex() && !hasNextInRange) {
|
|
|
|
skipToNextRangeStart();
|
|
|
|
hasNextInRange = hasNextInRange();
|
|
|
|
}
|
|
|
|
return hasNextIndex();
|
2009-02-20 12:06:03 -05:00
|
|
|
}
|
|
|
|
|
2009-02-04 16:55:42 -05:00
|
|
|
public ExchangeSession.Message next() {
|
2009-03-27 06:31:52 -04:00
|
|
|
return currentFolder.get(currentIndex++);
|
2009-02-04 16:55:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
}
|
2009-02-17 17:59:32 -05:00
|
|
|
|
|
|
|
class IMAPTokenizer extends StringTokenizer {
|
2009-04-16 17:52:17 -04:00
|
|
|
IMAPTokenizer(String value) {
|
2009-02-17 17:59:32 -05:00
|
|
|
super(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String nextToken() {
|
2009-03-16 20:25:10 -04:00
|
|
|
return removeQuotes(nextQuotedToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
public String nextQuotedToken() {
|
2009-02-17 17:59:32 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
while (hasMoreTokens() && nextToken.length() > 0 && nextToken.charAt(0) == '('
|
|
|
|
&& nextToken.charAt(nextToken.length() - 1) != ')') {
|
|
|
|
nextToken.append(' ').append(super.nextToken());
|
|
|
|
}
|
2009-03-16 20:25:10 -04:00
|
|
|
return nextToken.toString();
|
2009-02-17 17:59:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-02 10:38:02 -05:00
|
|
|
}
|