2019-02-27 15:05:15 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2019-05-04 20:49:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2023-01-01 07:52:55 +00:00
|
|
|
Copyright 2018-2023 by Marcel Bokhorst (M66B)
|
2019-05-04 20:49:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-07-30 05:47:47 +00:00
|
|
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
|
|
|
import static javax.mail.Folder.READ_WRITE;
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
import android.app.AlarmManager;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2019-09-21 08:05:24 +00:00
|
|
|
import android.database.sqlite.SQLiteConstraintException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.net.Uri;
|
2020-02-11 13:56:22 +00:00
|
|
|
import android.os.OperationCanceledException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.text.TextUtils;
|
2019-07-09 19:07:56 +00:00
|
|
|
import android.util.Pair;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-02-11 15:09:29 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2019-11-22 12:50:16 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2020-05-01 19:47:59 +00:00
|
|
|
import com.sun.mail.gimap.GmailFolder;
|
2020-06-25 07:14:05 +00:00
|
|
|
import com.sun.mail.gimap.GmailMessage;
|
2020-11-16 19:12:40 +00:00
|
|
|
import com.sun.mail.iap.BadCommandException;
|
|
|
|
import com.sun.mail.iap.CommandFailedException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.iap.ConnectionException;
|
2019-10-21 07:54:20 +00:00
|
|
|
import com.sun.mail.iap.ProtocolException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.iap.Response;
|
2020-07-02 15:32:49 +00:00
|
|
|
import com.sun.mail.imap.AppendUID;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
|
import com.sun.mail.imap.IMAPStore;
|
2019-08-24 07:38:11 +00:00
|
|
|
import com.sun.mail.imap.protocol.FLAGS;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.imap.protocol.FetchResponse;
|
|
|
|
import com.sun.mail.imap.protocol.IMAPProtocol;
|
2022-11-25 08:22:14 +00:00
|
|
|
import com.sun.mail.imap.protocol.Status;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.imap.protocol.UID;
|
2021-10-25 14:40:17 +00:00
|
|
|
import com.sun.mail.imap.protocol.UIDSet;
|
2019-09-19 15:41:26 +00:00
|
|
|
import com.sun.mail.pop3.POP3Folder;
|
|
|
|
import com.sun.mail.pop3.POP3Message;
|
|
|
|
import com.sun.mail.pop3.POP3Store;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
2019-09-28 16:36:07 +00:00
|
|
|
import java.io.BufferedOutputStream;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2019-07-29 05:47:11 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.SocketException;
|
2019-07-16 17:28:42 +00:00
|
|
|
import java.net.UnknownHostException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Collections;
|
2019-03-30 10:57:29 +00:00
|
|
|
import java.util.Comparator;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.Date;
|
2019-02-27 17:33:46 +00:00
|
|
|
import java.util.HashMap;
|
2019-10-05 18:44:45 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.List;
|
2020-10-23 15:17:48 +00:00
|
|
|
import java.util.Locale;
|
2019-02-27 17:33:46 +00:00
|
|
|
import java.util.Map;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.Objects;
|
2019-07-29 09:17:12 +00:00
|
|
|
import java.util.Properties;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
2023-06-26 11:59:42 +00:00
|
|
|
import javax.mail.BodyPart;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.FetchProfile;
|
|
|
|
import javax.mail.Flags;
|
|
|
|
import javax.mail.Folder;
|
|
|
|
import javax.mail.FolderClosedException;
|
|
|
|
import javax.mail.FolderNotFoundException;
|
2021-12-08 11:18:38 +00:00
|
|
|
import javax.mail.Header;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.Message;
|
|
|
|
import javax.mail.MessageRemovedException;
|
|
|
|
import javax.mail.MessagingException;
|
2023-06-19 07:34:10 +00:00
|
|
|
import javax.mail.Multipart;
|
|
|
|
import javax.mail.Part;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Store;
|
2020-03-06 14:29:33 +00:00
|
|
|
import javax.mail.StoreClosedException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.UIDFolder;
|
2020-02-07 18:20:22 +00:00
|
|
|
import javax.mail.internet.AddressException;
|
2023-08-20 20:10:13 +00:00
|
|
|
import javax.mail.internet.ContentType;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.internet.InternetAddress;
|
2023-06-26 11:59:42 +00:00
|
|
|
import javax.mail.internet.MimeBodyPart;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.search.ComparisonTerm;
|
|
|
|
import javax.mail.search.FlagTerm;
|
2020-06-08 17:36:45 +00:00
|
|
|
import javax.mail.search.HeaderTerm;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.search.MessageIDTerm;
|
|
|
|
import javax.mail.search.OrTerm;
|
|
|
|
import javax.mail.search.ReceivedDateTerm;
|
|
|
|
import javax.mail.search.SearchTerm;
|
2020-04-01 16:28:01 +00:00
|
|
|
import javax.mail.search.SentDateTerm;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
class Core {
|
2022-01-18 12:21:49 +00:00
|
|
|
static final int DEFAULT_CHUNK_SIZE = 50;
|
2021-10-17 06:47:11 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
private static final int SYNC_BATCH_SIZE = 20;
|
|
|
|
private static final int DOWNLOAD_BATCH_SIZE = 20;
|
|
|
|
private static final long YIELD_DURATION = 200L; // milliseconds
|
2021-11-27 09:09:43 +00:00
|
|
|
private static final long JOIN_WAIT_ALIVE = 5 * 60 * 1000L; // milliseconds
|
|
|
|
private static final long JOIN_WAIT_INTERRUPT = 1 * 60 * 1000L; // milliseconds
|
2020-07-24 11:26:18 +00:00
|
|
|
private static final long FUTURE_RECEIVED = 30 * 24 * 3600 * 1000L; // milliseconds
|
2020-08-05 15:44:55 +00:00
|
|
|
private static final int LOCAL_RETRY_MAX = 2;
|
|
|
|
private static final long LOCAL_RETRY_DELAY = 5 * 1000L; // milliseconds
|
2020-09-22 06:37:25 +00:00
|
|
|
private static final int TOTAL_RETRY_MAX = LOCAL_RETRY_MAX * 5;
|
2021-02-22 19:11:34 +00:00
|
|
|
private static final long EXISTS_RETRY_DELAY = 20 * 1000L; // milliseconds
|
2021-03-11 09:21:17 +00:00
|
|
|
private static final int FIND_RETRY_COUNT = 3; // times
|
|
|
|
private static final long FIND_RETRY_DELAY = 5 * 1000L; // milliseconds
|
2023-09-04 17:26:17 +00:00
|
|
|
private static final int POP3_KEEP_EXTRA = 100; // messages
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-25 10:59:11 +00:00
|
|
|
private static final Map<Long, List<EntityIdentity>> accountIdentities = new HashMap<>();
|
|
|
|
|
|
|
|
static void clearIdentities() {
|
|
|
|
synchronized (accountIdentities) {
|
|
|
|
accountIdentities.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static List<EntityIdentity> getIdentities(long account, Context context) {
|
|
|
|
synchronized (accountIdentities) {
|
|
|
|
if (!accountIdentities.containsKey(account))
|
|
|
|
accountIdentities.put(account,
|
|
|
|
DB.getInstance(context).identity().getSynchronizingIdentities(account));
|
|
|
|
return accountIdentities.get(account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
static void processOperations(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context,
|
2020-01-25 11:38:38 +00:00
|
|
|
EntityAccount account, EntityFolder folder, List<TupleOperationEx> ops,
|
2021-12-29 15:03:30 +00:00
|
|
|
EmailService iservice, Folder ifolder,
|
2021-09-16 08:43:14 +00:00
|
|
|
State state, long serial)
|
2021-12-01 17:00:16 +00:00
|
|
|
throws JSONException, FolderClosedException {
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " start process");
|
|
|
|
|
2021-12-29 15:03:30 +00:00
|
|
|
Store istore = iservice.getStore();
|
2019-02-27 15:05:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2020-01-25 07:23:38 +00:00
|
|
|
|
2022-01-29 07:49:48 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
int chunk_size = prefs.getInt("chunk_size", DEFAULT_CHUNK_SIZE);
|
|
|
|
|
2022-04-13 20:27:33 +00:00
|
|
|
NotificationManager nm = Helper.getSystemService(context, NotificationManager.class);
|
2021-04-30 17:45:20 +00:00
|
|
|
|
2020-02-07 21:07:16 +00:00
|
|
|
int retry = 0;
|
|
|
|
boolean group = true;
|
2021-09-16 08:43:14 +00:00
|
|
|
Log.i(folder.name + " executing serial=" + serial + " operations=" + ops.size());
|
2020-09-22 12:04:26 +00:00
|
|
|
while (retry < LOCAL_RETRY_MAX && ops.size() > 0 &&
|
|
|
|
state.isRunning() &&
|
2021-09-16 08:43:14 +00:00
|
|
|
state.getSerial() == serial) {
|
2020-02-07 21:07:16 +00:00
|
|
|
TupleOperationEx op = ops.get(0);
|
2019-09-28 13:27:35 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name +
|
|
|
|
" start op=" + op.id + "/" + op.name +
|
2019-04-28 07:01:53 +00:00
|
|
|
" folder=" + op.folder +
|
2019-02-27 15:05:15 +00:00
|
|
|
" msg=" + op.message +
|
2020-02-07 21:07:16 +00:00
|
|
|
" args=" + op.args +
|
2020-02-08 11:49:12 +00:00
|
|
|
" group=" + group +
|
2020-02-07 21:07:16 +00:00
|
|
|
" retry=" + retry);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-30 17:45:20 +00:00
|
|
|
if (EntityOperation.HEADERS.equals(op.name) ||
|
|
|
|
EntityOperation.RAW.equals(op.name))
|
2021-07-15 16:36:39 +00:00
|
|
|
nm.cancel(op.name + ":" + op.message, NotificationHelper.NOTIFICATION_TAGGED);
|
2021-04-30 17:45:20 +00:00
|
|
|
|
2020-10-07 05:38:22 +00:00
|
|
|
if (!Objects.equals(folder.id, op.folder))
|
|
|
|
throw new IllegalArgumentException("Invalid folder=" + folder.id + "/" + op.folder);
|
|
|
|
|
2022-10-19 19:14:55 +00:00
|
|
|
if (account.protocol == EntityAccount.TYPE_IMAP && !folder.local && ifolder != null) {
|
|
|
|
try {
|
|
|
|
((IMAPFolder) ifolder).doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
long ago = System.currentTimeMillis() - protocol.getTimestamp();
|
|
|
|
if (ago > 20000)
|
|
|
|
protocol.noop();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
throw new FolderClosedException(ifolder, account.name + "/" + folder.name + " unexpectedly closed", ex);
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 07:51:24 +00:00
|
|
|
|
|
|
|
if (account.protocol == EntityAccount.TYPE_POP &&
|
|
|
|
EntityFolder.INBOX.equals(folder.type) &&
|
2021-12-01 17:00:16 +00:00
|
|
|
ifolder != null && !ifolder.isOpen())
|
|
|
|
throw new FolderClosedException(ifolder, account.name + "/" + folder.name + " unexpectedly closed");
|
2020-03-05 10:16:25 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// Fetch most recent copy of message
|
|
|
|
EntityMessage message = null;
|
|
|
|
if (op.message != null)
|
|
|
|
message = db.message().getMessage(op.message);
|
|
|
|
|
|
|
|
JSONArray jargs = new JSONArray(op.args);
|
2020-02-07 21:07:16 +00:00
|
|
|
Map<TupleOperationEx, EntityMessage> similar = new HashMap<>();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Operations should use database transaction when needed
|
|
|
|
|
2019-09-28 14:41:02 +00:00
|
|
|
if (message == null &&
|
|
|
|
!EntityOperation.FETCH.equals(op.name) &&
|
2021-12-26 17:39:39 +00:00
|
|
|
!EntityOperation.REPORT.equals(op.name) &&
|
2019-09-28 14:41:02 +00:00
|
|
|
!EntityOperation.SYNC.equals(op.name) &&
|
2020-07-26 17:58:13 +00:00
|
|
|
!EntityOperation.SUBSCRIBE.equals(op.name) &&
|
2021-02-12 12:46:21 +00:00
|
|
|
!EntityOperation.PURGE.equals(op.name) &&
|
|
|
|
!EntityOperation.EXPUNGE.equals(op.name))
|
2019-09-28 14:41:02 +00:00
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
// Process similar operations
|
2019-09-17 14:36:19 +00:00
|
|
|
boolean skip = false;
|
2020-02-07 21:07:16 +00:00
|
|
|
for (int j = 1; j < ops.size(); j++) {
|
|
|
|
TupleOperationEx next = ops.get(j);
|
2019-09-17 14:36:19 +00:00
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
switch (op.name) {
|
2022-01-17 20:36:07 +00:00
|
|
|
case EntityOperation.SEEN:
|
|
|
|
case EntityOperation.FLAG:
|
|
|
|
if (group &&
|
|
|
|
message.uid != null &&
|
|
|
|
op.name.equals(next.name) &&
|
|
|
|
account.protocol == EntityAccount.TYPE_IMAP) {
|
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
|
|
|
// Same flag
|
|
|
|
if (jargs.getBoolean(0) == jnext.getBoolean(0)) {
|
|
|
|
EntityMessage m = db.message().getMessage(next.message);
|
|
|
|
if (m != null && m.uid != null)
|
|
|
|
similar.put(next, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
case EntityOperation.ADD:
|
|
|
|
// Same message
|
|
|
|
if (Objects.equals(op.message, next.message) &&
|
|
|
|
(EntityOperation.ADD.equals(next.name) ||
|
|
|
|
EntityOperation.DELETE.equals(next.name)))
|
2019-09-17 14:36:19 +00:00
|
|
|
skip = true;
|
2019-09-28 13:27:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.FETCH:
|
|
|
|
if (EntityOperation.FETCH.equals(next.name)) {
|
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
2022-01-23 07:29:11 +00:00
|
|
|
// Same uid, invalidate, delete flag
|
2021-12-29 14:59:37 +00:00
|
|
|
if (jargs.getLong(0) == jnext.getLong(0) &&
|
2022-01-23 07:29:11 +00:00
|
|
|
jargs.optBoolean(1) == jnext.optBoolean(1) &&
|
|
|
|
jargs.optBoolean(2) == jnext.optBoolean(2))
|
2019-09-28 13:27:35 +00:00
|
|
|
skip = true;
|
2019-09-17 14:36:19 +00:00
|
|
|
}
|
2019-09-28 13:27:35 +00:00
|
|
|
break;
|
|
|
|
|
2022-10-29 19:47:46 +00:00
|
|
|
case EntityOperation.DOWNLOAD:
|
|
|
|
if (EntityOperation.DOWNLOAD.equals(next.name)) {
|
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
|
|
|
// Same uid
|
|
|
|
if (jargs.getLong(0) == jnext.getLong(0))
|
|
|
|
skip = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
case EntityOperation.MOVE:
|
2020-02-07 21:07:16 +00:00
|
|
|
if (group &&
|
|
|
|
message.uid != null &&
|
2021-02-23 14:05:54 +00:00
|
|
|
op.name.equals(next.name) &&
|
2020-04-06 12:11:50 +00:00
|
|
|
account.protocol == EntityAccount.TYPE_IMAP) {
|
2019-09-28 13:27:35 +00:00
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
2019-10-02 07:58:54 +00:00
|
|
|
// Same target
|
2022-04-20 21:49:52 +00:00
|
|
|
if (jargs.getLong(0) == jnext.getLong(0) &&
|
|
|
|
jargs.optBoolean(4) == jnext.optBoolean(4)) {
|
2019-09-28 13:27:35 +00:00
|
|
|
EntityMessage m = db.message().getMessage(next.message);
|
2020-02-07 21:07:16 +00:00
|
|
|
if (m != null && m.uid != null)
|
2019-09-28 13:27:35 +00:00
|
|
|
similar.put(next, m);
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 06:46:11 +00:00
|
|
|
if (group &&
|
|
|
|
op.name.equals(next.name) &&
|
|
|
|
account.protocol == EntityAccount.TYPE_POP) {
|
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
|
|
|
// Same target
|
|
|
|
if (jargs.getLong(0) == jnext.getLong(0)) {
|
|
|
|
EntityMessage m = db.message().getMessage(next.message);
|
|
|
|
if (m != null)
|
|
|
|
similar.put(next, m);
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 13:27:35 +00:00
|
|
|
break;
|
2022-01-17 15:47:13 +00:00
|
|
|
|
|
|
|
case EntityOperation.DELETE:
|
|
|
|
if (group &&
|
|
|
|
message.uid != null &&
|
|
|
|
op.name.equals(next.name) &&
|
|
|
|
account.protocol == EntityAccount.TYPE_IMAP) {
|
|
|
|
EntityMessage m = db.message().getMessage(next.message);
|
|
|
|
if (m != null &&
|
|
|
|
m.uid != null && m.ui_deleted == message.ui_deleted)
|
|
|
|
similar.put(next, m);
|
|
|
|
}
|
2022-08-09 06:46:11 +00:00
|
|
|
if (group &&
|
|
|
|
op.name.equals(next.name) &&
|
|
|
|
account.protocol == EntityAccount.TYPE_POP) {
|
|
|
|
EntityMessage m = db.message().getMessage(next.message);
|
|
|
|
if (m != null)
|
|
|
|
similar.put(next, m);
|
|
|
|
}
|
2022-01-17 15:47:13 +00:00
|
|
|
break;
|
2019-09-28 13:27:35 +00:00
|
|
|
}
|
2022-01-29 07:49:48 +00:00
|
|
|
|
|
|
|
if (similar.size() >= chunk_size)
|
|
|
|
break;
|
2019-09-17 14:36:19 +00:00
|
|
|
}
|
2019-09-21 08:05:24 +00:00
|
|
|
|
2019-09-17 14:36:19 +00:00
|
|
|
if (skip) {
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name +
|
2019-09-17 14:36:19 +00:00
|
|
|
" skipping op=" + op.id + "/" + op.name +
|
2019-09-17 11:38:23 +00:00
|
|
|
" msg=" + op.message + " args=" + op.args);
|
2019-09-17 14:36:19 +00:00
|
|
|
db.operation().deleteOperation(op.id);
|
2020-02-07 21:07:16 +00:00
|
|
|
ops.remove(op);
|
2019-09-17 11:38:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
List<Long> sids = new ArrayList<>();
|
2020-02-07 21:07:16 +00:00
|
|
|
for (TupleOperationEx s : similar.keySet())
|
2019-09-28 13:27:35 +00:00
|
|
|
sids.add(s.id);
|
|
|
|
|
|
|
|
if (similar.size() > 0)
|
|
|
|
Log.i(folder.name + " similar=" + TextUtils.join(",", sids));
|
|
|
|
|
2020-11-29 09:39:03 +00:00
|
|
|
op.tries++;
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
// Leave crumb
|
2019-09-21 08:05:24 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("name", op.name);
|
|
|
|
crumb.put("args", op.args);
|
2020-11-05 09:33:37 +00:00
|
|
|
crumb.put("account", op.account + ":" + account.protocol);
|
|
|
|
crumb.put("folder", op.folder + ":" + folder.type);
|
2019-09-21 08:05:24 +00:00
|
|
|
if (op.message != null)
|
|
|
|
crumb.put("message", Long.toString(op.message));
|
2020-11-29 09:39:03 +00:00
|
|
|
crumb.put("tries", Integer.toString(op.tries));
|
2019-09-28 13:27:35 +00:00
|
|
|
crumb.put("similar", TextUtils.join(",", sids));
|
2019-11-09 13:13:18 +00:00
|
|
|
crumb.put("thread", Thread.currentThread().getName() + ":" + Thread.currentThread().getId());
|
2019-09-21 08:05:24 +00:00
|
|
|
Log.breadcrumb("start operation", crumb);
|
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
db.operation().setOperationError(op.id, null);
|
|
|
|
|
2020-02-08 11:49:12 +00:00
|
|
|
if (message != null)
|
2019-09-28 13:47:34 +00:00
|
|
|
db.message().setMessageError(message.id, null);
|
|
|
|
|
2020-02-12 16:34:31 +00:00
|
|
|
db.operation().setOperationState(op.id, "executing");
|
|
|
|
for (TupleOperationEx s : similar.keySet())
|
|
|
|
db.operation().setOperationState(s.id, "executing");
|
2019-09-28 13:47:34 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
2022-08-09 06:46:11 +00:00
|
|
|
if (istore instanceof POP3Store) {
|
|
|
|
List<EntityMessage> messages = new ArrayList<>();
|
|
|
|
messages.add(message);
|
|
|
|
messages.addAll(similar.values());
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
switch (op.name) {
|
|
|
|
case EntityOperation.DELETE:
|
2022-08-09 06:46:11 +00:00
|
|
|
onDelete(context, jargs, account, folder, messages, (POP3Folder) ifolder, (POP3Store) istore, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-28 08:05:45 +00:00
|
|
|
|
2021-09-05 20:01:28 +00:00
|
|
|
case EntityOperation.RAW:
|
2022-11-24 10:55:54 +00:00
|
|
|
onRaw(context, jargs, folder, message, (POP3Store) istore, (POP3Folder) ifolder);
|
2021-09-05 20:01:28 +00:00
|
|
|
break;
|
|
|
|
|
2023-01-06 07:45:53 +00:00
|
|
|
case EntityOperation.BODY:
|
|
|
|
onBody(context, jargs, folder, message, (POP3Folder) ifolder, (POP3Store) istore);
|
|
|
|
break;
|
|
|
|
|
2022-10-31 18:02:29 +00:00
|
|
|
case EntityOperation.ATTACHMENT:
|
2022-11-24 10:55:54 +00:00
|
|
|
onAttachment(context, jargs, folder, message, (POP3Folder) ifolder, (POP3Store) istore);
|
2022-10-31 18:02:29 +00:00
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.SYNC:
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2019-09-24 17:33:20 +00:00
|
|
|
onSynchronizeMessages(context, jargs, account, folder, (POP3Folder) ifolder, (POP3Store) istore, state);
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-28 08:05:45 +00:00
|
|
|
|
2020-07-26 17:58:13 +00:00
|
|
|
case EntityOperation.PURGE:
|
2020-07-27 14:54:03 +00:00
|
|
|
onPurgeFolder(context, folder);
|
2020-07-26 17:58:13 +00:00
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
default:
|
|
|
|
Log.w(folder.name + " ignored=" + op.name);
|
|
|
|
}
|
2022-08-09 06:46:11 +00:00
|
|
|
} else {
|
2022-01-17 16:24:15 +00:00
|
|
|
List<EntityMessage> messages = new ArrayList<>();
|
|
|
|
messages.add(message);
|
|
|
|
if (similar.size() == 0)
|
|
|
|
ensureUid(context, account, folder, message, op, (IMAPFolder) ifolder);
|
|
|
|
else
|
|
|
|
messages.addAll(similar.values());
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
switch (op.name) {
|
|
|
|
case EntityOperation.SEEN:
|
2022-01-17 20:36:07 +00:00
|
|
|
onSetFlag(context, jargs, folder, messages, (IMAPFolder) ifolder, Flags.Flag.SEEN);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.FLAG:
|
2022-01-17 20:36:07 +00:00
|
|
|
onSetFlag(context, jargs, folder, messages, (IMAPFolder) ifolder, Flags.Flag.FLAGGED);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ANSWERED:
|
|
|
|
onAnswered(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.KEYWORD:
|
|
|
|
onKeyword(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-06-25 07:14:05 +00:00
|
|
|
case EntityOperation.LABEL:
|
|
|
|
onLabel(context, jargs, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ADD:
|
2020-09-21 19:46:39 +00:00
|
|
|
onAdd(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-03-02 14:32:19 +00:00
|
|
|
|
2022-01-17 16:24:15 +00:00
|
|
|
case EntityOperation.MOVE:
|
2021-06-23 21:03:55 +00:00
|
|
|
onMove(context, jargs, false, account, folder, messages, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.COPY:
|
2021-06-23 21:03:55 +00:00
|
|
|
onMove(context, jargs, true, account, folder, Arrays.asList(message), (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.FETCH:
|
2020-09-21 19:46:39 +00:00
|
|
|
onFetch(context, jargs, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-01-17 16:24:15 +00:00
|
|
|
case EntityOperation.DELETE:
|
2022-01-17 15:47:13 +00:00
|
|
|
onDelete(context, jargs, account, folder, messages, (IMAPFolder) ifolder);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.HEADERS:
|
|
|
|
onHeaders(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.RAW:
|
2023-06-02 15:48:36 +00:00
|
|
|
onRaw(context, jargs, account, folder, message, (IMAPFolder) ifolder);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.BODY:
|
|
|
|
onBody(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ATTACHMENT:
|
|
|
|
onAttachment(context, jargs, folder, message, op, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-09-17 12:22:18 +00:00
|
|
|
|
2023-06-21 15:26:53 +00:00
|
|
|
case EntityOperation.DETACH:
|
2023-06-26 16:15:48 +00:00
|
|
|
onDetach(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2023-06-21 15:26:53 +00:00
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.EXISTS:
|
2021-02-22 19:11:34 +00:00
|
|
|
onExists(context, jargs, account, folder, message, op, (IMAPFolder) ifolder);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-12-26 17:39:39 +00:00
|
|
|
case EntityOperation.REPORT:
|
|
|
|
onReport(context, jargs, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.SYNC:
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2020-09-21 19:46:39 +00:00
|
|
|
onSynchronizeMessages(context, jargs, account, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-03-05 12:46:25 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.SUBSCRIBE:
|
|
|
|
onSubscribeFolder(context, jargs, folder, (IMAPFolder) ifolder);
|
|
|
|
break;
|
|
|
|
|
2020-07-26 17:58:13 +00:00
|
|
|
case EntityOperation.PURGE:
|
2021-09-06 17:12:46 +00:00
|
|
|
onPurgeFolder(context, jargs, account, folder, (IMAPFolder) ifolder);
|
2020-07-26 17:58:13 +00:00
|
|
|
break;
|
|
|
|
|
2021-02-12 11:51:34 +00:00
|
|
|
case EntityOperation.EXPUNGE:
|
|
|
|
onExpungeFolder(context, jargs, folder, (IMAPFolder) ifolder);
|
|
|
|
break;
|
|
|
|
|
2020-07-13 14:34:55 +00:00
|
|
|
case EntityOperation.RULE:
|
|
|
|
onRule(context, jargs, message);
|
|
|
|
break;
|
|
|
|
|
2022-10-29 19:47:46 +00:00
|
|
|
case EntityOperation.DOWNLOAD:
|
|
|
|
onDownload(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
|
|
|
break;
|
|
|
|
|
2023-07-22 05:30:09 +00:00
|
|
|
case EntityOperation.SUBJECT:
|
|
|
|
onSubject(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown operation=" + op.name);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 13:13:18 +00:00
|
|
|
crumb.put("thread", Thread.currentThread().getName() + ":" + Thread.currentThread().getId());
|
2019-09-21 08:05:24 +00:00
|
|
|
Log.breadcrumb("end operation", crumb);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// Operation succeeded
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
db.operation().deleteOperation(op.id);
|
2020-02-07 21:07:16 +00:00
|
|
|
for (TupleOperationEx s : similar.keySet())
|
2019-09-28 13:47:34 +00:00
|
|
|
db.operation().deleteOperation(s.id);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-02-07 21:07:16 +00:00
|
|
|
|
|
|
|
ops.remove(op);
|
|
|
|
for (TupleOperationEx s : similar.keySet())
|
|
|
|
ops.remove(s);
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (Throwable ex) {
|
2022-03-29 19:17:53 +00:00
|
|
|
iservice.dump(account.name + "/" + folder.name);
|
2022-05-11 12:43:55 +00:00
|
|
|
if (ex instanceof OperationCanceledException ||
|
|
|
|
(ex instanceof IllegalArgumentException &&
|
|
|
|
ex.getMessage() != null &&
|
|
|
|
ex.getMessage().startsWith("Message not found for")))
|
2021-07-29 10:44:10 +00:00
|
|
|
Log.i(folder.name, ex);
|
2021-07-28 15:02:11 +00:00
|
|
|
else
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
|
2020-02-11 16:46:04 +00:00
|
|
|
EntityLog.log(context, folder.name +
|
2020-05-15 15:21:54 +00:00
|
|
|
" op=" + op.name +
|
|
|
|
" try=" + op.tries +
|
2022-02-18 08:31:16 +00:00
|
|
|
" " + ex + "\n" + android.util.Log.getStackTraceString(ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-09-28 13:27:35 +00:00
|
|
|
|
2020-11-30 07:17:07 +00:00
|
|
|
db.operation().setOperationTries(op.id, op.tries);
|
2019-09-28 13:47:34 +00:00
|
|
|
|
2020-11-30 07:17:07 +00:00
|
|
|
op.error = Log.formatThrowable(ex);
|
|
|
|
db.operation().setOperationError(op.id, op.error);
|
|
|
|
|
|
|
|
if (message != null &&
|
2020-12-03 10:32:22 +00:00
|
|
|
!EntityOperation.FETCH.equals(op.name) &&
|
2021-04-30 17:45:20 +00:00
|
|
|
!EntityOperation.ATTACHMENT.equals(op.name) &&
|
2020-11-30 07:17:07 +00:00
|
|
|
!(ex instanceof IllegalArgumentException))
|
|
|
|
db.message().setMessageError(message.id, op.error);
|
2019-09-29 09:43:22 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-09-28 13:27:35 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-10-01 06:45:24 +00:00
|
|
|
if (similar.size() > 0 && op.tries < TOTAL_RETRY_MAX) {
|
2020-11-30 07:17:07 +00:00
|
|
|
// Retry individually
|
|
|
|
group = false;
|
|
|
|
// Finally will reset state
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-23 06:47:53 +00:00
|
|
|
long attachments = (op.message == null ? 0 : db.attachment().countAttachments(op.message));
|
|
|
|
|
2020-06-11 17:21:50 +00:00
|
|
|
if (op.tries >= TOTAL_RETRY_MAX ||
|
2022-10-15 15:40:18 +00:00
|
|
|
ex instanceof JSONException ||
|
2020-06-11 17:21:50 +00:00
|
|
|
ex instanceof OutOfMemoryError ||
|
2019-07-29 05:47:11 +00:00
|
|
|
ex instanceof FileNotFoundException ||
|
2019-02-27 15:05:15 +00:00
|
|
|
ex instanceof FolderNotFoundException ||
|
2019-04-29 20:05:12 +00:00
|
|
|
ex instanceof IllegalArgumentException ||
|
2019-11-10 10:25:13 +00:00
|
|
|
ex instanceof SQLiteConstraintException ||
|
2022-02-20 09:10:03 +00:00
|
|
|
ex instanceof OperationCanceledException ||
|
2020-11-16 19:12:40 +00:00
|
|
|
(!ConnectionHelper.isIoError(ex) &&
|
|
|
|
(ex.getCause() instanceof BadCommandException ||
|
2021-05-12 15:14:27 +00:00
|
|
|
ex.getCause() instanceof CommandFailedException /* NO */) &&
|
|
|
|
// https://sebastian.marsching.com/wiki/Network/Zimbra#Mailbox_Selected_READ-ONLY_Error_in_Thunderbird
|
|
|
|
(ex.getMessage() == null ||
|
|
|
|
!ex.getMessage().contains("mailbox selected READ-ONLY"))) ||
|
2020-11-13 07:32:12 +00:00
|
|
|
MessageHelper.isRemoved(ex) ||
|
2021-04-30 17:45:20 +00:00
|
|
|
EntityOperation.HEADERS.equals(op.name) ||
|
|
|
|
EntityOperation.RAW.equals(op.name) ||
|
2022-01-28 21:35:16 +00:00
|
|
|
(op.tries >= LOCAL_RETRY_MAX &&
|
|
|
|
EntityOperation.BODY.equals(op.name)) ||
|
2020-10-12 15:36:11 +00:00
|
|
|
EntityOperation.ATTACHMENT.equals(op.name) ||
|
2023-07-22 05:30:22 +00:00
|
|
|
EntityOperation.DETACH.equals(op.name) ||
|
2023-07-22 05:30:09 +00:00
|
|
|
EntityOperation.SUBJECT.equals(op.name) ||
|
2022-01-23 06:47:53 +00:00
|
|
|
((op.tries >= LOCAL_RETRY_MAX || attachments > 0) &&
|
2022-09-30 10:03:14 +00:00
|
|
|
EntityOperation.ADD.equals(op.name)) ||
|
2021-10-31 11:30:50 +00:00
|
|
|
(op.tries >= LOCAL_RETRY_MAX &&
|
|
|
|
EntityOperation.SYNC.equals(op.name) &&
|
2022-02-18 09:12:08 +00:00
|
|
|
(account.protocol == EntityAccount.TYPE_POP ||
|
|
|
|
!ConnectionHelper.isIoError(ex)))) {
|
2020-11-21 07:07:03 +00:00
|
|
|
// com.sun.mail.iap.BadCommandException: BAD [TOOBIG] Message too large
|
|
|
|
// com.sun.mail.iap.CommandFailedException: NO [CANNOT] Cannot APPEND to a SPAM folder
|
|
|
|
// com.sun.mail.iap.CommandFailedException: NO [ALERT] Cannot MOVE messages out of the Drafts folder
|
|
|
|
// com.sun.mail.iap.CommandFailedException: NO [OVERQUOTA] quota exceeded
|
2019-12-29 13:59:29 +00:00
|
|
|
// Drafts: javax.mail.FolderClosedException: * BYE Jakarta Mail Exception:
|
|
|
|
// javax.net.ssl.SSLException: Write error: ssl=0x8286cac0: I/O error during system call, Broken pipe
|
2020-09-11 10:41:42 +00:00
|
|
|
// Drafts: * BYE Jakarta Mail Exception: java.io.IOException: Connection dropped by server?
|
2020-12-05 06:42:00 +00:00
|
|
|
// Sync: BAD Could not parse command
|
2021-05-12 14:37:29 +00:00
|
|
|
// Sync: SEARCH not allowed now
|
2022-01-18 20:50:11 +00:00
|
|
|
// Sync: BAD Command SEARCH invalid in AUTHENTICATED state (MARKER:xxx)
|
2020-12-03 07:09:19 +00:00
|
|
|
// Seen: NO mailbox selected READ-ONLY
|
2021-02-19 12:21:33 +00:00
|
|
|
// Fetch: BAD Error in IMAP command FETCH: Invalid messageset (n.nnn + n.nnn secs).
|
2020-12-02 14:35:24 +00:00
|
|
|
// Fetch: NO all of the requested messages have been expunged
|
2020-12-06 08:28:45 +00:00
|
|
|
// Fetch: BAD parse error: invalid message sequence number:
|
2020-12-18 07:02:55 +00:00
|
|
|
// Fetch: NO The specified message set is invalid.
|
2020-12-06 08:28:45 +00:00
|
|
|
// Fetch: NO [SERVERBUG] SELECT Server error - Please try again later
|
|
|
|
// Fetch: NO [SERVERBUG] UID FETCH Server error - Please try again later
|
2021-11-12 07:01:02 +00:00
|
|
|
// Fetch: NO Invalid message number (took nnn ms)
|
2022-02-11 19:26:55 +00:00
|
|
|
// Fetch: NO Invalid message sequence ID: nnn
|
2021-01-22 07:24:11 +00:00
|
|
|
// Fetch: BAD Internal Server Error
|
2021-05-12 14:37:29 +00:00
|
|
|
// Fetch: BAD Error in IMAP command FETCH: Invalid messageset (n.nnn + n .nnn secs).
|
2021-11-12 07:01:02 +00:00
|
|
|
// Fetch: NO FETCH sequence parse error in: nnn
|
2022-02-11 19:26:55 +00:00
|
|
|
// Fetch: NO [NONEXISTENT] No matching messages
|
2021-01-31 11:18:01 +00:00
|
|
|
// Fetch UID: NO Some messages could not be FETCHed (Failure)
|
2021-03-13 07:54:07 +00:00
|
|
|
// Fetch UID: NO [LIMIT] UID FETCH Rate limit hit.
|
|
|
|
// Fetch UID: NO Server Unavailable. 15
|
2021-03-15 20:24:15 +00:00
|
|
|
// Fetch UID: NO [UNAVAILABLE] Failed to open mailbox
|
2021-05-12 14:37:29 +00:00
|
|
|
// Fetch UID: NO [TEMPFAIL] SELECT completed
|
2022-01-18 20:50:11 +00:00
|
|
|
// Fetch UID: NO Internal error. Try again later... (MARKER:xxx)
|
2022-02-06 08:54:17 +00:00
|
|
|
// Fetch UID: BAD Serious error while processing UID FETCH (NioRecvFail (nn/nn))
|
2022-03-24 17:31:51 +00:00
|
|
|
// Fetch UID: NO SELECT: libmapper: Internal error: No servers available or value handling error!
|
2022-03-26 07:51:13 +00:00
|
|
|
// Fetch UID: BAD Serious error while processing UID FETCH (CassdbDatabaseError (nnn/n))
|
2020-12-06 08:28:45 +00:00
|
|
|
// Move: NO Over quota
|
2020-11-21 07:07:03 +00:00
|
|
|
// Move: NO No matching messages
|
2022-01-05 19:41:21 +00:00
|
|
|
// Move: NO [EXPUNGEISSUED] Some of the requested messages no longer exist (n.nnn + n.nnn + n.nnn secs)
|
2020-12-05 06:42:00 +00:00
|
|
|
// Move: BAD parse error: invalid message sequence number:
|
2020-12-08 07:30:23 +00:00
|
|
|
// Move: NO MOVE failed or partially completed.
|
|
|
|
// Move: NO mailbox selected READ-ONLY
|
2020-12-20 17:02:42 +00:00
|
|
|
// Move: NO read only
|
2021-01-30 08:41:33 +00:00
|
|
|
// Move: NO COPY failed
|
2021-01-31 11:18:01 +00:00
|
|
|
// Move: NO [SERVERBUG] Internal error occurred. Refer to server log for more information.
|
2021-02-12 15:52:55 +00:00
|
|
|
// Move: NO STORE: mtd: internal error: Cannot set message attributes.<404, ebox: no such entity: LiteMessage 29215 does not exist>
|
|
|
|
// Move: NO mailbox selected READ-ONLY
|
|
|
|
// Move: NO System Error (Failure)
|
2021-02-13 15:51:36 +00:00
|
|
|
// Move: NO APPEND processing failed.
|
2021-05-12 14:37:29 +00:00
|
|
|
// Move: NO Server Unavailable. 15
|
|
|
|
// Move: NO [CANNOT] Operation is not supported on mailbox
|
2023-08-06 07:40:42 +00:00
|
|
|
// Move: NO [CANNOT] Can't save messages to this virtual mailbox (0.001 + 0.000 secs).
|
2021-08-15 07:08:52 +00:00
|
|
|
// Move: NO [ALREADYEXISTS] Mailbox already exists
|
2021-03-04 18:48:51 +00:00
|
|
|
// Copy: NO Client tried to access nonexistent namespace. (Mailbox name should probably be prefixed with: INBOX.) (n.nnn + n.nnn secs).
|
2022-02-17 11:02:41 +00:00
|
|
|
// Copy: NO Message not found
|
2021-01-10 16:14:55 +00:00
|
|
|
// Add: BAD Data length exceeds limit
|
2021-01-30 08:41:33 +00:00
|
|
|
// Add: NO [LIMIT] APPEND Command exceeds the maximum allowed size
|
|
|
|
// Add: NO APPEND failed: Unknown flag: SEEN
|
|
|
|
// Add: BAD mtd: internal error: APPEND Message too long. 12345678
|
2021-02-12 15:52:55 +00:00
|
|
|
// Add: NO [OVERQUOTA] Not enough disk quota (n.nnn + n.nnn + n.nnn secs).
|
2021-03-17 12:24:30 +00:00
|
|
|
// Add: NO [OVERQUOTA] Quota exceeded (mailbox for user is full) (n.nnn + n.nnn secs).
|
2021-02-13 15:51:36 +00:00
|
|
|
// Add: NO APPEND failed
|
2021-04-13 10:00:46 +00:00
|
|
|
// Add: BAD [TOOBIG] Message too large.
|
2022-02-11 19:26:55 +00:00
|
|
|
// Add: NO Permission denied
|
2020-11-23 08:34:59 +00:00
|
|
|
// Delete: NO [CANNOT] STORE It's not possible to perform specified operation
|
2020-12-02 14:35:24 +00:00
|
|
|
// Delete: NO [UNAVAILABLE] EXPUNGE Backend error
|
2020-12-08 07:30:23 +00:00
|
|
|
// Delete: NO mailbox selected READ-ONLY
|
2022-02-19 12:25:45 +00:00
|
|
|
// Delete: NO Mails not exist!
|
2021-01-31 19:51:05 +00:00
|
|
|
// Flags: NO mailbox selected READ-ONLY
|
2022-03-29 07:20:27 +00:00
|
|
|
// Flags: BAD Server error: 'NoneType' object has no attribute 'message_id'
|
2021-01-30 08:41:33 +00:00
|
|
|
// Keyword: NO STORE completed
|
2021-04-05 16:34:35 +00:00
|
|
|
// Keyword: NO [CANNOT] Keyword length too long (n.nnn + n.nnn secs).
|
2021-01-31 11:18:01 +00:00
|
|
|
// Search: BAD command syntax error
|
2021-02-12 15:52:55 +00:00
|
|
|
// Search (sync): BAD Could not parse command
|
2020-12-08 09:45:31 +00:00
|
|
|
|
2020-11-13 07:32:12 +00:00
|
|
|
String msg = "Unrecoverable operation=" + op.name + " tries=" + op.tries + " created=" + new Date(op.created);
|
2020-12-08 09:45:31 +00:00
|
|
|
|
2020-11-13 07:32:12 +00:00
|
|
|
EntityLog.log(context, msg +
|
|
|
|
" folder=" + folder.id + ":" + folder.name +
|
|
|
|
" message=" + (message == null ? null : message.id + ":" + message.subject) +
|
|
|
|
" reason=" + Log.formatThrowable(ex, false));
|
2020-12-08 09:45:31 +00:00
|
|
|
|
|
|
|
if (ifolder != null && ifolder.isOpen() &&
|
|
|
|
(op.tries > 1 ||
|
|
|
|
ex.getCause() instanceof BadCommandException ||
|
|
|
|
ex.getCause() instanceof CommandFailedException))
|
2020-11-16 11:40:12 +00:00
|
|
|
Log.e(new Throwable(msg, ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-02-08 11:49:12 +00:00
|
|
|
// Cleanup operation
|
2020-11-05 15:45:30 +00:00
|
|
|
op.cleanup(context, true);
|
2020-02-08 11:49:12 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
// There is no use in repeating
|
|
|
|
db.operation().deleteOperation(op.id);
|
2019-04-30 10:21:26 +00:00
|
|
|
|
2019-09-28 14:16:55 +00:00
|
|
|
// Cleanup messages
|
2021-10-11 06:40:16 +00:00
|
|
|
if (MessageHelper.isRemoved(ex)) {
|
|
|
|
if (message != null &&
|
2022-11-29 19:02:08 +00:00
|
|
|
!EntityOperation.SEEN.equals(op.name) &&
|
2023-01-18 19:54:42 +00:00
|
|
|
(!EntityOperation.FLAG.equals(op.name) ||
|
|
|
|
EntityFolder.FLAGGED.equals(folder.subtype)))
|
2021-10-11 06:40:16 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
}
|
2019-09-28 13:47:34 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:07:16 +00:00
|
|
|
ops.remove(op);
|
2021-04-30 17:45:20 +00:00
|
|
|
|
2021-07-19 05:11:42 +00:00
|
|
|
if (!MessageHelper.isRemoved(ex)) {
|
|
|
|
int resid = context.getResources().getIdentifier(
|
|
|
|
"title_op_title_" + op.name,
|
|
|
|
"string",
|
|
|
|
context.getPackageName());
|
|
|
|
String title = (resid == 0 ? null : context.getString(resid));
|
|
|
|
if (title != null) {
|
|
|
|
NotificationCompat.Builder builder =
|
2021-07-31 18:13:58 +00:00
|
|
|
getNotificationError(context, "warning", account, message.id, new Throwable(title, ex));
|
2022-06-09 10:38:17 +00:00
|
|
|
if (NotificationHelper.areNotificationsEnabled(nm))
|
|
|
|
nm.notify(op.name + ":" + op.message,
|
|
|
|
NotificationHelper.NOTIFICATION_TAGGED,
|
|
|
|
builder.build());
|
2021-07-19 05:11:42 +00:00
|
|
|
}
|
2021-04-30 17:45:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:07:16 +00:00
|
|
|
} else {
|
|
|
|
retry++;
|
2020-09-22 12:04:26 +00:00
|
|
|
if (retry < LOCAL_RETRY_MAX &&
|
|
|
|
state.isRunning() &&
|
2021-09-16 08:43:14 +00:00
|
|
|
state.getSerial() == serial)
|
2020-02-08 11:29:13 +00:00
|
|
|
try {
|
2020-02-11 16:46:04 +00:00
|
|
|
Thread.sleep(LOCAL_RETRY_DELAY);
|
2020-02-08 11:29:13 +00:00
|
|
|
} catch (InterruptedException ex1) {
|
|
|
|
Log.w(ex1);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-05-11 19:04:27 +00:00
|
|
|
} finally {
|
2020-02-08 11:49:12 +00:00
|
|
|
// Reset operation state
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
db.operation().setOperationState(op.id, null);
|
2020-02-07 21:07:16 +00:00
|
|
|
for (TupleOperationEx s : similar.keySet())
|
2019-09-28 13:47:34 +00:00
|
|
|
db.operation().setOperationState(s.id, null);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end op=" + op.id + "/" + op.name);
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 21:07:16 +00:00
|
|
|
|
2022-02-18 09:14:50 +00:00
|
|
|
if (ops.size() != 0 && state.getSerial() == serial) {
|
|
|
|
List<String> names = new ArrayList<>();
|
|
|
|
for (EntityOperation op : ops)
|
|
|
|
names.add(op.name);
|
|
|
|
state.error(new OperationCanceledException("Processing " + TextUtils.join(",", names)));
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
} finally {
|
2020-02-11 15:57:12 +00:00
|
|
|
Log.i(folder.name + " end process state=" + state + " pending=" + ops.size());
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 17:12:46 +00:00
|
|
|
private static void ensureUid(Context context, EntityAccount account, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws MessagingException, IOException {
|
2020-12-17 14:05:44 +00:00
|
|
|
if (folder.local)
|
|
|
|
return;
|
2019-09-19 15:41:26 +00:00
|
|
|
if (message == null || message.uid != null)
|
2019-06-15 16:10:33 +00:00
|
|
|
return;
|
2019-09-27 11:05:13 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
if (EntityOperation.ADD.equals(op.name))
|
|
|
|
return;
|
2019-09-17 14:01:29 +00:00
|
|
|
if (EntityOperation.FETCH.equals(op.name))
|
|
|
|
return;
|
2019-09-17 12:22:18 +00:00
|
|
|
if (EntityOperation.EXISTS.equals(op.name))
|
|
|
|
return;
|
2019-06-20 07:09:05 +00:00
|
|
|
if (EntityOperation.DELETE.equals(op.name) && !TextUtils.isEmpty(message.msgid))
|
|
|
|
return;
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
Log.i(folder.name + " ensure uid op=" + op.name + " msgid=" + message.msgid);
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
2019-09-27 11:05:13 +00:00
|
|
|
throw new IllegalArgumentException("Message without msgid for " + op.name);
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2021-04-23 19:33:03 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2021-10-25 07:25:04 +00:00
|
|
|
Long uid = findUid(context, account, ifolder, message.msgid);
|
2021-04-23 19:33:03 +00:00
|
|
|
if (uid == null) {
|
2021-04-24 17:46:21 +00:00
|
|
|
if (EntityOperation.MOVE.equals(op.name) &&
|
|
|
|
EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
try {
|
|
|
|
long fid = new JSONArray(op.args).optLong(0, -1L);
|
|
|
|
EntityFolder target = db.folder().getFolder(fid);
|
2021-04-24 20:23:15 +00:00
|
|
|
if (target != null && EntityFolder.TRASH.equals(target.type)) {
|
|
|
|
Log.w(folder.name + " deleting id=" + message.id);
|
2021-04-24 17:46:21 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
2021-04-24 20:23:15 +00:00
|
|
|
}
|
2021-04-24 17:46:21 +00:00
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
2022-05-11 12:43:55 +00:00
|
|
|
throw new IllegalArgumentException("Message not found for " + op.name + " folder=" + folder.name);
|
2021-04-23 19:33:03 +00:00
|
|
|
}
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-09-27 11:05:13 +00:00
|
|
|
db.message().setMessageUid(message.id, message.uid);
|
2019-11-10 10:25:13 +00:00
|
|
|
message.uid = uid;
|
2019-09-27 11:05:13 +00:00
|
|
|
}
|
2019-06-15 16:10:33 +00:00
|
|
|
|
2021-10-25 07:25:04 +00:00
|
|
|
private static Long findUid(Context context, EntityAccount account, IMAPFolder ifolder, String msgid) throws MessagingException, IOException {
|
2019-09-27 11:05:13 +00:00
|
|
|
String name = ifolder.getFullName();
|
|
|
|
Log.i(name + " searching for msgid=" + msgid);
|
2019-06-15 16:10:33 +00:00
|
|
|
|
2019-09-27 11:05:13 +00:00
|
|
|
Long uid = null;
|
|
|
|
|
2021-10-25 07:47:54 +00:00
|
|
|
Message[] imessages = findMsgId(context, account, ifolder, msgid);
|
2021-10-25 07:25:04 +00:00
|
|
|
if (imessages != null)
|
2021-10-25 15:34:34 +00:00
|
|
|
for (Message iexisting : imessages)
|
|
|
|
try {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
if (muid < 0)
|
|
|
|
continue;
|
|
|
|
Log.i(name + " found uid=" + muid + " for msgid=" + msgid);
|
|
|
|
// RFC3501: Unique identifiers are assigned in a strictly ascending fashion
|
|
|
|
if (uid == null || muid > uid)
|
|
|
|
uid = muid;
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-09-27 11:05:13 +00:00
|
|
|
|
|
|
|
Log.i(name + " got uid=" + uid + " for msgid=" + msgid);
|
|
|
|
return uid;
|
2019-06-14 08:41:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 07:47:54 +00:00
|
|
|
private static Message[] findMsgId(Context context, EntityAccount account, IMAPFolder ifolder, String msgid) throws MessagingException, IOException {
|
|
|
|
// https://stackoverflow.com/questions/18891509/how-to-get-message-from-messageidterm-for-yahoo-imap-profile
|
|
|
|
if (account.isYahooJp()) {
|
|
|
|
Message[] itemps = ifolder.search(new ReceivedDateTerm(ComparisonTerm.GE, new Date()));
|
|
|
|
List<Message> tmp = new ArrayList<>();
|
|
|
|
for (Message itemp : itemps) {
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) itemp, context);
|
|
|
|
if (msgid.equals(helper.getMessageID()))
|
|
|
|
tmp.add(itemp);
|
|
|
|
}
|
|
|
|
return tmp.toArray(new Message[0]);
|
|
|
|
} else
|
|
|
|
return ifolder.search(new MessageIDTerm(msgid));
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:55:54 +00:00
|
|
|
private static Map<EntityMessage, Message> findMessages(Context context, EntityFolder folder, List<EntityMessage> messages, POP3Store istore, POP3Folder ifolder) throws MessagingException, IOException {
|
2021-09-05 19:47:14 +00:00
|
|
|
Map<String, String> caps = istore.capabilities();
|
2022-11-24 10:55:54 +00:00
|
|
|
boolean hasUidl = caps.containsKey("UIDL");
|
2021-09-06 05:56:57 +00:00
|
|
|
|
|
|
|
Message[] imessages = ifolder.getMessages();
|
|
|
|
|
2021-09-05 19:47:14 +00:00
|
|
|
if (hasUidl) {
|
|
|
|
FetchProfile ifetch = new FetchProfile();
|
|
|
|
ifetch.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(imessages, ifetch);
|
|
|
|
}
|
|
|
|
|
2022-08-09 06:46:11 +00:00
|
|
|
Map<EntityMessage, Message> result = new HashMap<>();
|
|
|
|
|
|
|
|
for (EntityMessage message : messages) {
|
|
|
|
result.put(message, null);
|
|
|
|
Log.i(folder.name + " POP searching for=" + message.uidl + "/" + message.msgid +
|
|
|
|
" messages=" + imessages.length + " uidl=" + hasUidl);
|
|
|
|
|
|
|
|
for (Message imessage : imessages) {
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
2021-09-05 19:47:14 +00:00
|
|
|
|
2022-08-09 06:46:11 +00:00
|
|
|
String uidl = (hasUidl ? ifolder.getUID(imessage) : null);
|
2022-11-24 10:31:47 +00:00
|
|
|
String msgid = (TextUtils.isEmpty(uidl) ? helper.getPOP3MessageID() : null);
|
2021-09-05 19:47:14 +00:00
|
|
|
|
2022-08-09 06:46:11 +00:00
|
|
|
if ((uidl != null && uidl.equals(message.uidl)) ||
|
|
|
|
(msgid != null && msgid.equals(message.msgid))) {
|
|
|
|
Log.i(folder.name + " POP found=" + uidl + "/" + msgid);
|
|
|
|
result.put(message, imessage);
|
|
|
|
}
|
2021-09-06 05:56:57 +00:00
|
|
|
}
|
2021-09-05 19:47:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 06:46:11 +00:00
|
|
|
return result;
|
2021-09-05 19:47:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:36:07 +00:00
|
|
|
private static void onSetFlag(Context context, JSONArray jargs, EntityFolder folder, List<EntityMessage> messages, IMAPFolder ifolder, Flags.Flag flag) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Mark message (un)seen
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2022-01-28 09:36:35 +00:00
|
|
|
if (flag != Flags.Flag.SEEN &&
|
|
|
|
flag != Flags.Flag.ANSWERED &&
|
|
|
|
flag != Flags.Flag.FLAGGED &&
|
|
|
|
flag != Flags.Flag.DELETED)
|
2022-01-17 20:36:07 +00:00
|
|
|
throw new IllegalArgumentException("Invalid flag=" + flag);
|
|
|
|
|
2021-11-13 13:09:17 +00:00
|
|
|
if (folder.read_only)
|
|
|
|
return;
|
|
|
|
|
2022-01-17 20:36:07 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(flag)) {
|
|
|
|
for (EntityMessage message : messages)
|
|
|
|
if (flag == Flags.Flag.SEEN) {
|
|
|
|
db.message().setMessageSeen(message.id, false);
|
|
|
|
db.message().setMessageUiSeen(message.id, false);
|
2022-01-28 09:36:35 +00:00
|
|
|
} else if (flag == Flags.Flag.ANSWERED) {
|
|
|
|
db.message().setMessageAnswered(message.id, false);
|
|
|
|
db.message().setMessageUiAnswered(message.id, false);
|
2022-01-17 20:36:07 +00:00
|
|
|
} else if (flag == Flags.Flag.FLAGGED) {
|
|
|
|
db.message().setMessageFlagged(message.id, false);
|
|
|
|
db.message().setMessageUiFlagged(message.id, false, null);
|
2022-01-28 09:36:35 +00:00
|
|
|
} else if (flag == Flags.Flag.DELETED) {
|
|
|
|
db.message().setMessageDeleted(message.id, false);
|
|
|
|
db.message().setMessageUiDeleted(message.id, false);
|
2022-01-17 20:36:07 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:36:07 +00:00
|
|
|
List<Long> uids = new ArrayList<>();
|
|
|
|
boolean set = jargs.getBoolean(0);
|
|
|
|
for (EntityMessage message : messages) {
|
|
|
|
if (message.uid == null)
|
|
|
|
if (messages.size() == 1)
|
|
|
|
throw new IllegalArgumentException("Set flag: uid missing");
|
|
|
|
else
|
|
|
|
throw new MessagingException("Set flag: uid missing");
|
|
|
|
if (flag == Flags.Flag.SEEN && !message.seen.equals(set))
|
|
|
|
uids.add(message.uid);
|
2022-01-28 09:36:35 +00:00
|
|
|
else if (flag == Flags.Flag.ANSWERED && !message.answered.equals(set))
|
|
|
|
uids.add(message.uid);
|
2022-01-17 20:36:07 +00:00
|
|
|
else if (flag == Flags.Flag.FLAGGED && !message.flagged.equals(set))
|
|
|
|
uids.add(message.uid);
|
2022-01-28 09:36:35 +00:00
|
|
|
else if (flag == Flags.Flag.DELETED && !message.deleted.equals(set))
|
|
|
|
uids.add(message.uid);
|
2022-01-17 20:36:07 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-01-17 20:36:07 +00:00
|
|
|
if (uids.size() == 0)
|
|
|
|
return;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-01-28 11:21:20 +00:00
|
|
|
Message[] imessages = ifolder.getMessagesByUID(Helper.toLongArray(uids));
|
|
|
|
for (Message imessage : imessages)
|
|
|
|
if (imessage == null)
|
|
|
|
if (messages.size() == 1)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
else
|
|
|
|
throw new MessagingException("Set flag: message missing");
|
2022-01-28 07:41:02 +00:00
|
|
|
|
2022-01-28 11:21:20 +00:00
|
|
|
ifolder.setFlags(imessages, new Flags(flag), set);
|
2022-01-17 20:36:07 +00:00
|
|
|
|
|
|
|
for (EntityMessage message : messages)
|
|
|
|
if (flag == Flags.Flag.SEEN && !message.seen.equals(set))
|
|
|
|
db.message().setMessageSeen(message.id, set);
|
2022-01-28 09:36:35 +00:00
|
|
|
else if (flag == Flags.Flag.ANSWERED && !message.answered.equals(set))
|
|
|
|
db.message().setMessageAnswered(message.id, set);
|
2022-01-17 20:36:07 +00:00
|
|
|
else if (flag == Flags.Flag.FLAGGED && !message.flagged.equals(set))
|
|
|
|
db.message().setMessageFlagged(message.id, set);
|
2022-01-28 09:36:35 +00:00
|
|
|
else if (flag == Flags.Flag.DELETED && !message.deleted.equals(set))
|
|
|
|
db.message().setMessageDeleted(message.id, set);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onAnswered(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Mark message (un)answered
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2021-11-14 07:06:36 +00:00
|
|
|
if (folder.read_only)
|
|
|
|
return;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.ANSWERED)) {
|
|
|
|
db.message().setMessageAnswered(message.id, false);
|
|
|
|
db.message().setMessageUiAnswered(message.id, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean answered = jargs.getBoolean(0);
|
|
|
|
if (message.answered.equals(answered))
|
|
|
|
return;
|
|
|
|
|
2019-09-23 11:52:54 +00:00
|
|
|
// This will be fixed when moving the message
|
2019-03-08 15:26:12 +00:00
|
|
|
if (message.uid == null)
|
|
|
|
return;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
imessage.setFlag(Flags.Flag.ANSWERED, answered);
|
|
|
|
|
|
|
|
db.message().setMessageAnswered(message.id, answered);
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onKeyword(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Set/reset user flag
|
2020-06-27 09:02:29 +00:00
|
|
|
// https://tools.ietf.org/html/rfc3501#section-2.3.2
|
2021-11-13 13:09:17 +00:00
|
|
|
|
2020-06-27 09:02:29 +00:00
|
|
|
String keyword = jargs.getString(0);
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(keyword))
|
|
|
|
throw new IllegalArgumentException("keyword/empty");
|
2019-02-27 17:02:30 +00:00
|
|
|
|
2020-06-27 09:02:29 +00:00
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("keyword/uid");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-11-13 15:20:00 +00:00
|
|
|
if (folder.read_only ||
|
|
|
|
!ifolder.getPermanentFlags().contains(Flags.Flag.USER))
|
2020-09-17 15:42:08 +00:00
|
|
|
return;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
Flags flags = new Flags(keyword);
|
|
|
|
imessage.setFlags(flags, set);
|
|
|
|
}
|
|
|
|
|
2020-06-25 07:14:05 +00:00
|
|
|
private static void onLabel(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
|
|
|
// Set/clear Gmail label
|
2020-06-27 09:00:13 +00:00
|
|
|
// Gmail does not push label changes
|
2020-06-25 07:14:05 +00:00
|
|
|
String label = jargs.getString(0);
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
2020-06-27 09:02:29 +00:00
|
|
|
if (TextUtils.isEmpty(label))
|
|
|
|
throw new IllegalArgumentException("label/empty");
|
|
|
|
|
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("label/uid");
|
|
|
|
|
2020-06-27 09:00:13 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
if (!set && label.equals(folder.name)) {
|
2020-07-05 08:25:09 +00:00
|
|
|
if (TextUtils.isEmpty(message.msgid)) {
|
|
|
|
Log.w("label/msgid");
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 09:02:29 +00:00
|
|
|
|
2020-06-27 09:00:13 +00:00
|
|
|
// Prevent deleting message
|
|
|
|
EntityFolder archive = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE);
|
2020-07-05 08:25:09 +00:00
|
|
|
if (archive == null) {
|
|
|
|
Log.w("label/archive");
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 09:00:13 +00:00
|
|
|
|
2021-01-02 07:48:54 +00:00
|
|
|
boolean archived;
|
2020-06-27 09:00:13 +00:00
|
|
|
Folder iarchive = istore.getFolder(archive.name);
|
|
|
|
try {
|
|
|
|
iarchive.open(Folder.READ_ONLY);
|
2021-01-02 07:48:54 +00:00
|
|
|
Message[] imessages = iarchive.search(new MessageIDTerm(message.msgid));
|
|
|
|
archived = (imessages != null && imessages.length > 0);
|
2020-06-27 09:00:13 +00:00
|
|
|
} finally {
|
|
|
|
if (iarchive.isOpen())
|
2022-09-23 16:12:42 +00:00
|
|
|
iarchive.close(false);
|
2020-06-27 09:00:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 07:48:54 +00:00
|
|
|
if (archived)
|
2020-06-27 09:00:13 +00:00
|
|
|
try {
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
2020-08-16 09:35:43 +00:00
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
2020-06-27 09:00:13 +00:00
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
2021-10-24 12:39:49 +00:00
|
|
|
expunge(context, ifolder, Arrays.asList(imessage));
|
2020-06-27 09:00:13 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-07-05 08:25:09 +00:00
|
|
|
else {
|
|
|
|
Log.w("label/delete folder=" + folder.name);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 09:00:13 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage instanceof GmailMessage)
|
|
|
|
((GmailMessage) imessage).setLabels(new String[]{label}, set);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-06-25 17:53:30 +00:00
|
|
|
}
|
2020-06-25 15:55:59 +00:00
|
|
|
|
2020-06-27 06:16:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
List<EntityMessage> messages = db.message().getMessagesByMsgId(message.account, message.msgid);
|
|
|
|
if (messages == null)
|
|
|
|
return;
|
|
|
|
|
2020-06-25 17:53:30 +00:00
|
|
|
for (EntityMessage m : messages) {
|
|
|
|
EntityFolder f = db.folder().getFolder(m.folder);
|
|
|
|
if (!label.equals(f.name) && m.setLabel(label, set)) {
|
|
|
|
Log.i("Set " + label + "=" + set + " id=" + m.id + " folder=" + f.name);
|
|
|
|
db.message().setMessageLabels(m.id, DB.Converters.fromStringArray(m.labels));
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 06:16:34 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-06-25 07:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void onAdd(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Add message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2020-12-17 14:05:44 +00:00
|
|
|
if (folder.local) {
|
|
|
|
Log.i(folder.name + " local add");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-08 07:03:40 +00:00
|
|
|
// Drafts can change accounts
|
|
|
|
if (jargs.length() == 0 && !folder.id.equals(message.folder))
|
|
|
|
throw new IllegalArgumentException("Message folder changed");
|
|
|
|
|
2019-05-14 08:40:48 +00:00
|
|
|
// Get arguments
|
2019-06-23 08:28:33 +00:00
|
|
|
long target = jargs.optLong(0, folder.id);
|
|
|
|
boolean autoread = jargs.optBoolean(1, false);
|
2021-08-17 18:28:58 +00:00
|
|
|
boolean copy = jargs.optBoolean(2, false); // Cross account
|
2019-05-14 08:40:48 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
if (target != folder.id)
|
|
|
|
throw new IllegalArgumentException("Invalid folder");
|
2019-05-14 08:40:48 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// External draft might have a uid only
|
|
|
|
if (TextUtils.isEmpty(message.msgid)) {
|
|
|
|
message.msgid = EntityMessage.generateMessageId();
|
|
|
|
db.message().setMessageMsgId(message.id, message.msgid);
|
|
|
|
}
|
2019-03-25 16:26:37 +00:00
|
|
|
|
2022-08-12 20:09:50 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties(account.unicode);
|
2019-07-29 09:17:12 +00:00
|
|
|
Session isession = Session.getInstance(props, null);
|
2019-09-23 11:52:54 +00:00
|
|
|
Flags flags = ifolder.getPermanentFlags();
|
2019-07-29 09:17:12 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Get raw message
|
|
|
|
MimeMessage imessage;
|
2020-07-03 08:40:10 +00:00
|
|
|
File file = message.getRawFile(context);
|
2019-06-14 06:31:37 +00:00
|
|
|
if (folder.id.equals(message.folder)) {
|
2019-07-27 09:42:19 +00:00
|
|
|
// Pre flight check
|
2019-06-14 06:31:37 +00:00
|
|
|
if (!message.content)
|
|
|
|
throw new IllegalArgumentException("Message body missing");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-10-29 07:12:58 +00:00
|
|
|
if (!BuildConfig.DEBUG) {
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
if (EntityAttachment.SMIME_SIGNATURE.equals(attachment.encryption))
|
|
|
|
for (EntityAttachment content : attachments)
|
|
|
|
if (EntityAttachment.SMIME_CONTENT.equals(content.encryption)) {
|
|
|
|
boolean afile = attachment.getFile(context).exists();
|
|
|
|
boolean cfile = content.getFile(context).exists();
|
|
|
|
if (!attachment.available || !afile || !content.available || !cfile) {
|
|
|
|
Log.e("S/MIME vanished" +
|
|
|
|
" available=" + attachment.available + "/" + content.available +
|
2022-11-12 18:38:41 +00:00
|
|
|
" file=" + afile + "/" + cfile +
|
|
|
|
" error=" + attachment.error + "/" + content.error);
|
2022-10-29 07:12:58 +00:00
|
|
|
db.attachment().setAvailable(attachment.id, false);
|
|
|
|
db.attachment().setAvailable(content.id, false);
|
|
|
|
db.attachment().setEncryption(attachment.id, null);
|
|
|
|
db.attachment().setEncryption(content.id, null);
|
|
|
|
}
|
2022-10-27 19:50:09 +00:00
|
|
|
}
|
2022-10-29 07:12:58 +00:00
|
|
|
}
|
2022-10-27 19:50:09 +00:00
|
|
|
|
2020-04-02 09:33:01 +00:00
|
|
|
imessage = MessageHelper.from(context, message, null, isession, false);
|
2020-07-03 08:40:10 +00:00
|
|
|
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
} else {
|
|
|
|
// Cross account move
|
|
|
|
if (!file.exists())
|
|
|
|
throw new IllegalArgumentException("raw message file not found");
|
2019-03-25 15:14:51 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
Log.i(folder.name + " reading " + file);
|
2019-09-28 16:36:07 +00:00
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
2021-03-10 17:12:44 +00:00
|
|
|
imessage = new MimeMessageEx(isession, is, message.msgid);
|
2021-02-19 12:17:33 +00:00
|
|
|
}
|
2021-03-12 06:45:02 +00:00
|
|
|
|
|
|
|
imessage.removeHeader(MessageHelper.HEADER_CORRELATION_ID);
|
2021-03-11 19:37:01 +00:00
|
|
|
imessage.addHeader(MessageHelper.HEADER_CORRELATION_ID, message.msgid);
|
2021-03-12 06:45:02 +00:00
|
|
|
|
2021-03-10 17:12:44 +00:00
|
|
|
imessage.saveChanges();
|
2022-02-24 07:26:46 +00:00
|
|
|
/*
|
|
|
|
javax.mail.internet.ParseException: Unbalanced quoted string
|
|
|
|
at javax.mail.internet.HeaderTokenizer.collectString(SourceFile:15)
|
|
|
|
at javax.mail.internet.HeaderTokenizer.getNext(SourceFile:20)
|
|
|
|
at javax.mail.internet.HeaderTokenizer.next(SourceFile:4)
|
|
|
|
at javax.mail.internet.HeaderTokenizer.next(SourceFile:1)
|
|
|
|
at javax.mail.internet.ParameterList.<init>(SourceFile:23)
|
|
|
|
at javax.mail.internet.ContentType.<init>(SourceFile:17)
|
|
|
|
at javax.mail.internet.MimeBodyPart.updateHeaders(SourceFile:12)
|
|
|
|
at javax.mail.internet.MimeBodyPart.updateHeaders(SourceFile:1)
|
|
|
|
at javax.mail.internet.MimeMultipart.updateHeaders(SourceFile:3)
|
|
|
|
at javax.mail.internet.MimeBodyPart.updateHeaders(SourceFile:24)
|
|
|
|
at javax.mail.internet.MimeMessage.updateHeaders(SourceFile:1)
|
|
|
|
at javax.mail.internet.MimeMessage.saveChanges(SourceFile:3)
|
|
|
|
*/
|
2021-04-19 13:20:40 +00:00
|
|
|
|
2021-06-26 15:10:49 +00:00
|
|
|
if (flags.contains(Flags.Flag.SEEN))
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, message.ui_seen);
|
|
|
|
if (flags.contains(Flags.Flag.ANSWERED))
|
|
|
|
imessage.setFlag(Flags.Flag.ANSWERED, message.ui_answered);
|
|
|
|
if (flags.contains(Flags.Flag.FLAGGED))
|
|
|
|
imessage.setFlag(Flags.Flag.FLAGGED, message.ui_flagged);
|
|
|
|
if (flags.contains(Flags.Flag.DELETED))
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, message.ui_deleted);
|
|
|
|
|
|
|
|
if (flags.contains(Flags.Flag.USER)) {
|
|
|
|
if (message.isForwarded()) {
|
|
|
|
Flags fwd = new Flags(MessageHelper.FLAG_FORWARDED);
|
|
|
|
imessage.setFlags(new Flags(fwd), true);
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
}
|
2019-03-25 15:14:51 +00:00
|
|
|
|
2020-07-03 08:40:10 +00:00
|
|
|
db.message().setMessageRaw(message.id, true);
|
|
|
|
|
|
|
|
// Check size
|
|
|
|
if (account.max_size != null) {
|
|
|
|
long size = file.length();
|
|
|
|
if (size > account.max_size) {
|
|
|
|
String msg = "Too large" +
|
|
|
|
" size=" + Helper.humanReadableByteCount(size) +
|
|
|
|
"/" + Helper.humanReadableByteCount(account.max_size) +
|
|
|
|
" host=" + account.host;
|
2021-03-05 07:28:18 +00:00
|
|
|
Log.w(msg);
|
2020-07-03 08:40:10 +00:00
|
|
|
throw new IllegalArgumentException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Handle auto read
|
2021-03-11 19:37:01 +00:00
|
|
|
if (flags.contains(Flags.Flag.SEEN))
|
2019-06-14 06:31:37 +00:00
|
|
|
if (autoread && !imessage.isSet(Flags.Flag.SEEN)) {
|
|
|
|
Log.i(folder.name + " autoread");
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Handle draft
|
2019-09-23 11:52:54 +00:00
|
|
|
if (flags.contains(Flags.Flag.DRAFT))
|
2019-06-24 06:34:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.DRAFT, EntityFolder.DRAFTS.equals(folder.type));
|
2019-06-13 10:41:50 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Add message
|
2020-07-02 15:32:49 +00:00
|
|
|
Long newuid = null;
|
2020-08-28 10:02:08 +00:00
|
|
|
if (MessageHelper.hasCapability(ifolder, "UIDPLUS")) {
|
2020-07-02 15:32:49 +00:00
|
|
|
// https://tools.ietf.org/html/rfc4315
|
|
|
|
AppendUID[] uids = ifolder.appendUIDMessages(new Message[]{imessage});
|
2020-07-10 06:45:04 +00:00
|
|
|
if (uids != null && uids.length > 0 && uids[0] != null && uids[0].uid > 0) {
|
2020-07-02 15:32:49 +00:00
|
|
|
newuid = uids[0].uid;
|
|
|
|
Log.i(folder.name + " appended uid=" + newuid);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ifolder.appendMessages(new Message[]{imessage});
|
2019-06-14 06:31:37 +00:00
|
|
|
|
2020-12-05 09:45:14 +00:00
|
|
|
if (folder.id.equals(message.folder)) {
|
|
|
|
// Prevent deleting message
|
2019-09-27 11:05:13 +00:00
|
|
|
db.message().setMessageUid(message.id, null);
|
2019-06-14 06:31:37 +00:00
|
|
|
|
2019-10-08 10:25:09 +00:00
|
|
|
// Some providers do not list the new message yet
|
2021-02-06 16:26:52 +00:00
|
|
|
try {
|
2021-10-28 12:29:37 +00:00
|
|
|
List<Message> delete = new ArrayList<>();
|
|
|
|
|
|
|
|
if (message.uid != null)
|
|
|
|
try {
|
|
|
|
Message iprev = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (iprev != null) {
|
|
|
|
Log.i(folder.name + " found prev uid=" + message.uid + " msgid=" + message.msgid);
|
|
|
|
iprev.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
delete.add(iprev);
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2021-10-25 11:24:05 +00:00
|
|
|
Log.i(folder.name + " searching for added msgid=" + message.msgid);
|
2021-10-25 07:47:54 +00:00
|
|
|
Message[] imessages = findMsgId(context, account, ifolder, message.msgid);
|
2021-10-25 07:25:04 +00:00
|
|
|
if (imessages != null) {
|
2021-10-28 12:29:37 +00:00
|
|
|
Long found = newuid;
|
2021-10-25 07:25:04 +00:00
|
|
|
|
2021-10-25 15:34:34 +00:00
|
|
|
for (Message iexisting : imessages)
|
|
|
|
try {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
if (muid < 0)
|
|
|
|
continue;
|
|
|
|
Log.i(folder.name + " found added uid=" + muid + " msgid=" + message.msgid);
|
|
|
|
if (found == null || muid > found)
|
|
|
|
found = muid;
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-10-25 07:25:04 +00:00
|
|
|
|
|
|
|
if (found != null) {
|
|
|
|
if (newuid == null || found > newuid)
|
|
|
|
newuid = found;
|
|
|
|
|
2021-10-25 15:34:34 +00:00
|
|
|
for (Message iexisting : imessages)
|
|
|
|
try {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
if (muid < 0)
|
|
|
|
continue;
|
2021-10-28 12:29:37 +00:00
|
|
|
if (muid < newuid &&
|
|
|
|
(message.uid == null || message.uid != muid))
|
2021-10-25 15:34:34 +00:00
|
|
|
try {
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
delete.add(iexisting);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-02-06 16:26:52 +00:00
|
|
|
}
|
2021-10-25 07:25:04 +00:00
|
|
|
}
|
2021-10-28 12:29:37 +00:00
|
|
|
|
|
|
|
expunge(context, ifolder, delete);
|
|
|
|
|
2021-02-06 16:26:52 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-12-05 09:45:14 +00:00
|
|
|
|
2020-04-01 13:44:05 +00:00
|
|
|
if (newuid != null && (message.uid == null || newuid > message.uid))
|
|
|
|
try {
|
2020-12-03 12:51:31 +00:00
|
|
|
Log.i(folder.name + " Fetching uid=" + newuid);
|
2020-04-01 13:44:05 +00:00
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(newuid);
|
2020-09-21 19:46:39 +00:00
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
2020-12-03 12:51:31 +00:00
|
|
|
} catch (Throwable ex) {
|
2020-04-01 13:44:05 +00:00
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
} else {
|
2021-03-13 11:42:46 +00:00
|
|
|
// Lookup added message
|
2021-03-11 09:21:17 +00:00
|
|
|
int count = 0;
|
2021-03-13 06:59:40 +00:00
|
|
|
Long found = newuid;
|
2021-03-11 09:21:17 +00:00
|
|
|
while (found == null && count++ < FIND_RETRY_COUNT) {
|
2021-10-25 07:25:04 +00:00
|
|
|
found = findUid(context, account, ifolder, message.msgid);
|
2021-03-11 09:21:17 +00:00
|
|
|
if (found == null)
|
|
|
|
try {
|
|
|
|
Thread.sleep(FIND_RETRY_DELAY);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 05:42:06 +00:00
|
|
|
|
2021-03-11 06:32:43 +00:00
|
|
|
try {
|
2021-03-11 07:05:41 +00:00
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
if (found == null) {
|
2021-03-12 07:20:08 +00:00
|
|
|
db.message().setMessageError(message.id,
|
|
|
|
"Message not found in target folder " + account.name + "/" + folder.name);
|
2021-03-11 07:05:41 +00:00
|
|
|
db.message().setMessageUiHide(message.id, false);
|
|
|
|
} else {
|
|
|
|
// Mark source read
|
|
|
|
if (autoread)
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.SEEN, true);
|
|
|
|
|
|
|
|
// Delete source
|
2021-08-17 18:28:58 +00:00
|
|
|
if (!copy)
|
2023-11-13 12:45:23 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.DELETE, true /* POP3: permanent */);
|
2021-03-11 07:05:41 +00:00
|
|
|
}
|
2021-03-11 06:32:43 +00:00
|
|
|
|
2021-03-11 07:05:41 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
|
2021-03-11 07:05:41 +00:00
|
|
|
// Fetch target
|
|
|
|
if (found != null)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " Fetching uid=" + found);
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(found);
|
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-04-19 11:33:41 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 21:03:55 +00:00
|
|
|
private static void onMove(Context context, JSONArray jargs, boolean copy, EntityAccount account, EntityFolder folder, List<EntityMessage> messages, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Move message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-03-14 11:08:19 +00:00
|
|
|
// Get arguments
|
2019-02-27 15:05:15 +00:00
|
|
|
long id = jargs.getLong(0);
|
2019-10-10 15:09:56 +00:00
|
|
|
boolean seen = jargs.optBoolean(1);
|
|
|
|
boolean unflag = jargs.optBoolean(3);
|
2022-04-20 21:49:52 +00:00
|
|
|
boolean delete = jargs.optBoolean(4);
|
2022-09-16 08:06:16 +00:00
|
|
|
boolean create = jargs.optBoolean(5);
|
2019-10-10 15:09:56 +00:00
|
|
|
|
2019-09-23 11:52:54 +00:00
|
|
|
Flags flags = ifolder.getPermanentFlags();
|
2019-03-14 11:08:19 +00:00
|
|
|
|
|
|
|
// Get target folder
|
2019-02-27 15:05:15 +00:00
|
|
|
EntityFolder target = db.folder().getFolder(id);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
2020-10-15 09:30:25 +00:00
|
|
|
if (folder.id.equals(target.id))
|
2023-02-04 08:14:28 +00:00
|
|
|
throw new IllegalArgumentException("self type=" + folder.type + "/" + target.type);
|
2021-02-07 14:36:26 +00:00
|
|
|
if (!target.selectable)
|
2021-02-10 19:50:07 +00:00
|
|
|
throw new IllegalArgumentException("not selectable type=" + target.type);
|
2020-10-15 09:30:25 +00:00
|
|
|
|
2022-09-16 08:06:16 +00:00
|
|
|
if (create) {
|
|
|
|
Folder icreate = istore.getFolder(target.name);
|
|
|
|
if (!icreate.exists()) {
|
|
|
|
((IMAPFolder) ifolder).doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
protocol.create(target.name);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ifolder.setSubscribed(true);
|
2022-09-16 11:05:41 +00:00
|
|
|
db.folder().resetFolderTbc(target.id);
|
2022-09-16 08:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 19:24:17 +00:00
|
|
|
// De-classify
|
2022-04-30 05:35:16 +00:00
|
|
|
if (!copy &&
|
|
|
|
!EntityFolder.TRASH.equals(target.type) &&
|
|
|
|
!EntityFolder.ARCHIVE.equals(target.type))
|
2021-06-23 21:04:21 +00:00
|
|
|
for (EntityMessage message : messages)
|
2022-04-28 11:59:46 +00:00
|
|
|
MessageClassifier.classify(message, folder, false, context);
|
2021-01-02 19:24:17 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
IMAPFolder itarget = (IMAPFolder) istore.getFolder(target.name);
|
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
// Get source messages
|
|
|
|
Map<Message, EntityMessage> map = new HashMap<>();
|
2021-06-23 21:03:55 +00:00
|
|
|
Map<EntityMessage, String> msgids = new HashMap<>();
|
2019-09-28 11:49:56 +00:00
|
|
|
for (EntityMessage message : messages)
|
2019-11-24 11:33:50 +00:00
|
|
|
try {
|
2019-12-01 11:52:40 +00:00
|
|
|
if (message.uid == null)
|
2019-12-03 06:34:00 +00:00
|
|
|
throw new IllegalArgumentException("move without uid");
|
2019-11-24 11:33:50 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
2019-12-01 11:52:40 +00:00
|
|
|
throw new MessageRemovedException("move without message");
|
2019-11-24 11:33:50 +00:00
|
|
|
map.put(imessage, message);
|
|
|
|
} catch (MessageRemovedException ex) {
|
2019-12-01 11:52:40 +00:00
|
|
|
Log.e(ex);
|
2019-11-24 11:33:50 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
}
|
2019-09-28 11:49:56 +00:00
|
|
|
|
2020-08-26 08:38:15 +00:00
|
|
|
// Some servers return different capabilities for different sessions
|
2021-09-06 17:12:46 +00:00
|
|
|
// NO [CANNOT] MOVE It's not possible to perform specified operation
|
|
|
|
// https://stackoverflow.com/questions/56148668/cannot-delete-emails-of-domain-co-jp-type
|
|
|
|
boolean canMove = !account.isYahooJp() &&
|
|
|
|
MessageHelper.hasCapability(ifolder, "MOVE");
|
2020-08-07 18:49:27 +00:00
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
// Some providers do not support the COPY operation for drafts
|
2019-10-02 08:25:00 +00:00
|
|
|
boolean draft = (EntityFolder.DRAFTS.equals(folder.type) || EntityFolder.DRAFTS.equals(target.type));
|
2022-06-25 05:13:31 +00:00
|
|
|
boolean duplicate = (copy && !account.isGmail()) || (draft && account.isGmail());
|
2021-06-23 21:03:55 +00:00
|
|
|
if (draft || duplicate) {
|
2021-08-17 17:07:58 +00:00
|
|
|
Log.i(folder.name + " " + (duplicate ? "copy" : "move") +
|
|
|
|
" from " + folder.type + " to " + target.type);
|
2019-06-24 07:07:43 +00:00
|
|
|
|
2021-09-12 19:35:19 +00:00
|
|
|
if (!duplicate && account.isSeznam())
|
|
|
|
ifolder.copyMessages(map.keySet().toArray(new Message[0]), itarget);
|
|
|
|
else {
|
|
|
|
List<Message> icopies = new ArrayList<>();
|
|
|
|
for (Message imessage : map.keySet()) {
|
|
|
|
EntityMessage message = map.get(imessage);
|
2019-09-28 11:49:56 +00:00
|
|
|
|
2022-11-01 07:21:16 +00:00
|
|
|
Message icopy;
|
2022-04-30 17:19:10 +00:00
|
|
|
File file = new File(message.getFile(context).getAbsoluteFile() + ".copy");
|
2022-11-01 07:21:16 +00:00
|
|
|
try {
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
2021-09-12 19:35:19 +00:00
|
|
|
|
2022-11-01 07:21:16 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties(account.unicode);
|
|
|
|
Session isession = Session.getInstance(props, null);
|
|
|
|
|
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
|
|
|
if (duplicate) {
|
|
|
|
String msgid = EntityMessage.generateMessageId();
|
|
|
|
msgids.put(message, msgid);
|
|
|
|
icopy = new MimeMessageEx(isession, is, msgid);
|
|
|
|
icopy.saveChanges();
|
|
|
|
|
|
|
|
if (!copy) {
|
|
|
|
List<EntityMessage> tmps = db.message().getMessagesByMsgId(message.account, message.msgid);
|
|
|
|
for (EntityMessage tmp : tmps)
|
|
|
|
if (target.id.equals(tmp.folder)) {
|
|
|
|
db.message().setMessageMsgId(tmp.id, msgid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
icopy = new MimeMessage(isession, is);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
file.delete();
|
2021-09-12 19:35:19 +00:00
|
|
|
}
|
2019-09-28 11:49:56 +00:00
|
|
|
|
2021-09-12 19:35:19 +00:00
|
|
|
for (Flags.Flag flag : imessage.getFlags().getSystemFlags())
|
|
|
|
icopy.setFlag(flag, true);
|
2020-07-27 12:53:54 +00:00
|
|
|
|
2021-09-12 19:35:19 +00:00
|
|
|
icopies.add(icopy);
|
|
|
|
}
|
2019-06-24 11:16:32 +00:00
|
|
|
|
2021-09-12 19:35:19 +00:00
|
|
|
itarget.appendMessages(icopies.toArray(new Message[0]));
|
|
|
|
}
|
2019-06-24 11:16:32 +00:00
|
|
|
} else {
|
2019-09-28 11:49:56 +00:00
|
|
|
for (Message imessage : map.keySet()) {
|
2020-11-07 13:26:00 +00:00
|
|
|
Log.i((copy ? "Copy" : "Move") + " seen=" + seen + " unflag=" + unflag + " flags=" + imessage.getFlags() + " can=" + canMove);
|
2019-06-24 11:16:32 +00:00
|
|
|
|
2019-10-10 15:09:56 +00:00
|
|
|
// Mark read
|
2020-08-07 18:52:20 +00:00
|
|
|
if (seen && !imessage.isSet(Flags.Flag.SEEN) && flags.contains(Flags.Flag.SEEN))
|
2019-10-10 15:09:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.SEEN, true);
|
2019-09-23 11:52:54 +00:00
|
|
|
|
2019-10-10 15:09:56 +00:00
|
|
|
// Remove star
|
2020-08-07 18:52:20 +00:00
|
|
|
if (unflag && imessage.isSet(Flags.Flag.FLAGGED) && flags.contains(Flags.Flag.FLAGGED))
|
2019-10-10 15:09:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.FLAGGED, false);
|
2021-06-30 19:35:00 +00:00
|
|
|
|
|
|
|
// Mark not spam
|
2021-09-18 20:34:25 +00:00
|
|
|
if (!copy && ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
|
|
|
|
Flags junk = new Flags(MessageHelper.FLAG_JUNK);
|
2021-07-01 06:00:04 +00:00
|
|
|
Flags notJunk = new Flags(MessageHelper.FLAG_NOT_JUNK);
|
2021-09-18 20:34:25 +00:00
|
|
|
List<String> userFlags = Arrays.asList(imessage.getFlags().getUserFlags());
|
|
|
|
if (EntityFolder.JUNK.equals(target.type)) {
|
|
|
|
// To junk
|
|
|
|
if (userFlags.contains(MessageHelper.FLAG_NOT_JUNK))
|
|
|
|
imessage.setFlags(notJunk, false);
|
|
|
|
imessage.setFlags(junk, true);
|
|
|
|
} else if (EntityFolder.JUNK.equals(folder.type)) {
|
|
|
|
// From junk
|
|
|
|
if (userFlags.contains(MessageHelper.FLAG_JUNK))
|
|
|
|
imessage.setFlags(junk, false);
|
|
|
|
imessage.setFlags(notJunk, true);
|
|
|
|
}
|
2021-06-30 19:35:00 +00:00
|
|
|
}
|
2019-09-28 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 18:49:27 +00:00
|
|
|
// https://tools.ietf.org/html/rfc6851
|
|
|
|
if (!copy && canMove)
|
2022-08-08 20:31:28 +00:00
|
|
|
try {
|
|
|
|
ifolder.moveMessages(map.keySet().toArray(new Message[0]), itarget);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
if (!(map.size() == 1 &&
|
|
|
|
ex.getCause() instanceof CommandFailedException &&
|
|
|
|
ex.getCause().getMessage() != null &&
|
|
|
|
ex.getCause().getMessage().contains("[EXPUNGEISSUED]")))
|
|
|
|
throw ex;
|
|
|
|
}
|
2020-08-07 18:49:27 +00:00
|
|
|
else
|
|
|
|
ifolder.copyMessages(map.keySet().toArray(new Message[0]), itarget);
|
2019-06-24 11:16:32 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-25 15:14:51 +00:00
|
|
|
// Delete source
|
2020-08-07 18:49:27 +00:00
|
|
|
if (!copy && (draft || !canMove)) {
|
2021-10-25 17:36:03 +00:00
|
|
|
List<Message> deleted = new ArrayList<>();
|
|
|
|
for (Message imessage : map.keySet())
|
|
|
|
try {
|
2019-09-28 11:49:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
2021-10-25 17:36:03 +00:00
|
|
|
deleted.add(imessage);
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
expunge(context, ifolder, deleted);
|
2020-08-07 18:58:42 +00:00
|
|
|
} else {
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2022-05-02 13:23:16 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count, new Date().getTime());
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-09-12 15:44:36 +00:00
|
|
|
|
2019-09-27 13:32:29 +00:00
|
|
|
// Fetch appended/copied when needed
|
2022-04-20 21:49:52 +00:00
|
|
|
boolean fetch = (copy || delete ||
|
2020-12-28 11:21:00 +00:00
|
|
|
!"connected".equals(target.state) ||
|
|
|
|
!MessageHelper.hasCapability(ifolder, "IDLE"));
|
2020-01-17 09:22:36 +00:00
|
|
|
if (draft || fetch)
|
2019-09-27 12:14:04 +00:00
|
|
|
try {
|
2020-01-17 09:22:36 +00:00
|
|
|
Log.i(target.name + " moved message fetch=" + fetch);
|
2019-09-27 13:32:29 +00:00
|
|
|
itarget.open(READ_WRITE);
|
2019-09-28 11:49:56 +00:00
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
boolean sync = false;
|
2022-04-20 21:49:52 +00:00
|
|
|
List<Message> ideletes = new ArrayList<>();
|
2019-09-28 11:49:56 +00:00
|
|
|
for (EntityMessage message : map.values())
|
2021-02-06 17:30:26 +00:00
|
|
|
try {
|
2021-06-23 21:03:55 +00:00
|
|
|
String msgid = msgids.get(message);
|
|
|
|
if (msgid == null)
|
|
|
|
msgid = message.msgid;
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(msgid))
|
2021-02-06 17:30:26 +00:00
|
|
|
throw new IllegalArgumentException("move: msgid missing");
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-10-25 07:25:04 +00:00
|
|
|
Long uid = findUid(context, account, itarget, msgid);
|
2021-02-06 17:30:26 +00:00
|
|
|
if (uid == null)
|
|
|
|
throw new IllegalArgumentException("move: uid not found");
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-06-30 11:25:31 +00:00
|
|
|
if (draft || duplicate) {
|
2021-02-06 17:30:26 +00:00
|
|
|
Message icopy = itarget.getMessageByUID(uid);
|
2021-06-30 11:25:31 +00:00
|
|
|
if (icopy == null)
|
2021-02-06 17:30:26 +00:00
|
|
|
throw new IllegalArgumentException("move: gone uid=" + uid);
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
// Mark read
|
|
|
|
if (seen && !icopy.isSet(Flags.Flag.SEEN) && flags.contains(Flags.Flag.SEEN))
|
|
|
|
icopy.setFlag(Flags.Flag.SEEN, true);
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
// Remove star
|
|
|
|
if (unflag && icopy.isSet(Flags.Flag.FLAGGED) && flags.contains(Flags.Flag.FLAGGED))
|
|
|
|
icopy.setFlag(Flags.Flag.FLAGGED, false);
|
|
|
|
|
|
|
|
// Set drafts flag
|
|
|
|
if (flags.contains(Flags.Flag.DRAFT))
|
|
|
|
icopy.setFlag(Flags.Flag.DRAFT, EntityFolder.DRAFTS.equals(target.type));
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:49:52 +00:00
|
|
|
if (delete) {
|
|
|
|
Log.i(target.name + " Deleting uid=" + uid);
|
|
|
|
Message idelete = itarget.getMessageByUID(uid);
|
|
|
|
idelete.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
ideletes.add(idelete);
|
|
|
|
} else if (fetch) {
|
2021-02-06 17:30:26 +00:00
|
|
|
Log.i(target.name + " Fetching uid=" + uid);
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(uid);
|
|
|
|
onFetch(context, fargs, target, istore, itarget, state);
|
2019-09-28 11:49:56 +00:00
|
|
|
}
|
2021-02-06 17:30:26 +00:00
|
|
|
} catch (Throwable ex) {
|
2021-02-07 17:15:26 +00:00
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
Log.i(ex);
|
|
|
|
else
|
|
|
|
Log.e(ex);
|
2021-02-06 17:30:26 +00:00
|
|
|
if (fetch)
|
|
|
|
sync = true;
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:49:52 +00:00
|
|
|
expunge(context, itarget, ideletes);
|
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
if (sync)
|
|
|
|
EntityOperation.sync(context, target.id, false);
|
2019-09-27 13:32:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
2019-09-28 11:49:56 +00:00
|
|
|
} finally {
|
|
|
|
if (itarget.isOpen())
|
2022-09-23 16:12:42 +00:00
|
|
|
itarget.close(false);
|
2019-09-27 12:14:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:44:36 +00:00
|
|
|
// Delete junk contacts
|
2019-09-28 11:49:56 +00:00
|
|
|
if (EntityFolder.JUNK.equals(target.type))
|
|
|
|
for (EntityMessage message : map.values()) {
|
|
|
|
Address[] recipients = (message.reply != null ? message.reply : message.from);
|
|
|
|
if (recipients != null)
|
|
|
|
for (Address recipient : recipients) {
|
|
|
|
String email = ((InternetAddress) recipient).getAddress();
|
|
|
|
int count = db.contact().deleteContact(target.account, EntityContact.TYPE_FROM, email);
|
|
|
|
Log.i("Deleted contact email=" + email + " count=" + count);
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void onFetch(Context context, JSONArray jargs, EntityFolder folder, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
2019-09-17 11:38:23 +00:00
|
|
|
long uid = jargs.getLong(0);
|
2022-01-23 07:29:11 +00:00
|
|
|
boolean invalidate = jargs.optBoolean(1);
|
|
|
|
boolean removed = jargs.optBoolean(2);
|
2020-08-09 05:35:53 +00:00
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2019-09-18 16:30:12 +00:00
|
|
|
EntityAccount account = db.account().getAccount(folder.account);
|
2020-11-05 20:44:13 +00:00
|
|
|
if (account == null)
|
|
|
|
throw new IllegalArgumentException("account missing");
|
2019-02-27 17:02:30 +00:00
|
|
|
|
2019-09-18 16:30:12 +00:00
|
|
|
try {
|
2022-11-04 11:36:15 +00:00
|
|
|
if (uid < 0)
|
|
|
|
throw new MessageRemovedException(folder.name + " fetch uid=" + uid);
|
|
|
|
if (removed)
|
2020-08-09 05:35:53 +00:00
|
|
|
throw new MessageRemovedException("removed uid=" + uid);
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2019-09-26 08:53:27 +00:00
|
|
|
MimeMessage imessage = (MimeMessage) ifolder.getMessageByUID(uid);
|
2019-09-18 16:30:12 +00:00
|
|
|
if (imessage == null)
|
2020-07-16 18:11:07 +00:00
|
|
|
throw new MessageRemovedException(folder.name + " fetch not found uid=" + uid);
|
2021-06-18 08:17:10 +00:00
|
|
|
// synchronizeMessage will check expunged/deleted
|
2019-09-18 16:30:12 +00:00
|
|
|
|
2022-01-23 07:29:11 +00:00
|
|
|
if (invalidate && imessage instanceof IMAPMessage)
|
|
|
|
((IMAPMessage) imessage).invalidateHeaders();
|
|
|
|
|
2020-08-09 05:35:53 +00:00
|
|
|
SyncStats stats = new SyncStats();
|
|
|
|
boolean download = db.folder().getFolderDownload(folder.id);
|
2022-12-24 09:10:21 +00:00
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id, false);
|
2020-08-09 05:35:53 +00:00
|
|
|
|
2021-06-18 09:42:32 +00:00
|
|
|
FetchProfile fp = new FetchProfile();
|
2022-01-21 11:36:40 +00:00
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID); // To check if message exists
|
|
|
|
fp.add(FetchProfile.Item.FLAGS); // To update existing messages
|
|
|
|
if (account.isGmail())
|
2021-06-18 09:42:32 +00:00
|
|
|
fp.add(GmailFolder.FetchProfileItem.LABELS);
|
|
|
|
ifolder.fetch(new Message[]{imessage}, fp);
|
2020-06-25 17:53:30 +00:00
|
|
|
|
2021-06-18 09:42:32 +00:00
|
|
|
EntityMessage message = synchronizeMessage(context, account, folder, istore, ifolder, imessage, false, download, rules, state, stats);
|
|
|
|
if (message != null) {
|
|
|
|
if (account.isGmail() && EntityFolder.USER.equals(folder.type))
|
|
|
|
try {
|
|
|
|
JSONArray jlabel = new JSONArray();
|
|
|
|
jlabel.put(0, folder.name);
|
|
|
|
jlabel.put(1, true);
|
|
|
|
onLabel(context, jlabel, folder, message, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex1) {
|
|
|
|
Log.e(ex1);
|
|
|
|
}
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2022-10-22 12:50:39 +00:00
|
|
|
if (download) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean fast_fetch = prefs.getBoolean("fast_fetch", false);
|
2022-10-29 19:47:46 +00:00
|
|
|
|
|
|
|
boolean async = false;
|
2022-10-22 12:50:39 +00:00
|
|
|
if (fast_fetch) {
|
|
|
|
long maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE);
|
|
|
|
if (maxSize == 0)
|
|
|
|
maxSize = Long.MAX_VALUE;
|
|
|
|
boolean download_eml = prefs.getBoolean("download_eml", false);
|
|
|
|
|
|
|
|
if (!message.content)
|
|
|
|
if (state.getNetworkState().isUnmetered() || (message.size != null && message.size < maxSize))
|
2022-10-29 19:47:46 +00:00
|
|
|
async = true;
|
2022-10-22 12:50:39 +00:00
|
|
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
if (!attachment.available)
|
|
|
|
if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize))
|
2022-10-29 19:47:46 +00:00
|
|
|
async = true;
|
2022-10-22 12:50:39 +00:00
|
|
|
|
|
|
|
if (download_eml &&
|
|
|
|
(message.raw == null || !message.raw) &&
|
|
|
|
(state.getNetworkState().isUnmetered() || (message.total != null && message.total < maxSize)))
|
2022-10-29 19:47:46 +00:00
|
|
|
async = true;
|
|
|
|
}
|
|
|
|
|
2022-10-30 15:14:07 +00:00
|
|
|
if (async && message.uid != null && !message.ui_hide)
|
2022-10-29 19:47:46 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.DOWNLOAD, message.uid);
|
|
|
|
else
|
2022-10-22 12:50:39 +00:00
|
|
|
downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state, stats);
|
|
|
|
}
|
2019-09-18 16:30:12 +00:00
|
|
|
}
|
2021-06-18 09:42:32 +00:00
|
|
|
|
|
|
|
if (!stats.isEmpty())
|
2021-08-16 07:36:23 +00:00
|
|
|
EntityLog.log(context, EntityLog.Type.Statistics,
|
2021-08-15 18:32:34 +00:00
|
|
|
account.name + "/" + folder.name + " fetch stats " + stats);
|
2022-11-04 11:36:15 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
if (MessageHelper.isRemoved(ex)) {
|
|
|
|
Log.i(ex);
|
2020-06-25 17:53:30 +00:00
|
|
|
|
2022-11-04 11:36:15 +00:00
|
|
|
if (account.isGmail() && EntityFolder.USER.equals(folder.type)) {
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
if (message != null)
|
|
|
|
try {
|
|
|
|
JSONArray jlabel = new JSONArray();
|
|
|
|
jlabel.put(0, folder.name);
|
|
|
|
jlabel.put(1, false);
|
|
|
|
onLabel(context, jlabel, folder, message, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex1) {
|
|
|
|
Log.e(ex1);
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 17:53:30 +00:00
|
|
|
|
2022-11-04 12:17:24 +00:00
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
|
|
|
Log.i(folder.name + " delete local uid=" + uid + " count=" + count);
|
2022-11-04 11:36:15 +00:00
|
|
|
} else
|
|
|
|
throw ex;
|
2019-09-17 11:38:23 +00:00
|
|
|
} finally {
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2022-05-02 13:23:16 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count, new Date().getTime());
|
2019-06-14 08:12:08 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
private static void onDelete(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, List<EntityMessage> messages, IMAPFolder ifolder) throws MessagingException, IOException {
|
2019-09-17 11:38:23 +00:00
|
|
|
// Delete message
|
|
|
|
DB db = DB.getInstance(context);
|
2021-02-11 08:51:48 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
2020-12-17 14:05:44 +00:00
|
|
|
|
|
|
|
if (folder.local) {
|
|
|
|
Log.i(folder.name + " local delete");
|
2022-01-17 15:47:13 +00:00
|
|
|
for (EntityMessage message : messages)
|
|
|
|
db.message().deleteMessage(message.id);
|
2020-12-17 14:05:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
try {
|
2022-01-17 15:47:13 +00:00
|
|
|
if (messages.size() > 1) {
|
|
|
|
boolean ui_deleted = messages.get(0).ui_deleted;
|
|
|
|
|
|
|
|
List<Long> uids = new ArrayList<>();
|
|
|
|
for (EntityMessage message : messages) {
|
|
|
|
if (message.uid == null)
|
|
|
|
throw new MessagingException("Delete: uid missing");
|
|
|
|
if (message.ui_deleted != ui_deleted)
|
|
|
|
throw new MessagingException("Delete: flag inconsistent");
|
|
|
|
uids.add(message.uid);
|
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
Message[] idelete = ifolder.getMessagesByUID(Helper.toLongArray(uids));
|
|
|
|
for (Message imessage : idelete)
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessagingException("Delete: message missing");
|
2019-04-11 11:02:56 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
EntityLog.log(context, folder.name + " deleting messages=" + uids.size());
|
2021-10-25 07:47:54 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
if (perform_expunge) {
|
|
|
|
ifolder.setFlags(idelete, new Flags(Flags.Flag.DELETED), true);
|
|
|
|
expunge(context, ifolder, Arrays.asList(idelete));
|
|
|
|
for (EntityMessage message : messages)
|
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
} else {
|
|
|
|
ifolder.setFlags(idelete, new Flags(Flags.Flag.DELETED), ui_deleted);
|
|
|
|
for (EntityMessage message : messages)
|
|
|
|
db.message().setMessageDeleted(message.id, message.ui_deleted);
|
|
|
|
}
|
2021-10-25 07:48:11 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
EntityLog.log(context, folder.name + " deleted messages=" + uids.size());
|
|
|
|
} else if (messages.size() == 1) {
|
|
|
|
List<Message> deleted = new ArrayList<>();
|
2021-10-24 12:39:49 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
EntityMessage message = messages.get(0);
|
|
|
|
if (message.uid != null) {
|
|
|
|
Message iexisting = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (iexisting == null)
|
|
|
|
Log.w(folder.name + " existing not found uid=" + message.uid);
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " deleting uid=" + message.uid);
|
|
|
|
if (perform_expunge)
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
else
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, message.ui_deleted);
|
|
|
|
deleted.add(iexisting);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
Log.w(folder.name + " existing gone uid=" + message.uid);
|
|
|
|
}
|
2020-10-04 17:57:19 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
boolean found = (deleted.size() > 0);
|
|
|
|
if (!TextUtils.isEmpty(message.msgid) &&
|
|
|
|
(!found || EntityFolder.DRAFTS.equals(folder.type)))
|
|
|
|
try {
|
|
|
|
Message[] imessages = findMsgId(context, account, ifolder, message.msgid);
|
|
|
|
if (imessages != null)
|
|
|
|
for (Message iexisting : imessages)
|
|
|
|
try {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
if (found && muid == message.uid)
|
|
|
|
continue;
|
2021-02-11 09:41:07 +00:00
|
|
|
|
2022-01-17 15:47:13 +00:00
|
|
|
// Fail safe
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) iexisting, context);
|
|
|
|
if (!message.msgid.equals(helper.getMessageID()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Log.i(folder.name + " deleting uid=" + muid);
|
|
|
|
if (perform_expunge)
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
else
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, message.ui_deleted);
|
|
|
|
|
|
|
|
deleted.add(iexisting);
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perform_expunge) {
|
|
|
|
if (deleted.size() == 0 || expunge(context, ifolder, deleted))
|
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
} else {
|
|
|
|
if (deleted.size() > 0)
|
|
|
|
db.message().setMessageDeleted(message.id, message.ui_deleted);
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
} finally {
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2022-05-02 13:23:16 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count, new Date().getTime());
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:45:23 +00:00
|
|
|
private static void onDelete(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, List<EntityMessage> messages, POP3Folder ifolder, POP3Store istore, State state) throws JSONException, MessagingException, IOException {
|
|
|
|
boolean permanent = (jargs.length() > 0 && jargs.getBoolean(0));
|
|
|
|
|
2021-11-07 06:48:24 +00:00
|
|
|
// Delete from server
|
2023-11-13 12:45:23 +00:00
|
|
|
if (!EntityFolder.INBOX.equals(folder.type) || (account.leave_deleted && !permanent))
|
2023-01-26 17:32:53 +00:00
|
|
|
throw new IllegalArgumentException("POP3: invalid DELETE" +
|
2023-11-13 12:45:23 +00:00
|
|
|
" folder=" + folder.type +
|
|
|
|
" leave=" + account.leave_deleted +
|
|
|
|
" permanent=" + permanent);
|
2020-05-05 16:03:14 +00:00
|
|
|
|
2023-01-25 13:42:36 +00:00
|
|
|
Map<EntityMessage, Message> map = findMessages(context, folder, messages, istore, ifolder);
|
2022-08-09 06:46:11 +00:00
|
|
|
for (EntityMessage message : messages) {
|
2023-01-25 13:42:36 +00:00
|
|
|
Message imessage = map.get(message);
|
|
|
|
if (imessage != null) {
|
|
|
|
Log.i(folder.name + " POP delete=" + message.uidl + "/" + message.msgid);
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
2021-11-07 06:48:24 +00:00
|
|
|
}
|
2022-08-09 06:46:11 +00:00
|
|
|
}
|
2023-01-25 13:42:36 +00:00
|
|
|
|
|
|
|
if (map.size() > 0)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " POP expunge");
|
|
|
|
ifolder.close(true);
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
state.error(new FolderClosedException(ifolder, "POP", new Exception(ex)));
|
|
|
|
}
|
2019-09-19 19:42:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:43:13 +00:00
|
|
|
private static void onHeaders(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException {
|
2019-02-27 17:02:30 +00:00
|
|
|
// Download headers
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (message.headers != null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage, context);
|
2019-02-27 15:05:15 +00:00
|
|
|
db.message().setMessageHeaders(message.id, helper.getHeaders());
|
|
|
|
}
|
|
|
|
|
2023-06-02 15:48:36 +00:00
|
|
|
private static void onRaw(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException, JSONException {
|
2019-02-27 17:02:30 +00:00
|
|
|
// Download raw message
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (message.raw == null || !message.raw) {
|
|
|
|
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2023-06-01 17:04:43 +00:00
|
|
|
EntityLog.log(context, "Downloading raw id=" + message.id + " subject=" + message.subject);
|
2019-03-14 07:45:13 +00:00
|
|
|
File file = message.getRawFile(context);
|
2019-09-28 16:36:07 +00:00
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
2019-02-27 15:05:15 +00:00
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
2019-09-28 16:36:07 +00:00
|
|
|
|
2023-06-02 15:48:36 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties(account.unicode);
|
|
|
|
Session isession = Session.getInstance(props, null);
|
|
|
|
|
|
|
|
MessageHelper helper;
|
|
|
|
try (InputStream is = new FileInputStream(file)) {
|
|
|
|
helper = new MessageHelper(new MimeMessage(isession, is), context);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yahoo is returning incorrect messages
|
|
|
|
if (!Objects.equals(message.msgid, helper.getMessageID()))
|
|
|
|
throw new MessagingException("Incorrect msgid=" + message.msgid + "/" + helper.getMessageID());
|
|
|
|
|
2019-09-28 16:36:07 +00:00
|
|
|
db.message().setMessageRaw(message.id, true);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jargs.length() > 0) {
|
2021-08-17 18:28:58 +00:00
|
|
|
// Cross account move/copy
|
2020-02-06 19:08:34 +00:00
|
|
|
long tid = jargs.getLong(0);
|
|
|
|
EntityFolder target = db.folder().getFolder(tid);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
|
|
|
|
|
|
|
Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
2020-02-06 19:08:34 +00:00
|
|
|
operation.account = target.account;
|
|
|
|
operation.folder = target.id;
|
2019-02-27 15:05:15 +00:00
|
|
|
operation.message = message.id;
|
|
|
|
operation.name = EntityOperation.ADD;
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:55:54 +00:00
|
|
|
private static void onRaw(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, POP3Store istore, POP3Folder ifolder) throws MessagingException, IOException, JSONException {
|
2021-09-05 20:01:28 +00:00
|
|
|
// Download raw message
|
2023-11-13 12:45:23 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2021-09-05 20:01:28 +00:00
|
|
|
if (!EntityFolder.INBOX.equals(folder.type))
|
|
|
|
throw new IllegalArgumentException("Unexpected folder=" + folder.type);
|
|
|
|
|
|
|
|
if (message.raw == null || !message.raw) {
|
2022-11-24 10:55:54 +00:00
|
|
|
Map<EntityMessage, Message> map = findMessages(context, folder, Arrays.asList(message), istore, ifolder);
|
2022-08-09 06:46:11 +00:00
|
|
|
if (map.get(message) == null)
|
2021-09-05 20:01:28 +00:00
|
|
|
throw new IllegalArgumentException("Message not found msgid=" + message.msgid);
|
|
|
|
|
|
|
|
File file = message.getRawFile(context);
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
2022-08-09 06:46:11 +00:00
|
|
|
map.get(message).writeTo(os);
|
2021-09-05 20:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.message().setMessageRaw(message.id, true);
|
|
|
|
}
|
2023-11-13 12:45:23 +00:00
|
|
|
|
|
|
|
if (jargs.length() > 0) {
|
|
|
|
// Cross account move/copy
|
|
|
|
long tid = jargs.getLong(0);
|
|
|
|
EntityFolder target = db.folder().getFolder(tid);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
|
|
|
|
|
|
|
Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target.id);
|
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
|
|
|
operation.account = target.account;
|
|
|
|
operation.folder = target.id;
|
|
|
|
operation.message = message.id;
|
|
|
|
operation.name = EntityOperation.ADD;
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
}
|
2021-09-05 20:01:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:22:18 +00:00
|
|
|
private static void onBody(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException {
|
2023-06-03 08:38:53 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean download_plain = prefs.getBoolean("download_plain", false);
|
|
|
|
|
|
|
|
boolean plain_text = jargs.optBoolean(0, download_plain);
|
2023-01-11 16:17:22 +00:00
|
|
|
String charset = (jargs.isNull(1) ? null : jargs.optString(1, null));
|
2022-02-13 08:53:03 +00:00
|
|
|
|
2022-06-11 06:20:36 +00:00
|
|
|
if (message.uid == null)
|
2022-06-11 06:07:51 +00:00
|
|
|
throw new IllegalArgumentException("uid missing");
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// Download message body
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2022-06-10 18:34:24 +00:00
|
|
|
if (message.content && message.isPlainOnly() == plain_text && charset == null)
|
2019-02-27 15:05:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
2023-09-13 18:50:01 +00:00
|
|
|
if (imessage == null) {
|
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, "");
|
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
context.getString(R.string.title_not_existing));
|
|
|
|
return;
|
|
|
|
//throw new MessageRemovedException();
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2022-06-10 18:34:24 +00:00
|
|
|
String body = parts.getHtml(context, plain_text, charset);
|
2020-02-20 09:35:01 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
2021-01-19 10:57:59 +00:00
|
|
|
String text = HtmlHelper.getFullText(body);
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
2021-01-25 13:49:56 +00:00
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
2022-02-13 10:36:38 +00:00
|
|
|
Integer plain_only = parts.isPlainOnly();
|
|
|
|
if (plain_text)
|
|
|
|
plain_only = 1 | (plain_only == null ? 0 : plain_only & 0x80);
|
2019-05-04 18:52:21 +00:00
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
2021-01-19 11:18:03 +00:00
|
|
|
message.language,
|
2022-02-13 10:36:38 +00:00
|
|
|
plain_only,
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.getWarnings(message.warning));
|
2022-04-28 11:59:46 +00:00
|
|
|
MessageClassifier.classify(message, folder, true, context);
|
2020-07-15 16:58:03 +00:00
|
|
|
|
|
|
|
if (body != null)
|
|
|
|
EntityLog.log(context, "Operation body size=" + body.length());
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onAttachment(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Download attachment
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-05-08 08:32:37 +00:00
|
|
|
long id = jargs.getLong(0);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// Get attachment
|
2019-06-26 19:21:09 +00:00
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id);
|
|
|
|
if (attachment == null)
|
|
|
|
attachment = db.attachment().getAttachment(message.id, (int) id); // legacy
|
|
|
|
if (attachment == null)
|
2019-05-16 18:44:42 +00:00
|
|
|
throw new IllegalArgumentException("Local attachment not found");
|
2020-10-18 09:03:29 +00:00
|
|
|
if (attachment.subsequence != null)
|
|
|
|
throw new IllegalArgumentException("Download of sub attachment");
|
2023-06-21 15:26:53 +00:00
|
|
|
if (attachment.available)
|
2019-02-27 15:05:15 +00:00
|
|
|
return;
|
2021-10-15 19:04:03 +00:00
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("Attachment/message uid missing");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2023-06-21 15:26:53 +00:00
|
|
|
// Get message parts
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-05-12 13:29:18 +00:00
|
|
|
|
2023-06-21 15:26:53 +00:00
|
|
|
// Download attachment
|
|
|
|
parts.downloadAttachment(context, attachment, folder);
|
2023-06-19 07:34:10 +00:00
|
|
|
|
2023-06-21 15:26:53 +00:00
|
|
|
if (attachment.size != null)
|
|
|
|
EntityLog.log(context, "Operation attachment size=" + attachment.size);
|
|
|
|
}
|
2023-06-19 07:34:10 +00:00
|
|
|
|
2023-06-26 16:15:48 +00:00
|
|
|
private static void onDetach(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
2023-06-21 15:26:53 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2023-06-19 07:34:10 +00:00
|
|
|
|
2023-06-26 09:35:26 +00:00
|
|
|
JSONArray jids = jargs.getJSONArray(0);
|
|
|
|
List<Long> ids = new ArrayList<>();
|
|
|
|
for (int i = 0; i < jids.length(); i++)
|
|
|
|
ids.add(jids.getLong(i));
|
2023-06-19 07:34:10 +00:00
|
|
|
|
2023-06-21 15:26:53 +00:00
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("Delete attachments uid missing");
|
|
|
|
|
2023-06-26 09:35:26 +00:00
|
|
|
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
|
|
|
|
|
2023-06-21 15:26:53 +00:00
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
String msgid = EntityMessage.generateMessageId();
|
2023-06-27 19:35:13 +00:00
|
|
|
String ref = (TextUtils.isEmpty(message.references)
|
|
|
|
? message.msgid
|
|
|
|
: message.references + " " + message.msgid);
|
2023-06-21 15:26:53 +00:00
|
|
|
MimeMessage icopy = new MimeMessageEx((MimeMessage) imessage, msgid);
|
2023-06-27 19:35:13 +00:00
|
|
|
icopy.addHeader("References", MessageHelper.limitReferences(ref));
|
2023-06-21 15:26:53 +00:00
|
|
|
MessageHelper helper = new MessageHelper(icopy, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
List<MessageHelper.AttachmentPart> aparts = parts.getAttachmentParts();
|
|
|
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
for (EntityAttachment attachment : attachments)
|
2023-06-26 09:35:26 +00:00
|
|
|
if (ids.contains(attachment.id)) {
|
2023-06-21 15:26:53 +00:00
|
|
|
Part apart = aparts.get(attachment.sequence - 1).part;
|
|
|
|
if (!deletePart(icopy, apart))
|
|
|
|
throw new IllegalArgumentException("Attachment part not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
ifolder.appendMessages(new Message[]{icopy});
|
|
|
|
|
2023-06-26 16:15:48 +00:00
|
|
|
Long uid = findUid(context, account, ifolder, msgid);
|
|
|
|
if (uid != null) {
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(uid);
|
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
|
|
|
}
|
|
|
|
|
2023-06-26 09:35:26 +00:00
|
|
|
if (trash == null) {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
expunge(context, ifolder, Arrays.asList(imessage));
|
2023-07-23 05:35:28 +00:00
|
|
|
} else
|
2023-06-26 09:35:26 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, trash.id);
|
2023-06-19 07:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static boolean deletePart(Part part, Part attachment) throws MessagingException, IOException {
|
|
|
|
boolean deleted = false;
|
|
|
|
if (part.isMimeType("multipart/*")) {
|
|
|
|
Multipart multipart = (Multipart) part.getContent();
|
2023-06-26 09:35:26 +00:00
|
|
|
for (int i = 0; i < multipart.getCount(); i++) {
|
2023-06-19 07:34:10 +00:00
|
|
|
Part child = multipart.getBodyPart(i);
|
|
|
|
if (child == attachment) {
|
2023-06-26 11:59:42 +00:00
|
|
|
String fileName = child.getFileName();
|
|
|
|
String contentType = child.getContentType();
|
|
|
|
String disposition = child.getDisposition();
|
|
|
|
|
|
|
|
if (fileName != null && !fileName.startsWith("deleted"))
|
|
|
|
fileName = "deleted" + (TextUtils.isEmpty(fileName) ? "" : "_" + fileName);
|
|
|
|
if (TextUtils.isEmpty(contentType))
|
|
|
|
contentType = "application/octet-stream";
|
|
|
|
if (TextUtils.isEmpty(disposition))
|
|
|
|
disposition = Part.ATTACHMENT;
|
|
|
|
|
2023-06-19 07:34:10 +00:00
|
|
|
multipart.removeBodyPart(i);
|
2023-06-26 11:59:42 +00:00
|
|
|
|
2023-08-20 20:10:13 +00:00
|
|
|
try {
|
|
|
|
// Can't upload empty message/rfc822
|
|
|
|
ContentType ct = new ContentType(contentType);
|
|
|
|
if ("message/rfc822".equalsIgnoreCase(ct.getBaseType()))
|
|
|
|
contentType = "application/octet-stream";
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2023-06-26 11:59:42 +00:00
|
|
|
BodyPart placeholderPart = new MimeBodyPart();
|
|
|
|
placeholderPart.setContent("", contentType);
|
|
|
|
placeholderPart.setFileName(fileName);
|
|
|
|
placeholderPart.setDisposition(disposition);
|
|
|
|
multipart.addBodyPart(placeholderPart);
|
|
|
|
|
2023-06-19 07:34:10 +00:00
|
|
|
deleted = true;
|
|
|
|
} else {
|
|
|
|
if (deletePart(child, attachment))
|
|
|
|
deleted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part instanceof MimeMessage)
|
|
|
|
((MimeMessage) part).saveChanges();
|
|
|
|
else if (part instanceof Multipart)
|
|
|
|
part.setDataHandler(part.getDataHandler());
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2023-06-19 07:34:10 +00:00
|
|
|
return deleted;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 07:45:53 +00:00
|
|
|
private static void onBody(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, POP3Folder ifolder, POP3Store istore) throws MessagingException, IOException {
|
|
|
|
if (!EntityFolder.INBOX.equals(folder.type))
|
|
|
|
throw new IllegalArgumentException("Not INBOX");
|
|
|
|
|
|
|
|
Map<EntityMessage, Message> map = findMessages(context, folder, Arrays.asList(message), istore, ifolder);
|
2023-03-24 06:45:42 +00:00
|
|
|
if (map.get(message) == null)
|
|
|
|
throw new IllegalArgumentException("Message not found msgid=" + message.msgid);
|
2023-01-06 07:45:53 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean download_plain = prefs.getBoolean("download_plain", false);
|
|
|
|
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) map.entrySet().iterator().next().getValue(), context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
String body = parts.getHtml(context, download_plain);
|
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
|
|
|
String text = HtmlHelper.getFullText(body);
|
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
message.language,
|
|
|
|
parts.isPlainOnly(download_plain),
|
|
|
|
message.preview,
|
|
|
|
parts.getWarnings(message.warning));
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:55:54 +00:00
|
|
|
private static void onAttachment(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, POP3Folder ifolder, POP3Store istore) throws JSONException, MessagingException, IOException {
|
2022-10-31 18:02:29 +00:00
|
|
|
long id = jargs.getLong(0);
|
|
|
|
|
|
|
|
if (!EntityFolder.INBOX.equals(folder.type))
|
|
|
|
throw new IllegalArgumentException("Not INBOX");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id);
|
|
|
|
if (attachment == null)
|
|
|
|
throw new IllegalArgumentException("Local attachment not found");
|
|
|
|
if (attachment.subsequence != null)
|
|
|
|
throw new IllegalArgumentException("Download of sub attachment");
|
|
|
|
if (attachment.available)
|
|
|
|
return;
|
|
|
|
|
2022-11-24 10:55:54 +00:00
|
|
|
Map<EntityMessage, Message> map = findMessages(context, folder, Arrays.asList(message), istore, ifolder);
|
2023-03-24 06:45:42 +00:00
|
|
|
if (map.get(message) == null)
|
|
|
|
throw new IllegalArgumentException("Message not found msgid=" + message.msgid);
|
2022-10-31 18:02:29 +00:00
|
|
|
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) map.entrySet().iterator().next().getValue(), context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
// Download attachment
|
2023-06-06 20:22:08 +00:00
|
|
|
parts.downloadAttachment(context, attachment, folder);
|
2022-10-31 18:02:29 +00:00
|
|
|
|
|
|
|
if (attachment.size != null)
|
|
|
|
EntityLog.log(context, "Operation attachment size=" + attachment.size);
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
private static void onExists(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws MessagingException, IOException {
|
2021-10-26 07:39:31 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
boolean retry = jargs.optBoolean(0);
|
|
|
|
|
2019-09-17 12:22:18 +00:00
|
|
|
if (message.uid != null)
|
|
|
|
return;
|
|
|
|
|
2019-12-01 13:43:17 +00:00
|
|
|
if (message.msgid == null)
|
|
|
|
throw new IllegalArgumentException("exists without msgid");
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
// Search for message
|
2023-07-19 17:39:13 +00:00
|
|
|
// Alternative, inconsistent for Outlook: X-Microsoft-Original-Message-ID
|
|
|
|
Message[] imessages = ifolder.search(account.isOutlook()
|
|
|
|
? new HeaderTerm(MessageHelper.HEADER_CORRELATION_ID, message.msgid)
|
|
|
|
: new MessageIDTerm(message.msgid));
|
2023-08-27 05:36:19 +00:00
|
|
|
EntityLog.log(context, folder.name + " exists" +
|
2023-08-25 11:40:40 +00:00
|
|
|
" retry=" + retry +
|
|
|
|
" host=" + account.host +
|
|
|
|
" outlook=" + account.isOutlook() +
|
|
|
|
" messages=" + (imessages == null ? null : imessages.length));
|
2023-07-19 17:39:13 +00:00
|
|
|
|
2023-08-27 05:36:19 +00:00
|
|
|
if (account.isOutlook() && (imessages == null || imessages.length == 0)) {
|
|
|
|
EntityLog.log(context, folder.name + " exists alt" +
|
|
|
|
" retry=" + retry +
|
|
|
|
" host=" + account.host +
|
|
|
|
" outlook=" + account.isOutlook() +
|
|
|
|
" messages=" + (imessages == null ? null : imessages.length));
|
2023-08-27 06:40:24 +00:00
|
|
|
imessages = ifolder.search(
|
|
|
|
new HeaderTerm(MessageHelper.HEADER_MICROSOFT_ORIGINAL_MESSAGE_ID, message.msgid));
|
2023-08-27 05:36:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 17:39:13 +00:00
|
|
|
// Searching for random header:
|
|
|
|
// iCloud: NO [UNAVAILABLE] Unexpected exception
|
|
|
|
// Seznam: Jakarta Mail Exception: java.io.IOException: Connection dropped by server?
|
2020-11-26 17:07:36 +00:00
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
// Some email servers are slow with adding sent messages
|
2021-02-24 13:05:11 +00:00
|
|
|
if (retry)
|
|
|
|
Log.w(folder.name + " EXISTS retry" +
|
2021-02-22 19:11:34 +00:00
|
|
|
" found=" + (imessages == null ? null : imessages.length) +
|
2021-02-24 13:05:11 +00:00
|
|
|
" host=" + account.host);
|
|
|
|
else if (imessages == null || imessages.length == 0) {
|
2021-02-22 19:11:34 +00:00
|
|
|
long next = new Date().getTime() + EXISTS_RETRY_DELAY;
|
|
|
|
|
2021-05-10 12:52:39 +00:00
|
|
|
Intent intent = new Intent(context, ServiceSynchronize.class);
|
2021-02-22 19:11:34 +00:00
|
|
|
intent.setAction("exists:" + message.id);
|
2021-03-30 07:36:51 +00:00
|
|
|
PendingIntent piExists = PendingIntentCompat.getForegroundService(
|
2021-05-10 12:52:39 +00:00
|
|
|
context, ServiceSynchronize.PI_EXISTS, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2021-02-22 19:11:34 +00:00
|
|
|
|
2022-04-13 20:27:33 +00:00
|
|
|
AlarmManager am = Helper.getSystemService(context, AlarmManager.class);
|
2021-04-27 05:53:09 +00:00
|
|
|
AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, next, piExists);
|
2021-02-22 19:11:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-26 17:07:36 +00:00
|
|
|
if (imessages != null && imessages.length == 1) {
|
2020-12-23 17:09:38 +00:00
|
|
|
String msgid;
|
|
|
|
try {
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessages[0], context);
|
|
|
|
msgid = helper.getMessageID();
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
msgid = message.msgid;
|
|
|
|
}
|
|
|
|
if (Objects.equals(message.msgid, msgid)) {
|
2021-10-26 07:39:31 +00:00
|
|
|
db.folder().setFolderAutoAdd(folder.id, false);
|
2020-12-23 17:09:38 +00:00
|
|
|
long uid = ifolder.getUID(imessages[0]);
|
|
|
|
EntityOperation.queue(context, folder, EntityOperation.FETCH, uid);
|
|
|
|
} else {
|
2021-10-26 07:39:31 +00:00
|
|
|
db.folder().setFolderAutoAdd(folder.id, true);
|
2020-12-23 17:09:38 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.ADD);
|
|
|
|
}
|
2020-11-26 17:07:36 +00:00
|
|
|
} else {
|
2021-10-26 07:39:31 +00:00
|
|
|
db.folder().setFolderAutoAdd(folder.id, true);
|
2020-11-26 17:07:36 +00:00
|
|
|
if (imessages != null && imessages.length > 1)
|
2021-02-22 19:11:34 +00:00
|
|
|
Log.e(folder.name + " EXISTS messages=" + imessages.length + " retry=" + retry);
|
2021-10-26 06:27:13 +00:00
|
|
|
EntityLog.log(context, folder.name +
|
|
|
|
" EXISTS messages=" + (imessages == null ? null : imessages.length));
|
2020-11-26 17:07:36 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.ADD);
|
2019-09-17 12:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 17:39:39 +00:00
|
|
|
private static void onReport(Context context, JSONArray jargs, EntityFolder folder, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException {
|
|
|
|
String msgid = jargs.getString(0);
|
|
|
|
String keyword = jargs.getString(1);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(msgid))
|
|
|
|
throw new IllegalArgumentException("msgid missing");
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(keyword))
|
|
|
|
throw new IllegalArgumentException("keyword missing");
|
|
|
|
|
2021-12-29 21:04:04 +00:00
|
|
|
if (folder.read_only) {
|
|
|
|
Log.w(folder.name + " read-only");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-26 17:39:39 +00:00
|
|
|
|
2021-12-29 21:04:04 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
|
|
|
|
Log.w(folder.name + " has no keywords");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-26 17:39:39 +00:00
|
|
|
|
|
|
|
Message[] imessages = ifolder.search(new MessageIDTerm(msgid));
|
2022-01-05 19:28:04 +00:00
|
|
|
if (imessages == null || imessages.length == 0) {
|
|
|
|
Log.w(folder.name + " " + msgid + " not found");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-26 17:39:39 +00:00
|
|
|
|
|
|
|
for (Message imessage : imessages) {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
|
|
|
Log.i("Report uid=" + uid + " keyword=" + keyword);
|
|
|
|
|
|
|
|
Flags flags = new Flags(keyword);
|
|
|
|
imessage.setFlags(flags, true);
|
|
|
|
|
2021-12-27 16:46:58 +00:00
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
try {
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(uid);
|
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-12-26 17:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 06:44:50 +00:00
|
|
|
static void onSynchronizeFolders(
|
2021-12-07 07:30:51 +00:00
|
|
|
Context context, EntityAccount account, Store istore, State state,
|
|
|
|
boolean keep_alive, boolean force) throws MessagingException {
|
2023-10-02 05:54:22 +00:00
|
|
|
// Folder names: https://datatracker.ietf.org/doc/html/rfc2060#section-5.1.3
|
2019-02-27 15:05:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2021-12-07 07:30:51 +00:00
|
|
|
if (account.protocol != EntityAccount.TYPE_IMAP)
|
|
|
|
return;
|
|
|
|
|
2019-05-23 08:02:43 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-02-25 11:31:34 +00:00
|
|
|
boolean sync_folders = prefs.getBoolean("sync_folders", true);
|
2021-12-07 07:30:51 +00:00
|
|
|
boolean sync_folders_poll = prefs.getBoolean("sync_folders_poll", false);
|
2020-06-09 14:46:37 +00:00
|
|
|
boolean sync_shared_folders = prefs.getBoolean("sync_shared_folders", false);
|
2023-02-19 17:12:01 +00:00
|
|
|
boolean sync_added_folders = prefs.getBoolean("sync_added_folders", false);
|
2022-01-28 18:46:42 +00:00
|
|
|
Log.i(account.name + " sync folders=" + sync_folders +
|
|
|
|
" poll=" + sync_folders_poll +
|
|
|
|
" shared=" + sync_shared_folders +
|
2023-02-19 17:12:01 +00:00
|
|
|
" added=" + sync_added_folders +
|
2022-01-28 18:46:42 +00:00
|
|
|
" keep_alive=" + keep_alive +
|
|
|
|
" force=" + force);
|
2021-02-25 11:31:34 +00:00
|
|
|
|
2023-08-12 18:24:08 +00:00
|
|
|
// Fix folder poll setting
|
|
|
|
boolean fixed = prefs.getBoolean("fixed_poll." + account.id, false);
|
2023-08-14 10:39:06 +00:00
|
|
|
if (!fixed &&
|
|
|
|
account.created != null &&
|
|
|
|
account.created > 1691193600 * 1000L /* 2023-08-05 00:00 */ &&
|
|
|
|
account.created < 1692223200 * 1000L /* 2023-05-17 00:00 */)
|
2023-08-12 18:24:08 +00:00
|
|
|
try {
|
2023-08-13 13:25:18 +00:00
|
|
|
int count = 0;
|
2023-08-12 18:24:08 +00:00
|
|
|
EntityFolder inbox = db.folder().getFolderByType(account.id, EntityFolder.INBOX);
|
|
|
|
List<EntityFolder> children = db.folder().getChildFolders(inbox.id);
|
|
|
|
for (EntityFolder child : children)
|
2023-08-13 19:21:29 +00:00
|
|
|
if (!child.poll && EntityFolder.USER.equals(child.type)) {
|
2023-08-13 13:25:18 +00:00
|
|
|
count++;
|
2023-08-12 18:24:08 +00:00
|
|
|
db.folder().setFolderPoll(child.id, true);
|
|
|
|
EntityLog.log(context, "Fixed poll=" + child.name + ":" + child.type);
|
|
|
|
}
|
2023-08-13 13:25:18 +00:00
|
|
|
if (count > 0)
|
|
|
|
Log.e("Fixed poll count=" + count);
|
2023-08-12 18:24:08 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
} finally {
|
|
|
|
prefs.edit().putBoolean("fixed_poll." + account.id, true).apply();
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:56:02 +00:00
|
|
|
if (force)
|
2021-02-25 11:31:34 +00:00
|
|
|
sync_folders = true;
|
2021-12-29 19:56:02 +00:00
|
|
|
if (keep_alive)
|
|
|
|
sync_folders = sync_folders_poll;
|
2021-09-24 08:50:01 +00:00
|
|
|
if (!sync_folders)
|
|
|
|
sync_shared_folders = false;
|
2019-04-04 16:18:13 +00:00
|
|
|
|
2019-05-01 07:23:31 +00:00
|
|
|
// Get folder names
|
2020-12-17 14:05:44 +00:00
|
|
|
boolean drafts = false;
|
2023-02-19 17:12:01 +00:00
|
|
|
boolean user = false;
|
2019-06-25 14:34:30 +00:00
|
|
|
Map<String, EntityFolder> local = new HashMap<>();
|
2020-12-17 14:05:44 +00:00
|
|
|
List<EntityFolder> folders = db.folder().getFolders(account.id, false, false);
|
2021-11-12 18:38:59 +00:00
|
|
|
for (EntityFolder folder : folders) {
|
2023-02-19 17:12:01 +00:00
|
|
|
if (EntityFolder.USER.equals(folder.type))
|
|
|
|
user = true;
|
2019-05-01 07:23:31 +00:00
|
|
|
if (folder.tbc != null) {
|
2021-03-17 06:51:31 +00:00
|
|
|
try {
|
2022-03-21 19:22:55 +00:00
|
|
|
// Prefix folder with namespace
|
2022-03-22 13:22:24 +00:00
|
|
|
try {
|
|
|
|
Folder[] ns = istore.getPersonalNamespaces();
|
2022-12-11 06:50:40 +00:00
|
|
|
Folder[] sh = istore.getSharedNamespaces();
|
|
|
|
if (ns != null && ns.length == 1 &&
|
|
|
|
!(sync_shared_folders && sh != null && sh.length > 0)) {
|
2022-03-22 13:22:24 +00:00
|
|
|
String n = ns[0].getFullName();
|
|
|
|
// Typically "" or "INBOX"
|
|
|
|
if (!TextUtils.isEmpty(n)) {
|
|
|
|
n += ns[0].getSeparator();
|
|
|
|
if (!folder.name.startsWith(n)) {
|
|
|
|
folder.name = n + folder.name;
|
|
|
|
db.folder().updateFolder(folder);
|
|
|
|
}
|
2022-03-21 19:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 13:22:24 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
2022-03-21 19:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 09:59:19 +00:00
|
|
|
EntityLog.log(context, folder.name + " creating");
|
2021-03-17 06:51:31 +00:00
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
2023-02-17 08:18:38 +00:00
|
|
|
if (ifolder.exists())
|
|
|
|
EntityLog.log(context, folder.name + " already exists on server");
|
|
|
|
else
|
2022-03-21 19:22:55 +00:00
|
|
|
try {
|
|
|
|
((IMAPFolder) ifolder).doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
protocol.create(folder.name);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ifolder.setSubscribed(true);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
// com.sun.mail.iap.CommandFailedException:
|
|
|
|
// K5 NO Client tried to access nonexistent namespace.
|
|
|
|
// (Mailbox name should probably be prefixed with: INBOX.) (n.nnn + n.nnn secs).
|
|
|
|
// com.sun.mail.iap.CommandFailedException:
|
|
|
|
// AN5 NO [OVERQUOTA] Quota exceeded (number of mailboxes exceeded) (n.nnn + n.nnn + n.nnn secs).
|
|
|
|
Log.w(ex);
|
|
|
|
EntityLog.log(context, folder.name + " creation " +
|
|
|
|
ex + "\n" + android.util.Log.getStackTraceString(ex));
|
|
|
|
db.account().setAccountError(account.id, Log.formatThrowable(ex));
|
|
|
|
}
|
2021-03-17 06:51:31 +00:00
|
|
|
local.put(folder.name, folder);
|
|
|
|
} finally {
|
|
|
|
db.folder().resetFolderTbc(folder.id);
|
|
|
|
sync_folders = true;
|
2019-09-09 06:35:29 +00:00
|
|
|
}
|
2019-07-29 19:53:32 +00:00
|
|
|
|
|
|
|
} else if (folder.rename != null) {
|
2021-03-17 06:51:31 +00:00
|
|
|
try {
|
2021-12-24 09:59:19 +00:00
|
|
|
EntityLog.log(context, folder.name + " rename into " + folder.rename);
|
2021-03-17 06:51:31 +00:00
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (ifolder.exists()) {
|
|
|
|
// https://tools.ietf.org/html/rfc3501#section-6.3.9
|
|
|
|
boolean subscribed = ifolder.isSubscribed();
|
|
|
|
if (subscribed)
|
|
|
|
ifolder.setSubscribed(false);
|
|
|
|
|
|
|
|
Folder itarget = istore.getFolder(folder.rename);
|
|
|
|
ifolder.renameTo(itarget);
|
|
|
|
|
|
|
|
if (subscribed && folder.selectable)
|
|
|
|
try {
|
|
|
|
itarget.open(READ_WRITE);
|
|
|
|
itarget.setSubscribed(subscribed);
|
2022-09-23 16:12:42 +00:00
|
|
|
itarget.close(false);
|
2021-03-17 06:51:31 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-10-04 13:59:02 +00:00
|
|
|
|
2021-03-17 06:51:31 +00:00
|
|
|
db.folder().renameFolder(folder.account, folder.name, folder.rename);
|
|
|
|
folder.name = folder.rename;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.folder().resetFolderRename(folder.id);
|
|
|
|
sync_folders = true;
|
2019-07-29 19:53:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 07:23:31 +00:00
|
|
|
} else if (folder.tbd != null && folder.tbd) {
|
2021-03-17 06:51:31 +00:00
|
|
|
try {
|
2022-06-30 18:30:39 +00:00
|
|
|
EntityLog.log(context, folder.name + " deleting server");
|
2021-03-17 06:51:31 +00:00
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (ifolder.exists()) {
|
2023-02-17 08:18:38 +00:00
|
|
|
try {
|
|
|
|
ifolder.setSubscribed(false);
|
|
|
|
((IMAPFolder) ifolder).doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
protocol.delete(folder.name);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
EntityLog.log(context, folder.name + " deleting device");
|
|
|
|
db.folder().deleteFolder(folder.id);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
EntityLog.log(context, folder.name + " deletion " +
|
|
|
|
ex + "\n" + android.util.Log.getStackTraceString(ex));
|
|
|
|
db.account().setAccountError(account.id, Log.formatThrowable(ex));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
EntityLog.log(context, folder.name + " does not exist on server anymore");
|
2021-03-17 12:08:54 +00:00
|
|
|
} finally {
|
|
|
|
db.folder().resetFolderTbd(folder.id);
|
2021-03-17 06:51:31 +00:00
|
|
|
sync_folders = true;
|
2019-09-09 06:35:29 +00:00
|
|
|
}
|
2019-07-29 19:53:32 +00:00
|
|
|
|
2019-06-08 14:33:25 +00:00
|
|
|
} else {
|
2020-12-17 14:05:44 +00:00
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
drafts = true;
|
2020-12-18 07:02:07 +00:00
|
|
|
|
|
|
|
if (folder.local) {
|
|
|
|
if (!EntityFolder.DRAFTS.equals(folder.type)) {
|
|
|
|
List<Long> ids = db.message().getMessageByFolder(folder.id);
|
|
|
|
if (ids == null || ids.size() == 0)
|
|
|
|
db.folder().deleteFolder(folder.id);
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-17 14:05:44 +00:00
|
|
|
local.put(folder.name, folder);
|
2021-02-25 11:31:34 +00:00
|
|
|
if (folder.selectable && folder.synchronize && folder.initialize != 0)
|
2020-12-17 14:05:44 +00:00
|
|
|
sync_folders = true;
|
|
|
|
}
|
2019-06-08 14:33:25 +00:00
|
|
|
}
|
2021-11-12 18:38:59 +00:00
|
|
|
|
|
|
|
String key = "label.color." + folder.name;
|
|
|
|
if (folder.color == null)
|
|
|
|
prefs.edit().remove(key).apply();
|
|
|
|
else
|
|
|
|
prefs.edit().putInt(key, folder.color).apply();
|
|
|
|
}
|
2020-12-17 14:05:44 +00:00
|
|
|
Log.i("Local folder count=" + local.size() + " drafts=" + drafts);
|
|
|
|
|
|
|
|
if (!drafts) {
|
|
|
|
EntityFolder d = new EntityFolder();
|
|
|
|
d.account = account.id;
|
|
|
|
d.name = context.getString(R.string.title_folder_local_drafts);
|
|
|
|
d.type = EntityFolder.DRAFTS;
|
|
|
|
d.local = true;
|
|
|
|
d.setProperties();
|
|
|
|
d.synchronize = false;
|
|
|
|
d.download = false;
|
|
|
|
d.sync_days = Integer.MAX_VALUE;
|
|
|
|
d.keep_days = Integer.MAX_VALUE;
|
|
|
|
db.folder().insertFolder(d);
|
|
|
|
}
|
2019-04-04 16:18:13 +00:00
|
|
|
|
2019-06-08 14:33:25 +00:00
|
|
|
if (!sync_folders)
|
|
|
|
return;
|
|
|
|
|
2021-12-07 07:30:51 +00:00
|
|
|
EntityLog.log(context, "Start sync folders account=" + account.name);
|
2019-06-08 14:33:25 +00:00
|
|
|
|
2019-05-25 14:21:25 +00:00
|
|
|
// Get default folder
|
|
|
|
Folder defaultFolder = istore.getDefaultFolder();
|
|
|
|
|
|
|
|
// Get remote folders
|
2019-05-26 12:07:10 +00:00
|
|
|
long start = new Date().getTime();
|
2021-09-24 07:51:57 +00:00
|
|
|
List<Pair<Folder, Folder>> ifolders = new ArrayList<>();
|
2020-06-10 13:22:29 +00:00
|
|
|
List<String> subscription = new ArrayList<>();
|
2021-09-23 13:17:18 +00:00
|
|
|
|
2021-10-03 08:12:31 +00:00
|
|
|
boolean root = false;
|
2021-09-30 09:35:38 +00:00
|
|
|
List<Folder> personal = new ArrayList<>();
|
2020-06-10 13:22:29 +00:00
|
|
|
try {
|
2021-09-30 09:35:38 +00:00
|
|
|
Folder[] pnamespaces = istore.getPersonalNamespaces();
|
|
|
|
if (pnamespaces != null) {
|
|
|
|
personal.addAll(Arrays.asList(pnamespaces));
|
|
|
|
for (Folder p : pnamespaces)
|
2021-10-03 08:12:31 +00:00
|
|
|
if (defaultFolder.getFullName().equals(p.getFullName())) {
|
2021-09-30 09:35:38 +00:00
|
|
|
root = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 13:17:18 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
2021-10-03 08:12:31 +00:00
|
|
|
if (!root)
|
|
|
|
personal.add(defaultFolder);
|
|
|
|
|
2021-09-23 13:17:18 +00:00
|
|
|
for (Folder namespace : personal) {
|
|
|
|
EntityLog.log(context, "Personal namespace=" + namespace.getFullName());
|
|
|
|
|
2021-09-24 07:51:57 +00:00
|
|
|
String pattern = namespace.getFullName() + "*";
|
|
|
|
for (Folder ifolder : defaultFolder.list(pattern))
|
|
|
|
ifolders.add(new Pair<>(namespace, ifolder));
|
|
|
|
|
|
|
|
try {
|
|
|
|
Folder[] isubscribed = defaultFolder.listSubscribed(pattern);
|
|
|
|
for (Folder ifolder : isubscribed) {
|
|
|
|
String fullName = ifolder.getFullName();
|
|
|
|
if (TextUtils.isEmpty(fullName)) {
|
|
|
|
Log.w("Subscribed folder name empty namespace=" + defaultFolder.getFullName());
|
|
|
|
continue;
|
2021-09-23 13:17:18 +00:00
|
|
|
}
|
2021-09-24 07:51:57 +00:00
|
|
|
subscription.add(fullName);
|
2021-12-26 15:52:46 +00:00
|
|
|
Log.i("Subscribed " + fullName);
|
2021-09-24 07:51:57 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
2021-09-23 13:17:18 +00:00
|
|
|
/*
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: java.lang.NullPointerException: Folder name is null
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.IMAPFolder.<init>(SourceFile:372)
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.IMAPFolder.<init>(SourceFile:411)
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.IMAPStore.newIMAPFolder(SourceFile:1809)
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.DefaultFolder.listSubscribed(SourceFile:89)
|
|
|
|
*/
|
2021-09-24 07:51:57 +00:00
|
|
|
Log.e(account.name, ex);
|
|
|
|
}
|
2020-06-10 13:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 14:46:37 +00:00
|
|
|
if (sync_shared_folders) {
|
2020-06-11 19:11:11 +00:00
|
|
|
// https://tools.ietf.org/html/rfc2342
|
2021-09-23 13:17:18 +00:00
|
|
|
Folder[] shared = istore.getSharedNamespaces();
|
|
|
|
EntityLog.log(context, "Shared namespaces=" + shared.length);
|
2021-09-24 07:51:57 +00:00
|
|
|
|
2021-09-23 13:17:18 +00:00
|
|
|
for (Folder namespace : shared) {
|
|
|
|
EntityLog.log(context, "Shared namespace=" + namespace.getFullName());
|
2020-06-10 13:22:29 +00:00
|
|
|
|
2021-09-24 07:51:57 +00:00
|
|
|
String pattern = namespace.getFullName() + "*";
|
|
|
|
try {
|
|
|
|
for (Folder ifolder : defaultFolder.list(pattern))
|
|
|
|
ifolders.add(new Pair<>(namespace, ifolder));
|
|
|
|
} catch (FolderNotFoundException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Folder[] isubscribed = namespace.listSubscribed(pattern);
|
|
|
|
for (Folder ifolder : isubscribed) {
|
|
|
|
String fullName = ifolder.getFullName();
|
|
|
|
if (TextUtils.isEmpty(fullName)) {
|
|
|
|
Log.e("Subscribed folder name empty namespace=" + namespace.getFullName());
|
|
|
|
continue;
|
2020-06-10 18:12:23 +00:00
|
|
|
}
|
2021-09-24 07:51:57 +00:00
|
|
|
subscription.add(fullName);
|
|
|
|
Log.i("Subscribed " + namespace.getFullName() + ":" + fullName);
|
2020-06-10 13:22:29 +00:00
|
|
|
}
|
2021-09-24 07:51:57 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(account.name, ex);
|
|
|
|
}
|
2020-06-09 14:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 12:07:10 +00:00
|
|
|
|
|
|
|
long duration = new Date().getTime() - start;
|
2019-05-25 14:21:25 +00:00
|
|
|
|
2020-06-09 14:46:37 +00:00
|
|
|
Log.i("Remote folder count=" + ifolders.size() +
|
2020-06-10 18:12:23 +00:00
|
|
|
" subscriptions=" + subscription.size() +
|
2019-05-26 12:07:10 +00:00
|
|
|
" fetched in " + duration + " ms");
|
2019-05-25 14:21:25 +00:00
|
|
|
|
2021-10-02 14:32:13 +00:00
|
|
|
if (ifolders.size() == 0) {
|
2021-10-03 05:27:57 +00:00
|
|
|
List<String> ns = new ArrayList<>();
|
2021-10-03 05:29:10 +00:00
|
|
|
for (Folder namespace : personal)
|
|
|
|
ns.add("'" + namespace.getFullName() + "'");
|
2021-10-03 05:27:57 +00:00
|
|
|
Log.e(account.host + " no folders listed" +
|
|
|
|
" namespaces=" + TextUtils.join(",", ns));
|
2021-10-02 14:32:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-19 06:09:08 +00:00
|
|
|
// Check if system folders were renamed
|
2021-08-24 11:30:14 +00:00
|
|
|
try {
|
2021-09-24 07:51:57 +00:00
|
|
|
for (Pair<Folder, Folder> ifolder : ifolders) {
|
|
|
|
String fullName = ifolder.second.getFullName();
|
2021-08-24 11:30:14 +00:00
|
|
|
if (TextUtils.isEmpty(fullName))
|
|
|
|
continue;
|
2021-08-19 06:09:08 +00:00
|
|
|
|
2021-09-24 07:51:57 +00:00
|
|
|
String[] attrs = ((IMAPFolder) ifolder.second).getAttributes();
|
2021-08-24 11:30:14 +00:00
|
|
|
String type = EntityFolder.getType(attrs, fullName, false);
|
|
|
|
if (type != null &&
|
|
|
|
!EntityFolder.USER.equals(type) &&
|
|
|
|
!EntityFolder.SYSTEM.equals(type)) {
|
2021-10-03 13:31:57 +00:00
|
|
|
|
|
|
|
// Rename system folders
|
2023-10-12 18:09:35 +00:00
|
|
|
for (EntityFolder folder : new ArrayList<>(local.values()))
|
|
|
|
if (type.equals(folder.type) &&
|
|
|
|
!fullName.equals(folder.name) &&
|
|
|
|
!local.containsKey(fullName) &&
|
|
|
|
!istore.getFolder(folder.name).exists()) {
|
|
|
|
Log.e(account.host +
|
|
|
|
" renaming " + type + " folder" +
|
|
|
|
" from " + folder.name + " to " + fullName);
|
|
|
|
local.remove(folder.name);
|
|
|
|
local.put(fullName, folder);
|
|
|
|
folder.name = fullName;
|
|
|
|
db.folder().setFolderName(folder.id, fullName);
|
|
|
|
}
|
2021-08-21 08:51:19 +00:00
|
|
|
|
2021-09-30 10:03:17 +00:00
|
|
|
// Reselect system folders once
|
2022-11-11 08:30:26 +00:00
|
|
|
String key = "unset." + account.id + "." + type;
|
|
|
|
boolean unset = prefs.getBoolean(key, false);
|
|
|
|
if (!unset) {
|
2021-09-30 10:03:17 +00:00
|
|
|
EntityFolder folder = db.folder().getFolderByType(account.id, type);
|
|
|
|
if (folder == null) {
|
|
|
|
folder = db.folder().getFolderByName(account.id, fullName);
|
2021-10-04 17:45:05 +00:00
|
|
|
if (folder != null && !folder.local) {
|
2022-11-11 08:30:26 +00:00
|
|
|
Log.e("Reselected " + account.host + " " + type + "=" + fullName);
|
2021-09-30 10:03:17 +00:00
|
|
|
folder.type = type;
|
2021-10-03 13:31:57 +00:00
|
|
|
folder.setProperties();
|
2021-10-03 14:55:16 +00:00
|
|
|
folder.setSpecials(account);
|
|
|
|
db.folder().updateFolder(folder);
|
2022-11-10 14:51:39 +00:00
|
|
|
|
|
|
|
if (EntityFolder.TRASH.equals(folder.type) &&
|
|
|
|
account.swipe_left != null && account.swipe_left > 0) {
|
|
|
|
EntityFolder swipe = db.folder().getFolder(account.swipe_left);
|
|
|
|
if (swipe == null) {
|
2022-11-11 08:30:26 +00:00
|
|
|
Log.e("Reselected " + account.host + " swipe left");
|
2022-11-10 14:51:39 +00:00
|
|
|
account.swipe_left = folder.id;
|
|
|
|
db.account().setAccountSwipes(account.id,
|
|
|
|
account.swipe_left, account.swipe_right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EntityFolder.ARCHIVE.equals(folder.type) &&
|
|
|
|
account.swipe_right != null && account.swipe_right > 0) {
|
|
|
|
EntityFolder swipe = db.folder().getFolder(account.swipe_right);
|
|
|
|
if (swipe == null) {
|
2022-11-11 08:30:26 +00:00
|
|
|
Log.e("Reselected " + account.host + " swipe right");
|
2022-11-10 14:51:39 +00:00
|
|
|
account.swipe_right = folder.id;
|
|
|
|
db.account().setAccountSwipes(account.id,
|
|
|
|
account.swipe_left, account.swipe_right);
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 08:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-19 06:09:08 +00:00
|
|
|
}
|
2021-08-24 11:30:14 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2021-08-19 06:09:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 12:59:21 +00:00
|
|
|
Map<String, EntityFolder> nameFolder = new HashMap<>();
|
|
|
|
Map<String, List<EntityFolder>> parentFolders = new HashMap<>();
|
2021-09-24 07:51:57 +00:00
|
|
|
for (Pair<Folder, Folder> ifolder : ifolders) {
|
|
|
|
String fullName = ifolder.second.getFullName();
|
2020-06-21 07:52:26 +00:00
|
|
|
if (TextUtils.isEmpty(fullName)) {
|
|
|
|
Log.e("Folder name empty");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-24 07:51:57 +00:00
|
|
|
String[] attrs = ((IMAPFolder) ifolder.second).getAttributes();
|
2020-02-24 08:32:03 +00:00
|
|
|
String type = EntityFolder.getType(attrs, fullName, false);
|
2023-01-18 19:54:42 +00:00
|
|
|
String subtype = EntityFolder.getSubtype(attrs, fullName);
|
2020-03-31 16:58:29 +00:00
|
|
|
boolean subscribed = subscription.contains(fullName);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-02-24 08:32:03 +00:00
|
|
|
boolean selectable = true;
|
|
|
|
boolean inferiors = true;
|
|
|
|
for (String attr : attrs) {
|
|
|
|
if (attr.equalsIgnoreCase("\\NoSelect"))
|
|
|
|
selectable = false;
|
|
|
|
if (attr.equalsIgnoreCase("\\NoInferiors"))
|
|
|
|
inferiors = false;
|
|
|
|
}
|
2021-09-24 07:51:57 +00:00
|
|
|
selectable = selectable && ((ifolder.second.getType() & IMAPFolder.HOLDS_MESSAGES) != 0);
|
|
|
|
inferiors = inferiors && ((ifolder.second.getType() & IMAPFolder.HOLDS_FOLDERS) != 0);
|
2020-02-24 08:32:03 +00:00
|
|
|
|
2021-03-05 17:13:16 +00:00
|
|
|
if (EntityFolder.INBOX.equals(type))
|
|
|
|
selectable = true;
|
|
|
|
|
2023-01-18 19:54:42 +00:00
|
|
|
Log.i(account.name + ":" + fullName + " type=" + type + ":" + subtype +
|
2020-02-24 08:32:03 +00:00
|
|
|
" subscribed=" + subscribed +
|
|
|
|
" selectable=" + selectable +
|
|
|
|
" inferiors=" + inferiors +
|
|
|
|
" attrs=" + TextUtils.join(" ", attrs));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-22 12:59:21 +00:00
|
|
|
if (type != null) {
|
2019-06-25 14:34:30 +00:00
|
|
|
local.remove(fullName);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-03-31 16:57:06 +00:00
|
|
|
EntityFolder folder;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-05-22 12:59:21 +00:00
|
|
|
|
2020-03-31 16:57:06 +00:00
|
|
|
folder = db.folder().getFolderByName(account.id, fullName);
|
|
|
|
if (folder == null) {
|
2021-02-20 07:17:32 +00:00
|
|
|
EntityFolder parent = null;
|
2021-09-24 07:51:57 +00:00
|
|
|
char separator = ifolder.first.getSeparator();
|
2021-02-22 14:51:02 +00:00
|
|
|
int sep = fullName.lastIndexOf(separator);
|
2021-02-20 13:29:24 +00:00
|
|
|
if (sep > 0)
|
2021-02-20 07:17:32 +00:00
|
|
|
parent = db.folder().getFolderByName(account.id, fullName.substring(0, sep));
|
|
|
|
|
2022-05-11 06:30:18 +00:00
|
|
|
if (!EntityFolder.USER.equals(type) && !EntityFolder.SYSTEM.equals(type)) {
|
|
|
|
EntityFolder has = db.folder().getFolderByType(account.id, type);
|
|
|
|
if (has != null)
|
|
|
|
type = EntityFolder.USER;
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
folder = new EntityFolder();
|
|
|
|
folder.account = account.id;
|
2021-09-24 07:51:57 +00:00
|
|
|
folder.namespace = ifolder.first.getFullName();
|
|
|
|
folder.separator = separator;
|
2019-05-23 16:13:03 +00:00
|
|
|
folder.name = fullName;
|
2022-05-11 06:30:18 +00:00
|
|
|
folder.type = type;
|
2023-01-18 19:54:42 +00:00
|
|
|
folder.subtype = type;
|
2019-05-23 16:13:03 +00:00
|
|
|
folder.subscribed = subscribed;
|
2019-07-07 06:17:27 +00:00
|
|
|
folder.selectable = selectable;
|
2020-02-24 08:32:03 +00:00
|
|
|
folder.inferiors = inferiors;
|
2021-02-20 07:17:32 +00:00
|
|
|
folder.setProperties();
|
2020-12-01 20:07:13 +00:00
|
|
|
folder.setSpecials(account);
|
2021-02-20 07:17:32 +00:00
|
|
|
|
2022-12-06 09:29:47 +00:00
|
|
|
if (selectable)
|
|
|
|
folder.inheritFrom(parent);
|
2023-02-19 17:12:01 +00:00
|
|
|
if (user && sync_added_folders && EntityFolder.USER.equals(type))
|
|
|
|
folder.synchronize = true;
|
2021-02-20 07:17:32 +00:00
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
folder.id = db.folder().insertFolder(folder);
|
2021-02-19 18:07:42 +00:00
|
|
|
Log.i(folder.name + " added type=" + folder.type + " sync=" + folder.synchronize);
|
|
|
|
if (folder.synchronize)
|
|
|
|
EntityOperation.sync(context, folder.id, false);
|
2020-03-31 16:57:06 +00:00
|
|
|
} else {
|
2019-05-23 16:13:03 +00:00
|
|
|
Log.i(folder.name + " exists type=" + folder.type);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-09-24 07:51:57 +00:00
|
|
|
folder.namespace = ifolder.first.getFullName();
|
|
|
|
folder.separator = ifolder.first.getSeparator();
|
|
|
|
db.folder().setFolderNamespace(folder.id, folder.namespace, folder.separator);
|
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
if (folder.subscribed == null || !folder.subscribed.equals(subscribed))
|
|
|
|
db.folder().setFolderSubscribed(folder.id, subscribed);
|
2019-04-25 15:23:56 +00:00
|
|
|
|
2019-09-18 06:56:33 +00:00
|
|
|
if (folder.selectable != selectable)
|
|
|
|
db.folder().setFolderSelectable(folder.id, selectable);
|
|
|
|
|
2020-02-24 08:32:03 +00:00
|
|
|
if (folder.inferiors != inferiors)
|
|
|
|
db.folder().setFolderInferiors(folder.id, inferiors);
|
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
// Compatibility
|
2019-05-28 11:42:19 +00:00
|
|
|
if (EntityFolder.USER.equals(folder.type) && EntityFolder.SYSTEM.equals(type))
|
2019-05-23 16:13:03 +00:00
|
|
|
db.folder().setFolderType(folder.id, type);
|
|
|
|
else if (EntityFolder.SYSTEM.equals(folder.type) && EntityFolder.USER.equals(type))
|
|
|
|
db.folder().setFolderType(folder.id, type);
|
|
|
|
else if (EntityFolder.INBOX.equals(type) && !EntityFolder.INBOX.equals(folder.type)) {
|
|
|
|
if (db.folder().getFolderByType(folder.account, EntityFolder.INBOX) == null)
|
2019-03-03 16:48:39 +00:00
|
|
|
db.folder().setFolderType(folder.id, type);
|
2019-05-23 16:13:03 +00:00
|
|
|
}
|
2023-01-18 19:54:42 +00:00
|
|
|
db.folder().setFolderSubtype(folder.id, subtype);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-08-17 06:48:14 +00:00
|
|
|
|
2020-03-31 16:57:06 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
Log.i("End sync folder");
|
2019-03-17 19:32:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
nameFolder.put(folder.name, folder);
|
2021-09-24 07:51:57 +00:00
|
|
|
String parentName = folder.getParentName();
|
2019-05-23 16:13:03 +00:00
|
|
|
if (!parentFolders.containsKey(parentName))
|
2022-08-07 13:27:17 +00:00
|
|
|
parentFolders.put(parentName, new ArrayList<>());
|
2019-05-23 16:13:03 +00:00
|
|
|
parentFolders.get(parentName).add(folder);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-05-22 12:59:21 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-10-03 08:03:01 +00:00
|
|
|
Log.i("Creating folders parents=" + parentFolders.size());
|
2019-05-22 12:59:21 +00:00
|
|
|
for (String parentName : parentFolders.keySet()) {
|
|
|
|
EntityFolder parent = nameFolder.get(parentName);
|
2019-10-02 20:50:25 +00:00
|
|
|
if (parent == null && parentName != null) {
|
2019-10-03 08:03:01 +00:00
|
|
|
parent = db.folder().getFolderByName(account.id, parentName);
|
|
|
|
if (parent == null) {
|
2020-03-26 07:39:49 +00:00
|
|
|
Log.i("Creating parent name=" + parentName);
|
2019-10-03 08:03:01 +00:00
|
|
|
parent = new EntityFolder();
|
|
|
|
parent.account = account.id;
|
|
|
|
parent.name = parentName;
|
|
|
|
parent.type = EntityFolder.SYSTEM;
|
|
|
|
parent.subscribed = false;
|
|
|
|
parent.selectable = false;
|
2020-03-26 07:39:49 +00:00
|
|
|
parent.inferiors = false;
|
|
|
|
parent.setProperties();
|
|
|
|
parent.display = parentName + "*";
|
2019-10-03 08:03:01 +00:00
|
|
|
parent.id = db.folder().insertFolder(parent);
|
|
|
|
}
|
2019-10-02 20:50:25 +00:00
|
|
|
nameFolder.put(parentName, parent);
|
|
|
|
}
|
2019-10-03 08:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("Updating folders parents=" + parentFolders.size());
|
|
|
|
for (String parentName : parentFolders.keySet()) {
|
|
|
|
EntityFolder parent = nameFolder.get(parentName);
|
2022-08-07 13:27:17 +00:00
|
|
|
for (EntityFolder child : parentFolders.get(parentName)) {
|
|
|
|
String rootType = null;
|
|
|
|
EntityFolder r = parent;
|
|
|
|
while (r != null) {
|
|
|
|
rootType = r.type;
|
2022-08-11 05:11:29 +00:00
|
|
|
if (!EntityFolder.USER.equals(r.type) && !EntityFolder.SYSTEM.equals(r.type))
|
|
|
|
break;
|
2022-08-07 13:27:17 +00:00
|
|
|
r = nameFolder.get(r.getParentName());
|
|
|
|
}
|
2022-08-11 05:11:29 +00:00
|
|
|
if (EntityFolder.USER.equals(rootType) || EntityFolder.SYSTEM.equals(rootType))
|
|
|
|
rootType = null;
|
2022-08-07 13:27:17 +00:00
|
|
|
db.folder().setFolderInheritedType(child.id, rootType);
|
2019-05-22 12:59:21 +00:00
|
|
|
db.folder().setFolderParent(child.id, parent == null ? null : parent.id);
|
2022-08-07 13:27:17 +00:00
|
|
|
}
|
2019-05-22 12:59:21 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-25 14:34:30 +00:00
|
|
|
Log.i("Delete local count=" + local.size());
|
|
|
|
for (String name : local.keySet()) {
|
|
|
|
EntityFolder folder = local.get(name);
|
2021-10-03 05:40:36 +00:00
|
|
|
if (EntityFolder.INBOX.equals(folder.type)) {
|
2023-09-13 05:40:53 +00:00
|
|
|
Log.w(account.host + " keep inbox");
|
2021-10-03 05:31:39 +00:00
|
|
|
continue;
|
2021-10-03 05:40:36 +00:00
|
|
|
}
|
2020-10-20 18:27:35 +00:00
|
|
|
List<EntityFolder> childs = parentFolders.get(name);
|
|
|
|
if (EntityFolder.USER.equals(folder.type) ||
|
|
|
|
childs == null || childs.size() == 0) {
|
2021-12-24 09:59:19 +00:00
|
|
|
EntityLog.log(context, name + " delete");
|
2019-06-25 14:34:30 +00:00
|
|
|
db.folder().deleteFolder(account.id, name);
|
2022-06-30 18:30:39 +00:00
|
|
|
EntityLog.log(context, name + " deleted");
|
2019-06-25 14:34:30 +00:00
|
|
|
} else
|
2021-12-24 09:59:19 +00:00
|
|
|
Log.w(name + " keep type=" + folder.type);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 16:47:52 +00:00
|
|
|
private static void onSubscribeFolder(Context context, JSONArray jargs, EntityFolder folder, IMAPFolder ifolder)
|
|
|
|
throws JSONException, MessagingException {
|
|
|
|
boolean subscribe = jargs.getBoolean(0);
|
|
|
|
ifolder.setSubscribed(subscribe);
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.folder().setFolderSubscribed(folder.id, subscribe);
|
|
|
|
|
|
|
|
Log.i(folder.name + " subscribed=" + subscribe);
|
|
|
|
}
|
|
|
|
|
2021-09-06 17:12:46 +00:00
|
|
|
private static void onPurgeFolder(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, IMAPFolder ifolder) throws MessagingException {
|
2020-07-26 17:58:13 +00:00
|
|
|
// Delete all messages from folder
|
|
|
|
try {
|
2021-05-14 17:03:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
List<Long> busy = db.message().getBusyUids(folder.id, new Date().getTime());
|
|
|
|
|
|
|
|
Message[] imessages = ifolder.getMessages();
|
|
|
|
Log.i(folder.name + " purge=" + imessages.length + " busy=" + busy.size());
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
List<Message> idelete = new ArrayList<>();
|
2021-10-25 15:34:34 +00:00
|
|
|
for (Message imessage : imessages)
|
|
|
|
try {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
|
|
|
if (!busy.contains(uid))
|
|
|
|
idelete.add(imessage);
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-05-14 17:03:15 +00:00
|
|
|
|
|
|
|
EntityLog.log(context, folder.name + " purging=" + idelete.size() + "/" + imessages.length);
|
2021-09-06 17:12:46 +00:00
|
|
|
if (account.isYahooJp()) {
|
2022-09-03 10:05:44 +00:00
|
|
|
for (Message imessage : new ArrayList<>(idelete))
|
|
|
|
try {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessagingException mex) {
|
|
|
|
Log.w(mex);
|
|
|
|
idelete.remove(imessage);
|
|
|
|
}
|
2021-10-25 17:38:54 +00:00
|
|
|
} else {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2022-01-18 12:21:49 +00:00
|
|
|
int chunk_size = prefs.getInt("chunk_size", DEFAULT_CHUNK_SIZE);
|
2021-10-25 17:38:54 +00:00
|
|
|
|
|
|
|
Flags flags = new Flags(Flags.Flag.DELETED);
|
2022-11-05 08:28:12 +00:00
|
|
|
List<Message> iremove = new ArrayList<>();
|
2021-10-25 17:38:54 +00:00
|
|
|
for (List<Message> list : Helper.chunkList(idelete, chunk_size))
|
2022-09-03 10:05:44 +00:00
|
|
|
try {
|
|
|
|
ifolder.setFlags(list.toArray(new Message[0]), flags, true);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
2022-11-05 08:28:12 +00:00
|
|
|
for (Message imessage : list)
|
2022-09-03 10:05:44 +00:00
|
|
|
try {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessagingException mex) {
|
|
|
|
Log.w(mex);
|
2022-11-05 08:28:12 +00:00
|
|
|
iremove.add(imessage);
|
2022-09-03 10:05:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-05 08:28:12 +00:00
|
|
|
|
|
|
|
for (Message imessage : iremove)
|
|
|
|
idelete.remove(imessage);
|
2021-10-25 17:38:54 +00:00
|
|
|
}
|
2020-07-26 17:58:13 +00:00
|
|
|
Log.i(folder.name + " purge deleted");
|
2021-10-24 12:39:49 +00:00
|
|
|
expunge(context, ifolder, idelete);
|
2020-07-26 19:35:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
throw ex;
|
2020-07-26 17:58:13 +00:00
|
|
|
} finally {
|
2020-10-22 09:09:10 +00:00
|
|
|
EntityOperation.sync(context, folder.id, false);
|
2020-07-26 17:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:51:34 +00:00
|
|
|
private static void onExpungeFolder(Context context, JSONArray jargs, EntityFolder folder, IMAPFolder ifolder) throws MessagingException {
|
|
|
|
Log.i(folder.name + " expunge");
|
2022-03-26 12:16:53 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean uid_expunge = prefs.getBoolean("uid_expunge", false);
|
|
|
|
|
|
|
|
if (uid_expunge)
|
|
|
|
uid_expunge = MessageHelper.hasCapability(ifolder, "UIDPLUS");
|
|
|
|
|
|
|
|
if (uid_expunge) {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
List<Long> uids = db.message().getDeletedUids(folder.id);
|
|
|
|
if (uids == null || uids.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Log.i(ifolder.getName() + " expunging " + TextUtils.join(",", uids));
|
2022-03-26 14:22:49 +00:00
|
|
|
uidExpunge(context, ifolder, uids);
|
2022-03-26 12:16:53 +00:00
|
|
|
Log.i(ifolder.getName() + " expunged " + TextUtils.join(",", uids));
|
|
|
|
} else
|
|
|
|
ifolder.expunge();
|
2021-02-12 11:51:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 14:54:03 +00:00
|
|
|
private static void onPurgeFolder(Context context, EntityFolder folder) {
|
2020-10-22 09:09:10 +00:00
|
|
|
// POP3
|
2022-12-09 18:45:23 +00:00
|
|
|
int count = 0;
|
|
|
|
int purged = 0;
|
|
|
|
do {
|
|
|
|
if (count > 0) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(YIELD_DURATION);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 17:58:13 +00:00
|
|
|
|
2022-12-09 18:45:23 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
count = db.message().deleteHiddenMessages(folder.id, 100);
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-07-26 17:58:13 +00:00
|
|
|
|
2022-12-09 18:45:23 +00:00
|
|
|
purged += count;
|
|
|
|
Log.i(folder.name + " purge count=" + count + "/" + purged);
|
|
|
|
} while (count > 0);
|
2020-07-26 17:58:13 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 16:45:50 +00:00
|
|
|
private static void onRule(Context context, JSONArray jargs, EntityMessage message) throws JSONException, MessagingException, IOException {
|
2022-12-24 09:10:21 +00:00
|
|
|
// Deferred rule (download headers, body, etc)
|
2020-07-13 14:34:55 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2023-04-01 18:15:46 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2020-07-13 14:34:55 +00:00
|
|
|
|
2023-04-01 18:15:46 +00:00
|
|
|
long id = jargs.getLong(0);
|
|
|
|
if (id < 0) {
|
2023-06-04 13:46:56 +00:00
|
|
|
EntityLog.log(context, "Executing deferred daily rules for message=" + message.id);
|
2023-04-01 18:15:46 +00:00
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(message.folder, true);
|
|
|
|
EntityRule.run(context, rules, message, null, null);
|
|
|
|
} else {
|
|
|
|
EntityRule rule = db.rule().getRule(id);
|
|
|
|
if (rule == null)
|
|
|
|
throw new IllegalArgumentException("Rule not found id=" + id);
|
2020-07-13 14:34:55 +00:00
|
|
|
|
2023-04-01 18:15:46 +00:00
|
|
|
if (!message.content)
|
|
|
|
throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name);
|
|
|
|
|
2023-08-14 08:58:36 +00:00
|
|
|
rule.execute(context, message, null);
|
2023-04-01 18:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2022-12-24 09:10:21 +00:00
|
|
|
}
|
2020-07-13 14:34:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 19:47:46 +00:00
|
|
|
private static void onDownload(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException, JSONException {
|
|
|
|
long uid = jargs.getLong(0);
|
|
|
|
if (!Objects.equals(uid, message.uid))
|
|
|
|
throw new IllegalArgumentException("Different uid" + uid + "/" + message.uid);
|
|
|
|
|
|
|
|
MimeMessage imessage = (MimeMessage) ifolder.getMessageByUID(uid);
|
|
|
|
downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state, new SyncStats());
|
|
|
|
}
|
|
|
|
|
2023-07-22 05:30:09 +00:00
|
|
|
private static void onSubject(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
String subject = (jargs.isNull(0) ? null : jargs.getString(0));
|
|
|
|
|
|
|
|
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
String msgid = EntityMessage.generateMessageId();
|
|
|
|
String ref = (TextUtils.isEmpty(message.references)
|
|
|
|
? message.msgid
|
|
|
|
: message.references + " " + message.msgid);
|
|
|
|
MimeMessage icopy = new MimeMessageEx((MimeMessage) imessage, msgid);
|
|
|
|
icopy.setSubject(subject); // Update or delete subject
|
|
|
|
icopy.addHeader("References", MessageHelper.limitReferences(ref));
|
|
|
|
|
|
|
|
ifolder.appendMessages(new Message[]{icopy});
|
|
|
|
|
|
|
|
Long uid = findUid(context, account, ifolder, msgid);
|
|
|
|
if (uid != null) {
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(uid);
|
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trash == null) {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
expunge(context, ifolder, Arrays.asList(imessage));
|
2023-07-23 05:35:28 +00:00
|
|
|
} else
|
2023-07-22 05:30:09 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, trash.id);
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
private static void onSynchronizeMessages(
|
|
|
|
Context context, JSONArray jargs,
|
|
|
|
EntityAccount account, final EntityFolder folder,
|
2021-07-27 19:04:48 +00:00
|
|
|
POP3Folder ifolder, POP3Store istore, State state) throws MessagingException, IOException {
|
2019-09-19 15:41:26 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2020-06-07 07:59:25 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-08-06 11:36:52 +00:00
|
|
|
boolean sync_quick_pop = prefs.getBoolean("sync_quick_pop", true);
|
2020-06-07 07:59:25 +00:00
|
|
|
boolean notify_known = prefs.getBoolean("notify_known", false);
|
2023-02-14 08:36:35 +00:00
|
|
|
boolean native_dkim = prefs.getBoolean("native_dkim", false);
|
2021-09-05 20:25:26 +00:00
|
|
|
boolean download_eml = prefs.getBoolean("download_eml", false);
|
2022-02-14 19:18:03 +00:00
|
|
|
boolean download_plain = prefs.getBoolean("download_plain", false);
|
2022-05-04 15:38:59 +00:00
|
|
|
boolean check_blocklist = prefs.getBoolean("check_blocklist", false);
|
|
|
|
boolean use_blocklist_pop = prefs.getBoolean("use_blocklist_pop", false);
|
2020-06-07 14:32:14 +00:00
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-09-19 19:42:09 +00:00
|
|
|
|
2021-07-29 06:43:33 +00:00
|
|
|
boolean force = jargs.optBoolean(5, false);
|
|
|
|
|
|
|
|
EntityLog.log(context, account.name + " POP sync type=" + folder.type +
|
2021-08-06 11:36:52 +00:00
|
|
|
" quick=" + sync_quick_pop + " force=" + force +
|
2021-07-29 06:43:33 +00:00
|
|
|
" connected=" + (ifolder != null));
|
2019-09-20 07:45:36 +00:00
|
|
|
|
2019-09-19 19:42:09 +00:00
|
|
|
if (!EntityFolder.INBOX.equals(folder.type)) {
|
2021-10-25 05:26:47 +00:00
|
|
|
folder.synchronize = false;
|
|
|
|
db.folder().setFolderSynchronize(folder.id, folder.synchronize);
|
2019-09-19 19:42:09 +00:00
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-24 09:10:21 +00:00
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id, false);
|
2020-07-01 06:57:17 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
try {
|
|
|
|
db.folder().setFolderSyncState(folder.id, "syncing");
|
|
|
|
|
2021-06-25 14:53:50 +00:00
|
|
|
// Get capabilities
|
2019-09-24 17:33:20 +00:00
|
|
|
Map<String, String> caps = istore.capabilities();
|
2022-11-24 10:55:54 +00:00
|
|
|
boolean hasUidl = caps.containsKey("UIDL");
|
2022-11-24 11:00:43 +00:00
|
|
|
EntityLog.log(context, account.name +
|
|
|
|
" POP capabilities= " + caps.keySet() +
|
|
|
|
" uidl=" + account.capability_uidl);
|
|
|
|
|
|
|
|
if (hasUidl) {
|
|
|
|
if (Boolean.FALSE.equals(account.capability_uidl)) {
|
|
|
|
hasUidl = false;
|
|
|
|
Log.w(account.host + " did not had UIDL before");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
account.capability_uidl = false;
|
|
|
|
db.account().setAccountUidl(account.id, account.capability_uidl);
|
|
|
|
}
|
2019-09-24 17:33:20 +00:00
|
|
|
|
2021-06-25 14:53:50 +00:00
|
|
|
// Get messages
|
2019-09-19 15:41:26 +00:00
|
|
|
Message[] imessages = ifolder.getMessages();
|
2023-02-14 20:48:49 +00:00
|
|
|
|
2021-06-25 13:55:16 +00:00
|
|
|
List<TupleUidl> ids = db.message().getUidls(folder.id);
|
2021-06-25 18:02:13 +00:00
|
|
|
int max = (account.max_messages == null
|
|
|
|
? imessages.length
|
2023-02-15 16:37:15 +00:00
|
|
|
: Math.min(imessages.length, Math.abs(account.max_messages)));
|
|
|
|
boolean reversed = (account.max_messages != null && account.max_messages < 0);
|
2021-06-25 13:55:16 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
boolean sync = true;
|
2023-02-15 08:14:10 +00:00
|
|
|
if (!hasUidl && sync_quick_pop && !force &&
|
2021-07-29 06:43:33 +00:00
|
|
|
imessages.length > 0 && folder.last_sync_count != null &&
|
2021-07-23 20:31:23 +00:00
|
|
|
imessages.length == folder.last_sync_count) {
|
|
|
|
// Check if last message known as new messages indicator
|
2023-04-04 17:15:59 +00:00
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessages[reversed ? 0 : imessages.length - 1], context);
|
2022-11-23 18:44:24 +00:00
|
|
|
String msgid = helper.getPOP3MessageID();
|
2021-07-23 20:31:23 +00:00
|
|
|
if (msgid != null) {
|
2022-10-11 06:41:44 +00:00
|
|
|
int count = db.message().countMessageByMsgId(folder.id, msgid, true);
|
2021-07-23 20:31:23 +00:00
|
|
|
if (count == 1) {
|
|
|
|
Log.i(account.name + " POP having last msgid=" + msgid);
|
|
|
|
sync = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:53:47 +00:00
|
|
|
// Index IDs
|
|
|
|
int flagged = 0;
|
|
|
|
Map<String, TupleUidl> uidlTuple = new HashMap<>();
|
|
|
|
Map<String, TupleUidl> msgIdTuple = new HashMap<>();
|
|
|
|
for (TupleUidl id : ids) {
|
|
|
|
if (id.ui_flagged && !id.ui_hide)
|
|
|
|
flagged++;
|
|
|
|
|
|
|
|
if (id.uidl != null) {
|
|
|
|
if (uidlTuple.containsKey(id.uidl))
|
|
|
|
Log.w(account.name + " POP duplicate uidl/msgid=" + id.uidl + "/" + id.msgid);
|
|
|
|
uidlTuple.put(id.uidl, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id.msgid != null) {
|
|
|
|
if (msgIdTuple.containsKey(id.msgid))
|
|
|
|
Log.w(account.name + " POP duplicate msgid/uidl=" + id.msgid + "/" + id.uidl);
|
|
|
|
msgIdTuple.put(id.msgid, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
max = Math.min(max + flagged, imessages.length);
|
|
|
|
|
2021-07-18 08:06:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP" +
|
2021-06-25 13:55:16 +00:00
|
|
|
" device=" + ids.size() +
|
|
|
|
" server=" + imessages.length +
|
2023-04-03 06:53:47 +00:00
|
|
|
" flagged=" + flagged +
|
2021-06-25 13:55:16 +00:00
|
|
|
" max=" + max + "/" + account.max_messages +
|
2023-02-15 16:37:15 +00:00
|
|
|
" reversed=" + reversed +
|
2021-07-23 20:31:23 +00:00
|
|
|
" last=" + folder.last_sync_count +
|
|
|
|
" sync=" + sync +
|
2021-06-25 13:55:16 +00:00
|
|
|
" uidl=" + hasUidl);
|
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
if (sync) {
|
|
|
|
// Fetch UIDLs
|
2020-08-10 13:45:55 +00:00
|
|
|
if (hasUidl) {
|
2021-07-23 20:31:23 +00:00
|
|
|
FetchProfile ifetch = new FetchProfile();
|
|
|
|
ifetch.add(UIDFolder.FetchProfileItem.UID); // This will fetch all UIDs
|
|
|
|
ifolder.fetch(imessages, ifetch);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2023-10-21 17:24:11 +00:00
|
|
|
long now = new Date().getTime();
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2023-10-21 17:24:11 +00:00
|
|
|
if (hasUidl) {
|
|
|
|
Map<String, TupleUidl> known = new HashMap<>();
|
|
|
|
for (TupleUidl id : ids)
|
|
|
|
if (id.uidl != null)
|
|
|
|
known.put(id.uidl, id);
|
|
|
|
|
|
|
|
for (Message imessage : imessages) {
|
|
|
|
String uidl = ifolder.getUID(imessage);
|
|
|
|
if (TextUtils.isEmpty(uidl))
|
|
|
|
known.clear(); // better safe than sorry
|
|
|
|
else
|
|
|
|
known.remove(uidl);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TupleUidl uidl : known.values())
|
2023-10-21 18:04:48 +00:00
|
|
|
if (account.leave_on_device
|
|
|
|
? uidl.ui_hide && (uidl.ui_busy == null || uidl.ui_busy < now)
|
|
|
|
: !uidl.ui_flagged) {
|
2023-10-21 17:24:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP purging" +
|
|
|
|
" uidl=" + uidl.uidl + " hidden=" + uidl.ui_hide);
|
|
|
|
db.message().deleteMessage(uidl.id);
|
2021-07-23 20:31:23 +00:00
|
|
|
}
|
2023-10-21 17:24:11 +00:00
|
|
|
} else {
|
|
|
|
Map<String, TupleUidl> known = new HashMap<>();
|
|
|
|
for (TupleUidl id : ids)
|
|
|
|
if (id.msgid != null)
|
|
|
|
known.put(id.msgid, id);
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2023-10-21 17:24:11 +00:00
|
|
|
for (int i = imessages.length - max; i < imessages.length; i++) {
|
|
|
|
Message imessage = imessages[i];
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
|
|
|
String msgid = helper.getPOP3MessageID(); // expensive!
|
|
|
|
known.remove(msgid);
|
2020-08-10 13:45:55 +00:00
|
|
|
}
|
2023-10-21 17:24:11 +00:00
|
|
|
|
|
|
|
for (TupleUidl uidl : known.values())
|
2023-10-21 18:04:48 +00:00
|
|
|
if (account.leave_on_device
|
|
|
|
? uidl.ui_hide && (uidl.ui_busy == null || uidl.ui_busy < now)
|
|
|
|
: !uidl.ui_flagged) {
|
2023-10-21 17:24:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP purging" +
|
|
|
|
" msgid=" + uidl.msgid + " hidden=" + uidl.ui_hide);
|
|
|
|
db.message().deleteMessage(uidl.id);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
}
|
2019-09-20 07:45:36 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
boolean _new = true;
|
2023-02-15 16:37:15 +00:00
|
|
|
for (int i = reversed ? 0 : imessages.length - 1; reversed ? i < max : i >= imessages.length - max; i += reversed ? 1 : -1) {
|
2021-07-27 16:32:10 +00:00
|
|
|
state.ensureRunning("Sync/POP3");
|
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
Message imessage = imessages[i];
|
|
|
|
try {
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
2019-09-24 17:33:20 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
String uidl;
|
|
|
|
String msgid;
|
|
|
|
if (hasUidl) {
|
|
|
|
uidl = ifolder.getUID(imessage);
|
|
|
|
if (TextUtils.isEmpty(uidl)) {
|
|
|
|
EntityLog.log(context, account.name + " POP no uidl");
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
|
2022-06-02 10:45:04 +00:00
|
|
|
TupleUidl tuple = uidlTuple.get(uidl);
|
|
|
|
msgid = (tuple == null ? null : tuple.msgid);
|
2021-07-23 20:31:23 +00:00
|
|
|
if (msgid == null) {
|
|
|
|
msgid = helper.getMessageID();
|
|
|
|
if (TextUtils.isEmpty(msgid))
|
|
|
|
msgid = uidl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uidl = null;
|
2022-11-22 13:20:41 +00:00
|
|
|
msgid = helper.getPOP3MessageID();
|
2020-08-10 13:45:55 +00:00
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
|
2020-08-31 05:55:54 +00:00
|
|
|
if (TextUtils.isEmpty(msgid)) {
|
2021-07-23 20:31:23 +00:00
|
|
|
EntityLog.log(context, account.name + " POP no msgid uidl=" + uidl);
|
|
|
|
continue;
|
2020-08-31 05:55:54 +00:00
|
|
|
}
|
2020-05-13 12:33:47 +00:00
|
|
|
|
2022-06-02 10:45:04 +00:00
|
|
|
TupleUidl tuple = (hasUidl ? uidlTuple.get(uidl) : msgIdTuple.get(msgid));
|
|
|
|
if (tuple != null) {
|
2023-02-14 20:48:49 +00:00
|
|
|
if (account.max_messages != null)
|
|
|
|
_new = false;
|
|
|
|
|
2023-02-23 17:45:01 +00:00
|
|
|
Log.i(account.name + " POP having index=" + i + " " +
|
2022-04-02 07:57:45 +00:00
|
|
|
msgid + "=" + msgIdTuple.containsKey(msgid) + "/" +
|
2022-06-02 10:45:04 +00:00
|
|
|
uidl + "=" + uidlTuple.containsKey(uidl));
|
|
|
|
|
2022-06-06 17:39:39 +00:00
|
|
|
// Restore orphan POP3 moves
|
|
|
|
if (tuple.ui_hide &&
|
|
|
|
tuple.ui_busy != null &&
|
|
|
|
tuple.ui_busy < new Date().getTime())
|
|
|
|
db.message().setMessageUiHide(tuple.id, false);
|
2021-09-06 05:59:07 +00:00
|
|
|
|
|
|
|
if (download_eml)
|
|
|
|
try {
|
|
|
|
File raw = EntityMessage.getRawFile(context, tuple.id);
|
|
|
|
if (raw.exists())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Log.i(account.name + " POP raw " + msgid + "/" + uidl);
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(raw))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.message().setMessageRaw(tuple.id, true);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
Long sent = helper.getSent();
|
2023-02-15 08:48:15 +00:00
|
|
|
long received = helper.getPOP3Received();
|
2021-10-12 17:07:50 +00:00
|
|
|
|
|
|
|
boolean seen = (received <= account.created);
|
2023-02-23 17:45:01 +00:00
|
|
|
EntityLog.log(context, account.name + " POP index=" + i + " sync=" + uidl + "/" + msgid +
|
2021-10-12 17:07:50 +00:00
|
|
|
" new=" + _new + " seen=" + seen);
|
|
|
|
|
|
|
|
String[] authentication = helper.getAuthentication();
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
EntityMessage message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.uid = null;
|
|
|
|
message.uidl = uidl;
|
|
|
|
message.msgid = msgid;
|
|
|
|
message.hash = helper.getHash();
|
|
|
|
message.references = TextUtils.join(" ", helper.getReferences());
|
|
|
|
message.inreplyto = helper.getInReplyTo();
|
|
|
|
message.deliveredto = helper.getDeliveredTo();
|
2022-03-09 16:35:19 +00:00
|
|
|
message.thread = helper.getThreadId(context, account.id, folder.id, 0, received);
|
2021-10-12 17:07:50 +00:00
|
|
|
message.priority = helper.getPriority();
|
2022-01-18 09:07:13 +00:00
|
|
|
message.sensitivity = helper.getSensitivity();
|
2021-10-12 17:07:50 +00:00
|
|
|
message.auto_submitted = helper.getAutoSubmitted();
|
|
|
|
message.receipt_request = helper.getReceiptRequested();
|
|
|
|
message.receipt_to = helper.getReceiptTo();
|
|
|
|
message.bimi_selector = helper.getBimiSelector();
|
2022-01-06 21:46:11 +00:00
|
|
|
message.tls = helper.getTLS();
|
2021-10-12 17:07:50 +00:00
|
|
|
message.dkim = MessageHelper.getAuthentication("dkim", authentication);
|
|
|
|
message.spf = MessageHelper.getAuthentication("spf", authentication);
|
|
|
|
if (message.spf == null && helper.getSPF())
|
|
|
|
message.spf = true;
|
|
|
|
message.dmarc = MessageHelper.getAuthentication("dmarc", authentication);
|
2021-12-12 13:02:34 +00:00
|
|
|
message.smtp_from = helper.getMailFrom(authentication);
|
2021-10-12 17:07:50 +00:00
|
|
|
message.return_path = helper.getReturnPath();
|
2023-09-15 13:00:18 +00:00
|
|
|
message.submitter = helper.getSubmitter();
|
2021-10-12 17:07:50 +00:00
|
|
|
message.from = helper.getFrom();
|
|
|
|
message.to = helper.getTo();
|
|
|
|
message.cc = helper.getCc();
|
|
|
|
message.bcc = helper.getBcc();
|
|
|
|
message.reply = helper.getReply();
|
|
|
|
message.list_post = helper.getListPost();
|
|
|
|
message.unsubscribe = helper.getListUnsubscribe();
|
|
|
|
message.headers = helper.getHeaders();
|
2021-11-09 17:44:54 +00:00
|
|
|
message.infrastructure = helper.getInfrastructure();
|
2021-10-12 17:07:50 +00:00
|
|
|
message.subject = helper.getSubject();
|
|
|
|
message.size = parts.getBodySize();
|
|
|
|
message.total = helper.getSize();
|
|
|
|
message.content = false;
|
|
|
|
message.encrypt = parts.getEncryption();
|
|
|
|
message.ui_encrypt = message.encrypt;
|
|
|
|
message.received = received;
|
|
|
|
message.sent = sent;
|
|
|
|
message.seen = seen;
|
|
|
|
message.answered = false;
|
|
|
|
message.flagged = false;
|
|
|
|
message.flags = null;
|
|
|
|
message.keywords = new String[0];
|
|
|
|
message.ui_seen = seen;
|
|
|
|
message.ui_answered = false;
|
|
|
|
message.ui_flagged = false;
|
|
|
|
message.ui_hide = false;
|
|
|
|
message.ui_found = false;
|
|
|
|
message.ui_ignored = !_new;
|
|
|
|
message.ui_browsed = false;
|
|
|
|
|
|
|
|
if (message.deliveredto != null)
|
|
|
|
try {
|
|
|
|
Address deliveredto = new InternetAddress(message.deliveredto);
|
|
|
|
if (MessageHelper.equalEmail(new Address[]{deliveredto}, message.to))
|
|
|
|
message.deliveredto = null;
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
if (MessageHelper.equalEmail(message.submitter, message.from))
|
|
|
|
message.submitter = null;
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2023-08-13 09:07:20 +00:00
|
|
|
if (native_dkim && !BuildConfig.PLAY_STORE_RELEASE) {
|
|
|
|
List<String> signers = helper.verifyDKIM(context);
|
|
|
|
message.signedby = (signers.size() == 0 ? null : TextUtils.join(",", signers));
|
|
|
|
if (Boolean.TRUE.equals(message.dkim)) {
|
|
|
|
if (signers.size() == 0)
|
|
|
|
message.dkim = false;
|
|
|
|
} else {
|
|
|
|
if (message.from != null)
|
|
|
|
for (Address from : message.from) {
|
|
|
|
String domain = UriHelper.getEmailDomain(((InternetAddress) from).getAddress());
|
|
|
|
if (domain != null && signers.contains(domain)) {
|
|
|
|
message.dkim = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
if (message.size == null && message.total != null)
|
|
|
|
message.size = message.total;
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
message.identity = (identity == null ? null : identity.id);
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
message.sender = MessageHelper.getSortKey(message.from);
|
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(message.from);
|
|
|
|
message.avatar = (lookupUri == null ? null : lookupUri.toString());
|
|
|
|
if (message.avatar == null && notify_known && pro)
|
|
|
|
message.ui_ignored = true;
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-12-12 13:02:34 +00:00
|
|
|
message.from_domain = (message.checkFromDomain(context) == null);
|
|
|
|
|
|
|
|
// No reply_domain
|
2021-10-12 17:07:50 +00:00
|
|
|
// No MX check
|
2022-05-04 15:38:59 +00:00
|
|
|
|
|
|
|
if (check_blocklist && use_blocklist_pop) {
|
|
|
|
message.blocklist = DnsBlockList.isJunk(context,
|
|
|
|
imessage.getHeader("Received"));
|
|
|
|
|
|
|
|
if (message.blocklist == null || !message.blocklist) {
|
|
|
|
List<Address> senders = new ArrayList<>();
|
|
|
|
if (message.reply != null)
|
|
|
|
senders.addAll(Arrays.asList(message.reply));
|
|
|
|
if (message.from != null)
|
|
|
|
senders.addAll(Arrays.asList(message.from));
|
|
|
|
message.blocklist = DnsBlockList.isJunk(context, senders);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Boolean.TRUE.equals(message.blocklist)) {
|
|
|
|
EntityLog.log(context, account.name + " POP blocklist=" +
|
|
|
|
MessageHelper.formatAddresses(message.from));
|
2023-02-27 16:13:14 +00:00
|
|
|
message.ui_hide = true;
|
2022-05-04 15:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
|
2022-03-26 10:10:51 +00:00
|
|
|
if (message.from != null) {
|
2022-03-26 11:39:21 +00:00
|
|
|
EntityContact badboy = null;
|
2022-03-26 10:10:51 +00:00
|
|
|
for (Address from : message.from) {
|
|
|
|
String email = ((InternetAddress) from).getAddress();
|
|
|
|
if (TextUtils.isEmpty(email))
|
|
|
|
continue;
|
|
|
|
|
2022-03-26 11:39:21 +00:00
|
|
|
badboy = db.contact().getContact(message.account, EntityContact.TYPE_JUNK, email);
|
|
|
|
if (badboy != null)
|
2022-03-26 10:10:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-03-26 11:39:21 +00:00
|
|
|
|
|
|
|
if (badboy != null) {
|
|
|
|
badboy.times_contacted++;
|
|
|
|
badboy.last_contacted = new Date().getTime();
|
|
|
|
db.contact().updateContact(badboy);
|
|
|
|
|
|
|
|
EntityLog.log(context, account.name + " POP blocked=" +
|
|
|
|
MessageHelper.formatAddresses(message.from));
|
|
|
|
|
2023-02-27 16:13:14 +00:00
|
|
|
message.ui_hide = true;
|
2022-03-26 11:39:21 +00:00
|
|
|
}
|
2022-03-26 10:10:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 07:12:51 +00:00
|
|
|
boolean needsHeaders = EntityRule.needsHeaders(message, rules);
|
2021-12-09 09:34:51 +00:00
|
|
|
List<Header> headers = (needsHeaders ? helper.getAllHeaders() : null);
|
2022-02-14 19:18:03 +00:00
|
|
|
String body = parts.getHtml(context, download_plain);
|
2021-12-08 11:18:38 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-09-19 19:47:19 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
|
|
|
EntityLog.log(context, account.name + " POP added id=" + message.id +
|
|
|
|
" uidl/msgid=" + message.uidl + "/" + message.msgid);
|
|
|
|
|
|
|
|
int sequence = 1;
|
|
|
|
for (EntityAttachment attachment : parts.getAttachments()) {
|
|
|
|
Log.i(account.name + " POP attachment seq=" + sequence +
|
|
|
|
" name=" + attachment.name + " type=" + attachment.type +
|
|
|
|
" cid=" + attachment.cid + " pgp=" + attachment.encryption +
|
|
|
|
" size=" + attachment.size);
|
|
|
|
attachment.message = message.id;
|
|
|
|
attachment.sequence = sequence++;
|
|
|
|
attachment.id = db.attachment().insertAttachment(attachment);
|
2021-07-23 20:31:23 +00:00
|
|
|
}
|
2020-06-22 08:22:25 +00:00
|
|
|
|
2021-12-08 21:46:05 +00:00
|
|
|
runRules(context, headers, body, account, folder, message, rules);
|
2021-10-12 17:07:50 +00:00
|
|
|
reportNewMessage(context, account, folder, message);
|
2021-07-23 20:31:23 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2021-09-06 05:56:57 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
|
|
|
String text = HtmlHelper.getFullText(body);
|
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
message.language,
|
2022-02-14 19:18:03 +00:00
|
|
|
parts.isPlainOnly(download_plain),
|
2021-10-12 17:07:50 +00:00
|
|
|
message.preview,
|
|
|
|
parts.getWarnings(message.warning));
|
|
|
|
|
2022-10-31 18:02:29 +00:00
|
|
|
try {
|
|
|
|
for (EntityAttachment attachment : parts.getAttachments())
|
|
|
|
if (attachment.subsequence == null)
|
2023-06-06 20:22:08 +00:00
|
|
|
parts.downloadAttachment(context, attachment, folder);
|
2022-10-31 18:02:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-10-12 17:07:50 +00:00
|
|
|
|
|
|
|
if (download_eml)
|
|
|
|
try {
|
|
|
|
Log.i(account.name + " POP raw " + msgid + "/" + uidl);
|
2021-09-05 20:25:26 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
File raw = message.getRawFile(context);
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(raw))) {
|
|
|
|
imessage.writeTo(os);
|
2021-09-05 20:25:26 +00:00
|
|
|
}
|
2021-07-23 20:31:23 +00:00
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
message.raw = true;
|
|
|
|
db.message().setMessageRaw(message.id, message.raw);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:44:10 +00:00
|
|
|
if (!account.leave_on_server && account.client_delete)
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
|
2021-10-12 17:07:50 +00:00
|
|
|
EntityContact.received(context, account, folder, message);
|
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
//if (!(ex instanceof MessagingException))
|
|
|
|
throw ex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
javax.mail.MessagingException: error loading POP3 headers;
|
|
|
|
nested exception is:
|
|
|
|
java.io.IOException: Unexpected response: ...
|
|
|
|
at com.sun.mail.pop3.POP3Message.loadHeaders(SourceFile:15)
|
|
|
|
at com.sun.mail.pop3.POP3Message.getHeader(SourceFile:5)
|
|
|
|
at eu.faircode.email.MessageHelper.getMessageID(SourceFile:2)
|
|
|
|
at eu.faircode.email.Core.onSynchronizeMessages(SourceFile:78)
|
|
|
|
at eu.faircode.email.Core.processOperations(SourceFile:89)
|
|
|
|
at eu.faircode.email.ServiceSynchronize$19$1$2.run(SourceFile:51)
|
|
|
|
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
|
|
|
|
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
|
|
|
|
at eu.faircode.email.Helper$PriorityFuture.run(SourceFile:1)
|
|
|
|
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
|
|
|
|
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
|
|
|
|
at java.lang.Thread.run(Thread.java:923)
|
|
|
|
Caused by: java.io.IOException: Unexpected response: Bd04v8G0fQOraFZwxNDLapHDdRM0xj8oW+4nG4FVG05/WuE/sW8i3xxzx3unQBWtyhU3KDqQSDzz
|
|
|
|
at com.sun.mail.pop3.Protocol.readResponse(SourceFile:12)
|
|
|
|
at com.sun.mail.pop3.Protocol.multilineCommand(SourceFile:3)
|
|
|
|
at com.sun.mail.pop3.Protocol.top(SourceFile:1)
|
|
|
|
at com.sun.mail.pop3.POP3Message.loadHeaders(SourceFile:5)
|
|
|
|
*/
|
2021-07-23 20:31:23 +00:00
|
|
|
} finally {
|
|
|
|
((POP3Message) imessage).invalidate(true);
|
2020-05-06 18:32:03 +00:00
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
}
|
2021-06-22 11:18:49 +00:00
|
|
|
}
|
2019-09-20 07:45:36 +00:00
|
|
|
|
2022-05-09 06:02:25 +00:00
|
|
|
if (account.max_messages != null && !account.leave_on_device) {
|
2023-09-13 10:57:27 +00:00
|
|
|
int hidden = db.message().setMessagesUiHide(folder.id,
|
|
|
|
Math.abs(account.max_messages) + flagged);
|
2023-09-03 06:00:34 +00:00
|
|
|
int deleted = db.message().deleteMessagesKeep(folder.id,
|
2023-09-13 10:57:27 +00:00
|
|
|
Math.abs(account.max_messages) + flagged + POP3_KEEP_EXTRA);
|
2022-05-07 14:00:30 +00:00
|
|
|
EntityLog.log(context, account.name + " POP" +
|
2023-04-03 18:48:19 +00:00
|
|
|
" cleanup max=" + account.max_messages +
|
|
|
|
" hidden=" + hidden +
|
|
|
|
" deleted=" + deleted);
|
2022-05-07 14:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 15:15:06 +00:00
|
|
|
folder.last_sync_count = imessages.length;
|
|
|
|
db.folder().setFolderLastSyncCount(folder.id, folder.last_sync_count);
|
2020-10-26 12:21:43 +00:00
|
|
|
db.folder().setFolderLastSync(folder.id, new Date().getTime());
|
2021-07-18 08:06:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP done");
|
2019-09-19 15:41:26 +00:00
|
|
|
} finally {
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:02:42 +00:00
|
|
|
private static void onSynchronizeMessages(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context, JSONArray jargs,
|
2019-02-27 19:04:26 +00:00
|
|
|
EntityAccount account, final EntityFolder folder,
|
2022-01-14 09:49:47 +00:00
|
|
|
IMAPStore istore, final IMAPFolder ifolder, State state)
|
2022-11-25 08:29:09 +00:00
|
|
|
throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
final DB db = DB.getInstance(context);
|
|
|
|
try {
|
2020-07-15 16:58:03 +00:00
|
|
|
SyncStats stats = new SyncStats();
|
|
|
|
|
2019-03-25 15:02:27 +00:00
|
|
|
// Legacy
|
|
|
|
if (jargs.length() == 0)
|
2020-12-02 15:58:32 +00:00
|
|
|
jargs = folder.getSyncArgs(false);
|
2019-03-25 15:02:27 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
int sync_days = jargs.getInt(0);
|
|
|
|
int keep_days = jargs.getInt(1);
|
2019-06-23 08:28:33 +00:00
|
|
|
boolean download = jargs.optBoolean(2, false);
|
|
|
|
boolean auto_delete = jargs.optBoolean(3, false);
|
2019-07-19 05:11:05 +00:00
|
|
|
int initialize = jargs.optInt(4, folder.initialize);
|
2020-12-02 15:58:32 +00:00
|
|
|
boolean force = jargs.optBoolean(5, false);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-24 10:45:52 +00:00
|
|
|
if (keep_days == sync_days && keep_days != Integer.MAX_VALUE)
|
2019-02-27 15:05:15 +00:00
|
|
|
keep_days++;
|
|
|
|
|
2019-05-06 13:25:11 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-08-06 11:36:52 +00:00
|
|
|
boolean sync_quick_imap = prefs.getBoolean("sync_quick_imap", false);
|
2020-03-31 19:07:40 +00:00
|
|
|
boolean sync_nodate = prefs.getBoolean("sync_nodate", false);
|
2019-05-06 13:25:11 +00:00
|
|
|
boolean sync_unseen = prefs.getBoolean("sync_unseen", false);
|
2019-07-24 14:33:00 +00:00
|
|
|
boolean sync_flagged = prefs.getBoolean("sync_flagged", false);
|
2019-06-15 11:14:41 +00:00
|
|
|
boolean sync_kept = prefs.getBoolean("sync_kept", true);
|
2019-07-06 16:13:38 +00:00
|
|
|
boolean delete_unseen = prefs.getBoolean("delete_unseen", false);
|
2021-04-08 05:57:28 +00:00
|
|
|
boolean use_modseq = prefs.getBoolean("use_modseq", true);
|
2021-02-09 15:49:51 +00:00
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
2022-11-25 08:29:09 +00:00
|
|
|
boolean log = prefs.getBoolean("protocol", false);
|
2019-05-06 13:25:11 +00:00
|
|
|
|
2022-06-13 15:42:17 +00:00
|
|
|
if (account.isYahoo() || account.isAol())
|
2022-06-11 16:07:18 +00:00
|
|
|
sync_nodate = false;
|
|
|
|
|
2021-10-26 11:22:25 +00:00
|
|
|
if (account.isZoho()) {
|
|
|
|
sync_unseen = false;
|
|
|
|
sync_flagged = false;
|
|
|
|
}
|
|
|
|
|
2019-05-06 13:25:11 +00:00
|
|
|
Log.i(folder.name + " start sync after=" + sync_days + "/" + keep_days +
|
2021-08-06 11:36:52 +00:00
|
|
|
" quick=" + sync_quick_imap + " force=" + force +
|
2019-07-06 16:13:38 +00:00
|
|
|
" sync unseen=" + sync_unseen + " flagged=" + sync_flagged +
|
|
|
|
" delete unseen=" + delete_unseen + " kept=" + sync_kept);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-10-04 17:45:05 +00:00
|
|
|
if (folder.local) {
|
|
|
|
folder.synchronize = false;
|
|
|
|
db.folder().setFolderSynchronize(folder.id, folder.synchronize);
|
2021-10-25 05:26:47 +00:00
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
2021-10-04 17:45:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
db.folder().setFolderSyncState(folder.id, "syncing");
|
|
|
|
|
2023-04-18 15:59:28 +00:00
|
|
|
Flags flags = ifolder.getPermanentFlags();
|
|
|
|
|
2023-04-18 19:00:47 +00:00
|
|
|
try {
|
|
|
|
List<String> f = new ArrayList<>();
|
|
|
|
if (flags != null) {
|
|
|
|
if (flags.contains(Flags.Flag.ANSWERED))
|
|
|
|
f.add("\\Answered");
|
|
|
|
if (flags.contains(Flags.Flag.DELETED))
|
|
|
|
f.add("\\Deleted");
|
|
|
|
if (flags.contains(Flags.Flag.DRAFT))
|
|
|
|
f.add("\\Draft");
|
|
|
|
if (flags.contains(Flags.Flag.FLAGGED))
|
|
|
|
f.add("\\Flagged");
|
|
|
|
if (flags.contains(Flags.Flag.RECENT))
|
|
|
|
f.add("\\Recent");
|
|
|
|
if (flags.contains(Flags.Flag.SEEN))
|
|
|
|
f.add("\\Seen");
|
|
|
|
if (flags.contains(Flags.Flag.USER))
|
|
|
|
f.add("\\*");
|
|
|
|
}
|
|
|
|
db.folder().setFolderFlags(folder.id, DB.Converters.fromStringArray(f.toArray(new String[0])));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2023-04-18 15:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String[] userFlags = flags.getUserFlags();
|
2022-07-05 06:06:31 +00:00
|
|
|
if (userFlags != null && userFlags.length > 0) {
|
|
|
|
List<String> keywords = new ArrayList<>(Arrays.asList(userFlags));
|
|
|
|
Collections.sort(keywords);
|
|
|
|
userFlags = keywords.toArray(new String[0]);
|
|
|
|
if (!Arrays.equals(folder.keywords, userFlags)) {
|
|
|
|
Log.i(folder.name + " updating flags=" + TextUtils.join(",", userFlags));
|
|
|
|
folder.keywords = userFlags;
|
|
|
|
db.folder().setFolderKeywords(folder.id, DB.Converters.fromStringArray(userFlags));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:01:27 +00:00
|
|
|
// Check uid validity
|
|
|
|
try {
|
|
|
|
long uidv = ifolder.getUIDValidity();
|
2019-07-24 20:04:58 +00:00
|
|
|
if (folder.uidv != null && !folder.uidv.equals(uidv)) {
|
2019-07-24 20:01:27 +00:00
|
|
|
Log.w(folder.name + " uid validity changed from " + folder.uidv + " to " + uidv);
|
|
|
|
db.message().deleteLocalMessages(folder.id);
|
|
|
|
}
|
|
|
|
folder.uidv = uidv;
|
|
|
|
db.folder().setFolderUidValidity(folder.id, uidv);
|
|
|
|
} catch (MessagingException ex) {
|
2019-09-17 19:25:41 +00:00
|
|
|
Log.w(folder.name, ex);
|
2019-07-24 20:01:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
// https://tools.ietf.org/html/rfc4551
|
|
|
|
// https://wiki.mozilla.org/Thunderbird:IMAP_RFC_4551_Implementation
|
2021-04-08 05:57:28 +00:00
|
|
|
Long modseq = null;
|
2021-04-07 06:42:47 +00:00
|
|
|
boolean modified = true;
|
2021-04-08 05:57:28 +00:00
|
|
|
if (use_modseq)
|
|
|
|
try {
|
|
|
|
if (MessageHelper.hasCapability(ifolder, "CONDSTORE")) {
|
|
|
|
modseq = ifolder.getHighestModSeq();
|
2022-02-18 17:44:09 +00:00
|
|
|
if (modseq < 0)
|
|
|
|
modseq = null;
|
|
|
|
modified = (force || initialize != 0 ||
|
2021-04-08 05:57:28 +00:00
|
|
|
folder.modseq == null || !folder.modseq.equals(modseq));
|
2022-02-18 17:44:09 +00:00
|
|
|
EntityLog.log(context, folder.name + " modseq=" + modseq + "/" + folder.modseq +
|
|
|
|
" force=" + force + " init=" + (initialize != 0) + " modified=" + modified);
|
2021-04-08 05:57:28 +00:00
|
|
|
}
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// Get reference times
|
|
|
|
Calendar cal_sync = Calendar.getInstance();
|
|
|
|
cal_sync.add(Calendar.DAY_OF_MONTH, -sync_days);
|
|
|
|
cal_sync.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal_sync.set(Calendar.MINUTE, 0);
|
|
|
|
cal_sync.set(Calendar.SECOND, 0);
|
|
|
|
cal_sync.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
Calendar cal_keep = Calendar.getInstance();
|
|
|
|
cal_keep.add(Calendar.DAY_OF_MONTH, -keep_days);
|
|
|
|
cal_keep.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal_keep.set(Calendar.MINUTE, 0);
|
|
|
|
cal_keep.set(Calendar.SECOND, 0);
|
|
|
|
cal_keep.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
2023-11-08 07:05:59 +00:00
|
|
|
Calendar cal_keep_unseen = Calendar.getInstance();
|
|
|
|
cal_keep_unseen.add(Calendar.DAY_OF_MONTH, -keep_days * 2);
|
|
|
|
cal_keep_unseen.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal_keep_unseen.set(Calendar.MINUTE, 0);
|
|
|
|
cal_keep_unseen.set(Calendar.SECOND, 0);
|
|
|
|
cal_keep_unseen.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
long sync_time = cal_sync.getTimeInMillis();
|
|
|
|
if (sync_time < 0)
|
|
|
|
sync_time = 0;
|
|
|
|
|
|
|
|
long keep_time = cal_keep.getTimeInMillis();
|
|
|
|
if (keep_time < 0)
|
|
|
|
keep_time = 0;
|
|
|
|
|
2023-11-08 07:05:59 +00:00
|
|
|
long keep_time_unseen = cal_keep_unseen.getTimeInMillis();
|
|
|
|
if (keep_time_unseen < 0)
|
|
|
|
keep_time_unseen = 0;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " sync=" + new Date(sync_time) + " keep=" + new Date(keep_time));
|
|
|
|
|
|
|
|
// Delete old local messages
|
2023-06-09 06:37:55 +00:00
|
|
|
long delete_time = new Date().getTime() - 3600 * 1000L;
|
2019-07-08 07:19:16 +00:00
|
|
|
if (auto_delete) {
|
2023-06-09 06:37:55 +00:00
|
|
|
List<Long> tbds = db.message().getMessagesBefore(folder.id, delete_time, keep_time, delete_unseen);
|
2019-05-01 09:26:32 +00:00
|
|
|
Log.i(folder.name + " local tbd=" + tbds.size());
|
2019-07-08 07:19:16 +00:00
|
|
|
EntityFolder trash = db.folder().getFolderByType(folder.account, EntityFolder.TRASH);
|
2019-05-01 09:26:32 +00:00
|
|
|
for (Long tbd : tbds) {
|
|
|
|
EntityMessage message = db.message().getMessage(tbd);
|
2019-07-08 07:19:16 +00:00
|
|
|
if (message != null && trash != null)
|
2022-02-22 12:00:01 +00:00
|
|
|
if (EntityFolder.TRASH.equals(folder.type) ||
|
|
|
|
EntityFolder.JUNK.equals(folder.type))
|
2019-07-08 07:19:16 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.DELETE);
|
|
|
|
else
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, trash.id);
|
2019-05-01 09:26:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-08 07:05:59 +00:00
|
|
|
int old = db.message().deleteMessagesBefore(folder.id, delete_time, keep_time, keep_time_unseen, delete_unseen);
|
2019-05-01 09:26:32 +00:00
|
|
|
Log.i(folder.name + " local old=" + old);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-30 17:45:54 +00:00
|
|
|
Message[] imessages;
|
2021-08-06 11:36:52 +00:00
|
|
|
long search;
|
|
|
|
Long[] ids;
|
|
|
|
if (modified || !sync_quick_imap || force) {
|
|
|
|
// Get list of local uids
|
|
|
|
final List<Long> uids = db.message().getUids(folder.id, sync_kept || force ? null : sync_time);
|
|
|
|
Log.i(folder.name + " local count=" + uids.size());
|
|
|
|
|
2022-11-25 08:29:09 +00:00
|
|
|
if (BuildConfig.DEBUG || log)
|
2022-11-25 08:22:14 +00:00
|
|
|
try {
|
|
|
|
Status status = (Status) ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
return protocol.status(ifolder.getFullName(), null);
|
|
|
|
}
|
|
|
|
});
|
2022-11-25 08:29:09 +00:00
|
|
|
EntityLog.log(context, EntityLog.Type.Protocol, folder.name + " status" +
|
|
|
|
" total=" + status.total + " unseen=" + status.unseen);
|
2022-11-25 08:22:14 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
// Reduce list of local uids
|
|
|
|
SearchTerm dateTerm = account.use_date
|
|
|
|
? new SentDateTerm(ComparisonTerm.GE, new Date(sync_time))
|
|
|
|
: new ReceivedDateTerm(ComparisonTerm.GE, new Date(sync_time));
|
|
|
|
|
|
|
|
SearchTerm searchTerm = dateTerm;
|
2021-12-03 08:14:16 +00:00
|
|
|
if (sync_nodate && !account.isOutlook())
|
2021-08-06 11:36:52 +00:00
|
|
|
searchTerm = new OrTerm(searchTerm, new ReceivedDateTerm(ComparisonTerm.LT, new Date(365 * 24 * 3600 * 1000L)));
|
2022-10-24 19:51:32 +00:00
|
|
|
if (sync_unseen && flags.contains(Flags.Flag.SEEN))
|
2021-08-06 11:36:52 +00:00
|
|
|
searchTerm = new OrTerm(searchTerm, new FlagTerm(new Flags(Flags.Flag.SEEN), false));
|
2022-10-24 19:51:32 +00:00
|
|
|
if (sync_flagged && flags.contains(Flags.Flag.FLAGGED))
|
2021-08-06 11:36:52 +00:00
|
|
|
searchTerm = new OrTerm(searchTerm, new FlagTerm(new Flags(Flags.Flag.FLAGGED), true));
|
|
|
|
|
|
|
|
search = SystemClock.elapsedRealtime();
|
|
|
|
if (sync_time == 0)
|
|
|
|
imessages = ifolder.getMessages();
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
imessages = ifolder.search(searchTerm);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
// Fallback to date only search
|
|
|
|
// BAD Could not parse command
|
|
|
|
imessages = ifolder.search(dateTerm);
|
|
|
|
}
|
|
|
|
if (imessages == null)
|
|
|
|
imessages = new Message[0];
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-01-19 19:48:34 +00:00
|
|
|
for (Message imessage : imessages)
|
|
|
|
if (imessage instanceof IMAPMessage)
|
|
|
|
((IMAPMessage) imessage).invalidateHeaders();
|
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
stats.search_ms = (SystemClock.elapsedRealtime() - search);
|
|
|
|
Log.i(folder.name + " remote count=" + imessages.length + " search=" + stats.search_ms + " ms");
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
ids = new Long[imessages.length];
|
2021-08-06 15:53:52 +00:00
|
|
|
|
|
|
|
if (!modified) {
|
|
|
|
Log.i(folder.name + " quick check");
|
2021-08-06 11:36:52 +00:00
|
|
|
long fetch = SystemClock.elapsedRealtime();
|
2019-03-30 10:57:29 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(imessages, fp);
|
2021-04-07 06:42:47 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
stats.flags = imessages.length;
|
|
|
|
stats.flags_ms = (SystemClock.elapsedRealtime() - fetch);
|
|
|
|
Log.i(folder.name + " remote fetched=" + stats.flags_ms + " ms");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2022-02-19 07:28:16 +00:00
|
|
|
for (int i = 0; i < imessages.length; i++) {
|
|
|
|
state.ensureRunning("Sync/IMAP/check");
|
2021-08-06 11:36:52 +00:00
|
|
|
|
2022-02-19 07:28:16 +00:00
|
|
|
try {
|
2021-08-06 11:36:52 +00:00
|
|
|
long uid = ifolder.getUID(imessages[i]);
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
ids[i] = (message == null ? null : message.id);
|
|
|
|
if (message == null || message.ui_hide) {
|
|
|
|
Log.i(folder.name + " missing uid=" + uid);
|
|
|
|
modified = true;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
uids.remove(uid);
|
2022-02-19 07:28:16 +00:00
|
|
|
} catch (Throwable ex) {
|
2021-08-06 11:36:52 +00:00
|
|
|
Log.w(ex);
|
2022-02-19 07:28:16 +00:00
|
|
|
modified = true;
|
|
|
|
modseq = null;
|
|
|
|
}
|
2021-08-06 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uids.size() > 0) {
|
|
|
|
Log.i(folder.name + " remaining=" + uids.size());
|
|
|
|
modified = true;
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
EntityLog.log(context, folder.name + " modified=" + modified);
|
2020-10-05 14:04:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
if (modified) {
|
|
|
|
long fetch = SystemClock.elapsedRealtime();
|
2021-04-07 06:42:47 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID); // To check if message exists
|
|
|
|
fp.add(FetchProfile.Item.FLAGS); // To update existing messages
|
|
|
|
if (account.isGmail())
|
|
|
|
fp.add(GmailFolder.FetchProfileItem.LABELS);
|
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
stats.flags = imessages.length;
|
|
|
|
stats.flags_ms = (SystemClock.elapsedRealtime() - fetch);
|
|
|
|
Log.i(folder.name + " remote fetched=" + stats.flags_ms + " ms");
|
|
|
|
|
|
|
|
// Sort for finding referenced/replied-to messages
|
|
|
|
// Sorting on date/time would be better, but requires fetching the headers
|
|
|
|
Arrays.sort(imessages, new Comparator<Message>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Message m1, Message m2) {
|
|
|
|
try {
|
|
|
|
return Long.compare(ifolder.getUID(m1), ifolder.getUID(m2));
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-21 07:54:20 +00:00
|
|
|
}
|
2021-08-06 11:36:52 +00:00
|
|
|
});
|
2019-10-21 07:54:20 +00:00
|
|
|
|
2021-10-24 12:39:49 +00:00
|
|
|
List<Message> deleted = new ArrayList<>();
|
2021-08-06 11:36:52 +00:00
|
|
|
for (int i = 0; i < imessages.length; i++) {
|
|
|
|
state.ensureRunning("Sync/IMAP/delete");
|
2021-07-27 16:32:10 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
try {
|
|
|
|
if (perform_expunge && imessages[i].isSet(Flags.Flag.DELETED))
|
2021-10-24 12:39:49 +00:00
|
|
|
deleted.add(imessages[i]);
|
2021-08-06 11:36:52 +00:00
|
|
|
else
|
|
|
|
uids.remove(ifolder.getUID(imessages[i]));
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
2022-02-18 17:44:09 +00:00
|
|
|
modseq = null;
|
2021-08-06 11:36:52 +00:00
|
|
|
EntityLog.log(context, folder.name + " expunge " + Log.formatThrowable(ex, false));
|
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 12:39:49 +00:00
|
|
|
expunge(context, ifolder, deleted);
|
2021-04-07 06:42:47 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
if (uids.size() > 0) {
|
|
|
|
// This is done outside of JavaMail to prevent changed notifications
|
|
|
|
if (!ifolder.isOpen())
|
|
|
|
throw new FolderClosedException(ifolder, "UID FETCH");
|
2021-04-07 06:42:47 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
long getuid = SystemClock.elapsedRealtime();
|
|
|
|
MessagingException ex = (MessagingException) ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
2022-01-14 09:49:47 +00:00
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
protocol.select(folder.name);
|
2021-02-19 17:07:06 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
// Build ranges
|
|
|
|
List<Pair<Long, Long>> ranges = new ArrayList<>();
|
|
|
|
long first = -1;
|
|
|
|
long last = -1;
|
|
|
|
for (long uid : uids)
|
|
|
|
if (first < 0)
|
|
|
|
first = uid;
|
|
|
|
else if ((last < 0 ? first : last) + 1 == uid)
|
|
|
|
last = uid;
|
|
|
|
else {
|
|
|
|
ranges.add(new Pair<>(first, last < 0 ? first : last));
|
|
|
|
first = uid;
|
|
|
|
last = -1;
|
|
|
|
}
|
|
|
|
if (first > 0)
|
2021-04-07 06:42:47 +00:00
|
|
|
ranges.add(new Pair<>(first, last < 0 ? first : last));
|
|
|
|
|
2021-10-16 19:06:05 +00:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc2683#section-3.2.1.5
|
2022-01-18 12:21:49 +00:00
|
|
|
int chunk_size = prefs.getInt("chunk_size", DEFAULT_CHUNK_SIZE);
|
2021-10-17 06:58:56 +00:00
|
|
|
if (chunk_size < 200 &&
|
|
|
|
(account.isGmail() || account.isOutlook()))
|
|
|
|
chunk_size = 200;
|
2021-10-17 06:47:11 +00:00
|
|
|
List<List<Pair<Long, Long>>> chunks = Helper.chunkList(ranges, chunk_size);
|
2021-08-06 11:36:52 +00:00
|
|
|
|
|
|
|
Log.i(folder.name + " executing uid fetch count=" + uids.size() +
|
|
|
|
" ranges=" + ranges.size() + " chunks=" + chunks.size());
|
|
|
|
for (int c = 0; c < chunks.size(); c++) {
|
|
|
|
List<Pair<Long, Long>> chunk = chunks.get(c);
|
|
|
|
Log.i(folder.name + " chunk #" + c + " size=" + chunk.size());
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (Pair<Long, Long> range : chunk) {
|
|
|
|
if (sb.length() > 0)
|
|
|
|
sb.append(',');
|
|
|
|
if (range.first.equals(range.second))
|
|
|
|
sb.append(range.first);
|
|
|
|
else
|
|
|
|
sb.append(range.first).append(':').append(range.second);
|
|
|
|
}
|
|
|
|
String command = "UID FETCH " + sb + " (UID FLAGS)";
|
|
|
|
Response[] responses = protocol.command(command, null);
|
|
|
|
|
|
|
|
if (responses.length > 0 && responses[responses.length - 1].isOK()) {
|
|
|
|
for (Response response : responses)
|
|
|
|
if (response instanceof FetchResponse) {
|
|
|
|
FetchResponse fr = (FetchResponse) response;
|
|
|
|
UID uid = fr.getItem(UID.class);
|
|
|
|
FLAGS flags = fr.getItem(FLAGS.class);
|
|
|
|
if (uid == null || flags == null)
|
|
|
|
continue;
|
|
|
|
if (perform_expunge && flags.contains(Flags.Flag.DELETED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uids.remove(uid.uid);
|
|
|
|
|
|
|
|
if (force) {
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid.uid);
|
|
|
|
if (message != null) {
|
|
|
|
boolean update = false;
|
2022-05-28 09:25:08 +00:00
|
|
|
boolean recent = flags.contains(Flags.Flag.RECENT);
|
2021-08-06 11:36:52 +00:00
|
|
|
boolean seen = flags.contains(Flags.Flag.SEEN);
|
|
|
|
boolean answered = flags.contains(Flags.Flag.ANSWERED);
|
|
|
|
boolean flagged = flags.contains(Flags.Flag.FLAGGED);
|
|
|
|
boolean deleted = flags.contains(Flags.Flag.DELETED);
|
2022-05-28 09:25:08 +00:00
|
|
|
if (message.recent != recent) {
|
|
|
|
update = true;
|
|
|
|
message.recent = recent;
|
|
|
|
Log.i("UID fetch recent=" + recent);
|
|
|
|
}
|
2021-08-06 11:36:52 +00:00
|
|
|
if (message.seen != seen) {
|
|
|
|
update = true;
|
|
|
|
message.seen = seen;
|
|
|
|
message.ui_seen = seen;
|
|
|
|
Log.i("UID fetch seen=" + seen);
|
|
|
|
}
|
|
|
|
if (message.answered != answered) {
|
|
|
|
update = true;
|
|
|
|
message.answered = answered;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
Log.i("UID fetch answered=" + answered);
|
|
|
|
}
|
|
|
|
if (message.flagged != flagged) {
|
|
|
|
update = true;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.ui_flagged = flagged;
|
|
|
|
Log.i("UID fetch flagged=" + flagged);
|
|
|
|
}
|
|
|
|
if (message.deleted != deleted) {
|
|
|
|
update = true;
|
|
|
|
message.deleted = deleted;
|
|
|
|
message.ui_deleted = deleted;
|
2022-01-10 08:38:17 +00:00
|
|
|
message.ui_ignored = deleted;
|
2021-08-06 11:36:52 +00:00
|
|
|
Log.i("UID fetch deleted=" + deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update)
|
|
|
|
db.message().updateMessage(message);
|
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
2021-02-19 17:07:06 +00:00
|
|
|
}
|
2021-08-06 11:36:52 +00:00
|
|
|
} else {
|
|
|
|
for (Response response : responses)
|
2022-01-29 08:47:48 +00:00
|
|
|
if (response.isBYE())
|
2021-08-06 11:36:52 +00:00
|
|
|
return new MessagingException("UID FETCH", new IOException(response.toString()));
|
2022-01-29 08:47:48 +00:00
|
|
|
else if (response.isNO()) {
|
2022-01-18 08:35:56 +00:00
|
|
|
Log.e("UID FETCH " + response);
|
2022-01-14 09:49:47 +00:00
|
|
|
throw new CommandFailedException(response);
|
2022-01-18 08:35:56 +00:00
|
|
|
} else if (response.isBAD()) {
|
|
|
|
Log.e("UID FETCH " + response);
|
|
|
|
// BAD Error in IMAP command UID FETCH: Too long argument (n.nnn + n.nnn + n.nnn secs).
|
2022-01-19 20:44:15 +00:00
|
|
|
if (response.toString().contains("Too long argument")) {
|
2022-01-18 08:35:56 +00:00
|
|
|
chunk_size = chunk_size / 2;
|
|
|
|
if (chunk_size > 0)
|
|
|
|
prefs.edit().putInt("chunk_size", chunk_size).apply();
|
|
|
|
}
|
2022-01-14 09:49:47 +00:00
|
|
|
throw new BadCommandException(response);
|
2022-01-18 08:35:56 +00:00
|
|
|
}
|
2022-01-14 09:49:47 +00:00
|
|
|
throw new ProtocolException("UID FETCH failed");
|
2021-08-06 11:36:52 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (ex != null)
|
|
|
|
throw ex;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
stats.uids = uids.size();
|
|
|
|
stats.uids_ms = (SystemClock.elapsedRealtime() - getuid);
|
|
|
|
Log.i(folder.name + " remote uids=" + stats.uids_ms + " ms");
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
2020-09-02 08:12:43 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
// Delete local messages not at remote
|
|
|
|
Log.i(folder.name + " delete=" + uids.size());
|
|
|
|
for (Long uid : uids) {
|
2022-11-04 12:17:24 +00:00
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
|
|
|
Log.i(folder.name + " delete local uid=" + uid + " count=" + count);
|
2021-08-06 11:36:52 +00:00
|
|
|
}
|
2021-07-27 16:32:10 +00:00
|
|
|
|
2022-12-24 09:10:21 +00:00
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id, false);
|
2021-08-06 11:36:52 +00:00
|
|
|
|
|
|
|
fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
//fp.add(FetchProfile.Item.FLAGS);
|
|
|
|
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
|
|
|
//fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
|
|
|
|
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
fp.add(FetchProfile.Item.SIZE);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
|
|
|
|
if (account.isGmail())
|
|
|
|
fp.add(GmailFolder.FetchProfileItem.THRID);
|
|
|
|
|
|
|
|
// Add/update local messages
|
2022-02-01 18:47:26 +00:00
|
|
|
DutyCycle dc = new DutyCycle(account.name + " sync");
|
2021-08-06 11:36:52 +00:00
|
|
|
Log.i(folder.name + " add=" + imessages.length);
|
|
|
|
for (int i = imessages.length - 1; i >= 0; i -= SYNC_BATCH_SIZE) {
|
|
|
|
state.ensureRunning("Sync/IMAP/sync/fetch");
|
|
|
|
|
|
|
|
int from = Math.max(0, i - SYNC_BATCH_SIZE + 1);
|
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
|
|
|
|
|
|
|
// Full fetch new/changed messages only
|
|
|
|
List<Message> full = new ArrayList<>();
|
|
|
|
for (Message imessage : isub) {
|
|
|
|
long uid = ifolder.getUID(imessage); // already fetched
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
if (message == null)
|
|
|
|
full.add(imessage);
|
|
|
|
}
|
|
|
|
if (full.size() > 0) {
|
|
|
|
long headers = SystemClock.elapsedRealtime();
|
|
|
|
ifolder.fetch(full.toArray(new Message[0]), fp);
|
|
|
|
stats.headers += full.size();
|
|
|
|
stats.headers_ms += (SystemClock.elapsedRealtime() - headers);
|
|
|
|
Log.i(folder.name + " fetched headers=" + full.size() + " " + stats.headers_ms + " ms");
|
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
int free = Log.getFreeMemMb();
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("account", account.id + ":" + account.protocol);
|
|
|
|
crumb.put("folder", folder.id + ":" + folder.type);
|
|
|
|
crumb.put("start", Integer.toString(from));
|
|
|
|
crumb.put("end", Integer.toString(i));
|
|
|
|
crumb.put("partial", Boolean.toString(account.partial_fetch));
|
|
|
|
Log.breadcrumb("sync", crumb);
|
|
|
|
Log.i("Sync " + from + ".." + i + " free=" + free);
|
|
|
|
|
|
|
|
for (int j = isub.length - 1; j >= 0; j--) {
|
|
|
|
state.ensureRunning("Sync/IMAP/sync");
|
2021-04-07 06:42:47 +00:00
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
try {
|
2022-02-01 18:47:26 +00:00
|
|
|
dc.start();
|
|
|
|
|
2021-08-06 11:36:52 +00:00
|
|
|
// Some providers erroneously return old messages
|
|
|
|
if (full.contains(isub[j]))
|
2021-04-07 06:42:47 +00:00
|
|
|
try {
|
2021-08-06 11:36:52 +00:00
|
|
|
Date received = isub[j].getReceivedDate();
|
2022-05-01 06:57:13 +00:00
|
|
|
if (received == null || received.getTime() == 0)
|
|
|
|
received = isub[j].getSentDate();
|
2021-08-06 11:36:52 +00:00
|
|
|
boolean unseen = (sync_unseen && !isub[j].isSet(Flags.Flag.SEEN));
|
|
|
|
boolean flagged = (sync_flagged && isub[j].isSet(Flags.Flag.FLAGGED));
|
|
|
|
if (received != null && received.getTime() < keep_time && !unseen && !flagged) {
|
|
|
|
long uid = ifolder.getUID(isub[j]);
|
|
|
|
Log.i(folder.name + " Skipping old uid=" + uid + " date=" + received);
|
|
|
|
ids[from + j] = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
2021-04-07 06:42:47 +00:00
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-08-06 11:36:52 +00:00
|
|
|
|
|
|
|
EntityMessage message = synchronizeMessage(
|
|
|
|
context,
|
|
|
|
account, folder,
|
|
|
|
istore, ifolder, (MimeMessage) isub[j],
|
|
|
|
false, download && initialize == 0,
|
|
|
|
rules, state, stats);
|
|
|
|
ids[from + j] = (message == null || message.ui_hide ? null : message.id);
|
|
|
|
} catch (MessageRemovedException ex) {
|
2021-04-07 06:42:47 +00:00
|
|
|
Log.w(folder.name, ex);
|
2021-08-06 11:36:52 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
2021-04-07 06:42:47 +00:00
|
|
|
throw ex;
|
2021-08-06 11:36:52 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
|
|
|
Log.w(folder.name, ex);
|
2022-02-18 17:44:09 +00:00
|
|
|
modseq = null;
|
2021-08-06 11:36:52 +00:00
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
} else
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
2022-02-18 17:44:09 +00:00
|
|
|
modseq = null;
|
2021-08-06 11:36:52 +00:00
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
} finally {
|
|
|
|
// Free memory
|
|
|
|
isub[j] = null;
|
2022-02-05 20:21:33 +00:00
|
|
|
dc.stop(state.getForeground(), from == 0 && j == 0);
|
2021-08-06 11:36:52 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
2021-07-27 16:32:10 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
2021-08-06 15:53:52 +00:00
|
|
|
|
|
|
|
// Delete not synchronized messages without uid
|
|
|
|
if (!EntityFolder.isOutgoing(folder.type)) {
|
|
|
|
int orphans = db.message().deleteOrphans(folder.id, new Date().getTime());
|
|
|
|
Log.i(folder.name + " deleted orphans=" + orphans);
|
|
|
|
}
|
2021-08-06 11:36:52 +00:00
|
|
|
} else {
|
|
|
|
List<Long> _ids = new ArrayList<>();
|
2021-08-28 17:16:13 +00:00
|
|
|
List<Long> _uids = new ArrayList<>();
|
|
|
|
|
|
|
|
if (download && initialize == 0) {
|
|
|
|
List<EntityMessage> messages = db.message().getMessagesWithoutContent(
|
|
|
|
folder.id, sync_kept || force ? null : sync_time);
|
|
|
|
if (messages != null) {
|
|
|
|
Log.i(folder.name + " needs content=" + messages.size());
|
|
|
|
for (EntityMessage message : messages) {
|
2021-08-06 11:36:52 +00:00
|
|
|
_ids.add(message.id);
|
2021-08-28 17:16:13 +00:00
|
|
|
_uids.add(message.uid);
|
2021-08-06 11:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 17:16:13 +00:00
|
|
|
// This will result in message changed events
|
|
|
|
imessages = ifolder.getMessagesByUID(Helper.toLongArray(_uids));
|
2021-08-06 11:36:52 +00:00
|
|
|
ids = _ids.toArray(new Long[0]);
|
2021-08-28 17:16:13 +00:00
|
|
|
|
|
|
|
search = SystemClock.elapsedRealtime();
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 05:57:28 +00:00
|
|
|
// Update modseq
|
|
|
|
folder.modseq = modseq;
|
2022-02-18 17:44:09 +00:00
|
|
|
Log.i(folder.name + " set modseq=" + modseq);
|
2021-04-08 05:57:28 +00:00
|
|
|
db.folder().setFolderModSeq(folder.id, folder.modseq);
|
|
|
|
|
|
|
|
// Update stats
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2019-03-09 08:20:46 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2020-01-19 07:33:37 +00:00
|
|
|
account.last_connected = new Date().getTime();
|
|
|
|
db.account().setAccountConnected(account.id, account.last_connected);
|
2019-03-09 08:20:46 +00:00
|
|
|
|
2019-07-19 05:11:05 +00:00
|
|
|
if (download && initialize == 0) {
|
2019-02-27 15:05:15 +00:00
|
|
|
db.folder().setFolderSyncState(folder.id, "downloading");
|
|
|
|
|
|
|
|
// Download messages/attachments
|
2022-02-01 18:47:26 +00:00
|
|
|
DutyCycle dc = new DutyCycle(account.name + " download");
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " download=" + imessages.length);
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0; i -= DOWNLOAD_BATCH_SIZE) {
|
2021-07-31 04:57:54 +00:00
|
|
|
state.ensureRunning("Sync/IMAP/download/fetch");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-07-27 16:32:10 +00:00
|
|
|
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1);
|
2019-02-27 15:05:15 +00:00
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
2021-03-01 19:03:23 +00:00
|
|
|
Arrays.fill(imessages, from, i + 1, null);
|
2019-02-27 15:05:15 +00:00
|
|
|
// Fetch on demand
|
|
|
|
|
2019-05-22 13:41:54 +00:00
|
|
|
int free = Log.getFreeMemMb();
|
2019-05-16 20:06:13 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
2020-11-05 09:33:37 +00:00
|
|
|
crumb.put("account", account.id + ":" + account.protocol);
|
|
|
|
crumb.put("folder", folder.id + ":" + folder.type);
|
2019-05-16 20:06:13 +00:00
|
|
|
crumb.put("start", Integer.toString(from));
|
|
|
|
crumb.put("end", Integer.toString(i));
|
2019-10-19 10:20:37 +00:00
|
|
|
crumb.put("partial", Boolean.toString(account.partial_fetch));
|
2019-08-12 11:07:14 +00:00
|
|
|
Log.breadcrumb("download", crumb);
|
2019-05-16 20:06:13 +00:00
|
|
|
Log.i("Download " + from + ".." + i + " free=" + free);
|
|
|
|
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int j = isub.length - 1; j >= 0; j--) {
|
2021-07-31 04:57:54 +00:00
|
|
|
state.ensureRunning("Sync/IMAP/download");
|
2021-07-27 16:32:10 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
2022-02-01 18:47:26 +00:00
|
|
|
dc.start();
|
|
|
|
if (ids[from + j] != null)
|
|
|
|
downloadMessage(
|
2020-09-21 19:46:39 +00:00
|
|
|
context,
|
2019-10-10 07:35:07 +00:00
|
|
|
account, folder,
|
|
|
|
istore, ifolder,
|
2020-07-15 16:58:03 +00:00
|
|
|
(MimeMessage) isub[j], ids[from + j],
|
2020-09-03 13:50:33 +00:00
|
|
|
state, stats);
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
} finally {
|
|
|
|
// Free memory
|
2021-03-01 19:03:23 +00:00
|
|
|
isub[j] = null;
|
2022-02-05 20:21:33 +00:00
|
|
|
dc.stop(state.getForeground(), from == 0 && j == 0);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2021-07-27 16:32:10 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 05:11:05 +00:00
|
|
|
if (state.running && initialize != 0) {
|
|
|
|
jargs.put(4, 0);
|
2019-06-24 10:45:52 +00:00
|
|
|
folder.initialize = 0;
|
|
|
|
db.folder().setFolderInitialize(folder.id, 0);
|
2019-07-11 09:51:21 +00:00
|
|
|
|
|
|
|
// Schedule download
|
|
|
|
if (download) {
|
|
|
|
EntityOperation operation = new EntityOperation();
|
|
|
|
operation.account = folder.account;
|
|
|
|
operation.folder = folder.id;
|
|
|
|
operation.message = null;
|
|
|
|
operation.name = EntityOperation.SYNC;
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
}
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-07-24 20:01:55 +00:00
|
|
|
db.folder().setFolderLastSync(folder.id, new Date().getTime());
|
2021-02-28 08:42:02 +00:00
|
|
|
//db.folder().setFolderError(folder.id, null);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
stats.total = (SystemClock.elapsedRealtime() - search);
|
2020-07-25 05:33:58 +00:00
|
|
|
|
2021-08-16 07:36:23 +00:00
|
|
|
EntityLog.log(context, EntityLog.Type.Statistics,
|
2021-08-15 18:32:34 +00:00
|
|
|
account.name + "/" + folder.name + " sync stats " + stats);
|
2019-02-27 15:05:15 +00:00
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end sync state=" + state);
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static EntityMessage synchronizeMessage(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context,
|
2019-03-18 09:52:20 +00:00
|
|
|
EntityAccount account, EntityFolder folder,
|
2019-10-05 18:44:45 +00:00
|
|
|
IMAPStore istore, IMAPFolder ifolder, MimeMessage imessage,
|
2019-07-15 10:54:53 +00:00
|
|
|
boolean browsed, boolean download,
|
2020-07-15 16:58:03 +00:00
|
|
|
List<EntityRule> rules, State state, SyncStats stats) throws MessagingException, IOException {
|
2021-02-11 08:51:48 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-04-26 06:15:53 +00:00
|
|
|
boolean download_headers = prefs.getBoolean("download_headers", false);
|
2022-02-14 19:18:03 +00:00
|
|
|
boolean download_plain = prefs.getBoolean("download_plain", false);
|
2021-02-11 08:51:48 +00:00
|
|
|
boolean notify_known = prefs.getBoolean("notify_known", false);
|
2023-02-13 20:54:33 +00:00
|
|
|
boolean native_dkim = prefs.getBoolean("native_dkim", false);
|
2021-12-26 17:39:39 +00:00
|
|
|
boolean experiments = prefs.getBoolean("experiments", false);
|
2023-07-13 05:57:01 +00:00
|
|
|
boolean mdn = prefs.getBoolean("mdn", experiments);
|
2021-02-11 08:51:48 +00:00
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-09-26 08:53:27 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
long uid = ifolder.getUID(imessage);
|
2020-06-14 15:18:29 +00:00
|
|
|
if (uid < 0) {
|
2021-02-27 17:21:22 +00:00
|
|
|
Log.w(folder.name + " invalid uid=" + uid);
|
2020-06-14 15:18:29 +00:00
|
|
|
throw new MessageRemovedException("uid");
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (imessage.isExpunged()) {
|
2021-02-27 17:21:22 +00:00
|
|
|
Log.w(folder.name + " expunged uid=" + uid);
|
2019-09-17 11:38:23 +00:00
|
|
|
throw new MessageRemovedException("Expunged");
|
|
|
|
}
|
2021-10-24 12:39:49 +00:00
|
|
|
|
|
|
|
if (imessage.isSet(Flags.Flag.DELETED)) {
|
2021-02-27 17:21:22 +00:00
|
|
|
Log.w(folder.name + " deleted uid=" + uid);
|
2021-12-20 19:24:00 +00:00
|
|
|
if (expunge(context, ifolder, Arrays.asList(imessage)))
|
|
|
|
throw new MessageRemovedException("Deleted");
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage, context);
|
2022-05-28 09:25:08 +00:00
|
|
|
boolean recent = helper.getRecent();
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean seen = helper.getSeen();
|
2020-05-23 14:06:32 +00:00
|
|
|
boolean answered = helper.getAnswered();
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean flagged = helper.getFlagged();
|
2021-02-11 08:51:48 +00:00
|
|
|
boolean deleted = helper.getDeleted();
|
2019-09-17 11:38:23 +00:00
|
|
|
String flags = helper.getFlags();
|
|
|
|
String[] keywords = helper.getKeywords();
|
2020-06-25 07:14:05 +00:00
|
|
|
String[] labels = helper.getLabels();
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean update = false;
|
|
|
|
boolean process = false;
|
2020-12-28 08:23:42 +00:00
|
|
|
boolean syncSimilar = false;
|
2019-07-15 11:48:27 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
// Find message by uid (fast, no headers required)
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
|
|
|
|
// Find message by Message-ID (slow, headers required)
|
|
|
|
// - messages in inbox have same id as message sent to self
|
|
|
|
// - messages in archive have same id as original
|
2021-06-26 10:19:23 +00:00
|
|
|
boolean have = false;
|
2020-02-10 10:40:53 +00:00
|
|
|
Integer color = null;
|
2021-03-01 14:42:05 +00:00
|
|
|
String notes = null;
|
2021-04-08 07:40:21 +00:00
|
|
|
Integer notes_color = null;
|
2019-09-17 11:38:23 +00:00
|
|
|
if (message == null) {
|
|
|
|
String msgid = helper.getMessageID();
|
|
|
|
Log.i(folder.name + " searching for " + msgid);
|
2020-04-04 09:21:08 +00:00
|
|
|
List<EntityMessage> dups = db.message().getMessagesByMsgId(folder.account, msgid);
|
2023-08-27 06:40:24 +00:00
|
|
|
if (dups.size() == 0 &&
|
|
|
|
account.isOutlook() &&
|
|
|
|
EntityFolder.SENT.equals(folder.type)) {
|
|
|
|
String originalId = imessage.getHeader(MessageHelper.HEADER_MICROSOFT_ORIGINAL_MESSAGE_ID, null);
|
|
|
|
if (originalId != null) {
|
|
|
|
dups = db.message().getMessagesByMsgId(folder.account, originalId);
|
|
|
|
EntityLog.log(context, folder.name + " found with original ID" +
|
|
|
|
" msgid=" + msgid + " count=" + dups.size());
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 09:21:08 +00:00
|
|
|
for (EntityMessage dup : dups) {
|
2019-09-17 11:38:23 +00:00
|
|
|
EntityFolder dfolder = db.folder().getFolder(dup.folder);
|
|
|
|
Log.i(folder.name + " found as id=" + dup.id + "/" + dup.uid +
|
|
|
|
" folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id +
|
|
|
|
" msgid=" + dup.msgid + " thread=" + dup.thread);
|
|
|
|
|
2021-06-26 10:19:23 +00:00
|
|
|
if (!EntityFolder.JUNK.equals(dfolder.type))
|
|
|
|
have = true;
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (dup.folder.equals(folder.id)) {
|
2022-03-09 16:35:19 +00:00
|
|
|
String thread = helper.getThreadId(context, account.id, folder.id, uid, dup.received);
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " found as id=" + dup.id +
|
|
|
|
" uid=" + dup.uid + "/" + uid +
|
|
|
|
" msgid=" + msgid + " thread=" + thread);
|
|
|
|
|
2020-03-25 07:14:32 +00:00
|
|
|
if (dup.uid == null) {
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " set uid=" + uid);
|
|
|
|
dup.uid = uid;
|
2022-07-23 13:26:42 +00:00
|
|
|
if (dup.thread == null)
|
|
|
|
dup.thread = thread;
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2021-10-26 07:39:31 +00:00
|
|
|
if (EntityFolder.SENT.equals(folder.type) &&
|
|
|
|
(folder.auto_add == null || !folder.auto_add)) {
|
2020-07-22 16:46:57 +00:00
|
|
|
Long sent = helper.getSent();
|
|
|
|
Long received = helper.getReceived();
|
|
|
|
if (sent != null)
|
|
|
|
dup.sent = sent;
|
|
|
|
if (received != null)
|
|
|
|
dup.received = received;
|
2019-08-29 19:41:52 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
|
|
|
|
dup.error = null;
|
2020-03-25 07:14:15 +00:00
|
|
|
|
|
|
|
message = dup;
|
2020-03-25 07:14:32 +00:00
|
|
|
process = true;
|
2022-08-30 16:42:22 +00:00
|
|
|
} else if (msgid != null && EntityFolder.DRAFTS.equals(folder.type)) {
|
2022-08-29 15:37:12 +00:00
|
|
|
try {
|
2022-08-30 16:42:22 +00:00
|
|
|
if (dup.uid < uid) {
|
|
|
|
MimeMessage existing = (MimeMessage) ifolder.getMessageByUID(dup.uid);
|
|
|
|
if (existing != null &&
|
|
|
|
msgid.equals(existing.getHeader(MessageHelper.HEADER_CORRELATION_ID, null))) {
|
2023-09-13 05:43:37 +00:00
|
|
|
Log.w(folder.name + " late draft" +
|
2022-08-30 16:42:22 +00:00
|
|
|
" host=" + account.host + " uid=" + dup.uid + "<" + uid);
|
|
|
|
existing.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
expunge(context, ifolder, Arrays.asList(existing));
|
|
|
|
db.message().setMessageUiHide(dup.id, true);
|
|
|
|
}
|
|
|
|
} else if (dup.uid > uid) {
|
|
|
|
if (msgid.equals(imessage.getHeader(MessageHelper.HEADER_CORRELATION_ID, null))) {
|
2023-09-13 05:43:37 +00:00
|
|
|
Log.w(folder.name + " late draft" +
|
2022-08-30 16:42:22 +00:00
|
|
|
" host=" + account.host + " uid=" + dup.uid + ">" + uid);
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
expunge(context, ifolder, Arrays.asList(imessage));
|
|
|
|
return null;
|
|
|
|
}
|
2022-08-29 15:37:12 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2020-03-25 07:14:32 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-02-10 10:40:53 +00:00
|
|
|
|
2022-05-28 09:25:08 +00:00
|
|
|
if (dup.recent != recent || dup.seen != seen || dup.answered != answered || dup.flagged != flagged)
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
|
|
|
|
2020-02-10 10:40:53 +00:00
|
|
|
if (dup.flagged && dup.color != null)
|
|
|
|
color = dup.color;
|
2021-04-08 07:40:21 +00:00
|
|
|
if (dup.notes != null) {
|
2021-03-01 14:42:05 +00:00
|
|
|
notes = dup.notes;
|
2021-04-08 07:40:21 +00:00
|
|
|
notes_color = dup.notes_color;
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (message == null) {
|
2020-02-06 21:35:36 +00:00
|
|
|
Long sent = helper.getSent();
|
2020-07-22 16:46:57 +00:00
|
|
|
|
|
|
|
Long received;
|
2020-07-24 11:26:18 +00:00
|
|
|
long future = new Date().getTime() + FUTURE_RECEIVED;
|
2020-07-22 16:46:57 +00:00
|
|
|
if (account.use_date) {
|
|
|
|
received = sent;
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceived();
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceivedHeader();
|
|
|
|
} else if (account.use_received) {
|
|
|
|
received = helper.getReceivedHeader();
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceived();
|
2020-07-22 12:57:47 +00:00
|
|
|
} else {
|
2020-02-06 21:35:36 +00:00
|
|
|
received = helper.getReceived();
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceivedHeader();
|
2020-02-06 21:35:36 +00:00
|
|
|
}
|
2022-04-30 19:34:05 +00:00
|
|
|
if (received == null || received == 0)
|
|
|
|
received = sent;
|
2020-07-22 16:46:57 +00:00
|
|
|
if (received == null)
|
|
|
|
received = 0L;
|
2020-02-06 21:35:36 +00:00
|
|
|
|
2020-06-12 06:21:16 +00:00
|
|
|
String[] authentication = helper.getAuthentication();
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-09-17 11:38:23 +00:00
|
|
|
|
|
|
|
message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.uid = uid;
|
|
|
|
|
|
|
|
message.msgid = helper.getMessageID();
|
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
|
|
|
Log.w("No Message-ID id=" + message.id + " uid=" + message.uid);
|
|
|
|
|
2020-04-01 10:50:15 +00:00
|
|
|
message.hash = helper.getHash();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.references = TextUtils.join(" ", helper.getReferences());
|
|
|
|
message.inreplyto = helper.getInReplyTo();
|
|
|
|
// Local address contains control or whitespace in string ``mailing list someone@example.org''
|
|
|
|
message.deliveredto = helper.getDeliveredTo();
|
2022-03-09 16:35:19 +00:00
|
|
|
message.thread = helper.getThreadId(context, account.id, folder.id, uid, received);
|
2022-01-14 08:30:16 +00:00
|
|
|
if (BuildConfig.DEBUG && message.thread.startsWith("outlook:"))
|
|
|
|
message.warning = message.thread;
|
2019-09-30 14:55:58 +00:00
|
|
|
message.priority = helper.getPriority();
|
2022-01-18 09:07:13 +00:00
|
|
|
message.sensitivity = helper.getSensitivity();
|
2021-12-29 08:31:46 +00:00
|
|
|
|
|
|
|
for (String keyword : keywords)
|
|
|
|
if (MessageHelper.FLAG_LOW_IMPORTANCE.equals(keyword))
|
|
|
|
message.importance = EntityMessage.PRIORITIY_LOW;
|
|
|
|
else if (MessageHelper.FLAG_HIGH_IMPORTANCE.equals(keyword))
|
|
|
|
message.importance = EntityMessage.PRIORITIY_HIGH;
|
|
|
|
|
2020-10-02 13:54:45 +00:00
|
|
|
message.auto_submitted = helper.getAutoSubmitted();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.receipt_request = helper.getReceiptRequested();
|
|
|
|
message.receipt_to = helper.getReceiptTo();
|
2021-07-16 18:03:27 +00:00
|
|
|
message.bimi_selector = helper.getBimiSelector();
|
2022-01-06 21:46:11 +00:00
|
|
|
message.tls = helper.getTLS();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.dkim = MessageHelper.getAuthentication("dkim", authentication);
|
|
|
|
message.spf = MessageHelper.getAuthentication("spf", authentication);
|
2021-08-02 07:10:44 +00:00
|
|
|
if (message.spf == null && helper.getSPF())
|
|
|
|
message.spf = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.dmarc = MessageHelper.getAuthentication("dmarc", authentication);
|
2021-12-12 13:02:34 +00:00
|
|
|
message.smtp_from = helper.getMailFrom(authentication);
|
2021-02-01 10:54:43 +00:00
|
|
|
message.return_path = helper.getReturnPath();
|
2023-09-15 13:00:18 +00:00
|
|
|
message.submitter = helper.getSubmitter();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.from = helper.getFrom();
|
|
|
|
message.to = helper.getTo();
|
|
|
|
message.cc = helper.getCc();
|
|
|
|
message.bcc = helper.getBcc();
|
|
|
|
message.reply = helper.getReply();
|
|
|
|
message.list_post = helper.getListPost();
|
|
|
|
message.unsubscribe = helper.getListUnsubscribe();
|
2019-12-24 16:18:48 +00:00
|
|
|
message.autocrypt = helper.getAutocrypt();
|
2021-04-26 06:15:53 +00:00
|
|
|
if (download_headers)
|
|
|
|
message.headers = helper.getHeaders();
|
2021-11-09 17:44:54 +00:00
|
|
|
message.infrastructure = helper.getInfrastructure();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.subject = helper.getSubject();
|
2019-09-30 19:00:28 +00:00
|
|
|
message.size = parts.getBodySize();
|
|
|
|
message.total = helper.getSize();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.content = false;
|
2019-12-02 09:49:27 +00:00
|
|
|
message.encrypt = parts.getEncryption();
|
2020-01-18 10:28:37 +00:00
|
|
|
message.ui_encrypt = message.encrypt;
|
2020-02-06 21:35:36 +00:00
|
|
|
message.received = received;
|
2021-03-01 14:42:05 +00:00
|
|
|
message.notes = notes;
|
2021-04-08 07:40:21 +00:00
|
|
|
message.notes_color = notes_color;
|
2020-02-06 21:35:36 +00:00
|
|
|
message.sent = sent;
|
2022-05-28 09:25:08 +00:00
|
|
|
message.recent = recent;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.seen = seen;
|
|
|
|
message.answered = answered;
|
|
|
|
message.flagged = flagged;
|
2021-02-11 08:51:48 +00:00
|
|
|
message.deleted = deleted;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.flags = flags;
|
|
|
|
message.keywords = keywords;
|
2020-06-25 07:14:05 +00:00
|
|
|
message.labels = labels;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.ui_seen = seen;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
message.ui_flagged = flagged;
|
2021-02-11 08:51:48 +00:00
|
|
|
message.ui_deleted = deleted;
|
2019-09-27 16:25:55 +00:00
|
|
|
message.ui_hide = false;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.ui_found = false;
|
2022-01-10 08:38:17 +00:00
|
|
|
message.ui_ignored = (seen || deleted);
|
2019-09-17 11:38:23 +00:00
|
|
|
message.ui_browsed = browsed;
|
|
|
|
|
2020-02-10 10:40:53 +00:00
|
|
|
if (message.flagged)
|
|
|
|
message.color = color;
|
|
|
|
|
2021-04-25 10:52:19 +00:00
|
|
|
if (message.deliveredto != null)
|
|
|
|
try {
|
|
|
|
Address deliveredto = new InternetAddress(message.deliveredto);
|
|
|
|
if (MessageHelper.equalEmail(new Address[]{deliveredto}, message.to))
|
|
|
|
message.deliveredto = null;
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:13:34 +00:00
|
|
|
if (MessageHelper.equalEmail(message.submitter, message.from))
|
2020-01-30 17:24:46 +00:00
|
|
|
message.submitter = null;
|
|
|
|
|
2023-08-13 09:07:20 +00:00
|
|
|
if (native_dkim && !BuildConfig.PLAY_STORE_RELEASE) {
|
|
|
|
List<String> signers = helper.verifyDKIM(context);
|
|
|
|
message.signedby = (signers.size() == 0 ? null : TextUtils.join(",", signers));
|
|
|
|
if (Boolean.TRUE.equals(message.dkim)) {
|
|
|
|
if (signers.size() == 0)
|
|
|
|
message.dkim = false;
|
|
|
|
} else {
|
|
|
|
if (message.from != null)
|
|
|
|
for (Address from : message.from) {
|
|
|
|
String domain = UriHelper.getEmailDomain(((InternetAddress) from).getAddress());
|
|
|
|
if (domain != null && signers.contains(domain)) {
|
|
|
|
message.dkim = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 12:03:35 +00:00
|
|
|
// Borrow reply name from sender name
|
|
|
|
if (message.from != null && message.from.length == 1 &&
|
|
|
|
message.reply != null && message.reply.length == 1) {
|
2022-03-03 09:20:02 +00:00
|
|
|
InternetAddress from = (InternetAddress) message.from[0];
|
2019-09-26 12:03:35 +00:00
|
|
|
InternetAddress reply = (InternetAddress) message.reply[0];
|
2022-03-03 09:20:02 +00:00
|
|
|
if (TextUtils.isEmpty(reply.getPersonal()) &&
|
2022-03-03 11:28:59 +00:00
|
|
|
Objects.equals(from.getAddress(), reply.getAddress()))
|
2019-09-26 12:03:35 +00:00
|
|
|
reply.setPersonal(from.getPersonal());
|
|
|
|
}
|
|
|
|
|
2022-06-25 07:10:34 +00:00
|
|
|
if (helper.isReport() && EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
message.dsn = EntityMessage.DSN_HARD_BOUNCE;
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
message.identity = (identity == null ? null : identity.id);
|
|
|
|
|
2020-09-15 07:37:23 +00:00
|
|
|
message.sender = MessageHelper.getSortKey(EntityFolder.isOutgoing(folder.type) ? message.to : message.from);
|
2020-05-06 20:30:47 +00:00
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(message.from);
|
2019-09-17 11:38:23 +00:00
|
|
|
message.avatar = (lookupUri == null ? null : lookupUri.toString());
|
2020-06-07 14:32:14 +00:00
|
|
|
if (message.avatar == null && notify_known && pro)
|
2020-06-07 07:59:25 +00:00
|
|
|
message.ui_ignored = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2021-12-12 13:02:34 +00:00
|
|
|
message.from_domain = (message.checkFromDomain(context) == null);
|
|
|
|
|
2021-06-10 14:19:58 +00:00
|
|
|
// For contact forms
|
|
|
|
boolean self = false;
|
|
|
|
if (identity != null && message.from != null)
|
|
|
|
for (Address from : message.from)
|
|
|
|
if (identity.sameAddress(from) || identity.similarAddress(from)) {
|
|
|
|
self = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-28 06:27:40 +00:00
|
|
|
|
2021-06-10 14:19:58 +00:00
|
|
|
if (!self) {
|
2021-12-11 09:16:34 +00:00
|
|
|
String[] warning = message.checkReplyDomain(context);
|
2021-06-10 14:19:58 +00:00
|
|
|
message.reply_domain = (warning == null);
|
2020-12-24 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean check_mx = prefs.getBoolean("check_mx", false);
|
|
|
|
if (check_mx)
|
|
|
|
try {
|
2021-06-28 06:27:40 +00:00
|
|
|
Address[] addresses =
|
|
|
|
(message.reply == null || message.reply.length == 0
|
|
|
|
? message.from : message.reply);
|
2020-04-16 16:28:12 +00:00
|
|
|
DnsHelper.checkMx(context, addresses);
|
|
|
|
message.mx = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
} catch (UnknownHostException ex) {
|
2021-12-15 13:23:30 +00:00
|
|
|
Log.w(ex);
|
2019-09-17 11:38:23 +00:00
|
|
|
message.mx = false;
|
|
|
|
} catch (Throwable ex) {
|
2019-09-17 19:25:41 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-12-06 07:50:46 +00:00
|
|
|
message.warning = Log.formatThrowable(ex, false);
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-07-16 12:44:21 +00:00
|
|
|
|
2021-09-04 18:41:18 +00:00
|
|
|
boolean check_blocklist = prefs.getBoolean("check_blocklist", false);
|
|
|
|
if (check_blocklist) {
|
|
|
|
if (!have &&
|
|
|
|
!EntityFolder.isOutgoing(folder.type) &&
|
|
|
|
!EntityFolder.ARCHIVE.equals(folder.type) &&
|
|
|
|
!EntityFolder.TRASH.equals(folder.type) &&
|
|
|
|
!EntityFolder.JUNK.equals(folder.type) &&
|
2022-09-19 13:35:26 +00:00
|
|
|
!message.isNotJunk(context) &&
|
2021-09-04 18:41:18 +00:00
|
|
|
!Arrays.asList(message.keywords).contains(MessageHelper.FLAG_NOT_JUNK))
|
|
|
|
try {
|
|
|
|
message.blocklist = DnsBlockList.isJunk(context,
|
|
|
|
imessage.getHeader("Received"));
|
|
|
|
|
|
|
|
if (message.blocklist == null || !message.blocklist) {
|
|
|
|
List<Address> senders = new ArrayList<>();
|
|
|
|
if (message.reply != null)
|
|
|
|
senders.addAll(Arrays.asList(message.reply));
|
|
|
|
if (message.from != null)
|
|
|
|
senders.addAll(Arrays.asList(message.from));
|
|
|
|
message.blocklist = DnsBlockList.isJunk(context, senders);
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(folder.name, ex);
|
2021-07-18 19:05:00 +00:00
|
|
|
}
|
2021-09-04 18:41:18 +00:00
|
|
|
}
|
2021-06-19 08:00:38 +00:00
|
|
|
|
2022-01-10 07:12:51 +00:00
|
|
|
boolean needsHeaders = EntityRule.needsHeaders(message, rules);
|
|
|
|
boolean needsBody = EntityRule.needsBody(message, rules);
|
2021-12-11 15:59:15 +00:00
|
|
|
if (needsHeaders || needsBody)
|
|
|
|
Log.i(folder.name + " needs headers=" + needsHeaders + " body=" + needsBody);
|
|
|
|
List<Header> headers = (needsHeaders ? helper.getAllHeaders() : null);
|
2022-02-14 19:18:03 +00:00
|
|
|
String body = (needsBody ? parts.getHtml(context, download_plain) : null);
|
2021-12-11 15:59:15 +00:00
|
|
|
|
2023-07-13 05:57:01 +00:00
|
|
|
if (mdn && helper.isReport())
|
2021-12-26 17:39:39 +00:00
|
|
|
try {
|
|
|
|
MessageHelper.Report r = parts.getReport();
|
2022-06-25 06:28:17 +00:00
|
|
|
boolean client_id = prefs.getBoolean("client_id", true);
|
|
|
|
String we = "dns;" + (client_id ? EmailService.getDefaultEhlo() : "example.com");
|
|
|
|
if (r != null && !we.equals(r.reporter)) {
|
2021-12-28 08:40:05 +00:00
|
|
|
String label = null;
|
2021-12-26 17:39:39 +00:00
|
|
|
if (r.isDeliveryStatus())
|
2021-12-29 08:10:43 +00:00
|
|
|
label = (r.isDelivered() ? MessageHelper.FLAG_DELIVERED : MessageHelper.FLAG_NOT_DELIVERED);
|
2021-12-26 17:39:39 +00:00
|
|
|
else if (r.isDispositionNotification())
|
2022-02-24 17:02:37 +00:00
|
|
|
label = (r.isMdnDisplayed() ? MessageHelper.FLAG_DISPLAYED : MessageHelper.FLAG_NOT_DISPLAYED);
|
2022-06-25 17:41:26 +00:00
|
|
|
else if (r.isFeedbackReport())
|
|
|
|
label = MessageHelper.FLAG_COMPLAINT;
|
2021-12-28 08:40:05 +00:00
|
|
|
|
|
|
|
if (label != null) {
|
|
|
|
Map<Long, EntityFolder> map = new HashMap<>();
|
|
|
|
|
|
|
|
EntityFolder s = db.folder().getFolderByType(folder.account, EntityFolder.SENT);
|
|
|
|
if (s != null)
|
|
|
|
map.put(s.id, s);
|
|
|
|
|
2022-05-12 06:53:59 +00:00
|
|
|
List<EntityMessage> all = new ArrayList<>();
|
|
|
|
|
2022-06-27 17:29:49 +00:00
|
|
|
if (message.inreplyto != null) {
|
|
|
|
List<EntityMessage> replied = db.message().getMessagesByMsgId(folder.account, message.inreplyto);
|
|
|
|
if (replied != null)
|
|
|
|
all.addAll(replied);
|
|
|
|
}
|
2023-01-08 09:32:40 +00:00
|
|
|
if (r.refid != null && !r.refid.equals(message.inreplyto)) {
|
2022-05-12 06:53:59 +00:00
|
|
|
List<EntityMessage> refs = db.message().getMessagesByMsgId(folder.account, r.refid);
|
|
|
|
if (refs != null)
|
|
|
|
all.addAll(refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (EntityMessage m : all)
|
|
|
|
if (!map.containsKey(m.folder)) {
|
|
|
|
EntityFolder f = db.folder().getFolder(m.folder);
|
|
|
|
if (f != null)
|
|
|
|
map.put(f.id, f);
|
|
|
|
}
|
2021-12-28 08:40:05 +00:00
|
|
|
|
2023-01-08 09:32:40 +00:00
|
|
|
for (String msgid : new String[]{message.inreplyto, r.refid})
|
|
|
|
if (msgid != null)
|
|
|
|
for (EntityFolder f : map.values())
|
|
|
|
EntityOperation.queue(context, f, EntityOperation.REPORT, msgid, label);
|
2021-12-28 08:40:05 +00:00
|
|
|
}
|
2021-12-26 17:39:39 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2021-01-27 14:48:49 +00:00
|
|
|
message.notifying = EntityMessage.NOTIFYING_IGNORE;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
|
|
|
Log.i(folder.name + " added id=" + message.id + " uid=" + message.uid);
|
|
|
|
|
|
|
|
int sequence = 1;
|
2019-10-13 14:26:37 +00:00
|
|
|
List<EntityAttachment> attachments = parts.getAttachments();
|
|
|
|
for (EntityAttachment attachment : attachments) {
|
2019-09-21 11:18:48 +00:00
|
|
|
Log.i(folder.name + " attachment seq=" + sequence + " " + attachment);
|
2019-09-17 11:38:23 +00:00
|
|
|
attachment.message = message.id;
|
|
|
|
attachment.sequence = sequence++;
|
|
|
|
attachment.id = db.attachment().insertAttachment(attachment);
|
|
|
|
}
|
2019-06-13 18:45:35 +00:00
|
|
|
|
2021-12-08 21:46:05 +00:00
|
|
|
runRules(context, headers, body, account, folder, message, rules);
|
2021-06-20 07:04:12 +00:00
|
|
|
|
2021-07-06 12:42:03 +00:00
|
|
|
if (message.blocklist != null && message.blocklist) {
|
2021-06-20 07:04:12 +00:00
|
|
|
boolean use_blocklist = prefs.getBoolean("use_blocklist", false);
|
|
|
|
if (use_blocklist) {
|
2021-08-16 11:40:42 +00:00
|
|
|
EntityLog.log(context, EntityLog.Type.General, message,
|
|
|
|
"Block list" +
|
|
|
|
" folder=" + folder.name +
|
|
|
|
" message=" + message.id +
|
|
|
|
"@" + new Date(message.received) +
|
|
|
|
":" + message.subject);
|
2021-06-20 07:04:12 +00:00
|
|
|
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
|
2021-06-20 07:22:20 +00:00
|
|
|
if (junk != null) {
|
2021-06-20 07:04:12 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, junk.id, false);
|
2021-06-20 07:22:20 +00:00
|
|
|
message.ui_hide = true;
|
|
|
|
}
|
2021-06-20 07:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 09:07:11 +00:00
|
|
|
if (download && !message.ui_hide &&
|
2021-01-13 09:22:52 +00:00
|
|
|
MessageClassifier.isEnabled(context) && folder.auto_classify_source)
|
2021-01-27 14:48:49 +00:00
|
|
|
db.message().setMessageUiHide(message.id, true); // keep local value
|
2019-11-22 12:50:16 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
db.setTransactionSuccessful();
|
2019-09-21 08:05:24 +00:00
|
|
|
} catch (SQLiteConstraintException ex) {
|
2022-05-11 12:38:08 +00:00
|
|
|
Log.i(ex);
|
2019-09-21 08:05:24 +00:00
|
|
|
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("folder", message.account + ":" + message.folder + ":" + folder.type);
|
|
|
|
crumb.put("message", uid + ":" + message.uid);
|
|
|
|
crumb.put("what", ex.getMessage());
|
|
|
|
Log.breadcrumb("insert", crumb);
|
2019-10-09 13:43:14 +00:00
|
|
|
|
|
|
|
return null;
|
2019-09-17 11:38:23 +00:00
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-01-27 14:48:49 +00:00
|
|
|
try {
|
2021-09-18 16:15:44 +00:00
|
|
|
EntityContact.received(context, account, folder, message);
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-12-28 08:44:13 +00:00
|
|
|
if (body == null && helper.isReport())
|
2022-02-14 19:18:03 +00:00
|
|
|
body = parts.getHtml(context, download_plain);
|
2021-12-28 08:44:13 +00:00
|
|
|
|
2021-01-27 14:48:49 +00:00
|
|
|
// Download small messages inline
|
2021-12-08 21:46:05 +00:00
|
|
|
if (body != null || (download && !message.ui_hide)) {
|
2021-01-27 14:48:49 +00:00
|
|
|
long maxSize;
|
|
|
|
if (state == null || state.networkState.isUnmetered())
|
2020-09-13 12:38:36 +00:00
|
|
|
maxSize = MessageHelper.SMALL_MESSAGE_SIZE;
|
2021-01-27 14:48:49 +00:00
|
|
|
else {
|
|
|
|
maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE);
|
|
|
|
if (maxSize == 0 || maxSize > MessageHelper.SMALL_MESSAGE_SIZE)
|
|
|
|
maxSize = MessageHelper.SMALL_MESSAGE_SIZE;
|
2021-01-15 17:22:18 +00:00
|
|
|
}
|
2021-01-27 14:48:49 +00:00
|
|
|
|
2021-12-08 21:46:05 +00:00
|
|
|
if (body != null ||
|
|
|
|
(message.size != null && message.size < maxSize) ||
|
2023-08-30 08:33:58 +00:00
|
|
|
account.isWpPl() ||
|
2021-01-27 14:48:49 +00:00
|
|
|
(MessageClassifier.isEnabled(context)) && folder.auto_classify_source)
|
|
|
|
try {
|
2021-12-08 21:46:05 +00:00
|
|
|
if (body == null)
|
2022-02-14 19:18:03 +00:00
|
|
|
body = parts.getHtml(context, download_plain);
|
2021-01-27 14:48:49 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
|
|
|
String text = HtmlHelper.getFullText(body);
|
2022-10-22 12:50:39 +00:00
|
|
|
message.content = true;
|
2021-01-27 14:48:49 +00:00
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
|
|
|
db.message().setMessageContent(message.id,
|
2022-10-22 12:50:39 +00:00
|
|
|
message.content,
|
2021-01-27 14:48:49 +00:00
|
|
|
message.language,
|
2022-02-14 19:18:03 +00:00
|
|
|
parts.isPlainOnly(download_plain),
|
2021-01-27 14:48:49 +00:00
|
|
|
message.preview,
|
|
|
|
parts.getWarnings(message.warning));
|
2022-04-28 11:59:46 +00:00
|
|
|
MessageClassifier.classify(message, folder, true, context);
|
2021-01-27 14:48:49 +00:00
|
|
|
|
|
|
|
if (stats != null && body != null)
|
|
|
|
stats.content += body.length();
|
|
|
|
Log.i(folder.name + " inline downloaded message id=" + message.id +
|
|
|
|
" size=" + message.size + "/" + (body == null ? null : body.length()));
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(body) && parts.hasBody())
|
|
|
|
reportEmptyMessage(context, state, account, istore);
|
|
|
|
} finally {
|
|
|
|
if (!message.ui_hide)
|
|
|
|
db.message().setMessageUiHide(message.id, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.message().setMessageNotifying(message.id, 0);
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2021-01-04 08:32:22 +00:00
|
|
|
|
|
|
|
reportNewMessage(context, account, folder, message);
|
2019-09-17 11:38:23 +00:00
|
|
|
} else {
|
|
|
|
if (process) {
|
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
if (identity != null &&
|
|
|
|
(message.identity == null || !message.identity.equals(identity.id))) {
|
|
|
|
message.identity = identity.id;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " identity=" + identity.id);
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2022-05-28 09:25:08 +00:00
|
|
|
if (!message.recent.equals(recent)) {
|
|
|
|
update = true;
|
|
|
|
message.recent = recent;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " recent=" + recent);
|
|
|
|
syncSimilar = true;
|
|
|
|
}
|
|
|
|
|
2021-11-14 07:06:36 +00:00
|
|
|
if ((!message.seen.equals(seen) ||
|
|
|
|
(!folder.read_only && !message.ui_seen.equals(seen))) &&
|
2019-09-17 11:38:23 +00:00
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.SEEN) == 0) {
|
|
|
|
update = true;
|
|
|
|
message.seen = seen;
|
|
|
|
message.ui_seen = seen;
|
|
|
|
if (seen)
|
|
|
|
message.ui_ignored = true;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " seen=" + seen);
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-11-14 07:06:36 +00:00
|
|
|
if ((!message.answered.equals(answered) ||
|
|
|
|
(!folder.read_only && !message.ui_answered.equals(message.answered))) &&
|
2019-09-25 19:23:33 +00:00
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.ANSWERED) == 0) {
|
2019-09-17 11:38:23 +00:00
|
|
|
update = true;
|
|
|
|
message.answered = answered;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " answered=" + answered);
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-11-14 07:06:36 +00:00
|
|
|
if ((!message.flagged.equals(flagged) ||
|
|
|
|
(!folder.read_only && !message.ui_flagged.equals(flagged))) &&
|
2019-09-17 11:38:23 +00:00
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.FLAG) == 0) {
|
|
|
|
update = true;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.ui_flagged = flagged;
|
2019-09-28 18:25:19 +00:00
|
|
|
if (!flagged)
|
|
|
|
message.color = null;
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " flagged=" + flagged);
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-02-11 08:51:48 +00:00
|
|
|
if ((!message.deleted.equals(deleted) || !message.ui_deleted.equals(deleted)) &&
|
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.DELETE) == 0) {
|
|
|
|
update = true;
|
|
|
|
message.deleted = deleted;
|
|
|
|
message.ui_deleted = deleted;
|
2022-01-10 08:38:17 +00:00
|
|
|
message.ui_ignored = deleted;
|
2021-02-11 08:51:48 +00:00
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " deleted=" + deleted);
|
|
|
|
syncSimilar = true;
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (!Objects.equals(flags, message.flags)) {
|
|
|
|
update = true;
|
|
|
|
message.flags = flags;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " flags=" + flags);
|
|
|
|
}
|
2019-05-06 17:19:37 +00:00
|
|
|
|
2020-09-17 15:42:08 +00:00
|
|
|
if (!Helper.equal(message.keywords, keywords) &&
|
2021-11-13 15:20:00 +00:00
|
|
|
!folder.read_only &&
|
2023-04-18 16:34:26 +00:00
|
|
|
(ifolder.getPermanentFlags().contains(Flags.Flag.USER) || keywords.length > 0)) {
|
2019-09-17 11:38:23 +00:00
|
|
|
update = true;
|
|
|
|
message.keywords = keywords;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid +
|
|
|
|
" keywords=" + TextUtils.join(" ", keywords));
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-06-25 07:14:05 +00:00
|
|
|
if (!Helper.equal(message.labels, labels)) {
|
|
|
|
update = true;
|
|
|
|
message.labels = labels;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid +
|
|
|
|
" labels=" + (labels == null ? null : TextUtils.join(" ", labels)));
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:12:29 +00:00
|
|
|
if (download_headers && message.headers == null) {
|
|
|
|
update = true;
|
|
|
|
message.headers = helper.getHeaders();
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " headers");
|
|
|
|
}
|
|
|
|
|
2020-04-01 14:05:45 +00:00
|
|
|
if (message.hash == null || process) {
|
2020-04-01 10:50:15 +00:00
|
|
|
update = true;
|
|
|
|
message.hash = helper.getHash();
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " hash=" + message.hash);
|
2020-04-01 14:17:54 +00:00
|
|
|
|
|
|
|
// Update archive to prevent visible > 1
|
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
for (EntityMessage dup : db.message().getMessagesByMsgId(message.account, message.msgid))
|
|
|
|
db.message().setMessageHash(dup.id, message.hash);
|
2020-04-01 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 09:29:46 +00:00
|
|
|
if (message.ui_hide &&
|
|
|
|
(message.ui_busy == null || message.ui_busy < new Date().getTime()) &&
|
2020-11-10 18:14:21 +00:00
|
|
|
db.operation().getOperationCount(folder.id, message.id) == 0 &&
|
|
|
|
db.operation().getOperationCount(folder.id, EntityOperation.PURGE) == 0) {
|
2019-09-17 11:38:23 +00:00
|
|
|
update = true;
|
2019-09-27 16:25:55 +00:00
|
|
|
message.ui_hide = false;
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " unhide");
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (message.ui_browsed != browsed) {
|
|
|
|
update = true;
|
|
|
|
message.ui_browsed = browsed;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " browsed=" + browsed);
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2020-05-06 20:30:47 +00:00
|
|
|
Uri uri = ContactInfo.getLookupUri(message.from);
|
2020-04-15 18:26:04 +00:00
|
|
|
if (uri != null) {
|
|
|
|
String avatar = uri.toString();
|
|
|
|
if (!Objects.equals(message.avatar, avatar)) {
|
|
|
|
update = true;
|
|
|
|
message.avatar = avatar;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " avatar=" + avatar);
|
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-12-11 15:59:15 +00:00
|
|
|
if (update || process) {
|
2022-01-17 21:23:33 +00:00
|
|
|
boolean needsHeaders = (process && EntityRule.needsHeaders(message, rules));
|
|
|
|
boolean needsBody = (process && EntityRule.needsBody(message, rules));
|
2021-12-11 15:59:15 +00:00
|
|
|
if (needsHeaders || needsBody)
|
|
|
|
Log.i(folder.name + " needs headers=" + needsHeaders + " body=" + needsBody);
|
|
|
|
List<Header> headers = (needsHeaders ? helper.getAllHeaders() : null);
|
2022-02-14 19:18:03 +00:00
|
|
|
String body = (needsBody ? helper.getMessageParts().getHtml(context, download_plain) : null);
|
2021-12-11 15:59:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-06-13 18:45:35 +00:00
|
|
|
|
2022-04-26 12:15:01 +00:00
|
|
|
EntityMessage existing = db.message().getMessage(message.id);
|
|
|
|
if (existing != null) {
|
|
|
|
message.revision = existing.revision;
|
|
|
|
message.revisions = existing.revisions;
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
db.message().updateMessage(message);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-01-04 08:32:22 +00:00
|
|
|
if (process)
|
2021-12-08 21:46:05 +00:00
|
|
|
runRules(context, headers, body, account, folder, message, rules);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2021-12-11 15:59:15 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-01-02 14:57:24 +00:00
|
|
|
if (process) {
|
2021-09-18 16:15:44 +00:00
|
|
|
EntityContact.received(context, account, folder, message);
|
2022-04-28 11:59:46 +00:00
|
|
|
MessageClassifier.classify(message, folder, true, context);
|
2021-01-02 14:57:24 +00:00
|
|
|
} else
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d(folder.name + " unchanged uid=" + uid);
|
2021-01-04 08:32:22 +00:00
|
|
|
|
|
|
|
if (process)
|
|
|
|
reportNewMessage(context, account, folder, message);
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-06-14 09:25:56 +00:00
|
|
|
|
2020-12-28 08:23:42 +00:00
|
|
|
if (syncSimilar && account.isGmail())
|
2022-12-14 16:00:43 +00:00
|
|
|
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid, message.hash)) {
|
2022-05-28 09:25:08 +00:00
|
|
|
if (similar.recent != message.recent) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " recent=" + message.recent);
|
|
|
|
db.message().setMessageRecent(similar.id, message.recent);
|
|
|
|
}
|
|
|
|
|
2020-12-28 08:23:42 +00:00
|
|
|
if (similar.seen != message.seen) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " seen=" + message.seen);
|
|
|
|
db.message().setMessageSeen(similar.id, message.seen);
|
|
|
|
db.message().setMessageUiSeen(similar.id, message.seen);
|
|
|
|
}
|
2021-10-06 10:29:13 +00:00
|
|
|
|
2020-12-28 08:23:42 +00:00
|
|
|
if (similar.answered != message.answered) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " answered=" + message.answered);
|
|
|
|
db.message().setMessageAnswered(similar.id, message.answered);
|
2021-10-06 10:29:13 +00:00
|
|
|
db.message().setMessageUiAnswered(similar.id, message.answered);
|
2020-12-28 08:23:42 +00:00
|
|
|
}
|
2021-10-06 10:29:13 +00:00
|
|
|
|
2020-12-28 08:23:42 +00:00
|
|
|
if (similar.flagged != flagged) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " flagged=" + message.flagged);
|
|
|
|
db.message().setMessageFlagged(similar.id, message.flagged);
|
|
|
|
db.message().setMessageUiFlagged(similar.id, message.flagged, flagged ? similar.color : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
List<String> fkeywords = new ArrayList<>(Arrays.asList(folder.keywords));
|
2019-06-14 09:25:56 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
for (String keyword : keywords)
|
|
|
|
if (!fkeywords.contains(keyword)) {
|
|
|
|
Log.i(folder.name + " adding keyword=" + keyword);
|
|
|
|
fkeywords.add(keyword);
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (folder.keywords.length != fkeywords.size()) {
|
|
|
|
Collections.sort(fkeywords);
|
|
|
|
db.folder().setFolderKeywords(folder.id, DB.Converters.fromStringArray(fkeywords.toArray(new String[0])));
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
|
|
|
|
return message;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 12:39:49 +00:00
|
|
|
private static boolean expunge(Context context, IMAPFolder ifolder, List<Message> messages) {
|
|
|
|
if (messages.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
2021-10-25 14:40:17 +00:00
|
|
|
boolean uid_expunge = prefs.getBoolean("uid_expunge", false);
|
|
|
|
|
2021-10-24 12:39:49 +00:00
|
|
|
if (!perform_expunge)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
2021-10-25 14:40:17 +00:00
|
|
|
if (uid_expunge)
|
|
|
|
uid_expunge = MessageHelper.hasCapability(ifolder, "UIDPLUS");
|
2022-01-28 11:21:20 +00:00
|
|
|
if (MessageHelper.hasCapability(ifolder, "X-UIDONLY"))
|
|
|
|
uid_expunge = true;
|
2021-10-25 14:40:17 +00:00
|
|
|
|
|
|
|
if (uid_expunge) {
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(messages.toArray(new Message[0]), fp);
|
|
|
|
|
2021-10-25 06:58:11 +00:00
|
|
|
List<Long> uids = new ArrayList<>();
|
|
|
|
for (Message m : messages)
|
|
|
|
try {
|
2021-10-25 14:40:17 +00:00
|
|
|
long uid = ifolder.getUID(m);
|
|
|
|
if (uid < 0)
|
|
|
|
continue;
|
|
|
|
uids.add(uid);
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
2021-10-25 06:58:11 +00:00
|
|
|
}
|
2021-10-25 14:40:17 +00:00
|
|
|
|
2021-10-25 06:58:11 +00:00
|
|
|
Log.i(ifolder.getName() + " expunging " + TextUtils.join(",", uids));
|
2022-03-26 14:22:49 +00:00
|
|
|
uidExpunge(context, ifolder, uids);
|
2021-10-25 06:58:11 +00:00
|
|
|
Log.i(ifolder.getName() + " expunged " + TextUtils.join(",", uids));
|
|
|
|
} else {
|
|
|
|
Log.i(ifolder.getName() + " expunging all");
|
2021-10-24 12:39:49 +00:00
|
|
|
ifolder.expunge();
|
2021-10-25 06:58:11 +00:00
|
|
|
Log.i(ifolder.getName() + " expunged all");
|
|
|
|
}
|
|
|
|
|
2021-10-24 12:39:49 +00:00
|
|
|
return true;
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
// NO EXPUNGE failed.
|
|
|
|
Log.w(ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 14:22:49 +00:00
|
|
|
private static void uidExpunge(Context context, IMAPFolder ifolder, List<Long> uids) throws MessagingException {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
int chunk_size = prefs.getInt("chunk_size", DEFAULT_CHUNK_SIZE);
|
|
|
|
|
|
|
|
ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc4315#section-2.1
|
|
|
|
for (List<Long> list : Helper.chunkList(uids, chunk_size))
|
|
|
|
protocol.uidexpunge(UIDSet.createUIDSets(Helper.toLongArray(list)));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
private static EntityIdentity matchIdentity(Context context, EntityFolder folder, EntityMessage message) {
|
2020-08-01 06:05:06 +00:00
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
return null;
|
|
|
|
|
2023-06-18 17:03:29 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
int level = prefs.getInt("log_level", android.util.Log.INFO);
|
|
|
|
boolean log = (level <= android.util.Log.INFO || BuildConfig.DEBUG);
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
List<Address> addresses = new ArrayList<>();
|
|
|
|
if (folder.isOutgoing()) {
|
|
|
|
if (message.from != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.from));
|
|
|
|
} else {
|
|
|
|
if (message.to != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.to));
|
|
|
|
if (message.cc != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.cc));
|
2019-10-13 14:37:38 +00:00
|
|
|
if (message.bcc != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.bcc));
|
2020-02-12 06:48:40 +00:00
|
|
|
if (message.from != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.from));
|
2019-06-13 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 14:57:12 +00:00
|
|
|
InternetAddress deliveredto = null;
|
|
|
|
if (message.deliveredto != null)
|
|
|
|
try {
|
|
|
|
deliveredto = new InternetAddress(message.deliveredto);
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
// Search for matching identity
|
2021-04-25 10:59:11 +00:00
|
|
|
List<EntityIdentity> identities = getIdentities(folder.account, context);
|
2019-09-22 18:03:31 +00:00
|
|
|
if (identities != null) {
|
|
|
|
for (Address address : addresses)
|
|
|
|
for (EntityIdentity identity : identities)
|
2023-06-18 17:03:29 +00:00
|
|
|
if (identity.sameAddress(address)) {
|
|
|
|
if (log)
|
|
|
|
Log.i("Matched same" +
|
|
|
|
" identity=" + identity.email +
|
|
|
|
" address=" + ((InternetAddress) address).getAddress() +
|
|
|
|
" folder=" + folder.name);
|
2019-09-22 18:03:31 +00:00
|
|
|
return identity;
|
2023-06-18 17:03:29 +00:00
|
|
|
}
|
2019-09-22 18:03:31 +00:00
|
|
|
|
|
|
|
for (Address address : addresses)
|
|
|
|
for (EntityIdentity identity : identities)
|
2023-06-18 17:03:29 +00:00
|
|
|
if (identity.similarAddress(address)) {
|
|
|
|
if (log)
|
|
|
|
Log.i("Matched similar" +
|
|
|
|
" identity=" + identity.email +
|
|
|
|
" regex=" + identity.sender_extra_regex +
|
|
|
|
" address=" + ((InternetAddress) address).getAddress() +
|
|
|
|
" folder=" + folder.name);
|
2019-09-22 18:03:31 +00:00
|
|
|
return identity;
|
2023-06-18 17:03:29 +00:00
|
|
|
}
|
2020-12-13 14:57:12 +00:00
|
|
|
|
|
|
|
if (deliveredto != null)
|
|
|
|
for (EntityIdentity identity : identities)
|
2023-06-18 17:03:29 +00:00
|
|
|
if (identity.sameAddress(deliveredto) || identity.similarAddress(deliveredto)) {
|
|
|
|
if (log)
|
|
|
|
Log.i("Matched deliveredto" +
|
|
|
|
" identity=" + identity.email +
|
|
|
|
" regex=" + identity.sender_extra_regex +
|
|
|
|
" address=" + ((InternetAddress) deliveredto).getAddress() +
|
|
|
|
" folder=" + folder.name);
|
2020-12-13 14:57:12 +00:00
|
|
|
return identity;
|
2023-06-18 17:03:29 +00:00
|
|
|
}
|
2019-06-13 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 17:03:29 +00:00
|
|
|
if (log)
|
|
|
|
Log.i("Matched none" +
|
|
|
|
" addresses=" + MessageHelper.formatAddresses(addresses.toArray(new Address[0])) +
|
|
|
|
" deliveredto=" + (deliveredto == null ? null : ((InternetAddress) deliveredto).getAddress()) +
|
|
|
|
" folder=" + folder.name);
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-05 19:13:05 +00:00
|
|
|
private static void runRules(
|
2021-12-08 21:46:05 +00:00
|
|
|
Context context, List<Header> headers, String html,
|
2020-05-05 19:13:05 +00:00
|
|
|
EntityAccount account, EntityFolder folder, EntityMessage message,
|
|
|
|
List<EntityRule> rules) {
|
2019-10-13 14:26:37 +00:00
|
|
|
|
2022-03-07 07:21:19 +00:00
|
|
|
if (EntityFolder.INBOX.equals(folder.type)) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
String mnemonic = prefs.getString("wipe_mnemonic", null);
|
|
|
|
if (mnemonic != null && message.subject != null &&
|
|
|
|
message.subject.toLowerCase(Locale.ROOT).contains(mnemonic))
|
|
|
|
Helper.clearAll(context);
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:52:56 +00:00
|
|
|
if (account.protocol == EntityAccount.TYPE_IMAP && folder.read_only)
|
|
|
|
return;
|
2023-02-27 20:47:14 +00:00
|
|
|
|
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-03-31 06:48:42 +00:00
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
2021-07-29 07:11:51 +00:00
|
|
|
boolean executed = false;
|
2023-04-01 18:15:46 +00:00
|
|
|
if (pro) {
|
|
|
|
int applied = EntityRule.run(context, rules, message, headers, html);
|
|
|
|
executed = (applied > 0);
|
|
|
|
}
|
2021-09-18 16:15:44 +00:00
|
|
|
|
2022-01-30 15:42:12 +00:00
|
|
|
if (EntityFolder.INBOX.equals(folder.type))
|
2022-03-26 11:39:21 +00:00
|
|
|
if (message.from != null) {
|
|
|
|
EntityContact badboy = null;
|
2022-01-30 15:42:12 +00:00
|
|
|
for (Address from : message.from) {
|
|
|
|
String email = ((InternetAddress) from).getAddress();
|
|
|
|
if (TextUtils.isEmpty(email))
|
|
|
|
continue;
|
2022-01-29 12:22:53 +00:00
|
|
|
|
2022-03-26 11:39:21 +00:00
|
|
|
badboy = db.contact().getContact(message.account, EntityContact.TYPE_JUNK, email);
|
|
|
|
if (badboy != null)
|
2022-01-30 15:42:12 +00:00
|
|
|
break;
|
2022-03-26 11:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (badboy != null) {
|
|
|
|
badboy.times_contacted++;
|
|
|
|
badboy.last_contacted = new Date().getTime();
|
|
|
|
db.contact().updateContact(badboy);
|
|
|
|
|
|
|
|
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
|
|
|
|
if (junk != null) {
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, junk.id);
|
|
|
|
message.ui_hide = true;
|
|
|
|
executed = true;
|
2021-09-18 16:15:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-26 11:39:21 +00:00
|
|
|
}
|
2021-09-18 16:15:44 +00:00
|
|
|
|
2021-07-29 08:33:06 +00:00
|
|
|
if (executed &&
|
|
|
|
!message.hasKeyword(MessageHelper.FLAG_FILTERED))
|
2021-07-29 07:11:51 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.KEYWORD, MessageHelper.FLAG_FILTERED, true);
|
2019-03-31 06:48:42 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-12-06 07:50:46 +00:00
|
|
|
db.message().setMessageError(message.id, Log.formatThrowable(ex));
|
2019-03-31 06:48:42 +00:00
|
|
|
}
|
2020-06-22 08:22:25 +00:00
|
|
|
}
|
2020-05-05 19:13:05 +00:00
|
|
|
|
2020-06-22 08:22:25 +00:00
|
|
|
private static void reportNewMessage(Context context, EntityAccount account, EntityFolder folder, EntityMessage message) {
|
2020-05-05 19:13:05 +00:00
|
|
|
// Prepare scroll to top
|
|
|
|
if (!message.ui_seen && !message.ui_hide &&
|
|
|
|
message.received > account.created) {
|
|
|
|
Intent report = new Intent(ActivityView.ACTION_NEW_MESSAGE);
|
|
|
|
report.putExtra("folder", folder.id);
|
2021-08-22 18:12:13 +00:00
|
|
|
report.putExtra("type", folder.type);
|
2020-05-05 19:13:05 +00:00
|
|
|
report.putExtra("unified", folder.unified);
|
2021-08-22 18:12:13 +00:00
|
|
|
Log.i("Report new id=" + message.id +
|
|
|
|
" folder=" + folder.type + ":" + folder.name +
|
|
|
|
" unified=" + folder.unified);
|
2020-05-05 19:13:05 +00:00
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(report);
|
|
|
|
}
|
2019-03-31 06:48:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 08:12:43 +00:00
|
|
|
private static boolean downloadMessage(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context,
|
2019-10-10 07:35:07 +00:00
|
|
|
EntityAccount account, EntityFolder folder,
|
|
|
|
IMAPStore istore, IMAPFolder ifolder,
|
2020-07-15 16:58:03 +00:00
|
|
|
MimeMessage imessage, long id, State state, SyncStats stats) throws MessagingException, IOException {
|
2019-04-19 16:54:40 +00:00
|
|
|
if (state.getNetworkState().isRoaming())
|
2020-09-02 08:12:43 +00:00
|
|
|
return false;
|
2019-04-19 16:54:40 +00:00
|
|
|
|
2022-01-16 10:41:49 +00:00
|
|
|
if (imessage == null)
|
|
|
|
return false;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
2020-05-25 12:31:29 +00:00
|
|
|
if (message == null || message.ui_hide)
|
2020-09-02 08:12:43 +00:00
|
|
|
return false;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2020-07-13 13:34:31 +00:00
|
|
|
long maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE);
|
2019-02-27 15:05:15 +00:00
|
|
|
if (maxSize == 0)
|
|
|
|
maxSize = Long.MAX_VALUE;
|
2021-04-26 06:15:53 +00:00
|
|
|
boolean download_eml = prefs.getBoolean("download_eml", false);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
|
|
|
|
boolean fetch = false;
|
|
|
|
if (!message.content)
|
2019-03-16 13:12:31 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() || (message.size != null && message.size < maxSize))
|
2019-02-27 15:05:15 +00:00
|
|
|
fetch = true;
|
|
|
|
|
|
|
|
if (!fetch)
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
if (!attachment.available)
|
2019-03-16 13:12:31 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize)) {
|
2019-02-27 15:05:15 +00:00
|
|
|
fetch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetch) {
|
|
|
|
Log.i(folder.name + " fetching message id=" + message.id);
|
2019-05-17 15:41:23 +00:00
|
|
|
|
2019-09-22 07:53:55 +00:00
|
|
|
// Fetch on demand to prevent OOM
|
|
|
|
|
|
|
|
//FetchProfile fp = new FetchProfile();
|
2019-05-17 15:41:23 +00:00
|
|
|
//fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
//fp.add(FetchProfile.Item.FLAGS);
|
2019-09-22 07:53:55 +00:00
|
|
|
//fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
2019-05-17 15:41:23 +00:00
|
|
|
//fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
//fp.add(IMAPFolder.FetchProfileItem.HEADERS);
|
|
|
|
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
//fp.add(FetchProfile.Item.SIZE);
|
|
|
|
//fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
|
2020-06-25 07:14:05 +00:00
|
|
|
//if (account.isGmail()) {
|
2020-05-01 19:47:59 +00:00
|
|
|
// fp.add(GmailFolder.FetchProfileItem.THRID);
|
2020-06-25 07:14:05 +00:00
|
|
|
// fp.add(GmailFolder.FetchProfileItem.LABELS);
|
|
|
|
//}
|
2019-09-22 07:53:55 +00:00
|
|
|
//ifolder.fetch(new Message[]{imessage}, fp);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
if (!message.content) {
|
2019-07-14 19:34:43 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() ||
|
|
|
|
(message.size != null && message.size < maxSize)) {
|
2019-02-27 15:05:15 +00:00
|
|
|
String body = parts.getHtml(context);
|
2020-02-20 09:35:01 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
2021-01-19 10:57:59 +00:00
|
|
|
String text = HtmlHelper.getFullText(body);
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
2021-01-25 13:49:56 +00:00
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
2019-05-04 18:52:21 +00:00
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
2021-01-19 11:18:03 +00:00
|
|
|
message.language,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.isPlainOnly(),
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.getWarnings(message.warning));
|
2022-04-28 11:59:46 +00:00
|
|
|
MessageClassifier.classify(message, folder, true, context);
|
2021-01-02 14:57:24 +00:00
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
if (stats != null && body != null)
|
|
|
|
stats.content += body.length();
|
2019-06-14 13:32:09 +00:00
|
|
|
Log.i(folder.name + " downloaded message id=" + message.id +
|
|
|
|
" size=" + message.size + "/" + (body == null ? null : body.length()));
|
2019-07-06 16:33:21 +00:00
|
|
|
|
2020-04-18 07:44:16 +00:00
|
|
|
if (TextUtils.isEmpty(body) && parts.hasBody())
|
2020-09-21 19:46:39 +00:00
|
|
|
reportEmptyMessage(context, state, account, istore);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 19:21:09 +00:00
|
|
|
for (EntityAttachment attachment : attachments)
|
2022-04-07 18:57:54 +00:00
|
|
|
if (!attachment.available &&
|
|
|
|
attachment.subsequence == null &&
|
|
|
|
TextUtils.isEmpty(attachment.error))
|
2019-06-29 06:52:15 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() ||
|
|
|
|
(attachment.size != null && attachment.size < maxSize))
|
2019-03-14 08:05:28 +00:00
|
|
|
try {
|
2023-06-06 20:22:08 +00:00
|
|
|
parts.downloadAttachment(context, attachment, folder);
|
2020-07-15 16:58:03 +00:00
|
|
|
if (stats != null && attachment.size != null)
|
|
|
|
stats.attachments += attachment.size;
|
2019-03-14 08:05:28 +00:00
|
|
|
} catch (Throwable ex) {
|
2019-09-17 19:25:41 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-12-06 07:50:46 +00:00
|
|
|
db.attachment().setError(attachment.id, Log.formatThrowable(ex, false));
|
2019-03-14 08:05:28 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-09-02 08:12:43 +00:00
|
|
|
|
2021-04-26 06:15:53 +00:00
|
|
|
if (download_eml &&
|
2021-04-26 08:12:29 +00:00
|
|
|
(message.raw == null || !message.raw) &&
|
2021-04-26 06:15:53 +00:00
|
|
|
(state.getNetworkState().isUnmetered() || (message.total != null && message.total < maxSize))) {
|
|
|
|
File file = message.getRawFile(context);
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:12:29 +00:00
|
|
|
message.raw = true;
|
|
|
|
db.message().setMessageRaw(message.id, message.raw);
|
2021-04-26 06:15:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 08:12:43 +00:00
|
|
|
return fetch;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void reportEmptyMessage(Context context, State state, EntityAccount account, IMAPStore istore) {
|
2019-10-10 07:35:07 +00:00
|
|
|
try {
|
|
|
|
if (istore.hasCapability("ID")) {
|
|
|
|
Map<String, String> id = new LinkedHashMap<>();
|
|
|
|
id.put("name", context.getString(R.string.app_name));
|
|
|
|
id.put("version", BuildConfig.VERSION_NAME);
|
|
|
|
Map<String, String> sid = istore.id(id);
|
|
|
|
if (sid != null) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (String key : sid.keySet())
|
|
|
|
sb.append(" ").append(key).append("=").append(sid.get(key));
|
2020-03-17 08:09:29 +00:00
|
|
|
if (!account.partial_fetch)
|
2020-07-06 10:29:20 +00:00
|
|
|
Log.w("Empty message" + sb.toString());
|
2019-10-10 07:35:07 +00:00
|
|
|
}
|
2020-03-17 08:09:29 +00:00
|
|
|
} else {
|
|
|
|
if (!account.partial_fetch)
|
2020-07-06 10:29:20 +00:00
|
|
|
Log.w("Empty message " + account.host);
|
2020-03-17 08:09:29 +00:00
|
|
|
}
|
2019-10-10 07:35:07 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-03-06 14:29:33 +00:00
|
|
|
|
|
|
|
// Auto disable partial fetch
|
2022-09-09 11:31:34 +00:00
|
|
|
if (account.partial_fetch && false) {
|
2020-03-06 14:29:33 +00:00
|
|
|
account.partial_fetch = false;
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.account().setAccountPartialFetch(account.id, account.partial_fetch);
|
2020-09-21 19:46:39 +00:00
|
|
|
state.error(new StoreClosedException(istore));
|
2020-03-06 14:29:33 +00:00
|
|
|
}
|
2019-10-10 07:35:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// FolderClosedException: can happen when no connectivity
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// IllegalStateException:
|
|
|
|
// - "This operation is not allowed on a closed folder"
|
|
|
|
// - can happen when syncing message
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// ConnectionException
|
|
|
|
// - failed to create new store connection (connectivity)
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// MailConnectException
|
|
|
|
// - on connectivity problems when connecting to store
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-07-31 18:13:58 +00:00
|
|
|
static NotificationCompat.Builder getNotificationError(Context context, String channel, EntityAccount account, long id, Throwable ex) {
|
|
|
|
String title = context.getString(R.string.title_notification_failed, account.name);
|
|
|
|
String message = Log.formatThrowable(ex, "\n", false);
|
|
|
|
|
2020-06-26 11:18:53 +00:00
|
|
|
// Build pending intent
|
2021-07-31 18:13:58 +00:00
|
|
|
Intent intent = new Intent(context, ActivityError.class);
|
|
|
|
intent.setAction(channel + ":" + account.id + ":" + id);
|
|
|
|
intent.putExtra("title", title);
|
|
|
|
intent.putExtra("message", message);
|
2021-09-25 14:29:47 +00:00
|
|
|
intent.putExtra("provider", account.provider);
|
2021-08-13 12:33:37 +00:00
|
|
|
intent.putExtra("account", account.id);
|
2021-09-25 14:29:47 +00:00
|
|
|
intent.putExtra("protocol", account.protocol);
|
|
|
|
intent.putExtra("auth_type", account.auth_type);
|
2023-11-28 12:48:09 +00:00
|
|
|
intent.putExtra("address", account.user);
|
2021-07-31 18:13:58 +00:00
|
|
|
intent.putExtra("faq", 22);
|
2022-06-25 12:27:46 +00:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent pi = PendingIntentCompat.getActivity(
|
2021-07-31 18:13:58 +00:00
|
|
|
context, ActivityError.PI_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-06-26 11:18:53 +00:00
|
|
|
// Build notification
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(context, channel)
|
|
|
|
.setSmallIcon(R.drawable.baseline_warning_white_24)
|
2021-07-31 18:13:58 +00:00
|
|
|
.setContentTitle(title)
|
2020-06-26 11:18:53 +00:00
|
|
|
.setContentText(Log.formatThrowable(ex, false))
|
|
|
|
.setContentIntent(pi)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MAX)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_ERROR)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
2021-07-31 18:13:58 +00:00
|
|
|
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
|
2020-06-26 11:18:53 +00:00
|
|
|
|
|
|
|
return builder;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static class State {
|
2019-12-11 10:43:32 +00:00
|
|
|
private int backoff;
|
2020-09-23 18:31:37 +00:00
|
|
|
private boolean backingoff = false;
|
2019-05-12 16:41:51 +00:00
|
|
|
private ConnectionHelper.NetworkState networkState;
|
2020-03-11 19:56:14 +00:00
|
|
|
private Thread thread = new Thread();
|
2019-02-27 15:05:15 +00:00
|
|
|
private Semaphore semaphore = new Semaphore(0);
|
2022-03-30 07:55:24 +00:00
|
|
|
private boolean started = false;
|
2019-02-27 15:05:15 +00:00
|
|
|
private boolean running = true;
|
2022-02-03 09:35:31 +00:00
|
|
|
private boolean foreground = false;
|
2019-04-10 18:17:58 +00:00
|
|
|
private boolean recoverable = true;
|
2020-10-30 11:16:49 +00:00
|
|
|
private Throwable unrecoverable = null;
|
2019-12-28 19:35:41 +00:00
|
|
|
private Long lastActivity = null;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-09-16 08:43:14 +00:00
|
|
|
private long serial = 0;
|
2020-09-22 12:04:26 +00:00
|
|
|
|
2019-05-12 16:41:51 +00:00
|
|
|
State(ConnectionHelper.NetworkState networkState) {
|
2019-03-16 13:12:31 +00:00
|
|
|
this.networkState = networkState;
|
|
|
|
}
|
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
void setNetworkState(ConnectionHelper.NetworkState networkState) {
|
|
|
|
this.networkState = networkState;
|
2019-03-16 13:12:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 16:41:51 +00:00
|
|
|
ConnectionHelper.NetworkState getNetworkState() {
|
2019-03-16 13:12:31 +00:00
|
|
|
return networkState;
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:39:18 +00:00
|
|
|
void setBackoff(int value) {
|
|
|
|
this.backoff = value;
|
2019-12-11 10:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int getBackoff() {
|
|
|
|
return backoff;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
void runnable(Runnable runnable, String name) {
|
|
|
|
thread = new Thread(runnable, name);
|
|
|
|
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:45:36 +00:00
|
|
|
boolean release() {
|
|
|
|
if (!thread.isAlive())
|
|
|
|
return false;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
semaphore.release();
|
2023-05-04 05:26:03 +00:00
|
|
|
microWait();
|
2020-08-10 14:45:36 +00:00
|
|
|
return true;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 18:31:37 +00:00
|
|
|
boolean acquire(long milliseconds, boolean backingoff) throws InterruptedException {
|
2020-09-21 20:11:42 +00:00
|
|
|
try {
|
2020-09-23 18:31:37 +00:00
|
|
|
this.backingoff = backingoff;
|
2020-09-21 20:11:42 +00:00
|
|
|
return semaphore.tryAcquire(milliseconds, TimeUnit.MILLISECONDS);
|
|
|
|
} finally {
|
2020-09-23 18:31:37 +00:00
|
|
|
this.backingoff = false;
|
2020-09-21 20:11:42 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
void error(Throwable ex) {
|
2019-05-15 19:21:05 +00:00
|
|
|
if (ex instanceof MessagingException &&
|
2019-06-13 12:31:46 +00:00
|
|
|
("connection failure".equals(ex.getMessage()) ||
|
2019-11-12 18:16:36 +00:00
|
|
|
"Not connected".equals(ex.getMessage()) || // POP3
|
2019-06-13 12:31:46 +00:00
|
|
|
ex.getCause() instanceof SocketException ||
|
2019-05-15 19:21:05 +00:00
|
|
|
ex.getCause() instanceof ConnectionException))
|
|
|
|
recoverable = false;
|
|
|
|
|
2019-06-27 07:18:57 +00:00
|
|
|
if (ex instanceof ConnectionException)
|
|
|
|
// failed to create new store connection
|
|
|
|
// BYE, Socket is closed
|
|
|
|
recoverable = false;
|
|
|
|
|
2020-03-06 14:29:33 +00:00
|
|
|
if (ex instanceof StoreClosedException ||
|
|
|
|
ex instanceof FolderClosedException ||
|
2019-06-24 06:35:13 +00:00
|
|
|
ex instanceof FolderNotFoundException)
|
2020-02-09 13:11:46 +00:00
|
|
|
// Lost folder connection to server
|
2019-05-10 13:08:29 +00:00
|
|
|
recoverable = false;
|
|
|
|
|
2019-05-10 13:22:31 +00:00
|
|
|
if (ex instanceof IllegalStateException && (
|
|
|
|
"Not connected".equals(ex.getMessage()) ||
|
|
|
|
"This operation is not allowed on a closed folder".equals(ex.getMessage())))
|
2019-05-10 13:08:29 +00:00
|
|
|
recoverable = false;
|
|
|
|
|
2020-02-11 15:57:12 +00:00
|
|
|
if (ex instanceof OperationCanceledException)
|
|
|
|
recoverable = false;
|
|
|
|
|
2020-10-30 11:16:49 +00:00
|
|
|
if (!recoverable)
|
|
|
|
unrecoverable = ex;
|
|
|
|
|
2020-09-23 18:31:37 +00:00
|
|
|
if (!backingoff) {
|
2020-09-21 20:11:42 +00:00
|
|
|
thread.interrupt();
|
2023-05-04 05:26:03 +00:00
|
|
|
microWait();
|
2020-09-21 20:11:42 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
void reset() {
|
2021-12-01 17:00:38 +00:00
|
|
|
Thread.currentThread().interrupted(); // clear interrupted status
|
2021-12-07 09:41:49 +00:00
|
|
|
Log.i("Permits=" + semaphore.drainPermits());
|
2019-04-10 18:17:58 +00:00
|
|
|
recoverable = true;
|
2019-12-28 19:35:41 +00:00
|
|
|
lastActivity = null;
|
2020-09-22 12:04:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 08:43:14 +00:00
|
|
|
void nextSerial() {
|
|
|
|
serial++;
|
2019-04-10 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 05:26:03 +00:00
|
|
|
private void microWait() {
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
// Give interrupted thread some time to acquire wake lock
|
|
|
|
Thread.sleep(YIELD_DURATION);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
thread.start();
|
2022-03-30 07:55:24 +00:00
|
|
|
started = true;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop() {
|
|
|
|
running = false;
|
|
|
|
semaphore.release();
|
|
|
|
}
|
|
|
|
|
2022-03-30 06:14:06 +00:00
|
|
|
boolean isAlive() {
|
2022-03-30 07:55:24 +00:00
|
|
|
if (!started)
|
|
|
|
return true;
|
|
|
|
if (!running)
|
|
|
|
return false;
|
2022-09-12 05:52:11 +00:00
|
|
|
if (thread == null)
|
|
|
|
return false;
|
2022-03-30 07:55:24 +00:00
|
|
|
return thread.isAlive();
|
2022-03-30 06:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 10:09:21 +00:00
|
|
|
void join() {
|
|
|
|
join(thread);
|
2022-04-17 19:34:33 +00:00
|
|
|
CoalMine.watch(thread, getClass().getSimpleName() + "#join()");
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 07:28:16 +00:00
|
|
|
void ensureRunning(String reason) throws OperationCanceledException {
|
2021-07-27 16:32:10 +00:00
|
|
|
if (!recoverable && unrecoverable != null)
|
2021-07-27 19:04:48 +00:00
|
|
|
throw new OperationCanceledExceptionEx(reason, unrecoverable);
|
2021-07-27 16:32:10 +00:00
|
|
|
if (!running)
|
|
|
|
throw new OperationCanceledException(reason);
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:50:46 +00:00
|
|
|
boolean isRunning() {
|
2019-02-27 15:05:15 +00:00
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:50:46 +00:00
|
|
|
boolean isRecoverable() {
|
2019-04-10 18:17:58 +00:00
|
|
|
return recoverable;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:16:49 +00:00
|
|
|
Throwable getUnrecoverable() {
|
|
|
|
return unrecoverable;
|
|
|
|
}
|
|
|
|
|
2021-11-28 10:09:21 +00:00
|
|
|
void join(Thread thread) {
|
2019-02-27 15:05:15 +00:00
|
|
|
boolean joined = false;
|
2020-08-26 07:42:52 +00:00
|
|
|
boolean interrupted = false;
|
2020-08-28 07:13:52 +00:00
|
|
|
String name = thread.getName();
|
2019-02-27 15:05:15 +00:00
|
|
|
while (!joined)
|
|
|
|
try {
|
2020-08-28 07:13:52 +00:00
|
|
|
Log.i("Joining " + name +
|
|
|
|
" alive=" + thread.isAlive() +
|
2021-11-27 09:09:43 +00:00
|
|
|
" state=" + thread.getState() +
|
|
|
|
" interrupted=" + interrupted);
|
2020-08-28 07:13:52 +00:00
|
|
|
|
2021-11-27 09:09:43 +00:00
|
|
|
thread.join(interrupted ? JOIN_WAIT_INTERRUPT : JOIN_WAIT_ALIVE);
|
2020-08-28 07:13:52 +00:00
|
|
|
|
|
|
|
// https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
|
|
|
|
Thread.State state = thread.getState();
|
2021-11-29 06:43:13 +00:00
|
|
|
if (thread.isAlive() &&
|
|
|
|
state != Thread.State.NEW &&
|
|
|
|
state != Thread.State.TERMINATED) {
|
2022-05-11 12:39:49 +00:00
|
|
|
if (interrupted)
|
|
|
|
Log.e("Join " + name + " failed" +
|
|
|
|
" state=" + state + " interrupted=" + interrupted);
|
2020-08-26 07:42:52 +00:00
|
|
|
if (interrupted)
|
2021-11-28 10:09:21 +00:00
|
|
|
joined = true; // giving up
|
2020-08-26 07:42:52 +00:00
|
|
|
else {
|
|
|
|
thread.interrupt();
|
|
|
|
interrupted = true;
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-28 07:13:52 +00:00
|
|
|
Log.i("Joined " + name + " " + " state=" + state);
|
2020-08-26 07:42:52 +00:00
|
|
|
joined = true;
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (InterruptedException ex) {
|
2021-12-05 15:32:17 +00:00
|
|
|
Log.i(new Throwable(name, ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 19:35:41 +00:00
|
|
|
synchronized void activity() {
|
|
|
|
lastActivity = SystemClock.elapsedRealtime();
|
|
|
|
}
|
|
|
|
|
|
|
|
long getIdleTime() {
|
2020-05-20 06:56:59 +00:00
|
|
|
Long last = lastActivity;
|
|
|
|
return (last == null ? 0 : SystemClock.elapsedRealtime() - last);
|
2019-12-28 19:35:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 08:43:14 +00:00
|
|
|
long getSerial() {
|
|
|
|
return serial;
|
2021-09-15 05:15:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:35:31 +00:00
|
|
|
void setForeground(boolean value) {
|
|
|
|
this.foreground = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean getForeground() {
|
|
|
|
return this.foreground;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2019-12-28 19:35:41 +00:00
|
|
|
return "[running=" + running +
|
|
|
|
",recoverable=" + recoverable +
|
2021-09-14 19:49:20 +00:00
|
|
|
",idle=" + getIdleTime() + "" +
|
2021-09-16 08:43:14 +00:00
|
|
|
",serial=" + serial + "]";
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2021-07-27 19:04:48 +00:00
|
|
|
static class OperationCanceledExceptionEx extends OperationCanceledException {
|
|
|
|
private Throwable cause;
|
|
|
|
|
|
|
|
OperationCanceledExceptionEx(String message, Throwable cause) {
|
|
|
|
super(message);
|
|
|
|
this.cause = cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Throwable getCause() {
|
|
|
|
return this.cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
private static class SyncStats {
|
|
|
|
long search_ms;
|
|
|
|
int flags;
|
|
|
|
long flags_ms;
|
2020-07-25 05:33:58 +00:00
|
|
|
int uids;
|
2020-07-15 16:58:03 +00:00
|
|
|
long uids_ms;
|
|
|
|
int headers;
|
|
|
|
long headers_ms;
|
|
|
|
long content;
|
|
|
|
long attachments;
|
|
|
|
long total;
|
|
|
|
|
2020-07-25 05:33:58 +00:00
|
|
|
boolean isEmpty() {
|
|
|
|
return (search_ms == 0 &&
|
|
|
|
flags == 0 &&
|
|
|
|
flags_ms == 0 &&
|
|
|
|
uids == 0 &&
|
|
|
|
uids_ms == 0 &&
|
|
|
|
headers == 0 &&
|
|
|
|
headers_ms == 0 &&
|
|
|
|
content == 0 &&
|
|
|
|
attachments == 0 &&
|
|
|
|
total == 0);
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "search=" + search_ms + " ms" +
|
|
|
|
" flags=" + flags + "/" + flags_ms + " ms" +
|
|
|
|
" uids=" + uids + "/" + uids_ms + " ms" +
|
|
|
|
" headers=" + headers + "/" + headers_ms + " ms" +
|
|
|
|
" content=" + Helper.humanReadableByteCount(content) +
|
|
|
|
" attachments=" + Helper.humanReadableByteCount(attachments) +
|
2020-07-25 05:42:51 +00:00
|
|
|
" total=" + total + " ms";
|
2020-07-15 16:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|