Added operation failsafes

pull/12/merge
M66B 6 years ago
parent ec93accafe
commit e5a475379b

@ -601,6 +601,9 @@ public class ServiceSynchronize extends LifecycleService {
// Append message // Append message
EntityMessage msg = message.getMessage(op.message); EntityMessage msg = message.getMessage(op.message);
if (msg == null)
return;
Properties props = MessageHelper.getSessionProperties(); Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getDefaultInstance(props, null); Session isession = Session.getDefaultInstance(props, null);
MimeMessage imessage = MessageHelper.from(msg, isession); MimeMessage imessage = MessageHelper.from(msg, isession);
@ -610,6 +613,9 @@ public class ServiceSynchronize extends LifecycleService {
try { try {
if (msg.uid != null) { if (msg.uid != null) {
Message previously = ifolder.getMessageByUID(msg.uid); Message previously = ifolder.getMessageByUID(msg.uid);
if (previously == null)
throw new MessageRemovedException();
previously.setFlag(Flags.Flag.DELETED, true); previously.setFlag(Flags.Flag.DELETED, true);
ifolder.expunge(); ifolder.expunge();
} }
@ -619,11 +625,17 @@ public class ServiceSynchronize extends LifecycleService {
} }
} else if (EntityOperation.MOVE.equals(op.name)) { } else if (EntityOperation.MOVE.equals(op.name)) {
EntityFolder target = db.folder().getFolder(jargs.getLong(0));
if (target == null)
throw new FolderNotFoundException();
// Move message // Move message
EntityFolder archive = db.folder().getFolder(jargs.getLong(0));
Message imessage = ifolder.getMessageByUID(op.uid); Message imessage = ifolder.getMessageByUID(op.uid);
Folder target = istore.getFolder(archive.name); if (imessage == null)
ifolder.moveMessages(new Message[]{imessage}, target); throw new MessageRemovedException();
Folder itarget = istore.getFolder(target.name);
ifolder.moveMessages(new Message[]{imessage}, itarget);
message.deleteMessage(op.message); message.deleteMessage(op.message);
@ -640,17 +652,22 @@ public class ServiceSynchronize extends LifecycleService {
} else if (EntityOperation.SEND.equals(op.name)) { } else if (EntityOperation.SEND.equals(op.name)) {
// Send message // Send message
EntityMessage msg = message.getMessage(op.message); EntityMessage msg = message.getMessage(op.message);
if (msg == null)
return;
EntityMessage reply = (msg.replying == null ? null : message.getMessage(msg.replying)); EntityMessage reply = (msg.replying == null ? null : message.getMessage(msg.replying));
EntityIdentity ident = db.identity().getIdentity(msg.identity); EntityIdentity ident = db.identity().getIdentity(msg.identity);
if (!ident.synchronize) { if (ident == null || !ident.synchronize) {
// Message will remain in outbox // Message will remain in outbox
return; return;
} }
// Create session
Properties props = MessageHelper.getSessionProperties(); Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getDefaultInstance(props, null); Session isession = Session.getDefaultInstance(props, null);
// Create message
MimeMessage imessage; MimeMessage imessage;
if (reply == null) if (reply == null)
imessage = MessageHelper.from(msg, isession); imessage = MessageHelper.from(msg, isession);
@ -659,17 +676,18 @@ public class ServiceSynchronize extends LifecycleService {
if (ident.replyto != null) if (ident.replyto != null)
imessage.setReplyTo(new Address[]{new InternetAddress(ident.replyto)}); imessage.setReplyTo(new Address[]{new InternetAddress(ident.replyto)});
// Create transport
Transport itransport = isession.getTransport(ident.starttls ? "smtp" : "smtps"); Transport itransport = isession.getTransport(ident.starttls ? "smtp" : "smtps");
try { try {
// Connect transport
itransport.connect(ident.host, ident.port, ident.user, ident.password); itransport.connect(ident.host, ident.port, ident.user, ident.password);
// Send message
Address[] to = imessage.getAllRecipients(); Address[] to = imessage.getAllRecipients();
itransport.sendMessage(imessage, to); itransport.sendMessage(imessage, to);
Log.i(Helper.TAG, "Sent via " + ident.host + "/" + ident.user + Log.i(Helper.TAG, "Sent via " + ident.host + "/" + ident.user +
" to " + TextUtils.join(", ", to)); " to " + TextUtils.join(", ", to));
// Make sure the message is sent only once
operation.deleteOperation(op.id);
message.deleteMessage(op.message); message.deleteMessage(op.message);
} finally { } finally {
itransport.close(); itransport.close();
@ -678,14 +696,13 @@ public class ServiceSynchronize extends LifecycleService {
} else if (EntityOperation.ATTACHMENT.equals(op.name)) { } else if (EntityOperation.ATTACHMENT.equals(op.name)) {
int sequence = jargs.getInt(0); int sequence = jargs.getInt(0);
EntityAttachment attachment = db.attachment().getAttachment(op.message, sequence); EntityAttachment attachment = db.attachment().getAttachment(op.message, sequence);
if (attachment == null)
return;
Message imessage = ifolder.getMessageByUID(op.uid); Message imessage = ifolder.getMessageByUID(op.uid);
if (imessage == null) if (imessage == null)
throw new MessageRemovedException(); throw new MessageRemovedException();
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getDefaultInstance(props, null);
MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper helper = new MessageHelper((MimeMessage) imessage);
EntityAttachment a = helper.getAttachments().get(sequence - 1); EntityAttachment a = helper.getAttachments().get(sequence - 1);
@ -708,6 +725,11 @@ public class ServiceSynchronize extends LifecycleService {
} catch (MessageRemovedException ex) { } catch (MessageRemovedException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
// There is no use in repeating
operation.deleteOperation(op.id);
} catch (FolderNotFoundException ex) {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
// There is no use in repeating // There is no use in repeating
operation.deleteOperation(op.id); operation.deleteOperation(op.id);
} }

Loading…
Cancel
Save