reset iterator on found passphrase Release_2.1.0-RC8
authorVolker Birk <vb@pep-project.org>
Sun, 05 Jul 2020 01:43:57 +0200
changeset 2767c3620e386d6
parent 275 e54a6008b7fa
child 277 e729ecd07115
reset iterator on found passphrase
callback_dispatcher.cc
passphrase_cache.cc
passphrase_cache.hh
     1.1 --- a/callback_dispatcher.cc	Sun Jul 05 01:03:35 2020 +0200
     1.2 +++ b/callback_dispatcher.cc	Sun Jul 05 01:43:57 2020 +0200
     1.3 @@ -95,7 +95,7 @@
     1.4          if (Adapter::on_sync_thread() && !msg) {
     1.5              semaphore.try_wait();
     1.6  
     1.7 -            PEP_STATUS status = PassphraseCache::messageToSend(passphrase_cache, Adapter::session());
     1.8 +            PEP_STATUS status = PassphraseCache::config_next_passphrase();
     1.9  
    1.10              // if the cache has no valid passphrase ask the app
    1.11              if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE)
    1.12 @@ -105,6 +105,11 @@
    1.13              return status;
    1.14          }
    1.15  
    1.16 +        if (Adapter::on_sync_thread()) {
    1.17 +            // a passphrase worked, reset passphrase_cache iterator
    1.18 +            PassphraseCache::config_next_passphrase(true);
    1.19 +        }
    1.20 +
    1.21          for (auto target : targets) {
    1.22              ::message *_msg = nullptr;
    1.23              if (msg) {
     2.1 --- a/passphrase_cache.cc	Sun Jul 05 01:03:35 2020 +0200
     2.2 +++ b/passphrase_cache.cc	Sun Jul 05 01:43:57 2020 +0200
     2.3 @@ -1,4 +1,5 @@
     2.4  #include <cassert>
     2.5 +#include "Adapter.hh"
     2.6  #include "passphrase_cache.hh"
     2.7  
     2.8  pEp::PassphraseCache pEp::passphrase_cache;
     2.9 @@ -117,16 +118,23 @@
    2.10          return _which->passphrase.c_str();
    2.11      }
    2.12  
    2.13 -	PEP_STATUS PassphraseCache::messageToSend(PassphraseCache& cache, PEP_SESSION session)
    2.14 +	PEP_STATUS PassphraseCache::config_next_passphrase(bool reset)
    2.15  	{
    2.16          static pEp::PassphraseCache _copy;
    2.17          static bool new_copy = true;
    2.18 +
    2.19 +        if (reset) {
    2.20 +            new_copy = true;
    2.21 +            return PEP_STATUS_OK;
    2.22 +        }
    2.23 +
    2.24          if (new_copy) {
    2.25 -            _copy = cache;
    2.26 +            _copy = passphrase_cache;
    2.27              new_copy = false;
    2.28          }
    2.29 +
    2.30          try {
    2.31 -            ::config_passphrase(session, _copy.latest_passphrase());
    2.32 +            ::config_passphrase(Adapter::session(), _copy.latest_passphrase());
    2.33              return PEP_STATUS_OK;
    2.34          }
    2.35          catch (pEp::PassphraseCache::Empty&) {
     3.1 --- a/passphrase_cache.hh	Sun Jul 05 01:03:35 2020 +0200
     3.2 +++ b/passphrase_cache.hh	Sun Jul 05 01:43:57 2020 +0200
     3.3 @@ -53,26 +53,12 @@
     3.4          // adds the stored passphrase to the cache, which will not timeout
     3.5          const char *add_stored(const std::string& passphrase);
     3.6  
     3.7 -        // get all passphrases in cache from latest to oldest one by each call
     3.8 -        // this function is throwing PassphraseCache::Empty when cache is empty
     3.9 -        // and PassphraseCache::Exhausted when no passphrases are left, then
    3.10 -        // starts over
    3.11 -
    3.12 -        const char *latest_passphrase();
    3.13 -
    3.14          // call this function inside the messageToSend() implementation of the adapter
    3.15          // this function is using latest_passphrase() to test one passphrase after the
    3.16          // other until the cache is exhausted
    3.17 +        // call with reset = true to reset the iterator
    3.18  
    3.19 -        static PEP_STATUS messageToSend(PassphraseCache& cache, PEP_SESSION session);
    3.20 -
    3.21 -        // for each passphrase call the callee until it returns true for a
    3.22 -        // matching passphrase or no passphrases are left
    3.23 -        // always tests empty passphrase first
    3.24 -        // returns true if a passphrase was matching, false otherwise
    3.25 -
    3.26 -        using passphrase_callee = std::function<bool(std::string)>;
    3.27 -        bool for_each_passphrase(const passphrase_callee& callee);
    3.28 +        static PEP_STATUS config_next_passphrase(bool reset=false);
    3.29  
    3.30          // convenience functions
    3.31          // i.e.
    3.32 @@ -87,6 +73,9 @@
    3.33      protected:
    3.34          void cleanup();
    3.35          void refresh(cache::iterator entry);
    3.36 +        const char *latest_passphrase();
    3.37 +        using passphrase_callee = std::function<bool(std::string)>;
    3.38 +        bool for_each_passphrase(const passphrase_callee& callee);
    3.39      };
    3.40  
    3.41      extern PassphraseCache passphrase_cache;