Better IMAP compatibility

This commit is contained in:
M66B 2019-03-14 11:08:19 +00:00
parent 8580e29bd4
commit dfc8fee370
1 changed files with 76 additions and 78 deletions

View File

@ -18,6 +18,7 @@ import android.text.TextUtils;
import com.sun.mail.iap.ConnectionException;
import com.sun.mail.iap.Response;
import com.sun.mail.imap.AppendUID;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
@ -423,7 +424,7 @@ class Core {
imessage.setFlag(Flags.Flag.DRAFT, true);
// Add message
long uid = append(ifolder, imessage);
long uid = append(istore, ifolder, imessage);
Log.i(folder.name + " appended id=" + message.id + " uid=" + uid);
db.message().setMessageUid(message.id, uid);
@ -467,30 +468,17 @@ class Core {
if (imessage == null)
throw new MessageRemovedException();
// Get target folder
// Get arguments
long id = jargs.getLong(0);
boolean autoread = (jargs.length() > 1 && jargs.getBoolean(1));
Long newid = (jargs.length() > 2 && !jargs.isNull(2) ? jargs.getLong(2) : null);
// Get target folder
EntityFolder target = db.folder().getFolder(id);
if (target == null)
throw new FolderNotFoundException();
IMAPFolder itarget = (IMAPFolder) istore.getFolder(target.name);
boolean autoread = (jargs.length() > 1 && jargs.getBoolean(1));
if (!copy &&
istore.hasCapability("MOVE") &&
!EntityFolder.DRAFTS.equals(folder.type) &&
!EntityFolder.DRAFTS.equals(target.type)) {
// Autoread
if (ifolder.getPermanentFlags().contains(Flags.Flag.SEEN))
if (autoread && !imessage.isSet(Flags.Flag.SEEN))
imessage.setFlag(Flags.Flag.SEEN, true);
// Move message to target folder
ifolder.moveMessages(new Message[]{imessage}, itarget);
} else {
if (!copy)
Log.w(folder.name + " MOVE by DELETE/APPEND");
// Serialize source message
ByteArrayOutputStream bos = new ByteArrayOutputStream();
imessage.writeTo(bos);
@ -526,7 +514,11 @@ class Core {
icopy.setFlag(Flags.Flag.DRAFT, true);
// Append target
long uid = append(itarget, (MimeMessage) icopy);
long uid = append(istore, itarget, (MimeMessage) icopy);
if (newid != null) {
Log.i("Moved id=" + newid + " uid=" + uid);
db.message().setMessageUid(newid, uid);
}
// Some providers, like Gmail, don't honor the appended seen flag
if (itarget.getPermanentFlags().contains(Flags.Flag.SEEN)) {
@ -558,7 +550,6 @@ class Core {
itarget.close();
}
}
}
private static void onDelete(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
// Delete message
@ -665,11 +656,18 @@ class Core {
parts.downloadAttachment(context, sequence - 1, attachment.id);
}
private static long append(IMAPFolder ifolder, MimeMessage imessage) throws MessagingException {
private static long append(IMAPStore istore, IMAPFolder ifolder, MimeMessage imessage) throws MessagingException {
String msgid = imessage.getMessageID();
if (msgid == null)
throw new IllegalArgumentException("Message ID missing");
if (istore.hasCapability("UIDPLUS")) {
AppendUID[] uids = ifolder.appendUIDMessages(new Message[]{imessage});
if (uids != null && uids.length > 0) {
Log.i("Appended uid=" + uids[0].uid);
return uids[0].uid;
}
} else
ifolder.appendMessages(new Message[]{imessage});
// Fixed timing issue of at least Courier based servers