Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ac970f2 authored by Hari's avatar Hari
Browse files

Add SelectByIdCommand, refactor and add tests

parent 184d02ff
Loading
Loading
Loading
Loading
+20 −26
Original line number Diff line number Diff line
@@ -5,13 +5,13 @@ package com.fsck.k9.mail;
 * Flags that can be applied to Messages.
 */
public enum Flag {
    DELETED("DELETED", "UNDELETED"),
    SEEN("SEEN", "UNSEEN"),
    ANSWERED("ANSWERED", "UNANSWERED"),
    FLAGGED("FLAGGED", "UNFLAGGED"),
    DRAFT("DRAFT", "UNDRAFT"),
    RECENT("RECENT", "UNRECENT"),
    FORWARDED(null, null),
    DELETED("DELETED"),
    SEEN("SEEN"),
    ANSWERED("ANSWERED"),
    FLAGGED("FLAGGED"),
    DRAFT("DRAFT"),
    RECENT("RECENT"),
    FORWARDED(null),

    /*
     * The following flags are for internal library use only.
@@ -19,35 +19,35 @@ public enum Flag {
    /**
     * Delete and remove from the LocalStore immediately.
     */
    X_DESTROYED(null, null),
    X_DESTROYED(null),

    /**
     * Sending of an unsent message failed. It will be retried. Used to show status.
     */
    X_SEND_FAILED(null, null),
    X_SEND_FAILED(null),

    /**
     * Sending of an unsent message is in progress.
     */
    X_SEND_IN_PROGRESS(null, null),
    X_SEND_IN_PROGRESS(null),

    /**
     * Indicates that a message is fully downloaded from the server and can be viewed normally.
     * This does not include attachments, which are never downloaded fully.
     */
    X_DOWNLOADED_FULL(null, null),
    X_DOWNLOADED_FULL(null),

    /**
     * Indicates that a message is partially downloaded from the server and can be viewed but
     * more content is available on the server.
     * This does not include attachments, which are never downloaded fully.
     */
    X_DOWNLOADED_PARTIAL(null, null),
    X_DOWNLOADED_PARTIAL(null),

    /**
     * Indicates that the copy of a message to the Sent folder has started.
     */
    X_REMOTE_COPY_STARTED(null, null),
    X_REMOTE_COPY_STARTED(null),

    /**
     * Messages with this flag have been migrated from database version 50 or earlier.
@@ -56,27 +56,21 @@ public enum Flag {
     * incomplete or broken.
     * TODO Messages with this flag should be redownloaded, if possible.
     */
    X_MIGRATED_FROM_V50(null, null),
    X_MIGRATED_FROM_V50(null),

    /**
     * This flag is used for drafts where the message should be sent as PGP/INLINE.
     */
    X_DRAFT_OPENPGP_INLINE(null, null);
    X_DRAFT_OPENPGP_INLINE(null);

    private String requiredImapString;
    private String forbiddenImapString;
    private String imapString;

    Flag(String requiredImapString, String forbiddenImapString) {
        this.requiredImapString = requiredImapString;
        this.forbiddenImapString = forbiddenImapString;
    Flag(String imapString) {
        this.imapString = imapString;
    }

    public String getRequiredImapString() {
        return requiredImapString;
    }

    public String getForbiddenImapString() {
        return forbiddenImapString;
    public String getImapString() {
        return imapString;
    }

}
+2 −1
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap;


class Commands {
public class Commands {
    public static final String IDLE = "IDLE";
    public static final String NAMESPACE = "NAMESPACE";
    public static final String CAPABILITY = "CAPABILITY";
@@ -14,4 +14,5 @@ class Commands {
    public static final String LOGIN = "LOGIN";
    public static final String LIST = "LIST";
    public static final String NOOP = "NOOP";
    public static final String UID_SEARCH = "UID SEARCH";
}
+4 −46
Original line number Diff line number Diff line
@@ -50,7 +50,6 @@ import javax.net.ssl.SSLException;
import org.apache.commons.io.IOUtils;
import timber.log.Timber;

import static android.R.attr.tag;
import static com.fsck.k9.mail.ConnectionSecurity.STARTTLS_REQUIRED;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_IMAP;
import static com.fsck.k9.mail.store.RemoteStore.SOCKET_CONNECT_TIMEOUT;
@@ -723,7 +722,7 @@ public class ImapConnection {
        return "conn" + hashCode();
    }

    public List<ImapResponse> executeSimpleCommand(String command) throws IOException, MessagingException {
    List<ImapResponse> executeSimpleCommand(String command) throws IOException, MessagingException {
        return executeSimpleCommand(command, false);
    }

@@ -745,30 +744,12 @@ public class ImapConnection {
        }
    }

    public List<ImapResponse> executeSimpleCommandNew(String command, boolean sensitive) throws IOException,
            MessagingException {
        String commandToLog = command;

        if (sensitive && !K9MailLib.isDebugSensitive()) {
            commandToLog = "*sensitive*";
        }

        String tag = sendCommandNew(command, sensitive);

        try {
            return responseParser.readStatusResponse(tag, commandToLog, getLogId(), null);
        } catch (IOException e) {
            close();
            throw e;
        }
    }

    public List<ImapResponse> readStatusResponse(String tag, String commandToLog, UntaggedHandler untaggedHandler)
    List<ImapResponse> readStatusResponse(String tag, String commandToLog, UntaggedHandler untaggedHandler)
            throws IOException, NegativeImapResponseException {
        return responseParser.readStatusResponse(tag, commandToLog, getLogId(), untaggedHandler);
    }

    public String sendSaslIrCommand(String command, String initialClientResponse, boolean sensitive)
    String sendSaslIrCommand(String command, String initialClientResponse, boolean sensitive)
            throws IOException, MessagingException {
        try {
            open();
@@ -793,7 +774,7 @@ public class ImapConnection {
        }
    }

    public String sendCommand(String command, boolean sensitive) throws MessagingException, IOException {
    String sendCommand(String command, boolean sensitive) throws MessagingException, IOException {
        try {
            open();

@@ -817,29 +798,6 @@ public class ImapConnection {
        }
    }

    public String sendCommandNew(String command, boolean sensitive) throws MessagingException, IOException {
        try {
            open();

            String commandToSend = command + "\r\n";
            outputStream.write(commandToSend.getBytes());
            outputStream.flush();

            if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) {
                if (sensitive && !K9MailLib.isDebugSensitive()) {
                    Timber.v("%s>>> [Command Hidden, Enable Sensitive Debug Logging To Show]", getLogId());
                } else {
                    Timber.v("%s>>> %s %s", getLogId(), tag, command);
                }
            }

            return command.split(" ")[0];
        } catch (IOException | MessagingException e) {
            close();
            throw e;
        }
    }

    public void sendContinuation(String continuation) throws IOException {
        outputStream.write(continuation.getBytes());
        outputStream.write('\r');
+3 −0
Original line number Diff line number Diff line
@@ -521,6 +521,7 @@ public class ImapFolder extends Folder<ImapMessage> {
            throw new MessagingException(String.format(Locale.US, "Invalid message set %d %d", start, end));
        }

        checkOpen();
        UidSearchCommand searchCommand = commandFactory.createUidSearchCommandBuilder(this, listener)
                .useUids(false)
                .addIdRange((long) start, (long) end)
@@ -573,6 +574,7 @@ public class ImapFolder extends Folder<ImapMessage> {
    protected List<ImapMessage> getMessages(final List<Long> mesgSeqs, final boolean includeDeleted,
            final MessageRetrievalListener<ImapMessage> listener) throws MessagingException {

        checkOpen();
        UidSearchCommand searchCommand = commandFactory.createUidSearchCommandBuilder(this, listener)
                .useUids(false)
                .idSet(mesgSeqs)
@@ -585,6 +587,7 @@ public class ImapFolder extends Folder<ImapMessage> {

    protected List<ImapMessage> getMessagesFromUids(final List<String> mesgUids) throws MessagingException {

        checkOpen();
        Set<Long> uidSet = new HashSet<>();
        for (String uid : mesgUids) {
            uidSet.add(Long.parseLong(uid));
+7 −8
Original line number Diff line number Diff line
@@ -13,20 +13,18 @@ import com.fsck.k9.mail.store.imap.response.BaseResponse;

abstract class BaseCommand {

    private static final int LENGTH_LIMIT_WITHOUT_CONDSTORE = 990;
    private static final int LENGTH_LIMIT_WITH_CONDSTORE = 8182;
    private static final int LENGTH_LIMIT_WITHOUT_CONDSTORE = 970;
    private static final int LENGTH_LIMIT_WITH_CONDSTORE = 8162;

    int tag;
    ImapCommandFactory commandFactory;

    BaseCommand(int tag, ImapCommandFactory commandFactory) {
        this.tag = tag;
    BaseCommand(ImapCommandFactory commandFactory) {
        this.commandFactory = commandFactory;
    }

    public abstract String createCommandString();
    abstract String createCommandString();

    public abstract List<? extends BaseCommand> splitCommand(int lengthLimit);
    abstract List<? extends BaseCommand> splitCommand(int lengthLimit);

    List<List<ImapResponse>> executeInternal(boolean sensitive) throws IOException, MessagingException {

@@ -41,13 +39,14 @@ abstract class BaseCommand {
        ImapConnection connection = commandFactory.getConnection();
        List<List<ImapResponse>> responses = new ArrayList<>();
        for (BaseCommand command : commands) {
            responses.add(connection.executeSimpleCommandNew(command.createCommandString(), sensitive));
            responses.add(connection.executeSimpleCommand(command.createCommandString(), sensitive));
        }
        return responses;
    }

    public abstract BaseResponse execute() throws MessagingException;


    private int getCommandLengthLimit() throws IOException, MessagingException  {
        boolean condstoreSupported = commandFactory.getConnection().isCondstoreCapable();
        if (condstoreSupported) {
Loading