Revise PEPIdentity.Create and support exchange users better.
authorDean
Thu, 25 Aug 2016 12:51:27 +0200
changeset 1202006d12ed6d7f
parent 1199 543e9c3fa0e3
child 1203 d4c1b735dd5a
Revise PEPIdentity.Create and support exchange users better.
CryptableMailItem.cs
PEPIdentity.cs
PEPMessage.cs
UI/FormRegionPrivacyStatus.cs
     1.1 --- a/CryptableMailItem.cs	Thu Aug 25 10:35:34 2016 +0200
     1.2 +++ b/CryptableMailItem.cs	Thu Aug 25 12:51:27 2016 +0200
     1.3 @@ -727,7 +727,7 @@
     1.4                      {
     1.5                          currRecipient = recipients[i];
     1.6  
     1.7 -                        sts = PEPIdentity.ToIdentity(currRecipient, out ident);
     1.8 +                        sts = PEPIdentity.Create(currRecipient, out ident);
     1.9                          if (sts == Globals.ReturnStatus.Success)
    1.10                          {
    1.11                              list.Add(ident);
     2.1 --- a/PEPIdentity.cs	Thu Aug 25 10:35:34 2016 +0200
     2.2 +++ b/PEPIdentity.cs	Thu Aug 25 12:51:27 2016 +0200
     2.3 @@ -1384,23 +1384,24 @@
     2.4          }
     2.5  
     2.6          /// <summary>
     2.7 -        /// Converts/determines the pEp identity of the given outlook recipient.
     2.8 +        /// Creates a new pEp identity from the given outlook recipient.
     2.9          /// The output will never be null.
    2.10          /// Warning: The result can contain identity groups.
    2.11          /// </summary>
    2.12          /// <param name="recipient">The recipient to determine pEp identity for.</param>
    2.13          /// <param name="identity">The output pEp identity of the recipient (will never be null).</param>
    2.14          /// <returns>The status of the method.</returns>
    2.15 -        public static Globals.ReturnStatus ToIdentity(Outlook.Recipient recipient,
    2.16 -                                                      out PEPIdentity identity)
    2.17 +        public static Globals.ReturnStatus Create(Outlook.Recipient recipient,
    2.18 +                                                  out PEPIdentity identity)
    2.19          {
    2.20              bool identityCreated = false;
    2.21              bool? forceUnencryptedProperty = null;
    2.22 -            string tempAddress = null;
    2.23 +            string tempAddress;
    2.24 +            string tempUserName;
    2.25              PEPIdentity newIdent = new PEPIdentity();
    2.26              PEPIdentity newIdent2;
    2.27              PEPIdentity member;
    2.28 -            Outlook.OlAddressEntryUserType? addressEntryType = null;
    2.29 +            Outlook.OlAddressEntryUserType? dlType = null;
    2.30              Outlook.AddressEntry addressEntry = null;
    2.31              Outlook.AddressEntry currAddressEntry = null;
    2.32              Outlook.ContactItem contact = null;
    2.33 @@ -1416,6 +1417,10 @@
    2.34              Globals.ReturnStatus sts;
    2.35              Globals.ReturnStatus status = Globals.ReturnStatus.Success;
    2.36  
    2.37 +            ///////////////////////////////////////////////////////////
    2.38 +            // Pre-process
    2.39 +            ///////////////////////////////////////////////////////////
    2.40 +
    2.41              try
    2.42              {
    2.43                  // Get the address entry of the recipient
    2.44 @@ -1441,12 +1446,12 @@
    2.45                          ((addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olOutlookDistributionListAddressEntry) ||
    2.46                           (addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)))
    2.47                      {
    2.48 -                        addressEntryType = addressEntry.AddressEntryUserType;
    2.49 +                        dlType = addressEntry.AddressEntryUserType;
    2.50                      }
    2.51                  }
    2.52                  catch
    2.53                  {
    2.54 -                    addressEntryType = null;
    2.55 +                    dlType = null;
    2.56                  }
    2.57  
    2.58                  // Add the contact force unencrypted property (and get contact)
    2.59 @@ -1469,10 +1474,14 @@
    2.60              }
    2.61              catch { }
    2.62  
    2.63 +            ///////////////////////////////////////////////////////////
    2.64 +            // Handle Outlook distribution lists
    2.65 +            ///////////////////////////////////////////////////////////
    2.66 +
    2.67              // Handle when the recipient is a local Outlook distribution list/contact group
    2.68              if ((identityCreated == false) &&
    2.69 -                (addressEntryType != null) &&
    2.70 -                (((Outlook.OlAddressEntryUserType)addressEntryType) == Outlook.OlAddressEntryUserType.olOutlookDistributionListAddressEntry))
    2.71 +                (dlType != null) &&
    2.72 +                (((Outlook.OlAddressEntryUserType)dlType) == Outlook.OlAddressEntryUserType.olOutlookDistributionListAddressEntry))
    2.73              {
    2.74                  try
    2.75                  {
    2.76 @@ -1505,7 +1514,7 @@
    2.77                                          currRecipient = currList.GetMember(j);
    2.78  
    2.79                                          // Recursively get the member, this should handle contact groups in contact groups
    2.80 -                                        sts = PEPIdentity.ToIdentity(currRecipient, out member);
    2.81 +                                        sts = PEPIdentity.Create(currRecipient, out member);
    2.82                                          newIdent.Members.Add(member);
    2.83  
    2.84                                          // Free currRecipient
    2.85 @@ -1586,12 +1595,16 @@
    2.86                  }
    2.87              }
    2.88  
    2.89 +            ///////////////////////////////////////////////////////////
    2.90 +            // Handle exchange distribution lists
    2.91 +            ///////////////////////////////////////////////////////////
    2.92 +
    2.93              // Handle when the recipient is an exchange distribution list
    2.94              // See: https://msdn.microsoft.com/en-us/library/office/bb645998.aspx
    2.95              if ((identityCreated == false) &&
    2.96                  (addressEntry != null) &&
    2.97 -                (addressEntryType != null) &&
    2.98 -                (((Outlook.OlAddressEntryUserType)addressEntryType) == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry))
    2.99 +                (dlType != null) &&
   2.100 +                (((Outlook.OlAddressEntryUserType)dlType) == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry))
   2.101              {
   2.102                  try
   2.103                  {
   2.104 @@ -1701,13 +1714,40 @@
   2.105                  }
   2.106              }
   2.107  
   2.108 +            ///////////////////////////////////////////////////////////
   2.109              // Handle standard recipients (not lists)
   2.110 +            ///////////////////////////////////////////////////////////
   2.111 +
   2.112              if (identityCreated == false)
   2.113              {
   2.114 +                exchUser = null;
   2.115 +                tempAddress = null;
   2.116 +                tempUserName = null;
   2.117                  newIdent = new PEPIdentity();
   2.118                  newIdent.IsForceUnencrypted = forceUnencryptedProperty;
   2.119  
   2.120 -                /* Check for an address from the attached SMTP address
   2.121 +                // Handle any exchange users first
   2.122 +                if ((addressEntry != null) &&
   2.123 +                    (addressEntry.Type == "EX"))
   2.124 +                {
   2.125 +                    try
   2.126 +                    {
   2.127 +                        exchUser = addressEntry.GetExchangeUser();
   2.128 +                        tempAddress = exchUser.PrimarySmtpAddress;
   2.129 +                        tempUserName = exchUser.Name;
   2.130 +                    }
   2.131 +                    catch { }
   2.132 +                    finally
   2.133 +                    {
   2.134 +                        if (exchUser != null)
   2.135 +                        {
   2.136 +                            Marshal.ReleaseComObject(exchUser);
   2.137 +                            exchUser = null;
   2.138 +                        }
   2.139 +                    }
   2.140 +                }
   2.141 +
   2.142 +                /* Check for an address from the recipient's attached SMTP address
   2.143                   * This is the microsoft recommented method.
   2.144                   * See: https://msdn.microsoft.com/en-us/library/office/ff868695.aspx
   2.145                   */
   2.146 @@ -1720,13 +1760,19 @@
   2.147                      catch { }
   2.148                  }
   2.149  
   2.150 -                // Check for an address from the associated contact
   2.151 +                // Get user name from the recipient
   2.152 +                if (string.IsNullOrWhiteSpace(tempUserName))
   2.153 +                {
   2.154 +                    tempUserName = recipient.Name;
   2.155 +                }
   2.156 +
   2.157 +                // As a fallback, get information from the associated contact
   2.158                  if (string.IsNullOrWhiteSpace(tempAddress))
   2.159                  {
   2.160                      try
   2.161                      {
   2.162 -                        // Can fail from no AddressEntry/Contact
   2.163                          tempAddress = contact.Email1Address;
   2.164 +                        tempUserName = contact.Email1DisplayName;
   2.165                      }
   2.166                      catch { }
   2.167                  }
   2.168 @@ -1734,11 +1780,16 @@
   2.169                  // Add address if possible
   2.170                  if (string.IsNullOrWhiteSpace(tempAddress) == false)
   2.171                  {
   2.172 -                    newIdent.Address = tempAddress;
   2.173 +                    newIdent.Address = tempAddress.Trim();
   2.174                  }
   2.175  
   2.176                  // Add username if possible
   2.177 -                newIdent.Username = recipient.Name;
   2.178 +                if (string.IsNullOrWhiteSpace(tempUserName) == false)
   2.179 +                {
   2.180 +                    newIdent.Username = tempUserName.Trim();
   2.181 +                }
   2.182 +
   2.183 +                // Set the address as the username if username was empty
   2.184                  if ((string.IsNullOrEmpty(newIdent.Username) == true) &&
   2.185                      (string.IsNullOrWhiteSpace(newIdent.Address) == false))
   2.186                  {
   2.187 @@ -1746,8 +1797,7 @@
   2.188                  }
   2.189  
   2.190                  // Get the unique user ID if possible
   2.191 -                if ((string.IsNullOrEmpty(newIdent.Username) == false) &&
   2.192 -                    (string.IsNullOrWhiteSpace(newIdent.Address) == false))
   2.193 +                if (string.IsNullOrEmpty(newIdent.Address) == false)
   2.194                  {
   2.195                      newIdent.UserID = Globals.ThisAddIn.GetUserID(newIdent.Address, contact);
   2.196                  }
     3.1 --- a/PEPMessage.cs	Thu Aug 25 10:35:34 2016 +0200
     3.2 +++ b/PEPMessage.cs	Thu Aug 25 12:51:27 2016 +0200
     3.3 @@ -1653,7 +1653,7 @@
     3.4                      {
     3.5                          case Outlook.OlMailRecipientType.olBCC:
     3.6                              {
     3.7 -                                sts = PEPIdentity.ToIdentity(currRecipient, out ident);
     3.8 +                                sts = PEPIdentity.Create(currRecipient, out ident);
     3.9                                  if (sts == Globals.ReturnStatus.Success)
    3.10                                  {
    3.11                                      newMessage.BCC.Add(ident);
    3.12 @@ -1673,7 +1673,7 @@
    3.13                              }
    3.14                          case Outlook.OlMailRecipientType.olCC:
    3.15                              {
    3.16 -                                sts = PEPIdentity.ToIdentity(currRecipient, out ident);
    3.17 +                                sts = PEPIdentity.Create(currRecipient, out ident);
    3.18                                  if (sts == Globals.ReturnStatus.Success)
    3.19                                  {
    3.20                                      newMessage.CC.Add(ident);
    3.21 @@ -1693,7 +1693,7 @@
    3.22                              }
    3.23                          case Outlook.OlMailRecipientType.olTo:
    3.24                              {
    3.25 -                                sts = PEPIdentity.ToIdentity(currRecipient, out ident);
    3.26 +                                sts = PEPIdentity.Create(currRecipient, out ident);
    3.27                                  if (sts == Globals.ReturnStatus.Success)
    3.28                                  {
    3.29                                      newMessage.To.Add(ident);
     4.1 --- a/UI/FormRegionPrivacyStatus.cs	Thu Aug 25 10:35:34 2016 +0200
     4.2 +++ b/UI/FormRegionPrivacyStatus.cs	Thu Aug 25 12:51:27 2016 +0200
     4.3 @@ -727,7 +727,7 @@
     4.4  
     4.5                          if (sendingAccount == null)
     4.6                          {
     4.7 -                            sts = PEPIdentity.ToIdentity(currUser, out currIdent);
     4.8 +                            sts = PEPIdentity.Create(currUser, out currIdent);
     4.9  
    4.10                              if (sts == Globals.ReturnStatus.Success)
    4.11                              {