Search in sources :

Example 26 with Flag

use of com.fsck.k9.mail.Flag in project k-9 by k9mail.

the class ImapFolder method search.

/**
     * Search the remote ImapFolder.
     * @param queryString String to query for.
     * @param requiredFlags Mandatory flags
     * @param forbiddenFlags Flags to exclude
     * @return List of messages found
     * @throws MessagingException On any error.
     */
@Override
public List<ImapMessage> search(final String queryString, final Set<Flag> requiredFlags, final Set<Flag> forbiddenFlags) throws MessagingException {
    if (!store.getStoreConfig().allowRemoteSearch()) {
        throw new MessagingException("Your settings do not allow remote searching of this account");
    }
    // Setup the searcher
    final ImapSearcher searcher = new ImapSearcher() {

        @Override
        public List<ImapResponse> search() throws IOException, MessagingException {
            String imapQuery = "UID SEARCH ";
            if (requiredFlags != null) {
                for (Flag flag : requiredFlags) {
                    switch(flag) {
                        case DELETED:
                            {
                                imapQuery += "DELETED ";
                                break;
                            }
                        case SEEN:
                            {
                                imapQuery += "SEEN ";
                                break;
                            }
                        case ANSWERED:
                            {
                                imapQuery += "ANSWERED ";
                                break;
                            }
                        case FLAGGED:
                            {
                                imapQuery += "FLAGGED ";
                                break;
                            }
                        case DRAFT:
                            {
                                imapQuery += "DRAFT ";
                                break;
                            }
                        case RECENT:
                            {
                                imapQuery += "RECENT ";
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                }
            }
            if (forbiddenFlags != null) {
                for (Flag flag : forbiddenFlags) {
                    switch(flag) {
                        case DELETED:
                            {
                                imapQuery += "UNDELETED ";
                                break;
                            }
                        case SEEN:
                            {
                                imapQuery += "UNSEEN ";
                                break;
                            }
                        case ANSWERED:
                            {
                                imapQuery += "UNANSWERED ";
                                break;
                            }
                        case FLAGGED:
                            {
                                imapQuery += "UNFLAGGED ";
                                break;
                            }
                        case DRAFT:
                            {
                                imapQuery += "UNDRAFT ";
                                break;
                            }
                        case RECENT:
                            {
                                imapQuery += "UNRECENT ";
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                }
            }
            String encodedQuery = ImapUtility.encodeString(queryString);
            if (store.getStoreConfig().isRemoteSearchFullText()) {
                imapQuery += "TEXT " + encodedQuery;
            } else {
                imapQuery += "OR SUBJECT " + encodedQuery + " FROM " + encodedQuery;
            }
            return executeSimpleCommand(imapQuery);
        }
    };
    try {
        open(OPEN_MODE_RO);
        checkOpen();
        inSearch = true;
        return search(searcher, null);
    } finally {
        inSearch = false;
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Flag(com.fsck.k9.mail.Flag)

Example 27 with Flag

use of com.fsck.k9.mail.Flag in project k-9 by k9mail.

the class ImapFolder method handleFetchResponse.

// Returns value of body field
private Object handleFetchResponse(ImapMessage message, ImapList fetchList) throws MessagingException {
    Object result = null;
    if (fetchList.containsKey("FLAGS")) {
        ImapList flags = fetchList.getKeyedList("FLAGS");
        if (flags != null) {
            for (int i = 0, count = flags.size(); i < count; i++) {
                String flag = flags.getString(i);
                if (flag.equalsIgnoreCase("\\Deleted")) {
                    message.setFlagInternal(Flag.DELETED, true);
                } else if (flag.equalsIgnoreCase("\\Answered")) {
                    message.setFlagInternal(Flag.ANSWERED, true);
                } else if (flag.equalsIgnoreCase("\\Seen")) {
                    message.setFlagInternal(Flag.SEEN, true);
                } else if (flag.equalsIgnoreCase("\\Flagged")) {
                    message.setFlagInternal(Flag.FLAGGED, true);
                } else if (flag.equalsIgnoreCase("$Forwarded")) {
                    message.setFlagInternal(Flag.FORWARDED, true);
                    /* a message contains FORWARDED FLAG -> so we can also create them */
                    store.getPermanentFlagsIndex().add(Flag.FORWARDED);
                }
            }
        }
    }
    if (fetchList.containsKey("INTERNALDATE")) {
        Date internalDate = fetchList.getKeyedDate("INTERNALDATE");
        message.setInternalDate(internalDate);
    }
    if (fetchList.containsKey("RFC822.SIZE")) {
        int size = fetchList.getKeyedNumber("RFC822.SIZE");
        message.setSize(size);
    }
    if (fetchList.containsKey("BODYSTRUCTURE")) {
        ImapList bs = fetchList.getKeyedList("BODYSTRUCTURE");
        if (bs != null) {
            try {
                parseBodyStructure(bs, message, "TEXT");
            } catch (MessagingException e) {
                if (K9MailLib.isDebug()) {
                    Log.d(LOG_TAG, "Error handling message for " + getLogId(), e);
                }
                message.setBody(null);
            }
        }
    }
    if (fetchList.containsKey("BODY")) {
        int index = fetchList.getKeyIndex("BODY") + 2;
        int size = fetchList.size();
        if (index < size) {
            result = fetchList.getObject(index);
            // Check if there's an origin octet
            if (result instanceof String) {
                String originOctet = (String) result;
                if (originOctet.startsWith("<") && (index + 1) < size) {
                    result = fetchList.getObject(index + 1);
                }
            }
        }
    }
    return result;
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Date(java.util.Date)

Example 28 with Flag

use of com.fsck.k9.mail.Flag in project k-9 by k9mail.

the class MigrationTo51 method db51MigrateMessageFormat.

/**
     * This method converts from the old message table structure to the new one.
     *
     * This is a complex migration, and ultimately we do not have enough
     * information to recreate the mime structure of the original mails.
     * What we have:
     *  - general mail info
     *  - html_content and text_content data, which is the squashed readable content of the mail
     *  - a table with message headers
     *  - attachments
     *
     * What we need to do:
     *  - migrate general mail info as-is
     *  - flag mails as migrated for re-download
     *  - for each message, recreate a mime structure from its message content and attachments:
     *    + insert one or both of textContent and htmlContent, depending on mimeType
     *    + if mimeType is text/plain, text/html or multipart/alternative and no
     *      attachments are present, just insert that.
     *    + otherwise, use multipart/mixed, adding attachments after textual content
     *    + revert content:// URIs in htmlContent to original cid: URIs.
     */
public static void db51MigrateMessageFormat(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    renameOldMessagesTableAndCreateNew(db);
    copyMessageMetadataToNewTable(db);
    File attachmentDirNew, attachmentDirOld;
    Account account = migrationsHelper.getAccount();
    attachmentDirNew = StorageManager.getInstance(K9.app).getAttachmentDirectory(account.getUuid(), account.getLocalStorageProviderId());
    attachmentDirOld = renameOldAttachmentDirAndCreateNew(account, attachmentDirNew);
    Cursor msgCursor = db.query("messages_old", new String[] { "id", "flags", "html_content", "text_content", "mime_type", "attachment_count" }, null, null, null, null, null);
    try {
        Timber.d("migrating %d messages", msgCursor.getCount());
        ContentValues cv = new ContentValues();
        while (msgCursor.moveToNext()) {
            long messageId = msgCursor.getLong(0);
            String messageFlags = msgCursor.getString(1);
            String htmlContent = msgCursor.getString(2);
            String textContent = msgCursor.getString(3);
            String mimeType = msgCursor.getString(4);
            int attachmentCount = msgCursor.getInt(5);
            try {
                updateFlagsForMessage(db, messageId, messageFlags, migrationsHelper);
                MimeHeader mimeHeader = loadHeaderFromHeadersTable(db, messageId);
                MimeStructureState structureState = MimeStructureState.getNewRootState();
                boolean messageHadSpecialFormat = false;
                // we do not rely on the protocol parameter here but guess by the multipart structure
                boolean isMaybePgpMimeEncrypted = attachmentCount == 2 && MimeUtil.isSameMimeType(mimeType, "multipart/encrypted");
                if (isMaybePgpMimeEncrypted) {
                    MimeStructureState maybeStructureState = migratePgpMimeEncryptedContent(db, messageId, attachmentDirOld, attachmentDirNew, mimeHeader, structureState);
                    if (maybeStructureState != null) {
                        structureState = maybeStructureState;
                        messageHadSpecialFormat = true;
                    }
                }
                if (!messageHadSpecialFormat) {
                    boolean isSimpleStructured = attachmentCount == 0 && Utility.isAnyMimeType(mimeType, "text/plain", "text/html", "multipart/alternative");
                    if (isSimpleStructured) {
                        structureState = migrateSimpleMailContent(db, htmlContent, textContent, mimeType, mimeHeader, structureState);
                    } else {
                        mimeType = "multipart/mixed";
                        structureState = migrateComplexMailContent(db, attachmentDirOld, attachmentDirNew, messageId, htmlContent, textContent, mimeHeader, structureState);
                    }
                }
                cv.clear();
                cv.put("mime_type", mimeType);
                cv.put("message_part_id", structureState.rootPartId);
                cv.put("attachment_count", attachmentCount);
                db.update("messages", cv, "id = ?", new String[] { Long.toString(messageId) });
            } catch (IOException e) {
                Timber.e(e, "error inserting into database");
            }
        }
    } finally {
        msgCursor.close();
    }
    cleanUpOldAttachmentDirectory(attachmentDirOld);
    dropOldMessagesTable(db);
}
Also used : ContentValues(android.content.ContentValues) Account(com.fsck.k9.Account) MimeHeader(com.fsck.k9.mail.internet.MimeHeader) IOException(java.io.IOException) Cursor(android.database.Cursor) File(java.io.File)

Example 29 with Flag

use of com.fsck.k9.mail.Flag in project k-9 by k9mail.

the class MigrationTo60 method migrateCommandSetFlagBulk.

private static PendingCommand migrateCommandSetFlagBulk(OldPendingCommand command) {
    String folder = command.arguments[0];
    boolean newState = Boolean.parseBoolean(command.arguments[1]);
    Flag flag = Flag.valueOf(command.arguments[2]);
    List<String> uids = new ArrayList<>(command.arguments.length - 3);
    uids.addAll(Arrays.asList(command.arguments).subList(3, command.arguments.length));
    return PendingSetFlag.create(folder, newState, flag, uids);
}
Also used : ArrayList(java.util.ArrayList) PendingSetFlag(com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag) Flag(com.fsck.k9.mail.Flag)

Example 30 with Flag

use of com.fsck.k9.mail.Flag in project k-9 by k9mail.

the class MigrationTo60 method migrateCommandSetFlag.

private static PendingCommand migrateCommandSetFlag(OldPendingCommand command) {
    String folder = command.arguments[0];
    String uid = command.arguments[1];
    boolean newState = Boolean.parseBoolean(command.arguments[2]);
    Flag flag = Flag.valueOf(command.arguments[3]);
    return PendingSetFlag.create(folder, newState, flag, singletonList(uid));
}
Also used : PendingSetFlag(com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag) Flag(com.fsck.k9.mail.Flag)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)10 Message (com.fsck.k9.mail.Message)9 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)9 LocalMessage (com.fsck.k9.mailstore.LocalMessage)9 Flag (com.fsck.k9.mail.Flag)8 LocalFolder (com.fsck.k9.mailstore.LocalFolder)8 LocalStore (com.fsck.k9.mailstore.LocalStore)8 ArrayList (java.util.ArrayList)8 Folder (com.fsck.k9.mail.Folder)7 PendingSetFlag (com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag)6 Store (com.fsck.k9.mail.Store)6 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)6 Account (com.fsck.k9.Account)5 IOException (java.io.IOException)5 SuppressLint (android.annotation.SuppressLint)4 Cursor (android.database.Cursor)4 EmailProviderCache (com.fsck.k9.cache.EmailProviderCache)4 FetchProfile (com.fsck.k9.mail.FetchProfile)4 OldPendingCommand (com.fsck.k9.mailstore.migrations.MigrationTo60.OldPendingCommand)4 Date (java.util.Date)4