diff --git a/passphrase_cache.cc b/passphrase_cache.cc index 70dc084..de7f2ad 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -81,5 +81,27 @@ namespace pEp { --_which; return _which->passphrase.c_str(); } + + PEP_STATUS PassphraseCache::messageToSend(PassphraseCache&& cache, PEP_SESSION session) + { + static pEp::PassphraseCache _copy; + static bool new_copy = true; + if (new_copy) { + _copy = cache; + new_copy = false; + } + try { + ::config_passphrase(session, _copy.latest_passphrase()); + return PEP_STATUS_OK; + } + catch (pEp::PassphraseCache::Empty&) { + new_copy = true; + return PEP_PASSPHRASE_REQUIRED; + } + catch (pEp::PassphraseCache::Exhausted&) { + new_copy = true; + return PEP_WRONG_PASSPHRASE; + } + } }; diff --git a/passphrase_cache.hh b/passphrase_cache.hh index 3205a72..7985072 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -48,6 +48,19 @@ namespace pEp { const char *add(std::string passphrase); + // get all passphrases in cache from latest to oldest one by each call + // this function is throwing PassphraseCache::Empty when cache is empty + // and PassphraseCache::Exhausted when no passphrases are left, then + // starts over + + const char *latest_passphrase(); + + // call this function inside the messageToSend() implementation of the adapter + // this function is using latest_passphrase() to test one passphrase after the + // other until the cache is exhausted + + static PEP_STATUS messageToSend(PassphraseCache&& cache, PEP_SESSION session); + // for each passphrase call the callee until it returns true for a // matching passphrase or no passphrases are left // always tests empty passphrase first @@ -56,13 +69,6 @@ namespace pEp { using passphrase_callee = std::function; bool for_each_passphrase(const passphrase_callee& callee); - // get all passphrases in cache from latest to oldest one by each call - // this function is throwing PassphraseCache::Empty when cache is empty - // and PassphraseCache::Exhausted when no passphrases are left, then - // starts over - - const char *latest_passphrase(); - // convenience functions // i.e. // status = cache.api(::encrypt_message, session, src, extra, dst, enc_format, flags)