2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.mail.store;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2010-03-06 19:30:40 -05:00
|
|
|
import android.content.Context;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
2010-05-16 20:30:32 -04:00
|
|
|
import android.os.PowerManager;
|
2008-11-01 17:32:06 -04:00
|
|
|
import android.util.Log;
|
2010-03-03 23:00:30 -05:00
|
|
|
import com.fsck.k9.Account;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.PeekableInputStream;
|
|
|
|
import com.fsck.k9.Utility;
|
2010-05-16 20:30:32 -04:00
|
|
|
import com.fsck.k9.helper.power.TracingPowerManager;
|
|
|
|
import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.*;
|
|
|
|
import com.fsck.k9.mail.internet.*;
|
|
|
|
import com.fsck.k9.mail.store.ImapResponseParser.ImapList;
|
|
|
|
import com.fsck.k9.mail.store.ImapResponseParser.ImapResponse;
|
|
|
|
import com.fsck.k9.mail.transport.CountingOutputStream;
|
|
|
|
import com.fsck.k9.mail.transport.EOLConvertingOutputStream;
|
2010-03-06 19:30:40 -05:00
|
|
|
import com.jcraft.jzlib.JZlib;
|
|
|
|
import com.jcraft.jzlib.ZInputStream;
|
|
|
|
import com.jcraft.jzlib.ZOutputStream;
|
2008-11-01 17:32:06 -04:00
|
|
|
import com.beetstra.jutf7.CharsetProvider;
|
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.SSLException;
|
|
|
|
import javax.net.ssl.TrustManager;
|
2009-12-20 00:15:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.commons.codec.binary.Hex;
|
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.CharBuffer;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.security.GeneralSecurityException;
|
2009-12-20 00:15:20 -05:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.util.*;
|
2010-05-15 15:35:07 -04:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
|
|
|
* <pre>
|
|
|
|
* TODO Need to start keeping track of UIDVALIDITY
|
|
|
|
* TODO Need a default response handler for things like folder updates
|
|
|
|
* TODO In fetch(), if we need a ImapMessage and were given
|
|
|
|
* something else we can try to do a pre-fetch first.
|
|
|
|
* </pre>
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
public class ImapStore extends Store
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
public static final int CONNECTION_SECURITY_NONE = 0;
|
|
|
|
public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
|
|
|
|
public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
|
|
|
|
public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
|
|
|
|
public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-12-20 00:15:20 -05:00
|
|
|
private enum AuthType { PLAIN, CRAM_MD5 };
|
2009-12-20 00:41:43 -05:00
|
|
|
|
2010-05-09 11:27:41 -04:00
|
|
|
private static final int IDLE_READ_TIMEOUT_INCREMENT = 5 * 60 * 1000;
|
2010-04-14 23:17:25 -04:00
|
|
|
private static final int IDLE_FAILURE_COUNT_LIMIT = 10;
|
|
|
|
private static int MAX_DELAY_TIME = 5 * 60 * 1000; // 5 minutes
|
2010-04-29 00:59:14 -04:00
|
|
|
private static int NORMAL_DELAY_TIME = 5000;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED, Flag.SEEN };
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
private static final String CAPABILITY_IDLE = "IDLE";
|
|
|
|
private static final String COMMAND_IDLE = "IDLE";
|
|
|
|
private static final String CAPABILITY_NAMESPACE = "NAMESPACE";
|
|
|
|
private static final String COMMAND_NAMESPACE = "NAMESPACE";
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
private static final String CAPABILITY_CAPABILITY = "CAPABILITY";
|
|
|
|
private static final String COMMAND_CAPABILITY = "CAPABILITY";
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-03-06 19:30:40 -05:00
|
|
|
private static final String CAPABILITY_COMPRESS_DEFLATE = "COMPRESS=DEFLATE";
|
|
|
|
private static final String COMMAND_COMPRESS_DEFLATE = "COMPRESS DEFLATE";
|
2010-02-27 12:34:38 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
private String mHost;
|
|
|
|
private int mPort;
|
|
|
|
private String mUsername;
|
|
|
|
private String mPassword;
|
|
|
|
private int mConnectionSecurity;
|
2009-12-20 00:15:20 -05:00
|
|
|
private AuthType mAuthType;
|
2009-11-28 09:51:44 -05:00
|
|
|
private volatile String mPathPrefix;
|
|
|
|
private volatile String mCombinedPrefix = null;
|
|
|
|
private volatile String mPathDelimeter;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
private LinkedList<ImapConnection> mConnections =
|
2009-11-24 19:40:29 -05:00
|
|
|
new LinkedList<ImapConnection>();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Charset used for converting folder names to and from UTF-7 as defined by RFC 3501.
|
|
|
|
*/
|
|
|
|
private Charset mModifiedUtf7Charset;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of ImapFolder objects. ImapFolders are attached to a given folder on the server
|
|
|
|
* and as long as their associated connection remains open they are reusable between
|
|
|
|
* requests. This cache lets us make sure we always reuse, if possible, for a given
|
|
|
|
* folder name.
|
|
|
|
*/
|
|
|
|
private HashMap<String, ImapFolder> mFolderCache = new HashMap<String, ImapFolder>();
|
|
|
|
|
|
|
|
/**
|
2009-12-20 00:15:20 -05:00
|
|
|
* imap://auth:user:password@server:port CONNECTION_SECURITY_NONE
|
|
|
|
* imap+tls://auth:user:password@server:port CONNECTION_SECURITY_TLS_OPTIONAL
|
|
|
|
* imap+tls+://auth:user:password@server:port CONNECTION_SECURITY_TLS_REQUIRED
|
|
|
|
* imap+ssl+://auth:user:password@server:port CONNECTION_SECURITY_SSL_REQUIRED
|
|
|
|
* imap+ssl://auth:user:password@server:port CONNECTION_SECURITY_SSL_OPTIONAL
|
2010-03-06 19:30:40 -05:00
|
|
|
*
|
|
|
|
* @param _uri
|
2008-11-01 17:32:06 -04:00
|
|
|
*/
|
2010-03-03 23:00:30 -05:00
|
|
|
public ImapStore(Account account) throws MessagingException
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-03-03 23:00:30 -05:00
|
|
|
super(account);
|
2008-11-01 17:32:06 -04:00
|
|
|
URI uri;
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2010-03-03 23:00:30 -05:00
|
|
|
uri = new URI(mAccount.getStoreUri());
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (URISyntaxException use)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException("Invalid ImapStore URI", use);
|
|
|
|
}
|
|
|
|
|
|
|
|
String scheme = uri.getScheme();
|
2009-11-24 19:40:29 -05:00
|
|
|
if (scheme.equals("imap"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mConnectionSecurity = CONNECTION_SECURITY_NONE;
|
|
|
|
mPort = 143;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else if (scheme.equals("imap+tls"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
|
|
|
|
mPort = 143;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else if (scheme.equals("imap+tls+"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
|
|
|
|
mPort = 143;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else if (scheme.equals("imap+ssl+"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
|
|
|
|
mPort = 993;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else if (scheme.equals("imap+ssl"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
|
|
|
|
mPort = 993;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException("Unsupported protocol");
|
|
|
|
}
|
|
|
|
|
|
|
|
mHost = uri.getHost();
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (uri.getPort() != -1)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mPort = uri.getPort();
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (uri.getUserInfo() != null)
|
|
|
|
{
|
2010-02-01 21:06:29 -05:00
|
|
|
try
|
2009-12-20 01:14:04 -05:00
|
|
|
{
|
2010-02-01 21:06:29 -05:00
|
|
|
String[] userInfoParts = uri.getUserInfo().split(":");
|
|
|
|
if (userInfoParts.length == 2)
|
|
|
|
{
|
|
|
|
mAuthType = AuthType.PLAIN;
|
|
|
|
mUsername = URLDecoder.decode(userInfoParts[0], "UTF-8");
|
|
|
|
mPassword = URLDecoder.decode(userInfoParts[1], "UTF-8");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mAuthType = AuthType.valueOf(userInfoParts[0]);
|
|
|
|
mUsername = URLDecoder.decode(userInfoParts[1], "UTF-8");
|
|
|
|
mPassword = URLDecoder.decode(userInfoParts[2], "UTF-8");
|
|
|
|
}
|
2010-01-02 20:50:51 -05:00
|
|
|
}
|
2010-02-01 21:06:29 -05:00
|
|
|
catch (UnsupportedEncodingException enc)
|
2009-12-20 01:14:04 -05:00
|
|
|
{
|
2010-02-01 21:06:29 -05:00
|
|
|
// This shouldn't happen since the encoding is hardcoded to UTF-8
|
|
|
|
Log.e(K9.LOG_TAG, "Couldn't urldecode username or password.", enc);
|
2009-12-20 01:14:04 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if ((uri.getPath() != null) && (uri.getPath().length() > 0))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mPathPrefix = uri.getPath().substring(1);
|
2009-11-28 09:51:44 -05:00
|
|
|
if (mPathPrefix != null && mPathPrefix.trim().length() == 0)
|
|
|
|
{
|
|
|
|
mPathPrefix = null;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mModifiedUtf7Charset = new CharsetProvider().charsetForName("X-RFC-3501");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public Folder getFolder(String name) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapFolder folder;
|
2009-11-24 19:40:29 -05:00
|
|
|
synchronized (mFolderCache)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
folder = mFolderCache.get(name);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (folder == null)
|
|
|
|
{
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
folder = new ImapFolder(this, name);
|
2008-11-01 17:32:06 -04:00
|
|
|
mFolderCache.put(name, folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
private String getCombinedPrefix()
|
|
|
|
{
|
|
|
|
if (mCombinedPrefix == null)
|
|
|
|
{
|
|
|
|
if (mPathPrefix != null)
|
|
|
|
{
|
|
|
|
String tmpPrefix = mPathPrefix.trim();
|
|
|
|
String tmpDelim = (mPathDelimeter != null ? mPathDelimeter.trim() : "");
|
|
|
|
if (tmpPrefix.endsWith(tmpDelim))
|
|
|
|
{
|
|
|
|
mCombinedPrefix = tmpPrefix;
|
|
|
|
}
|
|
|
|
else if (tmpPrefix.length() > 0)
|
|
|
|
{
|
|
|
|
mCombinedPrefix = tmpPrefix + tmpDelim;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mCombinedPrefix = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mCombinedPrefix = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mCombinedPrefix;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
@Override
|
2010-04-21 22:20:35 -04:00
|
|
|
public List<? extends Folder> getPersonalNamespaces() throws MessagingException
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapConnection connection = getConnection();
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2010-04-21 22:20:35 -04:00
|
|
|
LinkedList<Folder> folders = new LinkedList<Folder>();
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
List<ImapResponse> responses =
|
2009-11-24 19:40:29 -05:00
|
|
|
connection.executeSimpleCommand(String.format("LIST \"\" \"%s*\"",
|
2009-11-28 09:51:44 -05:00
|
|
|
getCombinedPrefix()));
|
2008-11-03 01:12:04 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), "LIST"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
boolean includeFolder = true;
|
|
|
|
String folder = decodeFolderName(response.getString(3));
|
2008-12-02 23:14:39 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (mPathDelimeter == null)
|
|
|
|
{
|
|
|
|
mPathDelimeter = response.getString(2);
|
2009-11-28 09:51:44 -05:00
|
|
|
mCombinedPrefix = null;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2008-12-02 23:14:39 -05:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (folder.equalsIgnoreCase(K9.INBOX))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
continue;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
if (getCombinedPrefix().length() > 0)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
if (folder.length() >= getCombinedPrefix().length())
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
folder = folder.substring(getCombinedPrefix().length());
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2010-05-09 11:27:41 -04:00
|
|
|
if (!decodeFolderName(response.getString(3)).equalsIgnoreCase(getCombinedPrefix() + folder))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
|
|
|
includeFolder = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapList attributes = response.getList(1);
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = attributes.size(); i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
String attribute = attributes.getString(i);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (attribute.equalsIgnoreCase("\\NoSelect"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
includeFolder = false;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (includeFolder)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
folders.add(getFolder(folder));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
folders.add(getFolder("INBOX"));
|
2010-04-21 22:20:35 -04:00
|
|
|
return folders;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection.close();
|
|
|
|
throw new MessagingException("Unable to get folder list.", ioe);
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
releaseConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void checkSettings() throws MessagingException
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapConnection connection = new ImapConnection();
|
|
|
|
connection.open();
|
|
|
|
connection.close();
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException("Unable to connect.", ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a connection if one is available for reuse, or creates a new one if not.
|
|
|
|
* @return
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
private ImapConnection getConnection() throws MessagingException
|
|
|
|
{
|
|
|
|
synchronized (mConnections)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapConnection connection = null;
|
2009-11-24 19:40:29 -05:00
|
|
|
while ((connection = mConnections.poll()) != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection.executeSimpleCommand("NOOP");
|
|
|
|
break;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection.close();
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (connection == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection = new ImapConnection();
|
|
|
|
}
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private void releaseConnection(ImapConnection connection)
|
|
|
|
{
|
2010-02-13 12:02:27 -05:00
|
|
|
if (connection != null && connection.isOpen())
|
2009-11-22 12:01:04 -05:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
synchronized (mConnections)
|
|
|
|
{
|
|
|
|
mConnections.offer(connection);
|
|
|
|
}
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private String encodeFolderName(String name)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ByteBuffer bb = mModifiedUtf7Charset.encode(name);
|
|
|
|
byte[] b = new byte[bb.limit()];
|
|
|
|
bb.get(b);
|
|
|
|
return new String(b, "US-ASCII");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (UnsupportedEncodingException uee)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* The only thing that can throw this is getBytes("US-ASCII") and if US-ASCII doesn't
|
|
|
|
* exist we're totally screwed.
|
|
|
|
*/
|
2008-11-06 01:55:56 -05:00
|
|
|
throw new RuntimeException("Unable to encode folder name: " + name, uee);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private String decodeFolderName(String name)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* Convert the encoded name to US-ASCII, then pass it through the modified UTF-7
|
|
|
|
* decoder and return the Unicode String.
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
byte[] encoded = name.getBytes("US-ASCII");
|
|
|
|
CharBuffer cb = mModifiedUtf7Charset.decode(ByteBuffer.wrap(encoded));
|
|
|
|
return cb.toString();
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (UnsupportedEncodingException uee)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* The only thing that can throw this is getBytes("US-ASCII") and if US-ASCII doesn't
|
|
|
|
* exist we're totally screwed.
|
|
|
|
*/
|
|
|
|
throw new RuntimeException("Unable to decode folder name: " + name, uee);
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-03-05 02:32:45 -05:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean isMoveCapable()
|
|
|
|
{
|
|
|
|
return true;
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-03-05 02:32:45 -05:00
|
|
|
@Override
|
|
|
|
public boolean isCopyCapable()
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
return true;
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
@Override
|
|
|
|
public boolean isPushCapable()
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
return true;
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
Implementation of complete IMAP two-phase "delete/expunge" behavior.
On each IMAP account, the expunge behavior can be set to expunge
messages in a folder as soon as a move or delete is performed on the
folder ("immediately"), each time the folder is polled, or only when
executed manually.
In the Message List, there is now an Expunge action in the option
menu.
In the Folder List, there is now an Expunge action in the context
menu (long-press on the folder).
For IMAP accounts, it is also possible to disable the copying of deleted messages to the
Trash folder, by setting the Trash folder to -NONE-.
Fixes Issue 536.
Separately, in WebDAV accounts, the user can now choose the
server-side equivalents of the special folders, just like for IMAP.
2009-12-20 18:13:49 -05:00
|
|
|
@Override
|
|
|
|
public boolean isExpungeCapable()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
class ImapFolder extends Folder
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
private String mName;
|
2009-10-21 20:41:06 -04:00
|
|
|
protected int mMessageCount = -1;
|
|
|
|
protected int uidNext = -1;
|
|
|
|
protected ImapConnection mConnection;
|
2008-11-01 17:32:06 -04:00
|
|
|
private OpenMode mMode;
|
|
|
|
private boolean mExists;
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
private ImapStore store = null;
|
2010-05-15 15:35:07 -04:00
|
|
|
Map<Integer, String> msgSeqUidMap = new ConcurrentHashMap<Integer, String>();
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public ImapFolder(ImapStore nStore, String name)
|
|
|
|
{
|
2010-03-03 23:00:30 -05:00
|
|
|
super(nStore.getAccount());
|
2009-11-24 19:40:29 -05:00
|
|
|
store = nStore;
|
|
|
|
this.mName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPrefixedName()
|
|
|
|
{
|
|
|
|
String prefixedName = "";
|
2009-12-14 21:50:53 -05:00
|
|
|
if (!K9.INBOX.equalsIgnoreCase(mName))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
prefixedName = getCombinedPrefix();
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
prefixedName += mName;
|
|
|
|
return prefixedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<ImapResponse> executeSimpleCommand(String command) throws MessagingException, IOException
|
|
|
|
{
|
|
|
|
return handleUntaggedResponses(mConnection.executeSimpleCommand(command));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<ImapResponse> executeSimpleCommand(String command, boolean sensitve, UntaggedHandler untaggedHandler) throws MessagingException, IOException
|
|
|
|
{
|
|
|
|
return handleUntaggedResponses(mConnection.executeSimpleCommand(command, sensitve, untaggedHandler));
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void open(OpenMode mode) throws MessagingException
|
|
|
|
{
|
|
|
|
internalOpen(mode);
|
2009-11-26 00:10:12 -05:00
|
|
|
|
|
|
|
if (mMessageCount == -1)
|
|
|
|
{
|
|
|
|
throw new MessagingException(
|
|
|
|
"Did not find message count during open");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<ImapResponse> internalOpen(OpenMode mode) throws MessagingException
|
|
|
|
{
|
|
|
|
if (isOpen() && mMode == mode)
|
|
|
|
{
|
|
|
|
// Make sure the connection is valid. If it's not we'll close it down and continue
|
|
|
|
// on to get a new one.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
List<ImapResponse> responses = executeSimpleCommand("NOOP");
|
|
|
|
return responses;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
2010-02-13 12:02:27 -05:00
|
|
|
releaseConnection(mConnection);
|
2009-11-24 19:40:29 -05:00
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
mConnection = getConnection();
|
|
|
|
}
|
|
|
|
// * FLAGS (\Answered \Flagged \Deleted \Seen \Draft NonJunk
|
|
|
|
// $MDNSent)
|
|
|
|
// * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft
|
|
|
|
// NonJunk $MDNSent \*)] Flags permitted.
|
|
|
|
// * 23 EXISTS
|
|
|
|
// * 0 RECENT
|
|
|
|
// * OK [UIDVALIDITY 1125022061] UIDs valid
|
|
|
|
// * OK [UIDNEXT 57576] Predicted next UID
|
|
|
|
// 2 OK [READ-WRITE] Select completed.
|
|
|
|
try
|
|
|
|
{
|
2010-05-15 15:35:07 -04:00
|
|
|
msgSeqUidMap.clear();
|
2010-02-13 12:02:27 -05:00
|
|
|
String command = String.format((mode == OpenMode.READ_WRITE ? "SELECT" : "EXAMINE") + " \"%s\"",
|
2009-11-24 19:40:29 -05:00
|
|
|
encodeFolderName(getPrefixedName()));
|
|
|
|
|
|
|
|
List<ImapResponse> responses = executeSimpleCommand(command);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the command succeeds we expect the folder has been opened read-write
|
|
|
|
* unless we are notified otherwise in the responses.
|
|
|
|
*/
|
2010-02-13 12:02:27 -05:00
|
|
|
mMode = mode;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
|
|
|
if (response.mTag != null && response.size() >= 2)
|
|
|
|
{
|
|
|
|
Object bracketedObj = response.get(1);
|
|
|
|
if (bracketedObj instanceof ImapList)
|
|
|
|
{
|
|
|
|
ImapList bracketed = (ImapList)bracketedObj;
|
|
|
|
|
|
|
|
if (bracketed.size() > 0)
|
|
|
|
{
|
|
|
|
Object keyObj = bracketed.get(0);
|
|
|
|
if (keyObj instanceof String)
|
|
|
|
{
|
|
|
|
String key = (String)keyObj;
|
|
|
|
|
|
|
|
if ("READ-ONLY".equalsIgnoreCase(key))
|
|
|
|
{
|
|
|
|
mMode = OpenMode.READ_ONLY;
|
|
|
|
}
|
|
|
|
else if ("READ-WRITE".equalsIgnoreCase(key))
|
|
|
|
{
|
|
|
|
mMode = OpenMode.READ_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mExists = true;
|
2009-11-26 00:10:12 -05:00
|
|
|
return responses;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Unable to open connection for " + getLogId(), me);
|
2009-11-24 19:40:29 -05:00
|
|
|
throw me;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean isOpen()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mConnection != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public OpenMode getMode() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mMode;
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
Implementation of complete IMAP two-phase "delete/expunge" behavior.
On each IMAP account, the expunge behavior can be set to expunge
messages in a folder as soon as a move or delete is performed on the
folder ("immediately"), each time the folder is polled, or only when
executed manually.
In the Message List, there is now an Expunge action in the option
menu.
In the Folder List, there is now an Expunge action in the context
menu (long-press on the folder).
For IMAP accounts, it is also possible to disable the copying of deleted messages to the
Trash folder, by setting the Trash folder to -NONE-.
Fixes Issue 536.
Separately, in WebDAV accounts, the user can now choose the
server-side equivalents of the special folders, just like for IMAP.
2009-12-20 18:13:49 -05:00
|
|
|
public void close()
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
|
|
|
if (mMessageCount != -1)
|
|
|
|
{
|
|
|
|
mMessageCount = -1;
|
|
|
|
}
|
|
|
|
if (!isOpen())
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2010-01-02 20:50:51 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
synchronized (this)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
releaseConnection(mConnection);
|
|
|
|
mConnection = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getName()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mName;
|
|
|
|
}
|
2010-01-16 23:20:39 -05:00
|
|
|
|
2010-01-16 11:22:20 -05:00
|
|
|
private boolean exists(String folderName) throws MessagingException
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Since we don't care about RECENT, we'll use that for the check, because we're checking
|
|
|
|
// a folder other than ourself, and don't want any untagged responses to cause a change
|
|
|
|
// in our own fields
|
|
|
|
mConnection.executeSimpleCommand(String.format("STATUS \"%s\" (RECENT)", folderName));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean exists() throws MessagingException
|
|
|
|
{
|
|
|
|
if (mExists)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* This method needs to operate in the unselected mode as well as the selected mode
|
|
|
|
* so we must get the connection ourselves if it's not there. We are specifically
|
|
|
|
* not calling checkOpen() since we don't care if the folder is open.
|
|
|
|
*/
|
|
|
|
ImapConnection connection = null;
|
2009-11-24 19:40:29 -05:00
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
if (mConnection == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection = getConnection();
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection = mConnection;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection.executeSimpleCommand(String.format("STATUS \"%s\" (UIDVALIDITY)",
|
2009-11-24 19:40:29 -05:00
|
|
|
encodeFolderName(getPrefixedName())));
|
2008-11-01 17:32:06 -04:00
|
|
|
mExists = true;
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(connection, ioe);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (mConnection == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
releaseConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean create(FolderType type) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* This method needs to operate in the unselected mode as well as the selected mode
|
|
|
|
* so we must get the connection ourselves if it's not there. We are specifically
|
|
|
|
* not calling checkOpen() since we don't care if the folder is open.
|
|
|
|
*/
|
|
|
|
ImapConnection connection = null;
|
2009-11-24 19:40:29 -05:00
|
|
|
synchronized (this)
|
|
|
|
{
|
|
|
|
if (mConnection == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection = getConnection();
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection = mConnection;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
connection.executeSimpleCommand(String.format("CREATE \"%s\"",
|
2009-11-24 19:40:29 -05:00
|
|
|
encodeFolderName(getPrefixedName())));
|
2008-11-01 17:32:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (mConnection == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
releaseConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void copyMessages(Message[] messages, Folder folder) throws MessagingException
|
|
|
|
{
|
|
|
|
if (folder instanceof ImapFolder == false)
|
|
|
|
{
|
|
|
|
throw new MessagingException("ImapFolder.copyMessages passed non-ImapFolder");
|
|
|
|
}
|
2009-12-15 12:40:04 -05:00
|
|
|
|
2009-12-20 00:41:43 -05:00
|
|
|
if (messages.length == 0)
|
2009-12-15 12:40:04 -05:00
|
|
|
return;
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
ImapFolder iFolder = (ImapFolder)folder;
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
|
|
|
String[] uids = new String[messages.length];
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = messages.length; i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
uids[i] = messages[i].getUid();
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2010-01-16 11:22:20 -05:00
|
|
|
String remoteDestName = encodeFolderName(iFolder.getPrefixedName());
|
2010-01-16 23:20:39 -05:00
|
|
|
|
2010-01-16 11:22:20 -05:00
|
|
|
if (!exists(remoteDestName))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the remote trash folder doesn't exist we try to create it.
|
|
|
|
*/
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "IMAPMessage.copyMessages: attempting to create remote '" + remoteDestName + "' folder for " + getLogId());
|
|
|
|
iFolder.create(FolderType.HOLDS_MESSAGES);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists(remoteDestName))
|
|
|
|
{
|
|
|
|
executeSimpleCommand(String.format("UID COPY %s \"%s\"",
|
|
|
|
Utility.combine(uids, ','),
|
|
|
|
encodeFolderName(iFolder.getPrefixedName())));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-17 19:11:02 -05:00
|
|
|
throw new MessagingException("IMAPMessage.copyMessages: remote destination folder " + folder.getName()
|
|
|
|
+ " does not exist and could not be created for " + getLogId()
|
2010-01-16 11:22:20 -05:00
|
|
|
, true);
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-03-05 02:32:45 -05:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void moveMessages(Message[] messages, Folder folder) throws MessagingException
|
|
|
|
{
|
2009-12-20 00:41:43 -05:00
|
|
|
if (messages.length == 0)
|
2009-12-15 12:40:04 -05:00
|
|
|
return;
|
2009-11-24 19:40:29 -05:00
|
|
|
copyMessages(messages, folder);
|
|
|
|
setFlags(messages, new Flag[] { Flag.DELETED }, true);
|
2009-03-05 02:32:45 -05:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-29 13:07:34 -05:00
|
|
|
public void delete(Message[] messages, String trashFolderName) throws MessagingException
|
|
|
|
{
|
2009-12-20 00:41:43 -05:00
|
|
|
if (messages.length == 0)
|
2009-12-15 12:40:04 -05:00
|
|
|
return;
|
|
|
|
|
2010-05-09 11:27:41 -04:00
|
|
|
if (trashFolderName == null || getName().equalsIgnoreCase(trashFolderName))
|
2009-11-29 13:07:34 -05:00
|
|
|
{
|
|
|
|
setFlags(messages, new Flag[] { Flag.DELETED }, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImapFolder remoteTrashFolder = (ImapFolder)getStore().getFolder(trashFolderName);
|
2010-01-16 11:22:20 -05:00
|
|
|
String remoteTrashName = encodeFolderName(remoteTrashFolder.getPrefixedName());
|
2010-01-17 19:11:02 -05:00
|
|
|
|
2010-01-16 11:22:20 -05:00
|
|
|
if (!exists(remoteTrashName))
|
2009-11-29 13:07:34 -05:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the remote trash folder doesn't exist we try to create it.
|
|
|
|
*/
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "IMAPMessage.delete: attempting to create remote '" + trashFolderName + "' folder for " + getLogId());
|
2009-11-29 13:07:34 -05:00
|
|
|
remoteTrashFolder.create(FolderType.HOLDS_MESSAGES);
|
|
|
|
}
|
|
|
|
|
2010-01-16 11:22:20 -05:00
|
|
|
if (exists(remoteTrashName))
|
2009-11-29 13:07:34 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "IMAPMessage.delete: copying remote " + messages.length + " messages to '" + trashFolderName + "' for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-29 13:07:34 -05:00
|
|
|
moveMessages(messages, remoteTrashFolder);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new MessagingException("IMAPMessage.delete: remote Trash folder " + trashFolderName + " does not exist and could not be created for " + getLogId()
|
|
|
|
, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public int getMessageCount()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mMessageCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public int getUnreadMessageCount() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2010-02-13 12:02:27 -05:00
|
|
|
int count = 0;
|
|
|
|
int start = mMessageCount - 299;
|
|
|
|
if (start < 1)
|
|
|
|
{
|
|
|
|
start = 1;
|
|
|
|
}
|
|
|
|
List<ImapResponse> responses = executeSimpleCommand(String.format("SEARCH %d:* UNSEEN NOT DELETED", start));
|
2009-11-24 19:40:29 -05:00
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), "SEARCH"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-02-13 12:02:27 -05:00
|
|
|
count += response.size() - 1;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
2010-02-13 12:02:27 -05:00
|
|
|
return count;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-16 10:33:54 -04:00
|
|
|
@Override
|
|
|
|
public int getFlaggedMessageCount() throws MessagingException
|
|
|
|
{
|
|
|
|
checkOpen();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int start = mMessageCount - 299;
|
|
|
|
if (start < 1)
|
|
|
|
{
|
|
|
|
start = 1;
|
|
|
|
}
|
|
|
|
List<ImapResponse> responses = executeSimpleCommand(String.format("SEARCH %d:* FLAGGED NOT DELETED", start));
|
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), "SEARCH"))
|
2010-04-16 10:33:54 -04:00
|
|
|
{
|
|
|
|
count += response.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-27 00:06:26 -04:00
|
|
|
protected int getHighestUid()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ImapSearcher searcher = new ImapSearcher()
|
|
|
|
{
|
|
|
|
public List<ImapResponse> search() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
return executeSimpleCommand(String.format("UID SEARCH *:* "));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Message[] messages = search(searcher, null);
|
|
|
|
if (messages.length > 0)
|
|
|
|
{
|
|
|
|
return Integer.parseInt(messages[0].getUid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to find highest UID in folder " + getName(), e);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void delete(boolean recurse) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new Error("ImapStore.delete() not yet implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public Message getMessage(String uid) throws MessagingException
|
|
|
|
{
|
2009-10-21 20:41:06 -04:00
|
|
|
return new ImapMessage(uid, this);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
@Override
|
|
|
|
public Message[] getMessages(int start, int end, MessageRetrievalListener listener)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
2009-10-21 20:41:06 -04:00
|
|
|
return getMessages(start, end, false, listener);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
protected Message[] getMessages(final int start, final int end, final boolean includeDeleted, final MessageRetrievalListener listener)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
|
|
|
if (start < 1 || end < 1 || end < start)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException(
|
2009-11-24 19:40:29 -05:00
|
|
|
String.format("Invalid message set %d %d",
|
|
|
|
start, end));
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
ImapSearcher searcher = new ImapSearcher()
|
|
|
|
{
|
|
|
|
public List<ImapResponse> search() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
return executeSimpleCommand(String.format("UID SEARCH %d:%d" + (includeDeleted ? "" : " NOT DELETED"), start, end));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return search(searcher, listener);
|
|
|
|
|
|
|
|
}
|
|
|
|
protected Message[] getMessages(final List<Integer> mesgSeqs, final boolean includeDeleted, final MessageRetrievalListener listener)
|
|
|
|
throws MessagingException
|
|
|
|
{
|
|
|
|
ImapSearcher searcher = new ImapSearcher()
|
|
|
|
{
|
|
|
|
public List<ImapResponse> search() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
return executeSimpleCommand(String.format("UID SEARCH %s" + (includeDeleted ? "" : " NOT DELETED"), Utility.combine(mesgSeqs.toArray(), ',')));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return search(searcher, listener);
|
|
|
|
}
|
2010-05-15 15:46:16 -04:00
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
protected Message[] getMessagesFromUids(final List<String> mesgUids, final boolean includeDeleted, final MessageRetrievalListener listener)
|
|
|
|
throws MessagingException
|
|
|
|
{
|
|
|
|
ImapSearcher searcher = new ImapSearcher()
|
|
|
|
{
|
|
|
|
public List<ImapResponse> search() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
return executeSimpleCommand(String.format("UID SEARCH UID %s" + (includeDeleted ? "" : " NOT DELETED"), Utility.combine(mesgUids.toArray(), ',')));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return search(searcher, listener);
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
private Message[] search(ImapSearcher searcher, MessageRetrievalListener listener) throws MessagingException
|
|
|
|
{
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
|
|
|
ArrayList<Message> messages = new ArrayList<Message>();
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2009-04-21 00:22:02 -04:00
|
|
|
ArrayList<Integer> uids = new ArrayList<Integer>();
|
2009-11-26 00:10:12 -05:00
|
|
|
List<ImapResponse> responses = searcher.search(); //
|
2009-11-24 19:40:29 -05:00
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2010-03-24 21:24:21 -04:00
|
|
|
if (response.mTag == null)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), "SEARCH"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-03-24 21:24:21 -04:00
|
|
|
for (int i = 1, count = response.size(); i < count; i++)
|
|
|
|
{
|
|
|
|
uids.add(Integer.parseInt(response.getString(i)));
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-24 21:24:21 -04:00
|
|
|
|
2009-04-21 00:22:02 -04:00
|
|
|
// Sort the uids in numerically ascending order
|
|
|
|
Collections.sort(uids);
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = uids.size(); i < count; i++)
|
|
|
|
{
|
|
|
|
if (listener != null)
|
|
|
|
{
|
2009-04-21 00:22:02 -04:00
|
|
|
listener.messageStarted("" + uids.get(i), i, count);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-04-21 00:22:02 -04:00
|
|
|
ImapMessage message = new ImapMessage("" + uids.get(i), this);
|
2008-11-01 17:32:06 -04:00
|
|
|
messages.add(message);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (listener != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
listener.messageFinished(message, i, count);
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
return messages.toArray(new Message[] {});
|
|
|
|
}
|
|
|
|
|
2009-11-17 11:54:50 -05:00
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public Message[] getMessages(MessageRetrievalListener listener) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return getMessages(null, listener);
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2008-11-01 17:32:06 -04:00
|
|
|
public Message[] getMessages(String[] uids, MessageRetrievalListener listener)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
|
|
|
ArrayList<Message> messages = new ArrayList<Message>();
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (uids == null)
|
|
|
|
{
|
2009-04-21 00:22:02 -04:00
|
|
|
List<ImapResponse> responses = executeSimpleCommand("UID SEARCH 1:* NOT DELETED");
|
2008-11-01 17:32:06 -04:00
|
|
|
ArrayList<String> tempUids = new ArrayList<String>();
|
2009-11-24 19:40:29 -05:00
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), "SEARCH"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
|
|
|
for (int i = 1, count = response.size(); i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
tempUids.add(response.getString(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uids = tempUids.toArray(new String[] {});
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = uids.length; i < count; i++)
|
|
|
|
{
|
|
|
|
if (listener != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
listener.messageStarted(uids[i], i, count);
|
|
|
|
}
|
|
|
|
ImapMessage message = new ImapMessage(uids[i], this);
|
|
|
|
messages.add(message);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (listener != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
listener.messageFinished(message, i, count);
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
return messages.toArray(new Message[] {});
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2008-11-01 17:32:06 -04:00
|
|
|
public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
|
|
|
if (messages == null || messages.length == 0)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
checkOpen();
|
|
|
|
String[] uids = new String[messages.length];
|
|
|
|
HashMap<String, Message> messageMap = new HashMap<String, Message>();
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = messages.length; i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
uids[i] = messages[i].getUid();
|
|
|
|
messageMap.put(uids[i], messages[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out what command we are going to run:
|
|
|
|
* Flags - UID FETCH (FLAGS)
|
|
|
|
* Envelope - UID FETCH ([FLAGS] INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[HEADER.FIELDS (date subject from content-type to cc)])
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
LinkedHashSet<String> fetchFields = new LinkedHashSet<String>();
|
|
|
|
fetchFields.add("UID");
|
2009-11-24 19:40:29 -05:00
|
|
|
if (fp.contains(FetchProfile.Item.FLAGS))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
fetchFields.add("FLAGS");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (fp.contains(FetchProfile.Item.ENVELOPE))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
fetchFields.add("INTERNALDATE");
|
|
|
|
fetchFields.add("RFC822.SIZE");
|
2009-11-24 19:40:29 -05:00
|
|
|
fetchFields.add("BODY.PEEK[HEADER.FIELDS (date subject from content-type to cc reply-to "
|
2009-12-14 21:50:53 -05:00
|
|
|
+ K9.K9MAIL_IDENTITY + ")]");
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (fp.contains(FetchProfile.Item.STRUCTURE))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
fetchFields.add("BODYSTRUCTURE");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (fp.contains(FetchProfile.Item.BODY_SANE))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
fetchFields.add(String.format("BODY.PEEK[]<0.%d>", FETCH_BODY_SANE_SUGGESTED_SIZE));
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (fp.contains(FetchProfile.Item.BODY))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
fetchFields.add("BODY.PEEK[]");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
for (Object o : fp)
|
|
|
|
{
|
2010-01-16 11:22:20 -05:00
|
|
|
if (o != null && o instanceof Part)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
Part part = (Part) o;
|
2010-04-26 23:02:17 -04:00
|
|
|
String[] parts = part.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
|
|
|
|
if (parts != null)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-04-26 23:02:17 -04:00
|
|
|
String partId = parts[0];
|
2010-05-09 11:27:41 -04:00
|
|
|
if ("TEXT".equalsIgnoreCase(partId))
|
2010-04-26 23:02:17 -04:00
|
|
|
{
|
|
|
|
fetchFields.add(String.format("BODY.PEEK[TEXT]<0.%d>", FETCH_BODY_SANE_SUGGESTED_SIZE));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fetchFields.add("BODY.PEEK[" + partId + "]");
|
|
|
|
}
|
2009-05-20 00:27:51 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
mConnection.sendCommand(String.format("UID FETCH %s (%s)",
|
2009-11-24 19:40:29 -05:00
|
|
|
Utility.combine(uids, ','),
|
|
|
|
Utility.combine(fetchFields.toArray(new String[fetchFields.size()]), ' ')
|
|
|
|
), false);
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapResponse response;
|
|
|
|
int messageNumber = 0;
|
2009-11-24 19:40:29 -05:00
|
|
|
do
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
response = mConnection.readResponse();
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "response for fetch: " + response + " for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2010-05-09 11:27:41 -04:00
|
|
|
if (response.mTag == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapList fetchList = (ImapList)response.getKeyedValue("FETCH");
|
|
|
|
String uid = fetchList.getKeyedString("UID");
|
2010-05-15 15:35:07 -04:00
|
|
|
int msgSeq = response.getNumber(0);
|
|
|
|
if (uid != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
msgSeqUidMap.put(msgSeq, uid);
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.v(K9.LOG_TAG, "Stored uid '" + uid + "' for msgSeq " + msgSeq + " into map " + msgSeqUidMap.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to store uid '" + uid + "' for msgSeq " + msgSeq);
|
|
|
|
}
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
Message message = messageMap.get(uid);
|
2009-10-21 20:41:06 -04:00
|
|
|
if (message == null)
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Do not have message in messageMap for UID " + uid + " for " + getLogId());
|
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
handleUntaggedResponse(response);
|
2009-10-21 20:41:06 -04:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
if (listener != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
listener.messageStarted(uid, messageNumber++, messageMap.size());
|
|
|
|
}
|
|
|
|
|
2010-02-10 08:52:25 -05:00
|
|
|
ImapMessage imapMessage = (ImapMessage) message;
|
|
|
|
|
|
|
|
if (fetchList.containsKey("FLAGS"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapList flags = fetchList.getKeyedList("FLAGS");
|
2009-11-24 19:40:29 -05:00
|
|
|
if (flags != null)
|
|
|
|
{
|
|
|
|
for (int i = 0, count = flags.size(); i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
String flag = flags.getString(i);
|
2010-05-09 11:27:41 -04:00
|
|
|
if (flag.equalsIgnoreCase("\\Deleted"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
imapMessage.setFlagInternal(Flag.DELETED, true);
|
|
|
|
}
|
2010-05-09 11:27:41 -04:00
|
|
|
else if (flag.equalsIgnoreCase("\\Answered"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
imapMessage.setFlagInternal(Flag.ANSWERED, true);
|
|
|
|
}
|
2010-05-09 11:27:41 -04:00
|
|
|
else if (flag.equalsIgnoreCase("\\Seen"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
imapMessage.setFlagInternal(Flag.SEEN, true);
|
|
|
|
}
|
2010-05-09 11:27:41 -04:00
|
|
|
else if (flag.equalsIgnoreCase("\\Flagged"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
imapMessage.setFlagInternal(Flag.FLAGGED, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-10 08:52:25 -05:00
|
|
|
|
|
|
|
if (fetchList.containsKey("INTERNALDATE"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
Date internalDate = fetchList.getKeyedDate("INTERNALDATE");
|
|
|
|
message.setInternalDate(internalDate);
|
2010-02-10 08:52:25 -05:00
|
|
|
}
|
|
|
|
if (fetchList.containsKey("RFC822.SIZE"))
|
|
|
|
{
|
|
|
|
int size = fetchList.getKeyedNumber("RFC822.SIZE");
|
2008-11-01 17:32:06 -04:00
|
|
|
imapMessage.setSize(size);
|
|
|
|
}
|
2010-02-10 08:52:25 -05:00
|
|
|
if (fetchList.containsKey("BODYSTRUCTURE"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapList bs = fetchList.getKeyedList("BODYSTRUCTURE");
|
2009-11-24 19:40:29 -05:00
|
|
|
if (bs != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
parseBodyStructure(bs, message, "TEXT");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (MessagingException e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Error handling message for " + getLogId(), e);
|
2008-11-01 17:32:06 -04:00
|
|
|
message.setBody(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-10 08:52:25 -05:00
|
|
|
|
|
|
|
if (fetchList.containsKey("BODY"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-02-14 10:51:09 -05:00
|
|
|
Part part = null;
|
2010-02-10 08:52:25 -05:00
|
|
|
for (Object o : fp)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-02-10 08:52:25 -05:00
|
|
|
if (o instanceof Part)
|
2009-04-21 00:22:02 -04:00
|
|
|
{
|
2010-02-14 10:51:09 -05:00
|
|
|
part = (Part) o;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-02-14 10:51:09 -05:00
|
|
|
int index = fetchList.getKeyIndex("BODY") + 2;
|
|
|
|
Object literal = fetchList.getObject(index);
|
2010-02-10 08:52:25 -05:00
|
|
|
|
2010-02-14 10:51:09 -05:00
|
|
|
// Check if there's an origin octet
|
|
|
|
if (literal instanceof String)
|
|
|
|
{
|
|
|
|
String originOctet = (String)literal;
|
|
|
|
if (originOctet.startsWith("<"))
|
|
|
|
{
|
|
|
|
literal = fetchList.getObject(index + 1);
|
2009-04-21 00:22:02 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2010-02-14 10:51:09 -05:00
|
|
|
|
|
|
|
InputStream bodyStream;
|
|
|
|
if (literal instanceof InputStream)
|
|
|
|
{
|
|
|
|
bodyStream = (InputStream)literal;
|
|
|
|
}
|
|
|
|
else if (literal instanceof String)
|
|
|
|
{
|
|
|
|
String bodyString = (String)literal;
|
|
|
|
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Part is a String: '" + bodyString + "' for " + getLogId());
|
|
|
|
|
|
|
|
bodyStream = new ByteArrayInputStream(bodyString.getBytes());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This shouldn't happen
|
|
|
|
throw new MessagingException("Got FETCH response with bogus parameters");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part != null)
|
|
|
|
{
|
|
|
|
String contentTransferEncoding = part.getHeader(
|
2010-04-29 00:59:14 -04:00
|
|
|
MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
|
2010-02-14 10:51:09 -05:00
|
|
|
part.setBody(MimeUtility.decodeBody(bodyStream, contentTransferEncoding));
|
|
|
|
}
|
|
|
|
else
|
2010-02-10 08:52:25 -05:00
|
|
|
{
|
|
|
|
imapMessage.parse(bodyStream);
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (listener != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
listener.messageFinished(message, messageNumber, messageMap.size());
|
|
|
|
}
|
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
handleUntaggedResponse(response);
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
while (response.more());
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
while (response.mTag == null);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public Flag[] getPermanentFlags() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return PERMANENT_FLAGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle any untagged responses that the caller doesn't care to handle themselves.
|
|
|
|
* @param responses
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
protected List<ImapResponse> handleUntaggedResponses(List<ImapResponse> responses)
|
|
|
|
{
|
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
handleUntaggedResponse(response);
|
|
|
|
}
|
2009-04-21 00:22:02 -04:00
|
|
|
return responses;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
protected void handlePossibleUidNext(ImapResponse response)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), "OK") && response.size() > 1)
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
|
|
|
Object bracketedObj = response.get(1);
|
|
|
|
if (bracketedObj instanceof ImapList)
|
|
|
|
{
|
|
|
|
ImapList bracketed = (ImapList)bracketedObj;
|
|
|
|
|
|
|
|
if (bracketed.size() > 1)
|
|
|
|
{
|
|
|
|
Object keyObj = bracketed.get(0);
|
|
|
|
if (keyObj instanceof String)
|
|
|
|
{
|
|
|
|
String key = (String)keyObj;
|
2010-05-09 11:27:41 -04:00
|
|
|
if ("UIDNEXT".equalsIgnoreCase(key))
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
|
|
|
uidNext = bracketed.getNumber(1);
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got UidNext = " + uidNext + " for " + getLogId());
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an untagged response that the caller doesn't care to handle themselves.
|
|
|
|
* @param response
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
protected void handleUntaggedResponse(ImapResponse response)
|
|
|
|
{
|
|
|
|
if (response.mTag == null && response.size() > 1)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(1), "EXISTS"))
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
mMessageCount = response.getNumber(0);
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got untagged EXISTS with value " + mMessageCount + " for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
handlePossibleUidNext(response);
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(1), "EXPUNGE") && mMessageCount > 0)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
|
|
|
mMessageCount--;
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
2010-05-15 15:35:07 -04:00
|
|
|
Log.d(K9.LOG_TAG, "Got untagged EXPUNGE with mMessageCount " + mMessageCount + " for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-07 15:27:30 -05:00
|
|
|
// if (response.size() > 1) {
|
|
|
|
// Object bracketedObj = response.get(1);
|
|
|
|
// if (bracketedObj instanceof ImapList)
|
|
|
|
// {
|
|
|
|
// ImapList bracketed = (ImapList)bracketedObj;
|
2009-11-24 19:40:29 -05:00
|
|
|
//
|
2009-11-07 15:27:30 -05:00
|
|
|
// if (bracketed.size() > 0)
|
|
|
|
// {
|
|
|
|
// Object keyObj = bracketed.get(0);
|
|
|
|
// if (keyObj instanceof String)
|
|
|
|
// {
|
|
|
|
// String key = (String)keyObj;
|
2010-05-09 11:27:41 -04:00
|
|
|
// if ("ALERT".equalsIgnoreCase(key))
|
2009-11-07 15:27:30 -05:00
|
|
|
// {
|
|
|
|
// StringBuffer sb = new StringBuffer();
|
|
|
|
// for (int i = 2, count = response.size(); i < count; i++) {
|
|
|
|
// sb.append(response.get(i).toString());
|
|
|
|
// sb.append(' ');
|
|
|
|
// }
|
2009-11-24 19:40:29 -05:00
|
|
|
//
|
2009-12-14 21:50:53 -05:00
|
|
|
// Log.w(K9.LOG_TAG, "ALERT: " + sb.toString() + " for " + getLogId());
|
2009-11-07 15:27:30 -05:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2009-11-24 19:40:29 -05:00
|
|
|
//
|
|
|
|
//
|
2009-11-07 15:27:30 -05:00
|
|
|
// }
|
|
|
|
// }
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
//Log.i(K9.LOG_TAG, "mMessageCount = " + mMessageCount + " for " + getLogId());
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void parseBodyStructure(ImapList bs, Part part, String id)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
|
|
|
if (bs.get(0) instanceof ImapList)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* This is a multipart/*
|
|
|
|
*/
|
|
|
|
MimeMultipart mp = new MimeMultipart();
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = bs.size(); i < count; i++)
|
|
|
|
{
|
|
|
|
if (bs.get(i) instanceof ImapList)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* For each part in the message we're going to add a new BodyPart and parse
|
|
|
|
* into it.
|
|
|
|
*/
|
|
|
|
ImapBodyPart bp = new ImapBodyPart();
|
2010-05-09 11:27:41 -04:00
|
|
|
if (id.equalsIgnoreCase("TEXT"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
parseBodyStructure(bs.getList(i), bp, Integer.toString(i + 1));
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
parseBodyStructure(bs.getList(i), bp, id + "." + (i + 1));
|
|
|
|
}
|
|
|
|
mp.addBodyPart(bp);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* We've got to the end of the children of the part, so now we can find out
|
|
|
|
* what type it is and bail out.
|
|
|
|
*/
|
|
|
|
String subType = bs.getString(i);
|
|
|
|
mp.setSubType(subType.toLowerCase());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
part.setBody(mp);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* This is a body. We need to add as much information as we can find out about
|
|
|
|
* it to the Part.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
body type
|
|
|
|
body subtype
|
|
|
|
body parameter parenthesized list
|
|
|
|
body id
|
|
|
|
body description
|
|
|
|
body encoding
|
|
|
|
body size
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
String type = bs.getString(0);
|
|
|
|
String subType = bs.getString(1);
|
|
|
|
String mimeType = (type + "/" + subType).toLowerCase();
|
|
|
|
|
|
|
|
ImapList bodyParams = null;
|
2009-11-24 19:40:29 -05:00
|
|
|
if (bs.get(2) instanceof ImapList)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
bodyParams = bs.getList(2);
|
|
|
|
}
|
|
|
|
String encoding = bs.getString(5);
|
|
|
|
int size = bs.getNumber(6);
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (MimeUtility.mimeTypeMatches(mimeType, "message/rfc822"))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
// A body type of type MESSAGE and subtype RFC822
|
|
|
|
// contains, immediately after the basic fields, the
|
|
|
|
// envelope structure, body structure, and size in
|
|
|
|
// text lines of the encapsulated message.
|
|
|
|
// [MESSAGE, RFC822, [NAME, Fwd: [#HTR-517941]: update plans at 1am Friday - Memory allocation - displayware.eml], NIL, NIL, 7BIT, 5974, NIL, [INLINE, [FILENAME*0, Fwd: [#HTR-517941]: update plans at 1am Friday - Memory all, FILENAME*1, ocation - displayware.eml]], NIL]
|
|
|
|
/*
|
|
|
|
* This will be caught by fetch and handled appropriately.
|
|
|
|
*/
|
|
|
|
throw new MessagingException("BODYSTRUCTURE message/rfc822 not yet supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the content type with as much information as we know right now.
|
|
|
|
*/
|
|
|
|
String contentType = String.format("%s", mimeType);
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (bodyParams != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
/*
|
|
|
|
* If there are body params we might be able to get some more information out
|
|
|
|
* of them.
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = bodyParams.size(); i < count; i += 2)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
contentType += String.format(";\n %s=\"%s\"",
|
2009-11-24 19:40:29 -05:00
|
|
|
bodyParams.getString(i),
|
|
|
|
bodyParams.getString(i + 1));
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
|
|
|
|
|
|
|
|
// Extension items
|
|
|
|
ImapList bodyDisposition = null;
|
|
|
|
if (("text".equalsIgnoreCase(type))
|
|
|
|
&& (bs.size() > 8)
|
2009-11-24 19:40:29 -05:00
|
|
|
&& (bs.get(9) instanceof ImapList))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
bodyDisposition = bs.getList(9);
|
|
|
|
}
|
|
|
|
else if (!("text".equalsIgnoreCase(type))
|
2009-11-24 19:40:29 -05:00
|
|
|
&& (bs.size() > 7)
|
|
|
|
&& (bs.get(8) instanceof ImapList))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
bodyDisposition = bs.getList(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
String contentDisposition = "";
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (bodyDisposition != null && bodyDisposition.size() > 0)
|
|
|
|
{
|
|
|
|
if (!"NIL".equalsIgnoreCase(bodyDisposition.getString(0)))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
contentDisposition = bodyDisposition.getString(0).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((bodyDisposition.size() > 1)
|
2009-11-24 19:40:29 -05:00
|
|
|
&& (bodyDisposition.get(1) instanceof ImapList))
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapList bodyDispositionParams = bodyDisposition.getList(1);
|
|
|
|
/*
|
|
|
|
* If there is body disposition information we can pull some more information
|
|
|
|
* about the attachment out.
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = bodyDispositionParams.size(); i < count; i += 2)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
contentDisposition += String.format(";\n %s=\"%s\"",
|
2009-11-24 19:40:29 -05:00
|
|
|
bodyDispositionParams.getString(i).toLowerCase(),
|
|
|
|
bodyDispositionParams.getString(i + 1));
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (MimeUtility.getHeaderParameter(contentDisposition, "size") == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
contentDisposition += String.format(";\n size=%d", size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the content disposition containing at least the size. Attachment
|
|
|
|
* handling code will use this down the road.
|
|
|
|
*/
|
|
|
|
part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, contentDisposition);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the Content-Transfer-Encoding header. Attachment code will use this
|
|
|
|
* to parse the body.
|
|
|
|
*/
|
|
|
|
part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
if (part instanceof ImapMessage)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
((ImapMessage) part).setSize(size);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else if (part instanceof ImapBodyPart)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
((ImapBodyPart) part).setSize(size);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException("Unknown part type " + part.toString());
|
|
|
|
}
|
|
|
|
part.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the given messages to the selected folder. This implementation also determines
|
|
|
|
* the new UID of the given message on the IMAP server and sets the Message's UID to the
|
|
|
|
* new server UID.
|
|
|
|
*/
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void appendMessages(Message[] messages) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for (Message message : messages)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
CountingOutputStream out = new CountingOutputStream();
|
|
|
|
EOLConvertingOutputStream eolOut = new EOLConvertingOutputStream(out);
|
|
|
|
message.writeTo(eolOut);
|
|
|
|
eolOut.flush();
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
mConnection.sendCommand(
|
2009-11-24 19:40:29 -05:00
|
|
|
String.format("APPEND \"%s\" (%s) {%d}",
|
|
|
|
encodeFolderName(getPrefixedName()),
|
|
|
|
combineFlags(message.getFlags()),
|
|
|
|
out.getCount()), false);
|
2008-11-01 17:32:06 -04:00
|
|
|
ImapResponse response;
|
2009-11-24 19:40:29 -05:00
|
|
|
do
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
response = mConnection.readResponse();
|
2009-04-21 00:22:02 -04:00
|
|
|
handleUntaggedResponse(response);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (response.mCommandContinuationRequested)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
eolOut = new EOLConvertingOutputStream(mConnection.mOut);
|
|
|
|
message.writeTo(eolOut);
|
|
|
|
eolOut.write('\r');
|
|
|
|
eolOut.write('\n');
|
|
|
|
eolOut.flush();
|
|
|
|
}
|
|
|
|
while (response.more());
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
while (response.mTag == null);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
String newUid = getUidFromMessageId(message);
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got UID " + newUid + " for message for " + getLogId());
|
2009-11-24 19:40:29 -05:00
|
|
|
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
if (newUid != null)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
message.setUid(newUid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getUidFromMessageId(Message message) throws MessagingException
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/*
|
2009-11-26 00:10:12 -05:00
|
|
|
* Try to find the UID of the message we just appended using the
|
|
|
|
* Message-ID header.
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
String[] messageIdHeader = message.getHeader("Message-ID");
|
|
|
|
|
|
|
|
if (messageIdHeader == null || messageIdHeader.length == 0)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Did not get a message-id in order to search for UID for " + getLogId());
|
2009-11-24 19:40:29 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String messageId = messageIdHeader[0];
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Looking for UID for message with message-id " + messageId + " for " + getLogId());
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
List<ImapResponse> responses =
|
|
|
|
executeSimpleCommand(
|
2010-02-03 12:25:15 -05:00
|
|
|
String.format("UID SEARCH HEADER MESSAGE-ID %s", messageId));
|
2009-11-24 19:40:29 -05:00
|
|
|
for (ImapResponse response1 : responses)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (response1.mTag == null && ImapResponseParser.equalsIgnoreCase(response1.get(0), "SEARCH")
|
2009-11-24 19:40:29 -05:00
|
|
|
&& response1.size() > 1)
|
|
|
|
{
|
|
|
|
return response1.getString(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw new MessagingException("Could not find UID for message based on Message-ID", ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
Implementation of complete IMAP two-phase "delete/expunge" behavior.
On each IMAP account, the expunge behavior can be set to expunge
messages in a folder as soon as a move or delete is performed on the
folder ("immediately"), each time the folder is polled, or only when
executed manually.
In the Message List, there is now an Expunge action in the option
menu.
In the Folder List, there is now an Expunge action in the context
menu (long-press on the folder).
For IMAP accounts, it is also possible to disable the copying of deleted messages to the
Trash folder, by setting the Trash folder to -NONE-.
Fixes Issue 536.
Separately, in WebDAV accounts, the user can now choose the
server-side equivalents of the special folders, just like for IMAP.
2009-12-20 18:13:49 -05:00
|
|
|
public void expunge() throws MessagingException
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
|
|
|
checkOpen();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
executeSimpleCommand("EXPUNGE");
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String combineFlags(Flag[] flags)
|
|
|
|
{
|
|
|
|
ArrayList<String> flagNames = new ArrayList<String>();
|
|
|
|
for (int i = 0, count = flags.length; i < count; i++)
|
|
|
|
{
|
|
|
|
Flag flag = flags[i];
|
|
|
|
if (flag == Flag.SEEN)
|
|
|
|
{
|
|
|
|
flagNames.add("\\Seen");
|
|
|
|
}
|
|
|
|
else if (flag == Flag.DELETED)
|
|
|
|
{
|
|
|
|
flagNames.add("\\Deleted");
|
|
|
|
}
|
|
|
|
else if (flag == Flag.ANSWERED)
|
|
|
|
{
|
|
|
|
flagNames.add("\\Answered");
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else if (flag == Flag.FLAGGED)
|
|
|
|
{
|
|
|
|
flagNames.add("\\Flagged");
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
return Utility.combine(flagNames.toArray(new String[flagNames.size()]), ' ');
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFlags(Flag[] flags, boolean value)
|
|
|
|
throws MessagingException
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
executeSimpleCommand(String.format("UID STORE 1:* %sFLAGS.SILENT (%s)",
|
|
|
|
value ? "+" : "-", combineFlags(flags)));
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
2009-05-04 20:42:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-10-21 20:41:06 -04:00
|
|
|
public String getNewPushState(String oldPushStateS, Message message)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String messageUidS = message.getUid();
|
|
|
|
int messageUid = Integer.parseInt(messageUidS);
|
|
|
|
ImapPushState oldPushState = ImapPushState.parse(oldPushStateS);
|
|
|
|
if (messageUid >= oldPushState.uidNext)
|
|
|
|
{
|
|
|
|
int uidNext = messageUid + 1;
|
|
|
|
ImapPushState newPushState = new ImapPushState(uidNext);
|
|
|
|
return newPushState.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Exception while updated push state for " + getLogId(), e);
|
2009-10-21 20:41:06 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2008-11-01 17:32:06 -04:00
|
|
|
public void setFlags(Message[] messages, Flag[] flags, boolean value)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
checkOpen();
|
|
|
|
String[] uids = new String[messages.length];
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = messages.length; i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
uids[i] = messages[i].getUid();
|
|
|
|
}
|
|
|
|
ArrayList<String> flagNames = new ArrayList<String>();
|
2009-11-24 19:40:29 -05:00
|
|
|
for (int i = 0, count = flags.length; i < count; i++)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
Flag flag = flags[i];
|
2009-11-24 19:40:29 -05:00
|
|
|
if (flag == Flag.SEEN)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
flagNames.add("\\Seen");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else if (flag == Flag.DELETED)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
flagNames.add("\\Deleted");
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else if (flag == Flag.ANSWERED)
|
|
|
|
{
|
|
|
|
flagNames.add("\\Answered");
|
2009-01-13 00:52:37 -05:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
else if (flag == Flag.FLAGGED)
|
|
|
|
{
|
|
|
|
flagNames.add("\\Flagged");
|
2009-01-13 00:52:37 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2009-04-21 00:22:02 -04:00
|
|
|
executeSimpleCommand(String.format("UID STORE %s %sFLAGS.SILENT (%s)",
|
2009-11-24 19:40:29 -05:00
|
|
|
Utility.combine(uids, ','),
|
|
|
|
value ? "+" : "-",
|
|
|
|
Utility.combine(flagNames.toArray(new String[flagNames.size()]), ' ')));
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw ioExceptionHandler(mConnection, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private void checkOpen() throws MessagingException
|
|
|
|
{
|
|
|
|
if (!isOpen())
|
|
|
|
{
|
2008-11-03 02:01:46 -05:00
|
|
|
throw new MessagingException("Folder " + getPrefixedName() + " is not open.");
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private MessagingException ioExceptionHandler(ImapConnection connection, IOException ioe)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "IOException for " + getLogId(), ioe);
|
2008-11-01 17:32:06 -04:00
|
|
|
connection.close();
|
Implementation of complete IMAP two-phase "delete/expunge" behavior.
On each IMAP account, the expunge behavior can be set to expunge
messages in a folder as soon as a move or delete is performed on the
folder ("immediately"), each time the folder is polled, or only when
executed manually.
In the Message List, there is now an Expunge action in the option
menu.
In the Folder List, there is now an Expunge action in the context
menu (long-press on the folder).
For IMAP accounts, it is also possible to disable the copying of deleted messages to the
Trash folder, by setting the Trash folder to -NONE-.
Fixes Issue 536.
Separately, in WebDAV accounts, the user can now choose the
server-side equivalents of the special folders, just like for IMAP.
2009-12-20 18:13:49 -05:00
|
|
|
close();
|
2008-11-01 17:32:06 -04:00
|
|
|
return new MessagingException("IO Error", ioe);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean equals(Object o)
|
|
|
|
{
|
|
|
|
if (o instanceof ImapFolder)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
return ((ImapFolder)o).getPrefixedName().equalsIgnoreCase(getPrefixedName());
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
return super.equals(o);
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
return getPrefixedName().hashCode();
|
|
|
|
}
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
protected ImapStore getStore()
|
|
|
|
{
|
|
|
|
return store;
|
|
|
|
}
|
2009-11-22 12:01:04 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
protected String getLogId()
|
|
|
|
{
|
2010-04-14 23:17:25 -04:00
|
|
|
String id = getAccount().getDescription() + ":" + getName() + "/" + Thread.currentThread().getName();
|
2009-11-24 19:40:29 -05:00
|
|
|
if (mConnection != null)
|
|
|
|
{
|
|
|
|
id += "/" + mConnection.getLogId();
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A cacheable class that stores the details for a single IMAP connection.
|
|
|
|
*/
|
2009-11-24 19:40:29 -05:00
|
|
|
class ImapConnection
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
private Socket mSocket;
|
|
|
|
private PeekableInputStream mIn;
|
|
|
|
private OutputStream mOut;
|
|
|
|
private ImapResponseParser mParser;
|
|
|
|
private int mNextCommandTag;
|
2009-10-21 20:41:06 -04:00
|
|
|
protected Set<String> capabilities = new HashSet<String>();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
private String getLogId()
|
|
|
|
{
|
|
|
|
return "conn" + hashCode();
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
private List<ImapResponse> receiveCapabilities(List<ImapResponse> responses)
|
|
|
|
{
|
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
|
|
|
ImapList capabilityList = null;
|
2010-05-09 11:27:41 -04:00
|
|
|
if (response.size() > 0 && ImapResponseParser.equalsIgnoreCase(response.get(0), "OK"))
|
2010-02-27 12:34:38 -05:00
|
|
|
{
|
|
|
|
for (Object thisPart : response)
|
|
|
|
{
|
|
|
|
if (thisPart instanceof ImapList)
|
|
|
|
{
|
|
|
|
ImapList thisList = (ImapList)thisPart;
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(thisList.get(0), CAPABILITY_CAPABILITY))
|
2010-02-27 12:34:38 -05:00
|
|
|
{
|
|
|
|
capabilityList = thisList;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (response.mTag == null)
|
|
|
|
{
|
|
|
|
capabilityList = response;
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
if (capabilityList != null)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (capabilityList.size() > 0 && ImapResponseParser.equalsIgnoreCase(capabilityList.get(0), CAPABILITY_CAPABILITY))
|
2010-02-27 12:34:38 -05:00
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, "Saving " + capabilityList.size() + " capabilities for " + getLogId());
|
|
|
|
}
|
|
|
|
for (Object capability : capabilityList)
|
|
|
|
{
|
|
|
|
if (capability instanceof String)
|
|
|
|
{
|
|
|
|
// if (K9.DEBUG)
|
|
|
|
// {
|
|
|
|
// Log.v(K9.LOG_TAG, "Saving capability '" + capability + "' for " + getLogId());
|
|
|
|
// }
|
|
|
|
capabilities.add((String)capability);
|
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return responses;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
public void open() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
if (isOpen())
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-02-24 20:50:20 -05:00
|
|
|
boolean authSuccess = false;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
mNextCommandTag = 1;
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
try
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
Security.setProperty("networkaddress.cache.ttl", "0");
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.w(K9.LOG_TAG, "Could not set DNS ttl to 0 for " + getLogId(), e);
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
SocketAddress socketAddress = new InetSocketAddress(mHost, mPort);
|
|
|
|
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Connection " + getLogId() + " connecting to " + mHost + " @ IP addr " + socketAddress);
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED ||
|
2009-11-24 19:40:29 -05:00
|
|
|
mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
final boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED;
|
2009-11-24 19:40:29 -05:00
|
|
|
sslContext.init(null, new TrustManager[]
|
2009-11-26 00:10:12 -05:00
|
|
|
{
|
|
|
|
TrustManagerFactory.get(mHost, secure)
|
|
|
|
}, new SecureRandom());
|
2008-11-01 17:32:06 -04:00
|
|
|
mSocket = sslContext.getSocketFactory().createSocket();
|
|
|
|
mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mSocket = new Socket();
|
|
|
|
mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
setReadTimeout(Store.SOCKET_READ_TIMEOUT);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
mIn = new PeekableInputStream(new BufferedInputStream(mSocket.getInputStream(),
|
2009-11-24 19:40:29 -05:00
|
|
|
1024));
|
2008-11-01 17:32:06 -04:00
|
|
|
mParser = new ImapResponseParser(mIn);
|
|
|
|
mOut = mSocket.getOutputStream();
|
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
capabilities.clear();
|
2009-11-22 12:01:04 -05:00
|
|
|
ImapResponse nullResponse = mParser.readResponse();
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, getLogId() + "<<<" + nullResponse);
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
List<ImapResponse> nullResponses = new LinkedList<ImapResponse>();
|
|
|
|
nullResponses.add(nullResponse);
|
|
|
|
receiveCapabilities(nullResponses);
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
if (hasCapability(CAPABILITY_CAPABILITY) == false)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-03-06 19:30:40 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Did not get capabilities in banner, requesting CAPABILITY for " + getLogId());
|
2010-02-27 12:34:38 -05:00
|
|
|
List<ImapResponse> responses = receiveCapabilities(executeSimpleCommand(COMMAND_CAPABILITY));
|
|
|
|
if (responses.size() != 2)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2010-02-27 12:34:38 -05:00
|
|
|
throw new MessagingException("Invalid CAPABILITY response received");
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
if (mConnectionSecurity == CONNECTION_SECURITY_TLS_OPTIONAL
|
2009-11-24 19:40:29 -05:00
|
|
|
|| mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED)
|
|
|
|
{
|
|
|
|
|
2010-02-27 12:34:38 -05:00
|
|
|
if (hasCapability("STARTTLS"))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
// STARTTLS
|
|
|
|
executeSimpleCommand("STARTTLS");
|
|
|
|
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED;
|
2009-11-24 19:40:29 -05:00
|
|
|
sslContext.init(null, new TrustManager[]
|
2009-11-26 00:10:12 -05:00
|
|
|
{
|
|
|
|
TrustManagerFactory.get(mHost, secure)
|
|
|
|
}, new SecureRandom());
|
2008-11-01 17:32:06 -04:00
|
|
|
mSocket = sslContext.getSocketFactory().createSocket(mSocket, mHost, mPort,
|
2009-11-24 19:40:29 -05:00
|
|
|
true);
|
2008-11-01 17:32:06 -04:00
|
|
|
mSocket.setSoTimeout(Store.SOCKET_READ_TIMEOUT);
|
|
|
|
mIn = new PeekableInputStream(new BufferedInputStream(mSocket
|
2009-11-24 19:40:29 -05:00
|
|
|
.getInputStream(), 1024));
|
2008-11-01 17:32:06 -04:00
|
|
|
mParser = new ImapResponseParser(mIn);
|
|
|
|
mOut = mSocket.getOutputStream();
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else if (mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException("TLS not supported but required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-08 18:47:34 -05:00
|
|
|
mOut = new BufferedOutputStream(mOut, 1024);
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2010-05-11 22:51:59 -04:00
|
|
|
if (mHost.endsWith("yahoo.com"))
|
2010-05-09 11:27:41 -04:00
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Found Yahoo! account. Sending proprietary commands.");
|
|
|
|
executeSimpleCommand("ID (\"GUID\" \"1\")");
|
|
|
|
}
|
2009-12-20 00:41:43 -05:00
|
|
|
if (mAuthType == AuthType.CRAM_MD5)
|
2009-12-20 00:15:20 -05:00
|
|
|
{
|
2009-12-20 00:41:43 -05:00
|
|
|
authCramMD5();
|
2010-03-06 19:30:40 -05:00
|
|
|
// The authCramMD5 method called on the previous line does not allow for handling updated capabilities
|
2010-02-27 12:48:00 -05:00
|
|
|
// sent by the server. So, to make sure we update to the post-authentication capability list
|
|
|
|
// we fetch the capabilities here.
|
2010-03-06 19:30:40 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Updating capabilities after CRAM-MD5 authentication for " + getLogId());
|
2010-02-27 12:48:00 -05:00
|
|
|
List<ImapResponse> responses = receiveCapabilities(executeSimpleCommand(COMMAND_CAPABILITY));
|
|
|
|
if (responses.size() != 2)
|
|
|
|
{
|
|
|
|
throw new MessagingException("Invalid CAPABILITY response received");
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2009-12-20 00:15:20 -05:00
|
|
|
}
|
2009-12-20 00:41:43 -05:00
|
|
|
else if (mAuthType == AuthType.PLAIN)
|
2009-12-20 00:15:20 -05:00
|
|
|
{
|
2010-02-27 12:34:38 -05:00
|
|
|
receiveCapabilities(executeSimpleCommand("LOGIN \"" + escapeString(mUsername) + "\" \"" + escapeString(mPassword) + "\"", true));
|
2009-12-20 00:15:20 -05:00
|
|
|
}
|
2009-02-24 20:50:20 -05:00
|
|
|
authSuccess = true;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (ImapException ie)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new AuthenticationFailedException(ie.getAlertText(), ie);
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new AuthenticationFailedException(null, me);
|
|
|
|
}
|
2010-03-06 19:30:40 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, CAPABILITY_COMPRESS_DEFLATE + " = " + hasCapability(CAPABILITY_COMPRESS_DEFLATE));
|
|
|
|
}
|
|
|
|
if (hasCapability(CAPABILITY_COMPRESS_DEFLATE))
|
|
|
|
{
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager)K9.app.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
boolean useCompression = true;
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-03-06 19:30:40 -05:00
|
|
|
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
|
|
|
|
if (netInfo != null)
|
|
|
|
{
|
|
|
|
int type = netInfo.getType();
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "On network type " + type);
|
|
|
|
useCompression = mAccount.useCompression(type);
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-03-06 19:30:40 -05:00
|
|
|
}
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "useCompression " + useCompression);
|
|
|
|
if (useCompression)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
executeSimpleCommand(COMMAND_COMPRESS_DEFLATE);
|
|
|
|
ZInputStream zInputStream = new ZInputStream(mSocket.getInputStream(), true);
|
|
|
|
zInputStream.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
|
|
|
|
mIn = new PeekableInputStream(new BufferedInputStream(zInputStream, 1024));
|
|
|
|
mParser = new ImapResponseParser(mIn);
|
|
|
|
ZOutputStream zOutputStream = new ZOutputStream(mSocket.getOutputStream(), JZlib.Z_BEST_SPEED, true);
|
|
|
|
mOut = new BufferedOutputStream(zOutputStream, 1024);
|
|
|
|
zOutputStream.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.i(K9.LOG_TAG, "Compression enabled for " + getLogId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to negotiate compression", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "NAMESPACE = " + hasCapability(CAPABILITY_NAMESPACE)
|
2009-12-06 19:56:06 -05:00
|
|
|
+ ", mPathPrefix = " + mPathPrefix);
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
if (mPathPrefix == null)
|
|
|
|
{
|
|
|
|
if (hasCapability(CAPABILITY_NAMESPACE))
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "mPathPrefix is unset and server has NAMESPACE capability");
|
2009-11-28 09:51:44 -05:00
|
|
|
List<ImapResponse> namespaceResponses =
|
|
|
|
executeSimpleCommand(COMMAND_NAMESPACE);
|
|
|
|
for (ImapResponse response : namespaceResponses)
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(response.get(0), COMMAND_NAMESPACE))
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got NAMESPACE response " + response + " on " + getLogId());
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
Object personalNamespaces = response.get(1);
|
|
|
|
if (personalNamespaces != null && personalNamespaces instanceof ImapList)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got personal namespaces: " + personalNamespaces);
|
2009-11-28 09:51:44 -05:00
|
|
|
ImapList bracketed = (ImapList)personalNamespaces;
|
|
|
|
Object firstNamespace = bracketed.get(0);
|
|
|
|
if (firstNamespace != null && firstNamespace instanceof ImapList)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got first personal namespaces: " + firstNamespace);
|
2009-11-28 09:51:44 -05:00
|
|
|
bracketed = (ImapList)firstNamespace;
|
|
|
|
mPathPrefix = bracketed.getString(0);
|
|
|
|
mPathDelimeter = bracketed.getString(1);
|
|
|
|
mCombinedPrefix = null;
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got path '" + mPathPrefix + "' and separator '" + mPathDelimeter + "'");
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "mPathPrefix is unset but server does not have NAMESPACE capability");
|
2009-11-28 09:51:44 -05:00
|
|
|
mPathPrefix = "";
|
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (SSLException e)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new CertificateValidationException(e.getMessage(), e);
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (GeneralSecurityException gse)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
throw new MessagingException(
|
2009-11-24 19:40:29 -05:00
|
|
|
"Unable to open connection to IMAP server due to security error.", gse);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
catch (ConnectException ce)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
String ceMess = ce.getMessage();
|
|
|
|
String[] tokens = ceMess.split("-");
|
|
|
|
if (tokens != null && tokens.length > 1 && tokens[1] != null)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Stripping host/port from ConnectionException for " + getLogId(), ce);
|
2009-11-24 19:40:29 -05:00
|
|
|
throw new ConnectException(tokens[1].trim());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw ce;
|
|
|
|
}
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
}
|
2009-02-24 20:50:20 -05:00
|
|
|
finally
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
if (authSuccess == false)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Failed to login, closing connection for " + getLogId());
|
2009-11-24 19:40:29 -05:00
|
|
|
close();
|
|
|
|
}
|
2009-02-24 20:50:20 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-12-20 00:15:20 -05:00
|
|
|
protected void authCramMD5() throws AuthenticationFailedException, MessagingException
|
|
|
|
{
|
2009-12-20 00:41:43 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
String tag = sendCommand("AUTHENTICATE CRAM-MD5", false);
|
|
|
|
byte[] buf = new byte[ 1024 ];
|
|
|
|
int b64NonceLen = 0;
|
|
|
|
for (int i = 0; i < buf.length; i++)
|
|
|
|
{
|
|
|
|
buf[ i ] = (byte)mIn.read();
|
|
|
|
if (buf[i] == 0x0a)
|
|
|
|
{
|
|
|
|
b64NonceLen = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (b64NonceLen == 0)
|
|
|
|
{
|
|
|
|
throw new AuthenticationFailedException("Error negotiating CRAM-MD5: nonce too long.");
|
|
|
|
}
|
|
|
|
byte[] b64NonceTrim = new byte[ b64NonceLen - 2 ];
|
|
|
|
System.arraycopy(buf, 1, b64NonceTrim, 0, b64NonceLen - 2);
|
|
|
|
byte[] nonce = Base64.decodeBase64(b64NonceTrim);
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, "Got nonce: " + new String(b64NonceTrim, "US-ASCII"));
|
|
|
|
Log.d(K9.LOG_TAG, "Plaintext nonce: " + new String(nonce, "US-ASCII"));
|
|
|
|
}
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-12-20 00:41:43 -05:00
|
|
|
byte[] ipad = new byte[64];
|
|
|
|
byte[] opad = new byte[64];
|
|
|
|
byte[] secretBytes = mPassword.getBytes("US-ASCII");
|
|
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
if (secretBytes.length > 64)
|
|
|
|
{
|
|
|
|
secretBytes = md.digest(secretBytes);
|
|
|
|
}
|
|
|
|
System.arraycopy(secretBytes, 0, ipad, 0, secretBytes.length);
|
|
|
|
System.arraycopy(secretBytes, 0, opad, 0, secretBytes.length);
|
|
|
|
for (int i = 0; i < ipad.length; i++) ipad[i] ^= 0x36;
|
|
|
|
for (int i = 0; i < opad.length; i++) opad[i] ^= 0x5c;
|
|
|
|
md.update(ipad);
|
|
|
|
byte[] firstPass = md.digest(nonce);
|
|
|
|
md.update(opad);
|
|
|
|
byte[] result = md.digest(firstPass);
|
|
|
|
String plainCRAM = mUsername + " " + new String(Hex.encodeHex(result));
|
|
|
|
byte[] b64CRAM = Base64.encodeBase64(plainCRAM.getBytes("US-ASCII"));
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, "Username == " + mUsername);
|
|
|
|
Log.d(K9.LOG_TAG, "plainCRAM: " + plainCRAM);
|
|
|
|
Log.d(K9.LOG_TAG, "b64CRAM: " + new String(b64CRAM, "US-ASCII"));
|
|
|
|
}
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-12-20 00:41:43 -05:00
|
|
|
mOut.write(b64CRAM);
|
|
|
|
mOut.write(new byte[] { 0x0d, 0x0a });
|
|
|
|
mOut.flush();
|
|
|
|
int respLen = 0;
|
|
|
|
for (int i = 0; i < buf.length; i++)
|
|
|
|
{
|
|
|
|
buf[ i ] = (byte)mIn.read();
|
|
|
|
if (buf[i] == 0x0a)
|
|
|
|
{
|
|
|
|
respLen = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String toMatch = tag + " OK";
|
|
|
|
String respStr = new String(buf, 0, respLen);
|
|
|
|
if (!respStr.startsWith(toMatch))
|
|
|
|
{
|
|
|
|
throw new AuthenticationFailedException("CRAM-MD5 error: " + respStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
throw new AuthenticationFailedException("CRAM-MD5 Auth Failed.");
|
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException nsae)
|
|
|
|
{
|
|
|
|
throw new AuthenticationFailedException("MD5 Not Available.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
protected void setReadTimeout(int millis) throws SocketException
|
|
|
|
{
|
|
|
|
mSocket.setSoTimeout(millis);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
protected boolean isIdleCapable()
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Connection " + getLogId() + " has " + capabilities.size() + " capabilities");
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
return capabilities.contains(CAPABILITY_IDLE);
|
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
protected boolean hasCapability(String capability)
|
|
|
|
{
|
|
|
|
return capabilities.contains(capability);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private boolean isOpen()
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
return (mIn != null && mOut != null && mSocket != null && mSocket.isConnected() && !mSocket.isClosed());
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private void close()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
// if (isOpen()) {
|
|
|
|
// try {
|
|
|
|
// executeSimpleCommand("LOGOUT");
|
|
|
|
// } catch (Exception e) {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// }
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mIn.close();
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mOut.close();
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
try
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mSocket.close();
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
mIn = null;
|
|
|
|
mOut = null;
|
|
|
|
mSocket = null;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private ImapResponse readResponse() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
try
|
2009-11-22 12:01:04 -05:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
ImapResponse response = mParser.readResponse();
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, getLogId() + "<<<" + response);
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
throw ioe;
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-04-11 11:24:22 -04:00
|
|
|
private String escapeString(String in)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
if (in == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String out = in.replaceAll("\\\\", "\\\\\\\\");
|
|
|
|
out = out.replaceAll("\"", "\\\\\"");
|
|
|
|
return out;
|
2009-04-11 11:24:22 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
private void sendContinuation(String continuation) throws IOException
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
|
|
|
mOut.write(continuation.getBytes());
|
|
|
|
mOut.write('\r');
|
|
|
|
mOut.write('\n');
|
|
|
|
mOut.flush();
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, getLogId() + ">>> " + continuation);
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
public String sendCommand(String command, boolean sensitive)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException, IOException
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
open();
|
|
|
|
String tag = Integer.toString(mNextCommandTag++);
|
|
|
|
String commandToSend = tag + " " + command;
|
|
|
|
mOut.write(commandToSend.getBytes());
|
|
|
|
mOut.write('\r');
|
|
|
|
mOut.write('\n');
|
|
|
|
mOut.flush();
|
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (sensitive && !K9.DEBUG_SENSITIVE)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.v(K9.LOG_TAG, getLogId() + ">>> "
|
2009-11-24 19:40:29 -05:00
|
|
|
+ "[Command Hidden, Enable Sensitive Debug Logging To Show]");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.v(K9.LOG_TAG, getLogId() + ">>> " + commandToSend);
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
throw ioe;
|
|
|
|
}
|
|
|
|
catch (ImapException ie)
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
throw ie;
|
|
|
|
}
|
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
throw me;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<ImapResponse> executeSimpleCommand(String command) throws IOException,
|
2009-11-24 19:40:29 -05:00
|
|
|
ImapException, MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return executeSimpleCommand(command, false);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
public List<ImapResponse> executeSimpleCommand(String command, boolean sensitive) throws IOException,
|
2009-11-24 19:40:29 -05:00
|
|
|
ImapException, MessagingException
|
|
|
|
{
|
2009-10-21 20:41:06 -04:00
|
|
|
return executeSimpleCommand(command, sensitive, null);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
private List<ImapResponse> executeSimpleCommand(String command, boolean sensitive, UntaggedHandler untaggedHandler)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws IOException, ImapException, MessagingException
|
|
|
|
{
|
2009-10-25 19:56:25 -04:00
|
|
|
String commandToLog = command;
|
2009-12-14 21:50:53 -05:00
|
|
|
if (sensitive && !K9.DEBUG_SENSITIVE)
|
2009-10-25 19:56:25 -04:00
|
|
|
{
|
|
|
|
commandToLog = "*sensitive*";
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Sending IMAP command " + commandToLog + " on connection " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
String tag = sendCommand(command, sensitive);
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Sent IMAP command " + commandToLog + " with tag " + tag + " for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
ArrayList<ImapResponse> responses = new ArrayList<ImapResponse>();
|
|
|
|
ImapResponse response;
|
|
|
|
do
|
2009-05-05 20:16:47 -04:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
response = mParser.readResponse();
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, getLogId() + "<<<" + response);
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2010-05-09 11:27:41 -04:00
|
|
|
if (response.mTag != null && response.mTag.equalsIgnoreCase(tag) == false)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-01-03 15:34:22 -05:00
|
|
|
Log.w(K9.LOG_TAG, "After sending tag " + tag + ", got tag response from previous command " + response + " for " + getLogId());
|
2009-11-24 19:40:29 -05:00
|
|
|
Iterator<ImapResponse> iter = responses.iterator();
|
|
|
|
while (iter.hasNext())
|
|
|
|
{
|
|
|
|
ImapResponse delResponse = iter.next();
|
|
|
|
if (delResponse.mTag != null || delResponse.size() < 2
|
2010-05-09 11:27:41 -04:00
|
|
|
|| (ImapResponseParser.equalsIgnoreCase(delResponse.get(1), "EXISTS") == false && ImapResponseParser.equalsIgnoreCase(delResponse.get(1), "EXPUNGE") == false))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response.mTag = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (untaggedHandler != null)
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
untaggedHandler.handleAsyncUntaggedResponse(response);
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
responses.add(response);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
while (response.mTag == null);
|
2010-05-09 11:27:41 -04:00
|
|
|
if (response.size() < 1 || !ImapResponseParser.equalsIgnoreCase(response.get(0), "OK"))
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
throw new ImapException("Command: " + commandToLog + "; response: " + response.toString(), response.getAlertText());
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
return responses;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
class ImapMessage extends MimeMessage
|
|
|
|
{
|
2010-04-29 00:59:14 -04:00
|
|
|
ImapMessage(String uid, Folder folder)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mUid = uid;
|
|
|
|
this.mFolder = folder;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setSize(int size)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mSize = size;
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void parse(InputStream in) throws IOException, MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super.parse(in);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setFlagInternal(Flag flag, boolean set) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super.setFlag(flag, set);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setFlag(Flag flag, boolean set) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super.setFlag(flag, set);
|
|
|
|
mFolder.setFlags(new Message[] { this }, new Flag[] { flag }, set);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
Complete merge of DAmail functionality into K9mail. Following
features are added to K9mail:
1) Show unread message count on each folder
2) Sum unread count of all shown folders in an account to the account display
3) Periodically check selected folders for new mail, not just Inbox
4) Don't refresh folder when opened (unless folder is empty)
5) Show date and time of last sync for each folder
6) Fix timer for automatic periodic sync (use wakelock to assure completion)
7) Optimize local folder queries (speeds up account and folder lists)
8) Show Loading... message in status bar indicating which folder is being synced
9) Eliminate redundant sync of new messages (performance enhancement)
10) Improve notification text for multiple accounts
11) Do not automatically sync folders more often than the account-specific period
12) Use user-configured date and time formats
13) Select which folders are shown, using configurable Classes
14) Select which folders are synced, using configurable Classes
15) Added context (long press) menu to folders, to provide for Refresh
and Folder Settings
16) Status light flashes purple when there are unread messages
17) Folder list more quickly eliminates display of deleted and out-of-Class folders.
18) Delete works
19) Mark all messages as read (in the folder context menu)
20) Notifications only for new unread messages
21) One minute synchronization frequency
22) Deleting an unread message decrements unread counter
23) Notifications work for POP3 accounts
24) Message deletes work for POP3 accounts
25) Explicit errors show in folder list
26) Stack traces saved to folder K9mail-errors
27) Clear pending actions (danger, for emergencies only!)
28) Delete policy in Account settings
29) DNS cache in InetAddress disabled
30) Trapped some crash-causing error conditions
31) Eliminate duplicate copies to Sent folder
32) Prevent crashes due to message listener concurrency
33) Empty Trash
34) Nuclear "Mark all messages as read" (marks all messages as read in
server-side folder, irrespective of which messages have been downloaded)
35) Forward (alternate) to allow forwarding email through other programs
36) Accept text/plain Intents to allow other programs to send email through K9mail
37) Displays Outbox sending status
38) Manual retry of outbox sending when "Refresh"ing Outbox
39) Folder error status is persisted
40) Ability to log to arbitrary file
Fixes K9 issues 11, 23, 24, 65, 69, 71, 79, 81, 82, 83, 87, 101, 104,
107, 120, 148, 154
2008-12-30 22:49:09 -05:00
|
|
|
@Override
|
|
|
|
public void delete(String trashFolderName) throws MessagingException
|
|
|
|
{
|
2009-11-29 13:07:34 -05:00
|
|
|
getFolder().delete(new Message[] { this }, trashFolderName);
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
class ImapBodyPart extends MimeBodyPart
|
|
|
|
{
|
|
|
|
public ImapBodyPart() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setSize(int size)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mSize = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
class ImapException extends MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
String mAlertText;
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public ImapException(String message, String alertText, Throwable throwable)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super(message, throwable);
|
|
|
|
this.mAlertText = alertText;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public ImapException(String message, String alertText)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super(message);
|
|
|
|
this.mAlertText = alertText;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getAlertText()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mAlertText;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setAlertText(String alertText)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mAlertText = alertText;
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
public class ImapFolderPusher extends ImapFolder implements UntaggedHandler
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
final PushReceiver receiver;
|
2009-10-21 20:41:06 -04:00
|
|
|
Thread listeningThread = null;
|
2009-11-22 12:01:04 -05:00
|
|
|
final AtomicBoolean stop = new AtomicBoolean(false);
|
|
|
|
final AtomicBoolean idling = new AtomicBoolean(false);
|
|
|
|
final AtomicBoolean doneSent = new AtomicBoolean(false);
|
|
|
|
final AtomicInteger delayTime = new AtomicInteger(NORMAL_DELAY_TIME);
|
2010-04-14 23:17:25 -04:00
|
|
|
final AtomicInteger idleFailureCount = new AtomicInteger(0);
|
2010-05-15 15:35:07 -04:00
|
|
|
final AtomicBoolean needsPoll = new AtomicBoolean(false);
|
2009-11-26 00:10:12 -05:00
|
|
|
List<ImapResponse> storedUntaggedResponses = new ArrayList<ImapResponse>();
|
2010-05-16 20:30:32 -04:00
|
|
|
TracingWakeLock wakeLock = null;
|
2010-05-15 15:46:16 -04:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
public ImapFolderPusher(ImapStore store, String name, PushReceiver nReceiver)
|
|
|
|
{
|
|
|
|
super(store, name);
|
|
|
|
receiver = nReceiver;
|
2010-05-16 20:30:32 -04:00
|
|
|
TracingPowerManager pm = TracingPowerManager.getPowerManager(receiver.getContext());
|
|
|
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ImapFolderPusher " + store.getAccount().getDescription() + ":" + getName());
|
|
|
|
wakeLock.setReferenceCounted(false);
|
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
public void refresh() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
if (idling.get())
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
wakeLock.acquire(K9.PUSH_WAKE_LOCK_TIMEOUT);
|
2009-10-21 20:41:06 -04:00
|
|
|
sendDone();
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
private void sendDone() throws IOException, MessagingException
|
|
|
|
{
|
|
|
|
if (doneSent.compareAndSet(false, true) == true)
|
|
|
|
{
|
2010-01-16 09:14:12 -05:00
|
|
|
mConnection.setReadTimeout(Store.SOCKET_READ_TIMEOUT);
|
2009-10-21 20:41:06 -04:00
|
|
|
sendContinuation("DONE");
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
private void sendContinuation(String continuation)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException, IOException
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
|
|
|
if (mConnection != null)
|
|
|
|
{
|
|
|
|
mConnection.sendContinuation(continuation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
public void start()
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
|
|
|
Runnable runner = new Runnable()
|
|
|
|
{
|
|
|
|
public void run()
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
wakeLock.acquire(K9.PUSH_WAKE_LOCK_TIMEOUT);
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Pusher starting for " + getLogId());
|
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
while (stop.get() != true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int oldUidNext = -1;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
String pushStateS = receiver.getPushState(getName());
|
|
|
|
ImapPushState pushState = ImapPushState.parse(pushStateS);
|
|
|
|
oldUidNext = pushState.uidNext;
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Got oldUidNext " + oldUidNext + " for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Unable to get oldUidNext for " + getLogId(), e);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2010-04-26 23:02:17 -04:00
|
|
|
ImapConnection oldConnection = mConnection;
|
2010-05-15 15:35:07 -04:00
|
|
|
internalOpen(OpenMode.READ_ONLY);
|
2009-10-21 20:41:06 -04:00
|
|
|
if (mConnection == null)
|
|
|
|
{
|
|
|
|
receiver.pushError("Could not establish connection for IDLE", null);
|
|
|
|
throw new MessagingException("Could not establish connection for IDLE");
|
|
|
|
|
|
|
|
}
|
|
|
|
if (mConnection.isIdleCapable() == false)
|
|
|
|
{
|
|
|
|
stop.set(true);
|
|
|
|
receiver.pushError("IMAP server is not IDLE capable: " + mConnection.toString(), null);
|
|
|
|
throw new MessagingException("IMAP server is not IDLE capable:" + mConnection.toString());
|
|
|
|
}
|
2010-01-17 19:11:02 -05:00
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
if (mAccount.isPushPollOnConnect() && (mConnection != oldConnection || needsPoll.getAndSet(false) == true))
|
2010-04-26 23:02:17 -04:00
|
|
|
{
|
2010-05-15 15:35:07 -04:00
|
|
|
List<ImapResponse> untaggedResponses = new ArrayList<ImapResponse>(storedUntaggedResponses);
|
|
|
|
storedUntaggedResponses.clear();
|
|
|
|
processUntaggedResponses(untaggedResponses);
|
2010-04-26 23:02:17 -04:00
|
|
|
receiver.syncFolder(ImapFolderPusher.this);
|
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
int startUid = oldUidNext;
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-27 00:06:26 -04:00
|
|
|
int newUidNext = uidNext;
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-27 00:06:26 -04:00
|
|
|
if (newUidNext == -1)
|
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, "uidNext is -1, using search to find highest UID");
|
|
|
|
}
|
|
|
|
int highestUid = getHighestUid();
|
|
|
|
if (highestUid != -1)
|
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
2010-05-09 11:27:41 -04:00
|
|
|
Log.d(K9.LOG_TAG, "highest UID = " + highestUid);
|
2010-04-27 00:06:26 -04:00
|
|
|
newUidNext = highestUid + 1;
|
|
|
|
if (K9.DEBUG)
|
2010-05-11 22:51:59 -04:00
|
|
|
Log.d(K9.LOG_TAG, "highest UID = " + highestUid
|
|
|
|
+ ", set newUidNext to " + newUidNext);
|
2010-04-27 00:06:26 -04:00
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-27 00:06:26 -04:00
|
|
|
if (startUid < newUidNext - mAccount.getDisplayCount())
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2010-04-27 00:06:26 -04:00
|
|
|
startUid = newUidNext - mAccount.getDisplayCount();
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
|
|
|
if (startUid < 1)
|
|
|
|
{
|
|
|
|
startUid = 1;
|
|
|
|
}
|
2010-04-27 00:06:26 -04:00
|
|
|
if (newUidNext > startUid)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
2010-04-27 00:06:26 -04:00
|
|
|
Log.i(K9.LOG_TAG, "Needs sync from uid " + startUid + " to " + newUidNext + " for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
List<Message> messages = new ArrayList<Message>();
|
2010-04-27 00:06:26 -04:00
|
|
|
for (int uid = startUid; uid < newUidNext; uid++)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
|
|
|
ImapMessage message = new ImapMessage("" + uid, ImapFolderPusher.this);
|
|
|
|
messages.add(message);
|
|
|
|
}
|
|
|
|
if (messages.size() > 0)
|
|
|
|
{
|
|
|
|
pushMessages(messages, true);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
if (stop.get() == false)
|
2009-11-22 22:16:44 -05:00
|
|
|
{
|
2009-11-26 00:10:12 -05:00
|
|
|
List<ImapResponse> untaggedResponses = null;
|
|
|
|
if (storedUntaggedResponses.size() > 0)
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Processing " + storedUntaggedResponses.size() + " from previous commands for " + getLogId());
|
2009-11-26 00:10:12 -05:00
|
|
|
untaggedResponses = new ArrayList<ImapResponse>(storedUntaggedResponses);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "About to IDLE for " + getLogId());
|
2009-11-26 00:10:12 -05:00
|
|
|
|
|
|
|
receiver.setPushActive(getName(), true);
|
|
|
|
idling.set(true);
|
|
|
|
doneSent.set(false);
|
2010-05-09 11:27:41 -04:00
|
|
|
mConnection.setReadTimeout((getAccount().getIdleRefreshMinutes() * 60 * 1000) + IDLE_READ_TIMEOUT_INCREMENT);
|
2010-02-10 08:52:25 -05:00
|
|
|
untaggedResponses = executeSimpleCommand(COMMAND_IDLE, false, ImapFolderPusher.this);
|
2009-11-26 00:10:12 -05:00
|
|
|
idling.set(false);
|
|
|
|
|
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
if (stop.get() == false)
|
|
|
|
{
|
|
|
|
storedUntaggedResponses.clear();
|
|
|
|
processUntaggedResponses(untaggedResponses);
|
|
|
|
}
|
2009-11-22 22:16:44 -05:00
|
|
|
delayTime.set(NORMAL_DELAY_TIME);
|
2010-04-14 23:17:25 -04:00
|
|
|
idleFailureCount.set(0);
|
2009-11-22 22:16:44 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
wakeLock.acquire(K9.PUSH_WAKE_LOCK_TIMEOUT);
|
2009-11-26 00:10:12 -05:00
|
|
|
storedUntaggedResponses.clear();
|
2009-10-21 20:41:06 -04:00
|
|
|
idling.set(false);
|
|
|
|
receiver.setPushActive(getName(), false);
|
|
|
|
try
|
|
|
|
{
|
Implementation of complete IMAP two-phase "delete/expunge" behavior.
On each IMAP account, the expunge behavior can be set to expunge
messages in a folder as soon as a move or delete is performed on the
folder ("immediately"), each time the folder is polled, or only when
executed manually.
In the Message List, there is now an Expunge action in the option
menu.
In the Folder List, there is now an Expunge action in the context
menu (long-press on the folder).
For IMAP accounts, it is also possible to disable the copying of deleted messages to the
Trash folder, by setting the Trash folder to -NONE-.
Fixes Issue 536.
Separately, in WebDAV accounts, the user can now choose the
server-side equivalents of the special folders, just like for IMAP.
2009-12-20 18:13:49 -05:00
|
|
|
close();
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
catch (Exception me)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Got exception while closing for exception for " + getLogId(), me);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
if (stop.get() == true)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.i(K9.LOG_TAG, "Got exception while idling, but stop is set for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-14 23:17:25 -04:00
|
|
|
receiver.pushError("Push error for " + getName(), e);
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Got exception while idling for " + getLogId(), e);
|
2009-11-22 12:01:04 -05:00
|
|
|
int delayTimeInt = delayTime.get();
|
2010-05-16 20:30:32 -04:00
|
|
|
receiver.sleep(wakeLock, delayTimeInt);
|
2009-11-22 12:01:04 -05:00
|
|
|
delayTimeInt *= 2;
|
|
|
|
if (delayTimeInt > MAX_DELAY_TIME)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
delayTimeInt = MAX_DELAY_TIME;
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-22 12:01:04 -05:00
|
|
|
delayTime.set(delayTimeInt);
|
2010-04-14 23:17:25 -04:00
|
|
|
if (idleFailureCount.incrementAndGet() > IDLE_FAILURE_COUNT_LIMIT)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Disabling pusher for " + getLogId() + " after " + idleFailureCount.get() + " consecutive errors");
|
|
|
|
receiver.pushError("Push disabled for " + getName() + " after " + idleFailureCount.get() + " consecutive errors", e);
|
|
|
|
stop.set(true);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
receiver.setPushActive(getName(), false);
|
2009-10-21 20:41:06 -04:00
|
|
|
try
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Pusher for " + getLogId() + " is exiting");
|
Implementation of complete IMAP two-phase "delete/expunge" behavior.
On each IMAP account, the expunge behavior can be set to expunge
messages in a folder as soon as a move or delete is performed on the
folder ("immediately"), each time the folder is polled, or only when
executed manually.
In the Message List, there is now an Expunge action in the option
menu.
In the Folder List, there is now an Expunge action in the context
menu (long-press on the folder).
For IMAP accounts, it is also possible to disable the copying of deleted messages to the
Trash folder, by setting the Trash folder to -NONE-.
Fixes Issue 536.
Separately, in WebDAV accounts, the user can now choose the
server-side equivalents of the special folders, just like for IMAP.
2009-12-20 18:13:49 -05:00
|
|
|
close();
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
catch (Exception me)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Got exception while closing for " + getLogId(), me);
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
wakeLock.release();
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
listeningThread = new Thread(runner);
|
|
|
|
listeningThread.start();
|
|
|
|
}
|
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
@Override
|
|
|
|
protected void handleUntaggedResponse(ImapResponse response)
|
|
|
|
{
|
|
|
|
if (response.mTag == null && response.size() > 1)
|
|
|
|
{
|
|
|
|
Object responseType = response.get(1);
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(responseType, "FETCH")
|
|
|
|
|| ImapResponseParser.equalsIgnoreCase(responseType, "EXPUNGE")
|
|
|
|
|| ImapResponseParser.equalsIgnoreCase(responseType, "EXISTS"))
|
2009-11-26 00:10:12 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Storing response " + response + " for later processing");
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
storedUntaggedResponses.add(response);
|
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
handlePossibleUidNext(response);
|
2009-11-26 00:10:12 -05:00
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-04-05 22:39:57 -04:00
|
|
|
protected void processUntaggedResponses(List<ImapResponse> responses) throws MessagingException
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
boolean skipSync = false;
|
2009-10-21 20:41:06 -04:00
|
|
|
int oldMessageCount = mMessageCount;
|
2009-11-28 09:51:44 -05:00
|
|
|
if (oldMessageCount == -1)
|
|
|
|
{
|
|
|
|
skipSync = true;
|
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
List<Integer> flagSyncMsgSeqs = new ArrayList<Integer>();
|
2010-05-15 15:35:07 -04:00
|
|
|
List<String> removeMsgUids = new LinkedList<String>();
|
2009-10-21 20:41:06 -04:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
for (ImapResponse response : responses)
|
|
|
|
{
|
2010-05-15 15:35:07 -04:00
|
|
|
oldMessageCount += processUntaggedResponse(oldMessageCount, response, flagSyncMsgSeqs, removeMsgUids);
|
2009-11-26 00:10:12 -05:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
if (skipSync == false)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
if (oldMessageCount < 0)
|
|
|
|
{
|
|
|
|
oldMessageCount = 0;
|
|
|
|
}
|
|
|
|
if (mMessageCount > oldMessageCount)
|
|
|
|
{
|
2010-04-05 22:39:57 -04:00
|
|
|
syncMessages(mMessageCount, true);
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "There are " + flagSyncMsgSeqs + " messages needing flag sync for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
if (flagSyncMsgSeqs.size() > 0)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-26 00:10:12 -05:00
|
|
|
syncMessages(flagSyncMsgSeqs);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2010-05-15 15:35:07 -04:00
|
|
|
if (removeMsgUids.size() > 0)
|
|
|
|
{
|
|
|
|
removeMessages(removeMsgUids);
|
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-04-05 22:39:57 -04:00
|
|
|
private void syncMessages(int end, boolean newArrivals) throws MessagingException
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2010-04-05 22:39:57 -04:00
|
|
|
int oldUidNext = -1;
|
2009-10-21 20:41:06 -04:00
|
|
|
try
|
|
|
|
{
|
2010-04-05 22:39:57 -04:00
|
|
|
String pushStateS = receiver.getPushState(getName());
|
|
|
|
ImapPushState pushState = ImapPushState.parse(pushStateS);
|
|
|
|
oldUidNext = pushState.uidNext;
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Got oldUidNext " + oldUidNext + " for " + getLogId());
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to get oldUidNext for " + getLogId(), e);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-04-05 22:39:57 -04:00
|
|
|
Message[] messageArray = getMessages(end, end, true, null);
|
|
|
|
if (messageArray != null && messageArray.length > 0)
|
|
|
|
{
|
|
|
|
int newUid = Integer.parseInt(messageArray[0].getUid());
|
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Got newUid " + newUid + " for message " + end + " on " + getLogId());
|
|
|
|
int startUid = oldUidNext;
|
|
|
|
if (startUid < newUid - 10)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2010-04-05 22:39:57 -04:00
|
|
|
startUid = newUid - 10;
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2010-04-05 22:39:57 -04:00
|
|
|
if (startUid < 1)
|
|
|
|
{
|
|
|
|
startUid = 1;
|
|
|
|
}
|
|
|
|
if (newUid >= startUid)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-04-05 22:39:57 -04:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Needs sync from uid " + startUid + " to " + newUid + " for " + getLogId());
|
|
|
|
List<Message> messages = new ArrayList<Message>();
|
|
|
|
for (int uid = startUid; uid <= newUid; uid++)
|
|
|
|
{
|
|
|
|
ImapMessage message = new ImapMessage("" + uid, ImapFolderPusher.this);
|
|
|
|
messages.add(message);
|
|
|
|
}
|
|
|
|
if (messages.size() > 0)
|
|
|
|
{
|
|
|
|
pushMessages(messages, true);
|
|
|
|
}
|
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
private void syncMessages(List<Integer> flagSyncMsgSeqs)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Message[] messageArray = null;
|
|
|
|
|
|
|
|
messageArray = getMessages(flagSyncMsgSeqs, true, null);
|
|
|
|
|
|
|
|
List<Message> messages = new ArrayList<Message>();
|
|
|
|
for (Message message : messageArray)
|
|
|
|
{
|
|
|
|
messages.add(message);
|
|
|
|
}
|
|
|
|
pushMessages(messages, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
receiver.pushError("Exception while processing Push untagged responses", e);
|
|
|
|
}
|
|
|
|
}
|
2010-05-15 15:46:16 -04:00
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
private void removeMessages(List<String> removeUids)
|
|
|
|
{
|
|
|
|
List<Message> messages = new ArrayList<Message>(removeUids.size());
|
2010-05-15 15:46:16 -04:00
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Message[] existingMessages = getMessagesFromUids(removeUids, true, null);
|
|
|
|
for (Message existingMessage : existingMessages)
|
|
|
|
{
|
|
|
|
needsPoll.set(true);
|
|
|
|
msgSeqUidMap.clear();
|
|
|
|
String existingUid = existingMessage.getUid();
|
|
|
|
Log.w(K9.LOG_TAG, "Message with UID " + existingUid + " still exists on server, not expunging");
|
|
|
|
removeUids.remove(existingUid);
|
|
|
|
}
|
|
|
|
for (String uid : removeUids)
|
|
|
|
{
|
|
|
|
ImapMessage message = new ImapMessage(uid, this);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
message.setFlagInternal(Flag.DELETED, true);
|
|
|
|
}
|
|
|
|
catch (MessagingException me)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to set DELETED flag on message " + message.getUid());
|
|
|
|
}
|
|
|
|
messages.add(message);
|
|
|
|
}
|
|
|
|
receiver.messagesRemoved(this, messages);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Cannot remove EXPUNGEd messages", e);
|
|
|
|
return;
|
|
|
|
}
|
2010-05-15 15:46:16 -04:00
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
protected int processUntaggedResponse(int oldMessageCount, ImapResponse response, List<Integer> flagSyncMsgSeqs, List<String> removeMsgUids)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2009-10-21 20:41:06 -04:00
|
|
|
super.handleUntaggedResponse(response);
|
2009-11-26 00:10:12 -05:00
|
|
|
int messageCountDelta = 0;
|
2009-10-21 20:41:06 -04:00
|
|
|
if (response.mTag == null && response.size() > 1)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Object responseType = response.get(1);
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(responseType, "FETCH"))
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2010-05-15 15:35:07 -04:00
|
|
|
Log.i(K9.LOG_TAG, "Got FETCH " + response);
|
2009-10-21 20:41:06 -04:00
|
|
|
int msgSeq = response.getNumber(0);
|
2010-05-15 15:46:16 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got untagged FETCH for msgseq " + msgSeq + " for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
if (flagSyncMsgSeqs.contains(msgSeq) == false)
|
|
|
|
{
|
|
|
|
flagSyncMsgSeqs.add(msgSeq);
|
|
|
|
}
|
|
|
|
}
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(responseType, "EXPUNGE"))
|
2009-11-26 00:10:12 -05:00
|
|
|
{
|
|
|
|
int msgSeq = response.getNumber(0);
|
|
|
|
if (msgSeq <= oldMessageCount)
|
|
|
|
{
|
|
|
|
messageCountDelta = -1;
|
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got untagged EXPUNGE for msgseq " + msgSeq + " for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-26 00:10:12 -05:00
|
|
|
List<Integer> newSeqs = new ArrayList<Integer>();
|
|
|
|
Iterator<Integer> flagIter = flagSyncMsgSeqs.iterator();
|
|
|
|
while (flagIter.hasNext())
|
|
|
|
{
|
|
|
|
Integer flagMsg = flagIter.next();
|
|
|
|
if (flagMsg >= msgSeq)
|
|
|
|
{
|
|
|
|
flagIter.remove();
|
|
|
|
if (flagMsg > msgSeq)
|
|
|
|
{
|
|
|
|
newSeqs.add(flagMsg--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flagSyncMsgSeqs.addAll(newSeqs);
|
2010-05-15 15:46:16 -04:00
|
|
|
|
|
|
|
|
2010-05-15 15:35:07 -04:00
|
|
|
List<Integer> msgSeqs = new ArrayList<Integer>(msgSeqUidMap.keySet());
|
|
|
|
Collections.sort(msgSeqs); // Have to do comparisons in order because of msgSeq reductions
|
|
|
|
|
|
|
|
for (Integer msgSeqNumI : msgSeqs)
|
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.v(K9.LOG_TAG, "Comparing EXPUNGEd msgSeq " + msgSeq + " to " + msgSeqNumI);
|
|
|
|
}
|
|
|
|
int msgSeqNum = msgSeqNumI;
|
|
|
|
if (msgSeqNum == msgSeq)
|
|
|
|
{
|
|
|
|
String uid = msgSeqUidMap.get(msgSeqNum);
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, "Scheduling removal of UID " + uid + " because msgSeq " + msgSeqNum + " was expunged");
|
|
|
|
}
|
|
|
|
removeMsgUids.add(uid);
|
|
|
|
msgSeqUidMap.remove(msgSeqNum);
|
|
|
|
}
|
|
|
|
else if (msgSeqNum > msgSeq)
|
|
|
|
{
|
|
|
|
String uid = msgSeqUidMap.get(msgSeqNum);
|
|
|
|
if (K9.DEBUG)
|
|
|
|
{
|
|
|
|
Log.d(K9.LOG_TAG, "Reducing msgSeq for UID " + uid + " from " + msgSeqNum + " to " + (msgSeqNum - 1));
|
|
|
|
}
|
|
|
|
msgSeqUidMap.remove(msgSeqNum);
|
|
|
|
msgSeqUidMap.put(msgSeqNum-1, uid);
|
|
|
|
}
|
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Could not handle untagged FETCH for " + getLogId(), e);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
return messageCountDelta;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
|
|
|
|
private void pushMessages(List<Message> messages, boolean newArrivals)
|
|
|
|
{
|
|
|
|
RuntimeException holdException = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (newArrivals)
|
|
|
|
{
|
2009-11-26 00:10:12 -05:00
|
|
|
receiver.messagesArrived(this, messages);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-26 00:10:12 -05:00
|
|
|
receiver.messagesFlagsChanged(this, messages);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (RuntimeException e)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
holdException = e;
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
if (holdException != null)
|
|
|
|
{
|
|
|
|
throw holdException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
public void stop()
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
|
|
|
stop.set(true);
|
2009-11-28 09:51:44 -05:00
|
|
|
if (listeningThread != null)
|
|
|
|
{
|
|
|
|
listeningThread.interrupt();
|
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
if (mConnection != null)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Closing mConnection to stop pushing for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
mConnection.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.w(K9.LOG_TAG, "Attempt to interrupt null mConnection to stop pushing on folderPusher for " + getLogId());
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
public void handleAsyncUntaggedResponse(ImapResponse response)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "Got async response: " + response);
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
if (stop.get() == true)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got async untagged response: " + response + ", but stop is set for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
sendDone();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Exception while sending DONE for " + getLogId(), e);
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (response.mTag == null)
|
|
|
|
{
|
|
|
|
if (response.size() > 1)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
boolean started = false;
|
|
|
|
Object responseType = response.get(1);
|
2010-05-09 11:27:41 -04:00
|
|
|
if (ImapResponseParser.equalsIgnoreCase(responseType, "EXISTS") || ImapResponseParser.equalsIgnoreCase(responseType, "EXPUNGE") ||
|
|
|
|
ImapResponseParser.equalsIgnoreCase(responseType,"FETCH"))
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
if (started == false)
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
wakeLock.acquire(K9.PUSH_WAKE_LOCK_TIMEOUT);
|
2009-11-28 09:51:44 -05:00
|
|
|
started = true;
|
|
|
|
}
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Got useful async untagged response: " + response + " for " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
sendDone();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Exception while sending DONE for " + getLogId(), e);
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
2010-04-14 23:17:25 -04:00
|
|
|
else if (response.mCommandContinuationRequested)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2010-04-14 23:17:25 -04:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Idling " + getLogId());
|
2010-01-02 21:00:20 -05:00
|
|
|
|
2010-05-16 20:30:32 -04:00
|
|
|
wakeLock.release();
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
@Override
|
|
|
|
public Pusher getPusher(PushReceiver receiver)
|
|
|
|
{
|
|
|
|
return new ImapPusher(this, receiver);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
public class ImapPusher implements Pusher
|
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
final ImapStore mStore;
|
|
|
|
final PushReceiver mReceiver;
|
2010-05-09 11:27:41 -04:00
|
|
|
private long lastRefresh = -1;
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
HashMap<String, ImapFolderPusher> folderPushers = new HashMap<String, ImapFolderPusher>();
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
public ImapPusher(ImapStore store, PushReceiver receiver)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
mStore = store;
|
|
|
|
mReceiver = receiver;
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
public void start(List<String> folderNames)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
stop();
|
2009-12-06 19:56:06 -05:00
|
|
|
synchronized (folderPushers)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
setLastRefresh(System.currentTimeMillis());
|
2009-11-28 09:51:44 -05:00
|
|
|
for (String folderName : folderNames)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
ImapFolderPusher pusher = folderPushers.get(folderName);
|
|
|
|
if (pusher == null)
|
|
|
|
{
|
|
|
|
pusher = new ImapFolderPusher(mStore, folderName, mReceiver);
|
|
|
|
folderPushers.put(folderName, pusher);
|
|
|
|
pusher.start();
|
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
public void refresh()
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
synchronized (folderPushers)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
for (ImapFolderPusher folderPusher : folderPushers.values())
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
folderPusher.refresh();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Got exception while refreshing for " + folderPusher.getName(), e);
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
public void stop()
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Requested stop of IMAP pusher");
|
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
synchronized (folderPushers)
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
for (ImapFolderPusher folderPusher : folderPushers.values())
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
2009-11-28 09:51:44 -05:00
|
|
|
try
|
|
|
|
{
|
2010-01-02 21:00:20 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "Requesting stop of IMAP folderPusher " + folderPusher.getName());
|
2009-11-28 09:51:44 -05:00
|
|
|
folderPusher.stop();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Got exception while stopping " + folderPusher.getName(), e);
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
folderPushers.clear();
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getRefreshInterval()
|
|
|
|
{
|
2010-05-09 11:27:41 -04:00
|
|
|
return (getAccount().getIdleRefreshMinutes() * 60 * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getLastRefresh()
|
|
|
|
{
|
|
|
|
return lastRefresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLastRefresh(long lastRefresh)
|
|
|
|
{
|
|
|
|
this.lastRefresh = lastRefresh;
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
private interface UntaggedHandler
|
|
|
|
{
|
|
|
|
void handleAsyncUntaggedResponse(ImapResponse respose);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
protected static class ImapPushState
|
|
|
|
{
|
|
|
|
protected int uidNext;
|
|
|
|
protected ImapPushState(int nUidNext)
|
|
|
|
{
|
|
|
|
uidNext = nUidNext;
|
|
|
|
}
|
|
|
|
protected static ImapPushState parse(String pushState)
|
|
|
|
{
|
|
|
|
int newUidNext = -1;
|
|
|
|
if (pushState != null)
|
|
|
|
{
|
|
|
|
StringTokenizer tokenizer = new StringTokenizer(pushState, ";");
|
|
|
|
while (tokenizer.hasMoreTokens())
|
|
|
|
{
|
|
|
|
StringTokenizer thisState = new StringTokenizer(tokenizer.nextToken(), "=");
|
|
|
|
if (thisState.hasMoreTokens())
|
|
|
|
{
|
|
|
|
String key = thisState.nextToken();
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-05-09 11:27:41 -04:00
|
|
|
if ("uidNext".equalsIgnoreCase(key) && thisState.hasMoreTokens())
|
2009-10-21 20:41:06 -04:00
|
|
|
{
|
|
|
|
String value = thisState.nextToken();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
newUidNext = Integer.parseInt(value);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Unable to part uidNext value " + value, e);
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ImapPushState(newUidNext);
|
|
|
|
}
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2009-10-21 20:41:06 -04:00
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
return "uidNext=" + uidNext;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-10-21 20:41:06 -04:00
|
|
|
}
|
2009-11-26 00:10:12 -05:00
|
|
|
private interface ImapSearcher
|
|
|
|
{
|
|
|
|
List<ImapResponse> search() throws IOException, MessagingException;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|