Search in sources :

Example 21 with MessagingException

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

the class MessageDecryptVerifierTest method multipart.

MimeBodyPart multipart(String type, BodyPart... subParts) throws MessagingException {
    MimeMultipart multiPart = MimeMultipart.newInstance();
    multiPart.setSubType(type);
    for (BodyPart subPart : subParts) {
        multiPart.addBodyPart(subPart);
    }
    return new MimeBodyPart(multiPart);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 22 with MessagingException

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

the class MessagingController method refreshLocalMessageFlags.

private void refreshLocalMessageFlags(final Account account, final Folder remoteFolder, final LocalFolder localFolder, List<Message> syncFlagMessages, final AtomicInteger progress, final int todo) throws MessagingException {
    final String folder = remoteFolder.getName();
    if (remoteFolder.supportsFetchingFlags()) {
        Timber.d("SYNC: About to sync flags for %d remote messages for folder %s", syncFlagMessages.size(), folder);
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.FLAGS);
        List<Message> undeletedMessages = new LinkedList<>();
        for (Message message : syncFlagMessages) {
            if (!message.isSet(Flag.DELETED)) {
                undeletedMessages.add(message);
            }
        }
        remoteFolder.fetch(undeletedMessages, fp, null);
        for (Message remoteMessage : syncFlagMessages) {
            LocalMessage localMessage = localFolder.getMessage(remoteMessage.getUid());
            boolean messageChanged = syncFlags(localMessage, remoteMessage);
            if (messageChanged) {
                boolean shouldBeNotifiedOf = false;
                if (localMessage.isSet(Flag.DELETED) || isMessageSuppressed(localMessage)) {
                    for (MessagingListener l : getListeners()) {
                        l.synchronizeMailboxRemovedMessage(account, folder, localMessage);
                    }
                } else {
                    if (shouldNotifyForMessage(account, localFolder, localMessage)) {
                        shouldBeNotifiedOf = true;
                    }
                }
                // we're only interested in messages that need removing
                if (!shouldBeNotifiedOf) {
                    MessageReference messageReference = localMessage.makeMessageReference();
                    notificationController.removeNewMailNotification(account, messageReference);
                }
            }
            progress.incrementAndGet();
            for (MessagingListener l : getListeners()) {
                l.synchronizeMailboxProgress(account, folder, progress.get(), todo);
            }
        }
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) FetchProfile(com.fsck.k9.mail.FetchProfile) LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessageReference(com.fsck.k9.activity.MessageReference) LinkedList(java.util.LinkedList)

Example 23 with MessagingException

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

the class MessagingController method processPendingMoveOrCopy.

void processPendingMoveOrCopy(PendingMoveOrCopy command, Account account) throws MessagingException {
    Folder remoteSrcFolder = null;
    Folder remoteDestFolder = null;
    LocalFolder localDestFolder;
    try {
        String srcFolder = command.srcFolder;
        if (account.getErrorFolderName().equals(srcFolder)) {
            return;
        }
        String destFolder = command.destFolder;
        boolean isCopy = command.isCopy;
        Store remoteStore = account.getRemoteStore();
        remoteSrcFolder = remoteStore.getFolder(srcFolder);
        Store localStore = account.getLocalStore();
        localDestFolder = (LocalFolder) localStore.getFolder(destFolder);
        List<Message> messages = new ArrayList<>();
        Collection<String> uids = command.newUidMap != null ? command.newUidMap.keySet() : command.uids;
        for (String uid : uids) {
            if (!uid.startsWith(K9.LOCAL_UID_PREFIX)) {
                messages.add(remoteSrcFolder.getMessage(uid));
            }
        }
        if (!remoteSrcFolder.exists()) {
            throw new MessagingException("processingPendingMoveOrCopy: remoteFolder " + srcFolder + " does not exist", true);
        }
        remoteSrcFolder.open(Folder.OPEN_MODE_RW);
        if (remoteSrcFolder.getMode() != Folder.OPEN_MODE_RW) {
            throw new MessagingException("processingPendingMoveOrCopy: could not open remoteSrcFolder " + srcFolder + " read/write", true);
        }
        Timber.d("processingPendingMoveOrCopy: source folder = %s, %d messages, destination folder = %s, " + "isCopy = %s", srcFolder, messages.size(), destFolder, isCopy);
        Map<String, String> remoteUidMap = null;
        if (!isCopy && destFolder.equals(account.getTrashFolderName())) {
            Timber.d("processingPendingMoveOrCopy doing special case for deleting message");
            String destFolderName = destFolder;
            if (K9.FOLDER_NONE.equals(destFolderName)) {
                destFolderName = null;
            }
            remoteSrcFolder.delete(messages, destFolderName);
        } else {
            remoteDestFolder = remoteStore.getFolder(destFolder);
            if (isCopy) {
                remoteUidMap = remoteSrcFolder.copyMessages(messages, remoteDestFolder);
            } else {
                remoteUidMap = remoteSrcFolder.moveMessages(messages, remoteDestFolder);
            }
        }
        if (!isCopy && Expunge.EXPUNGE_IMMEDIATELY == account.getExpungePolicy()) {
            Timber.i("processingPendingMoveOrCopy expunging folder %s:%s", account.getDescription(), srcFolder);
            remoteSrcFolder.expunge();
        }
        /*
             * This next part is used to bring the local UIDs of the local destination folder
             * upto speed with the remote UIDs of remote destination folder.
             */
        if (command.newUidMap != null && remoteUidMap != null && !remoteUidMap.isEmpty()) {
            for (Map.Entry<String, String> entry : remoteUidMap.entrySet()) {
                String remoteSrcUid = entry.getKey();
                String newUid = entry.getValue();
                String localDestUid = command.newUidMap.get(remoteSrcUid);
                if (localDestUid == null) {
                    continue;
                }
                Message localDestMessage = localDestFolder.getMessage(localDestUid);
                if (localDestMessage != null) {
                    localDestMessage.setUid(newUid);
                    localDestFolder.changeUid((LocalMessage) localDestMessage);
                    for (MessagingListener l : getListeners()) {
                        l.messageUidChanged(account, destFolder, localDestUid, newUid);
                    }
                }
            }
        }
    } finally {
        closeFolder(remoteSrcFolder);
        closeFolder(remoteDestFolder);
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) ArrayList(java.util.ArrayList) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 24 with MessagingException

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

the class MessagingController method isCopyCapable.

public boolean isCopyCapable(final Account account) {
    try {
        Store localStore = account.getLocalStore();
        Store remoteStore = account.getRemoteStore();
        return localStore.isCopyCapable() && remoteStore.isCopyCapable();
    } catch (MessagingException me) {
        Timber.e(me, "Exception while ascertaining copy capability");
        return false;
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store)

Example 25 with MessagingException

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

the class MessagingController method deleteMessagesSynchronous.

private void deleteMessagesSynchronous(final Account account, final String folder, final List<? extends Message> messages, MessagingListener listener) {
    Folder localFolder = null;
    Folder localTrashFolder = null;
    List<String> uids = getUidsFromMessages(messages);
    try {
        //as messages get a new UID after being moved
        for (Message message : messages) {
            for (MessagingListener l : getListeners(listener)) {
                l.messageDeleted(account, folder, message);
            }
        }
        Store localStore = account.getLocalStore();
        localFolder = localStore.getFolder(folder);
        Map<String, String> uidMap = null;
        if (folder.equals(account.getTrashFolderName()) || !account.hasTrashFolder()) {
            Timber.d("Deleting messages in trash folder or trash set to -None-, not copying");
            localFolder.setFlags(messages, Collections.singleton(Flag.DELETED), true);
        } else {
            localTrashFolder = localStore.getFolder(account.getTrashFolderName());
            if (!localTrashFolder.exists()) {
                localTrashFolder.create(Folder.FolderType.HOLDS_MESSAGES);
            }
            if (localTrashFolder.exists()) {
                Timber.d("Deleting messages in normal folder, moving");
                uidMap = localFolder.moveMessages(messages, localTrashFolder);
            }
        }
        for (MessagingListener l : getListeners()) {
            l.folderStatusChanged(account, folder, localFolder.getUnreadMessageCount());
            if (localTrashFolder != null) {
                l.folderStatusChanged(account, account.getTrashFolderName(), localTrashFolder.getUnreadMessageCount());
            }
        }
        Timber.d("Delete policy for account %s is %s", account.getDescription(), account.getDeletePolicy());
        if (folder.equals(account.getOutboxFolderName())) {
            for (Message message : messages) {
                // If the message was in the Outbox, then it has been copied to local Trash, and has
                // to be copied to remote trash
                PendingCommand command = PendingAppend.create(account.getTrashFolderName(), message.getUid());
                queuePendingCommand(account, command);
            }
            processPendingCommands(account);
        } else if (account.getDeletePolicy() == DeletePolicy.ON_DELETE) {
            if (folder.equals(account.getTrashFolderName())) {
                queueSetFlag(account, folder, true, Flag.DELETED, uids);
            } else {
                queueMoveOrCopy(account, folder, account.getTrashFolderName(), false, uids, uidMap);
            }
            processPendingCommands(account);
        } else if (account.getDeletePolicy() == DeletePolicy.MARK_AS_READ) {
            queueSetFlag(account, folder, true, Flag.SEEN, uids);
            processPendingCommands(account);
        } else {
            Timber.d("Delete policy %s prevents delete from server", account.getDeletePolicy());
        }
        unsuppressMessages(account, messages);
    } catch (UnavailableStorageException e) {
        Timber.i("Failed to delete message because storage is not available - trying again later.");
        throw new UnavailableAccountException(e);
    } catch (MessagingException me) {
        addErrorMessage(account, null, me);
        throw new RuntimeException("Error deleting message from local store.", me);
    } finally {
        closeFolder(localFolder);
        closeFolder(localTrashFolder);
    }
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)159 Test (org.junit.Test)73 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)52 LocalFolder (com.fsck.k9.mailstore.LocalFolder)49 LocalStore (com.fsck.k9.mailstore.LocalStore)49 ArrayList (java.util.ArrayList)49 Message (com.fsck.k9.mail.Message)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)42 IOException (java.io.IOException)42 FetchProfile (com.fsck.k9.mail.FetchProfile)30 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)26 BodyPart (com.fsck.k9.mail.BodyPart)23 Part (com.fsck.k9.mail.Part)22 Account (com.fsck.k9.Account)21 Body (com.fsck.k9.mail.Body)21 TextBody (com.fsck.k9.mail.internet.TextBody)21 Date (java.util.Date)20 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)18