OUT-87: Complete the new blacklist implementation.
authorDean
Thu, 08 Sep 2016 12:31:27 +0200
changeset 1250e219136cec9d
parent 1249 00cc05656f71
child 1251 4fe048bff67e
OUT-87: Complete the new blacklist implementation.
PEPIdentity.cs
ThisAddIn.cs
     1.1 --- a/PEPIdentity.cs	Wed Sep 07 22:31:02 2016 +0200
     1.2 +++ b/PEPIdentity.cs	Thu Sep 08 12:31:27 2016 +0200
     1.3 @@ -588,7 +588,7 @@
     1.4           *************************************************************/
     1.5  
     1.6          /// <summary>
     1.7 -        /// Parses the given string as a PEPIdentity.
     1.8 +        /// Parses the given string as a new PEPIdentity. This will never return null.
     1.9          /// The string should be of the format 'user name &lt;email@address.com&gt;'
    1.10          /// If the string does not have both &lt; and &gt; it will be read as an address if it is a valid address, 
    1.11          /// otherwise a username.
    1.12 @@ -603,7 +603,7 @@
    1.13              string userName;
    1.14              string address;
    1.15              string[] temp;
    1.16 -            PEPIdentity result = null;
    1.17 +            PEPIdentity result = new PEPIdentity();
    1.18  
    1.19              if (string.IsNullOrWhiteSpace(str) == false)
    1.20              {
     2.1 --- a/ThisAddIn.cs	Wed Sep 07 22:31:02 2016 +0200
     2.2 +++ b/ThisAddIn.cs	Thu Sep 08 12:31:27 2016 +0200
     2.3 @@ -1205,7 +1205,7 @@
     2.4          /// <param name="state">The state to copy data from.</param>
     2.5          internal void SetOptionsState(FormControlOptions.State state)
     2.6          {
     2.7 -            bool exists;
     2.8 +            bool isBlacklisted;
     2.9              string fpr1;
    2.10              string fpr2;
    2.11              string[] blacklist;
    2.12 @@ -1213,45 +1213,79 @@
    2.13  
    2.14              if (state != null)
    2.15              {
    2.16 -                /*
    2.17                  // Remove any fingerprints from blacklist
    2.18 -                blacklist = ThisAddIn.PEPEngine.blacklist_retrieve();
    2.19 +                try
    2.20 +                {
    2.21 +                    blacklist = ThisAddIn.PEPEngine.blacklist_retrieve();
    2.22 +                }
    2.23 +                catch (COMException ex)
    2.24 +                {
    2.25 +                    blacklist = new string[0];
    2.26 +                    Log.Error("SetOptionsState: Error getting blacklist from engine. " + ex.ToString());
    2.27 +                }
    2.28 +
    2.29                  if (blacklist != null)
    2.30                  {
    2.31                      for (int i = (blacklist.Length - 1); i >= 0; i--)
    2.32                      {
    2.33                          fpr1 = this.RemoveFprFormatting(blacklist[i]);
    2.34 -
    2.35 -                        // Check if fingerprint still exists
    2.36 -                        exists = false;
    2.37 -                        for (int j = 0; j < state.Blacklist.Count; j++)
    2.38 +                        isBlacklisted = false;
    2.39 +
    2.40 +                        if (string.IsNullOrEmpty(fpr1) == false)
    2.41                          {
    2.42 -                            fpr2 = this.RemoveFprFormatting(state.Blacklist[j]);
    2.43 -                            if (fpr1 == fpr2)
    2.44 +                            // Check if fingerprint is still blacklisted
    2.45 +                            foreach (KVPair<PEPIdentity, bool> entry in state.KeyList)
    2.46                              {
    2.47 -                                exists = true;
    2.48 -                                break;
    2.49 +                                fpr2 = this.RemoveFprFormatting(entry.Key.Fingerprint);
    2.50 +
    2.51 +                                // Value is true if the entry is blacklisted
    2.52 +                                if ((string.IsNullOrEmpty(fpr2) == false) &&
    2.53 +                                    (fpr1 == fpr2) &&
    2.54 +                                    (entry.Value))
    2.55 +                                {
    2.56 +                                    isBlacklisted = true;
    2.57 +                                    break;
    2.58 +                                }
    2.59                              }
    2.60 -                        }
    2.61 -
    2.62 -                        if (exists == false)
    2.63 -                        {
    2.64 -                            ThisAddIn.PEPEngine.blacklist_delete(fpr1);
    2.65 +
    2.66 +                            if (isBlacklisted == false)
    2.67 +                            {
    2.68 +                                try
    2.69 +                                {
    2.70 +                                    ThisAddIn.PEPEngine.blacklist_delete(fpr1);
    2.71 +                                }
    2.72 +                                catch (COMException ex)
    2.73 +                                {
    2.74 +                                    Log.Error("SetOptionsState: Failed to delete blacklist entry, " + ex.ToString());
    2.75 +                                }
    2.76 +                            }
    2.77                          }
    2.78                      }
    2.79                  }
    2.80  
    2.81 -                // Add any fingerprints to the blacklist
    2.82 -                for (int i = 0; i < state.Blacklist.Count; i++)
    2.83 +                // Add any new fingerprints to the blacklist
    2.84 +                foreach (KVPair<PEPIdentity, bool> entry in state.KeyList)
    2.85                  {
    2.86 -                    fpr1 = this.RemoveFprFormatting(state.Blacklist[i]);
    2.87 -
    2.88 -                    if (ThisAddIn.PEPEngine.blacklist_is_listed(fpr1) == false)
    2.89 +                    fpr1 = this.RemoveFprFormatting(entry.Key.Fingerprint);
    2.90 +
    2.91 +                    // Value is true if the entry is blacklisted
    2.92 +                    if ((entry.Value) &&
    2.93 +                        (string.IsNullOrEmpty(fpr1) == false))
    2.94                      {
    2.95 -                        ThisAddIn.PEPEngine.blacklist_add(fpr1);
    2.96 +                        try
    2.97 +                        {
    2.98 +                            if (ThisAddIn.PEPEngine.blacklist_is_listed(fpr1) == false)
    2.99 +                            {
   2.100 +                                ThisAddIn.PEPEngine.blacklist_add(fpr1);
   2.101 +                            }
   2.102 +                        }
   2.103 +                        catch (COMException ex)
   2.104 +                        {
   2.105 +                            Log.Error("SetOptionsState: Failed to add new blacklist entry, " + ex.ToString());
   2.106 +                        }
   2.107                      }
   2.108                  }
   2.109 -                */
   2.110 +
   2.111                  foreach (PEPSettings.PEPAccountSettings entry in state.AccountSettingsList)
   2.112                  {
   2.113                      /* pEp internally uses account types as defined by microsoft.
   2.114 @@ -1300,7 +1334,11 @@
   2.115          internal FormControlOptions.State GetOptionsState()
   2.116          {
   2.117              bool isDefaultStore = false;
   2.118 +            bool isBlacklisted;
   2.119 +            string fpr1;
   2.120 +            string fpr2;
   2.121              string[] blacklist;
   2.122 +            opt_field[] keylist;
   2.123              PEPIdentity newIdent;
   2.124              PEPSettings.PEPAccountSettings newAcctSettings;
   2.125              FormControlOptions.State state;
   2.126 @@ -1328,7 +1366,18 @@
   2.127                  state = new FormControlOptions.State();
   2.128              }
   2.129  
   2.130 -            //  Build blacklist
   2.131 +            // Get complete OpenPGP key list
   2.132 +            try
   2.133 +            {
   2.134 +                keylist = ThisAddIn.PEPEngine.OpenPGP_list_keyinfo(null);
   2.135 +            }
   2.136 +            catch (COMException ex)
   2.137 +            {
   2.138 +                keylist = new opt_field[0];
   2.139 +                Log.Error("GetOptionsState: Error getting OpenPGP keylist from engine. " + ex.ToString());
   2.140 +            }
   2.141 +
   2.142 +            // Get the blacklist
   2.143              try
   2.144              {
   2.145                  blacklist = ThisAddIn.PEPEngine.blacklist_retrieve();
   2.146 @@ -1339,24 +1388,39 @@
   2.147                  Log.Error("GetOptionsState: Error getting blacklist from engine. " + ex.ToString());
   2.148              }
   2.149  
   2.150 -            // TODO: remove this when adapter is updated, blacklist is non-functional right now
   2.151 -            blacklist = new string[]
   2.152 +            // Merge the keylist and blacklist together creating final keylist
   2.153 +            state.KeyList.Clear();
   2.154 +            if (keylist != null)
   2.155              {
   2.156 -                "Fingerprint1",
   2.157 -                "Fingerprint2",
   2.158 -                "Fingerprint3"
   2.159 -            };
   2.160 -
   2.161 -            state.KeyList.Clear();
   2.162 -            if (blacklist != null)
   2.163 -            {
   2.164 -                for (int i = 0; i < blacklist.Length; i++)
   2.165 +                foreach (opt_field ident in keylist)
   2.166                  {
   2.167 -                    newIdent = new PEPIdentity();
   2.168 -                    newIdent.Username = "User " + i.ToString();
   2.169 -                    newIdent.Fingerprint = this.ToQuadruple(this.RemoveFprFormatting(blacklist[i]), false);
   2.170 -
   2.171 -                    state.KeyList.Add(new KVPair<PEPIdentity, bool>(newIdent, false));
   2.172 +                    fpr1 = this.RemoveFprFormatting(ident.name);
   2.173 +                    isBlacklisted = false;
   2.174 +
   2.175 +                    if (string.IsNullOrEmpty(fpr1) == false)
   2.176 +                    {
   2.177 +                        // Check if it exists in the blacklist
   2.178 +                        if (blacklist != null)
   2.179 +                        {
   2.180 +                            for (int i = 0; i < blacklist.Length; i++)
   2.181 +                            {
   2.182 +                                fpr2 = this.RemoveFprFormatting(blacklist[i]);
   2.183 +
   2.184 +                                if ((string.IsNullOrEmpty(fpr2) == false) &&
   2.185 +                                    (fpr1 == fpr2))
   2.186 +                                {
   2.187 +                                    isBlacklisted = true;
   2.188 +                                    break;
   2.189 +                                }
   2.190 +                            }
   2.191 +                        }
   2.192 +
   2.193 +                        // Build entry to add
   2.194 +                        newIdent = PEPIdentity.Parse(ident.value);
   2.195 +                        newIdent.Fingerprint = this.ToQuadruple(fpr1, false);
   2.196 +
   2.197 +                        state.KeyList.Add(new KVPair<PEPIdentity, bool>(newIdent, isBlacklisted));
   2.198 +                    }
   2.199                  }
   2.200              }
   2.201              state.KeyListSearchCriteria = null;