OUT-508: Get the Autocrypt header from messages and pass it to the engine.
authorThomas
Wed, 10 Oct 2018 13:52:41 +0200
changeset 24000d9a4a2dc8f4
parent 2395 92cd60e3aa02
child 2401 86384b7195bb
OUT-508: Get the Autocrypt header from messages and pass it to the engine.
PEPMessage.cs
     1.1 --- a/PEPMessage.cs	Mon Oct 08 13:37:58 2018 +0200
     1.2 +++ b/PEPMessage.cs	Wed Oct 10 13:52:41 2018 +0200
     1.3 @@ -5,6 +5,7 @@
     1.4  using System.Collections.Generic;
     1.5  using System.ComponentModel;
     1.6  using System.IO;
     1.7 +using System.Linq;
     1.8  using System.Text;
     1.9  using Outlook = Microsoft.Office.Interop.Outlook;
    1.10  
    1.11 @@ -48,6 +49,7 @@
    1.12          private static object                     mutexMirror           = new object();
    1.13          private static Dictionary<string, string> mirrorCache           = new Dictionary<string, string>();
    1.14  
    1.15 +        private List<StringPair>    _AdditionalHeaders;
    1.16          private List<PEPAttachment> _Attachments;
    1.17          private string              _AutoConsume;
    1.18          private List<PEPIdentity>   _Bcc;
    1.19 @@ -98,6 +100,17 @@
    1.20          #region Property Accessors
    1.21  
    1.22          /// <summary>
    1.23 +        /// Gets the list of additional headers for this message.
    1.24 +        /// Those are headers that are of no direct use for pEp for Outlook and
    1.25 +        /// will just get passed from the Outlook mail item to the engine (via
    1.26 +        /// optional fields)
    1.27 +        /// </summary>
    1.28 +        public List<StringPair> AdditionalHeaders
    1.29 +        {
    1.30 +            get { return this._AdditionalHeaders; }
    1.31 +        }
    1.32 +
    1.33 +        /// <summary>
    1.34          /// Gets the list of attachements for this message.
    1.35          ///          MailItem : Corresponds to property 'Attachments' which contains 'Attachment'
    1.36          /// CryptableMailItem : not exposed
    1.37 @@ -776,6 +789,12 @@
    1.38                  optionalFields.Add(field);
    1.39              }
    1.40  
    1.41 +            // Add additionalHeaders
    1.42 +            foreach (var additionalHeader in this._AdditionalHeaders)
    1.43 +            {
    1.44 +                optionalFields.Add(additionalHeader);
    1.45 +            }
    1.46 +
    1.47              // ReceivedOn
    1.48              recvSec = -1;
    1.49              if (this._ReceivedOn != null)
    1.50 @@ -1075,6 +1094,12 @@
    1.51                  }
    1.52              }
    1.53  
    1.54 +            // Additional headers
    1.55 +            foreach (var additionalHeader in pEpMessage.AdditionalHeaders)
    1.56 +            {
    1.57 +                message.Headers?.Add(new Header(additionalHeader.Name, additionalHeader.Value));
    1.58 +            }
    1.59 +
    1.60              // Message date
    1.61              try
    1.62              {
    1.63 @@ -1136,7 +1161,8 @@
    1.64                  return (false);
    1.65              }
    1.66  
    1.67 -            if (Comparisons.Equals(this.Attachments, obj.Attachments) &&
    1.68 +            if (Comparisons.Equals(this.AdditionalHeaders, obj.AdditionalHeaders) &&
    1.69 +                Comparisons.Equals(this.Attachments, obj.Attachments) &&
    1.70                  Comparisons.Equals(this.Bcc, obj.Bcc) &&
    1.71                  Comparisons.Equals(this.Cc, obj.Cc) &&
    1.72                  Comparisons.Equals(this.ConversationId, obj.ConversationId) &&
    1.73 @@ -1214,6 +1240,13 @@
    1.74          {
    1.75              PEPMessage copy = new PEPMessage();
    1.76  
    1.77 +            // Additional headers
    1.78 +            copy.AdditionalHeaders.Clear();
    1.79 +            foreach (var additionalHeader in this._AdditionalHeaders)
    1.80 +            {
    1.81 +                copy.AdditionalHeaders.Add(additionalHeader);
    1.82 +            }
    1.83 +
    1.84              // Attachments
    1.85              copy.Attachments.Clear();
    1.86              if (createWithoutContent == false)
    1.87 @@ -1314,6 +1347,7 @@
    1.88          /// </summary>
    1.89          public void Reset()
    1.90          {
    1.91 +            this._AdditionalHeaders = new List<StringPair>();
    1.92              this._Attachments = new List<PEPAttachment>();
    1.93              this._AutoConsume = null;
    1.94              this._Bcc = new List<PEPIdentity>();
    1.95 @@ -1588,7 +1622,7 @@
    1.96                          if (string.IsNullOrWhiteSpace(this._Bcc[i].Address) == false)
    1.97                          {
    1.98                              // Add by address
    1.99 -                            newRecipient = recipients.Add(this._Bcc[i].Address);                            
   1.100 +                            newRecipient = recipients.Add(this._Bcc[i].Address);
   1.101                              newRecipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
   1.102  
   1.103                              // Marshal.ReleaseComObject(newRecipient);
   1.104 @@ -1633,7 +1667,7 @@
   1.105                          {
   1.106                              // Add by address
   1.107                              newRecipient = recipients.Add(this._To[i].Address);
   1.108 -                            newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;                           
   1.109 +                            newRecipient.Type = (int)Outlook.OlMailRecipientType.olTo;
   1.110  
   1.111                              // Marshal.ReleaseComObject(newRecipient);
   1.112                              newRecipient = null;
   1.113 @@ -2747,6 +2781,26 @@
   1.114                      HeaderList headers = omi.GetParsedTransportMessageHeaders();
   1.115                      Log.Verbose("PEPMessage.Create: Transport headers retrieved.");
   1.116                      newMessage.SetPEPProperties(omi, headers);
   1.117 +
   1.118 +                    // Add additional headers
   1.119 +                    string autocryptHeaderKey = "Autocrypt";
   1.120 +                    try
   1.121 +                    {
   1.122 +                        Header ac = headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(autocryptHeaderKey, StringComparison.OrdinalIgnoreCase) == true));
   1.123 +                        if (ac != null)
   1.124 +                        {
   1.125 +                            StringPair autocryptHeader = new StringPair
   1.126 +                            {
   1.127 +                                Name = ac.Field.Trim(),
   1.128 +                                Value = ac.Value.Trim()
   1.129 +                            };
   1.130 +                            newMessage.AdditionalHeaders.Add(autocryptHeader);
   1.131 +                        }
   1.132 +                    }
   1.133 +                    catch (Exception ex)
   1.134 +                    {
   1.135 +                        Log.Error("PEPMessage.Create: Error adding additional headers. " + ex.ToString());
   1.136 +                    }
   1.137                  }
   1.138  
   1.139                  Log.Verbose("PEPMessage.Create: New PEPMessage created from OMI.");
   1.140 @@ -2915,39 +2969,70 @@
   1.141                      pEpMessage.From = ident;
   1.142                  }
   1.143  
   1.144 -                // pEp header fields
   1.145                  if (mimeMessage.Headers != null)
   1.146                  {
   1.147 -                    foreach (var header in mimeMessage.Headers)
   1.148 +                    // pEp header fields
   1.149 +                    // Key List
   1.150 +                    Header keyList = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(PEPMessage.PR_KEY_LIST_NAME, StringComparison.OrdinalIgnoreCase) == true));
   1.151 +                    if (keyList != null)
   1.152                      {
   1.153 -                        if (header?.Field != null)
   1.154 +                        pEpMessage.KeyList = keyList.Value?.Trim();
   1.155 +                    }
   1.156 +
   1.157 +                    // Key Import
   1.158 +                    Header keyImport = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(PEPMessage.PR_PEP_KEY_IMPORT_NAME, StringComparison.OrdinalIgnoreCase) == true));
   1.159 +                    if (keyImport != null)
   1.160 +                    {
   1.161 +                        pEpMessage.KeyImport = keyImport.Value?.Trim();
   1.162 +                    }
   1.163 +
   1.164 +                    // Auto consume
   1.165 +                    Header autoConsume = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(PEPMessage.PR_PEP_AUTO_CONSUME_NAME, StringComparison.OrdinalIgnoreCase) == true));
   1.166 +                    if (autoConsume != null)
   1.167 +                    {
   1.168 +                        pEpMessage.AutoConsume = autoConsume.Value?.Trim();
   1.169 +                    }
   1.170 +
   1.171 +                    // Force protection
   1.172 +                    Header forceProtection = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(PEPMessage.PR_PEP_FORCE_PROTECTION_NAME, StringComparison.OrdinalIgnoreCase) == true));
   1.173 +                    if (forceProtection != null)
   1.174 +                    {
   1.175 +                        pEpMessage.ForceProtectionId = forceProtection.Value?.Trim();
   1.176 +                    }
   1.177 +
   1.178 +                    // Never unsecure
   1.179 +                    Header neverUnsecure = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(PEPMessage.PR_PEP_NEVER_UNSECURE_NAME, StringComparison.OrdinalIgnoreCase) == true));
   1.180 +                    if (neverUnsecure != null)
   1.181 +                    {
   1.182 +                        pEpMessage.NeverUnsecure = true;
   1.183 +                    }
   1.184 +
   1.185 +                    // pEp protocol version
   1.186 +                    Header pEpVersion = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(PEPMessage.PR_PEP_PROTOCOL_VERSION_NAME, StringComparison.OrdinalIgnoreCase) == true));
   1.187 +                    if (pEpVersion != null)
   1.188 +                    {
   1.189 +                        pEpMessage.PEPProtocolVersion = pEpVersion.Value?.Trim();
   1.190 +                    }
   1.191 +
   1.192 +                    // Additional headers
   1.193 +                    string autocryptHeaderKey = "Autocrypt";
   1.194 +                    try
   1.195 +                    {
   1.196 +                        Header ac = mimeMessage.Headers.FirstOrDefault(a => (a.Field?.Trim()?.Equals(autocryptHeaderKey, StringComparison.OrdinalIgnoreCase) == true));
   1.197 +                        if (ac != null)
   1.198                          {
   1.199 -                            if (string.Equals(header.Field.Trim(), PEPMessage.PR_KEY_LIST_NAME, StringComparison.OrdinalIgnoreCase))
   1.200 +                            StringPair autocryptHeader = new StringPair
   1.201                              {
   1.202 -                                pEpMessage.KeyList = header.Value;
   1.203 -                            }
   1.204 -                            else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_KEY_IMPORT_NAME, StringComparison.OrdinalIgnoreCase))
   1.205 -                            {
   1.206 -                                pEpMessage.KeyImport = header.Value;
   1.207 -                            }
   1.208 -                            else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_AUTO_CONSUME_NAME, StringComparison.OrdinalIgnoreCase))
   1.209 -                            {
   1.210 -                                pEpMessage.AutoConsume = header.Value;
   1.211 -                            }
   1.212 -                            else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_FORCE_PROTECTION_NAME, StringComparison.OrdinalIgnoreCase))
   1.213 -                            {
   1.214 -                                pEpMessage.ForceProtectionId = header.Value;
   1.215 -                            }
   1.216 -                            else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_NEVER_UNSECURE_NAME, StringComparison.OrdinalIgnoreCase))
   1.217 -                            {
   1.218 -                                pEpMessage.NeverUnsecure = true;
   1.219 -                            }
   1.220 -                            else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_PROTOCOL_VERSION_NAME, StringComparison.OrdinalIgnoreCase))
   1.221 -                            {
   1.222 -                                pEpMessage.PEPProtocolVersion = header.Value;
   1.223 -                            }
   1.224 +                                Name = ac.Field.Trim(),
   1.225 +                                Value = ac.Value.Trim()
   1.226 +                            };
   1.227 +                            pEpMessage.AdditionalHeaders.Add(autocryptHeader);
   1.228                          }
   1.229                      }
   1.230 +                    catch (Exception ex)
   1.231 +                    {
   1.232 +                        Log.Error("PEPMessage.Create: Error adding additional headers. " + ex.ToString());
   1.233 +                    }
   1.234                  }
   1.235  
   1.236                  // Message date