OUT-571: Fix logic error in processing of message sync
authorThomas
Fri, 07 Jun 2019 14:52:25 +0200
branchsync
changeset 2661e90dfa2ce8f0
parent 2659 350f581a72b6
child 2662 80601ae81dd2
OUT-571: Fix logic error in processing of message
Wrappers/WatchedWindow.cs
     1.1 --- a/Wrappers/WatchedWindow.cs	Fri Jun 07 13:20:45 2019 +0200
     1.2 +++ b/Wrappers/WatchedWindow.cs	Fri Jun 07 14:52:25 2019 +0200
     1.3 @@ -1042,60 +1042,59 @@
     1.4  
     1.5              try
     1.6              {
     1.7 -                // Make sure that the calculated result corresponds to the currently selected mail item
     1.8 -                if (e?.EntryId?.Equals(this.CurrentMailItem?.EntryID) != true)
     1.9 +                /* Make sure that the calculated result corresponds to the currently selected mail item
    1.10 +                 * If both are null, it's probably a draft message.
    1.11 +                 */ 
    1.12 +                if ((e.EntryId == null && this.CurrentMailItem?.EntryID == null) ||
    1.13 +                    (e.EntryId.Equals(this.CurrentMailItem.EntryID)))
    1.14                  {
    1.15 -                    Log.Verbose("MailItem_ProcessingComplete: Calculated rating doesn't correspond to current mail item. Skipping...");
    1.16 -                    return;
    1.17 -                }
    1.18 -
    1.19 -                // Marshal code back to UI thread as necessary
    1.20 -                KeySyncWizard.Wizard.Dispatcher.Invoke(new Action(() =>
    1.21 -                {
    1.22 -                    if (this.isEnabled)
    1.23 +                    // Marshal code back to UI thread as necessary
    1.24 +                    KeySyncWizard.Wizard.Dispatcher.Invoke(new Action(() =>
    1.25                      {
    1.26 -                        try
    1.27 +                        if (this.isEnabled)
    1.28                          {
    1.29 -                            this.SetRating(e.ProcessedRating);
    1.30 +                            try
    1.31 +                            {
    1.32 +                                this.SetRating(e.ProcessedRating);
    1.33  
    1.34                              // Set MAPI properties if needed
    1.35                              if (e.PropertiesToSet?.Count > 0)
    1.36 +                                {
    1.37 +                                    this.CurrentMailItem?.SetMAPIProperties(e.PropertiesToSet);
    1.38 +                                }
    1.39 +                                Log.Verbose("MailItem_ProcessingComplete: Status bar updated with rating " + Enum.GetName(typeof(pEpRating), e.ProcessedRating));
    1.40 +                            }
    1.41 +                            catch (Exception ex)
    1.42                              {
    1.43 -                                this.CurrentMailItem?.SetMAPIProperties(e.PropertiesToSet);
    1.44 +                                Log.Verbose("MailItem_ProcessingComplete: Error. " + ex.ToString());
    1.45                              }
    1.46 -                            Log.Verbose("MailItem_ProcessingComplete: Status bar updated with rating " + Enum.GetName(typeof(pEpRating), e.ProcessedRating));
    1.47 -                        }
    1.48 -                        catch (Exception ex)
    1.49 -                        {
    1.50 -                            Log.Verbose("MailItem_ProcessingComplete: Error. " + ex.ToString());
    1.51 -                        }
    1.52  
    1.53 -                        if (repeatProcessing &&
    1.54 -                            (repeatCounter++ < maxRepeatCount))
    1.55 -                        {
    1.56 -                            repeatProcessing = false;
    1.57 -                            this.RequestRatingAndUIUpdate();
    1.58 -                        }
    1.59 -                        else
    1.60 -                        {
    1.61 +                            if (repeatProcessing &&
    1.62 +                                (repeatCounter++ < maxRepeatCount))
    1.63 +                            {
    1.64 +                                repeatProcessing = false;
    1.65 +                                this.RequestRatingAndUIUpdate();
    1.66 +                            }
    1.67 +                            else
    1.68 +                            {
    1.69                              /* Check if the mail item is in Outbox and update the inspector window if possible.
    1.70                               * This is the case when a submitted, but not yet sent email is opened again when working 
    1.71                               * offline or without internet connection. Without this update, Outlook removes the message class 
    1.72                               * "IPM.Note.SMIME.MultipartSigned" at the next send event and the message gets invalid and can't be
    1.73                               * opened again (i.e. is lost).
    1.74                               */
    1.75 -                            try
    1.76 -                            {
    1.77 -                                if ((this.CurrentMailItem?.GetIsSubmitted() == true) &&
    1.78 -                                    (this.IsDraft))
    1.79 +                                try
    1.80                                  {
    1.81 -                                    UpdateInspector();
    1.82 +                                    if ((this.CurrentMailItem?.GetIsSubmitted() == true) &&
    1.83 +                                        (this.IsDraft))
    1.84 +                                    {
    1.85 +                                        UpdateInspector();
    1.86 +                                    }
    1.87                                  }
    1.88 -                            }
    1.89 -                            catch (Exception ex)
    1.90 -                            {
    1.91 -                                Log.Verbose("MailItem_ProcessingComplete: Error while checking if mail item is in outbox or updating inspector. " + ex.Message);
    1.92 -                            }
    1.93 +                                catch (Exception ex)
    1.94 +                                {
    1.95 +                                    Log.Verbose("MailItem_ProcessingComplete: Error while checking if mail item is in outbox or updating inspector. " + ex.Message);
    1.96 +                                }
    1.97  
    1.98                              /* Create the unencrypted preview if the mail item is encrypted and
    1.99                               * it is in an encrypted (untrusted) store
   1.100 @@ -1103,77 +1102,82 @@
   1.101                               * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
   1.102                               * it also is initialized after FormRegionPreviewUnencrypted.
   1.103                               */
   1.104 -                            try
   1.105 -                            {
   1.106 -                                if (this.cryptableMailItem.IsSecurelyStored || this.cryptableMailItem.IsSecureAttachedMail)
   1.107 +                                try
   1.108                                  {
   1.109 -                                    Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
   1.110 -                                    this.cryptableMailItem.StartGetMirror(this.cryptableMailItem.MessageId);
   1.111 +                                    if (this.cryptableMailItem.IsSecurelyStored || this.cryptableMailItem.IsSecureAttachedMail)
   1.112 +                                    {
   1.113 +                                        Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
   1.114 +                                        this.cryptableMailItem.StartGetMirror(this.cryptableMailItem.MessageId);
   1.115 +                                    }
   1.116 +                                    else
   1.117 +                                    {
   1.118 +                                        this.SetNote();
   1.119 +                                    }
   1.120                                  }
   1.121 -                                else
   1.122 +                                catch (Exception ex)
   1.123                                  {
   1.124 -                                    this.SetNote();
   1.125 -                                }
   1.126 -                            }
   1.127 -                            catch (Exception ex)
   1.128 -                            {
   1.129                                  // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
   1.130                                  // While rare, just log the issue and stop the process
   1.131                                  Log.Warning("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
   1.132 -                            }
   1.133 +                                }
   1.134  
   1.135                              /* OUT-470: Workaround until this is implemented in the engine:
   1.136                               * Only enable Force Protection if all recipients are grey
   1.137                               */
   1.138 -                            if (this.IsDraft)
   1.139 -                            {
   1.140 -                                bool disableForceProtection = false;
   1.141 -                                Outlook.Recipients recipients = null;
   1.142 -                                Outlook.Recipient recipient = null;
   1.143 -                                PEPIdentity pEpIdentity = null;
   1.144 +                                if (this.IsDraft)
   1.145 +                                {
   1.146 +                                    bool disableForceProtection = false;
   1.147 +                                    Outlook.Recipients recipients = null;
   1.148 +                                    Outlook.Recipient recipient = null;
   1.149 +                                    PEPIdentity pEpIdentity = null;
   1.150  
   1.151 -                                try
   1.152 -                                {
   1.153 -                                    recipients = this.CurrentMailItem?.Recipients;
   1.154 -                                    if (recipients?.Count > 0)
   1.155 +                                    try
   1.156                                      {
   1.157 -                                        for (int i = 1; i <= recipients.Count; i++)
   1.158 +                                        recipients = this.CurrentMailItem?.Recipients;
   1.159 +                                        if (recipients?.Count > 0)
   1.160                                          {
   1.161 -                                            recipient = recipients[i];
   1.162 -                                            if ((PEPIdentity.Create(recipient, out pEpIdentity, false) == Globals.ReturnStatus.Success) &&
   1.163 -                                                ((PEPIdentity.GetIsOwnIdentity(pEpIdentity.Address) ||
   1.164 -                                                 (ThisAddIn.PEPEngine.IdentityRating(pEpIdentity.ToCOMType()) >= pEpRating.pEpRatingReliable))))
   1.165 +                                            for (int i = 1; i <= recipients.Count; i++)
   1.166                                              {
   1.167 -                                                disableForceProtection = true;
   1.168 -                                                Log.Verbose("MailItem_ProcessingComplete: Secure recipient found. Disabling force protection.");
   1.169 -                                                break;
   1.170 +                                                recipient = recipients[i];
   1.171 +                                                if ((PEPIdentity.Create(recipient, out pEpIdentity, false) == Globals.ReturnStatus.Success) &&
   1.172 +                                                    ((PEPIdentity.GetIsOwnIdentity(pEpIdentity.Address) ||
   1.173 +                                                     (ThisAddIn.PEPEngine.IdentityRating(pEpIdentity.ToCOMType()) >= pEpRating.pEpRatingReliable))))
   1.174 +                                                {
   1.175 +                                                    disableForceProtection = true;
   1.176 +                                                    Log.Verbose("MailItem_ProcessingComplete: Secure recipient found. Disabling force protection.");
   1.177 +                                                    break;
   1.178 +                                                }
   1.179                                              }
   1.180                                          }
   1.181 -                                    }
   1.182 -                                    else
   1.183 -                                    {
   1.184 +                                        else
   1.185 +                                        {
   1.186                                          // If there aren't any recipients (anymore), reset values
   1.187                                          this.DisableForceProtection = false;
   1.188 -                                        this.ForceProtection = false;
   1.189 -                                        this.NeverUnsecure = false;
   1.190 -                                        this.ForceUnencrypted = false;
   1.191 +                                            this.ForceProtection = false;
   1.192 +                                            this.NeverUnsecure = false;
   1.193 +                                            this.ForceUnencrypted = false;
   1.194 +                                        }
   1.195                                      }
   1.196 +                                    catch (Exception ex)
   1.197 +                                    {
   1.198 +                                        Log.Error("MailItem_ProcessingComplete: Error occured. " + ex.ToString());
   1.199 +                                    }
   1.200 +                                    finally
   1.201 +                                    {
   1.202 +                                        recipient = null;
   1.203 +                                        recipients = null;
   1.204 +                                    }
   1.205 +
   1.206 +                                    this.DisableForceProtection = disableForceProtection;
   1.207                                  }
   1.208 -                                catch (Exception ex)
   1.209 -                                {
   1.210 -                                    Log.Error("MailItem_ProcessingComplete: Error occured. " + ex.ToString());
   1.211 -                                }
   1.212 -                                finally
   1.213 -                                {
   1.214 -                                    recipient = null;
   1.215 -                                    recipients = null;
   1.216 -                                }
   1.217 -
   1.218 -                                this.DisableForceProtection = disableForceProtection;
   1.219                              }
   1.220                          }
   1.221 -                    }
   1.222 -                }));
   1.223 +                    }));
   1.224 +                }
   1.225 +                else
   1.226 +                {
   1.227 +                    Log.Verbose("MailItem_ProcessingComplete: Calculated rating doesn't correspond to current mail item. Skipping...");
   1.228 +                }
   1.229              }
   1.230              catch (Exception ex)
   1.231              {
   1.232 @@ -1362,8 +1366,8 @@
   1.233                      if ((this.Type == WindowType.Inspector) &&
   1.234                          (this.cryptableMailItem.IsInlineResponse == false) &&
   1.235                          (this.CurrentMailItem.GetProcessingState() == null))
   1.236 -                    {                        
   1.237 -                        IntPtr hWnd = NativeMethods.GetActiveWindow();    
   1.238 +                    {
   1.239 +                        IntPtr hWnd = NativeMethods.GetActiveWindow();
   1.240                          NativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SetWindowPosFlags.DoNotActivate |
   1.241                                                                                                  NativeMethods.SetWindowPosFlags.IgnoreMove |
   1.242                                                                                                  NativeMethods.SetWindowPosFlags.DoNotReposition |