aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/gpg/GpgAutomatonHandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/function/gpg/GpgAutomatonHandler.cpp')
-rw-r--r--src/core/function/gpg/GpgAutomatonHandler.cpp69
1 files changed, 33 insertions, 36 deletions
diff --git a/src/core/function/gpg/GpgAutomatonHandler.cpp b/src/core/function/gpg/GpgAutomatonHandler.cpp
index 656bb018..afa55299 100644
--- a/src/core/function/gpg/GpgAutomatonHandler.cpp
+++ b/src/core/function/gpg/GpgAutomatonHandler.cpp
@@ -39,65 +39,61 @@ GpgAutomatonHandler::GpgAutomatonHandler(int channel)
auto InteratorCbFunc(void* handle, const char* status, const char* args,
int fd) -> gpgme_error_t {
- auto* handle_struct = static_cast<AutomatonHandelStruct*>(handle);
- QString status_s = status;
- QString args_s = args;
+ auto* handel = static_cast<AutomatonHandelStruct*>(handle);
+ const auto status_s = QString::fromUtf8(status);
+ const auto args_s = QString::fromUtf8(args);
if (status_s == "KEY_CONSIDERED") {
auto tokens = QString(args).split(' ');
- if (handle_struct->KeyFpr().isEmpty()) return 0;
+ if (handel->KeyFpr().isEmpty()) return GPG_ERR_NO_ERROR;
- if (tokens.empty() || tokens[0] != handle_struct->KeyFpr()) {
- LOG_W() << "handle struct key fpr: " << handle_struct->KeyFpr()
+ if (tokens.empty() || tokens[0] != handel->KeyFpr()) {
+ LOG_W() << "handle struct key fpr: " << handel->KeyFpr()
<< "mismatch token: " << tokens[0] << ", exit...";
-
+ handel->SetSuccess(false);
return -1;
}
- return 0;
+ return GPG_ERR_NO_ERROR;
}
if (status_s == "CARDCTRL") {
auto tokens = QString(args).split(' ');
- if (handle_struct->SerialNumber().isEmpty()) return 0;
+ if (handel->SerialNumber().isEmpty()) return GPG_ERR_NO_ERROR;
- if (tokens.empty() || tokens[0] != handle_struct->SerialNumber()) {
- LOG_W() << "handle struct serial number: "
- << handle_struct->SerialNumber()
+ if (tokens.empty() || tokens[0] != handel->SerialNumber()) {
+ LOG_W() << "handle struct serial number: " << handel->SerialNumber()
<< "mismatch token: " << tokens[0] << ", exit...";
-
+ handel->SetSuccess(false);
return -1;
}
- return 0;
+ return GPG_ERR_NO_ERROR;
}
if (status_s == "GOT_IT" || status_s.isEmpty()) {
FLOG_D("gpg reply is GOT_IT, continue...");
- return 0;
+ return GPG_ERR_NO_ERROR;
}
- LOG_D() << "current state" << handle_struct->CurrentStatus()
+ LOG_D() << "current state" << handel->CurrentStatus()
<< "gpg status: " << status_s << ", args: " << args_s;
- handle_struct->SetPromptStatus(status_s, args_s);
+ handel->SetPromptStatus(status_s, args_s);
- AutomatonState next_state = handle_struct->NextState(status_s, args_s);
+ AutomatonState next_state = handel->NextState(status_s, args_s);
if (next_state == GpgAutomatonHandler::kAS_ERROR) {
- FLOG_D("handle struct next state caught error, abort...");
+ FLOG_D("handel next state caught error, abort...");
+ handel->SetSuccess(false);
return -1;
}
- LOG_D() << "next state" << next_state;
-
- if (next_state == GpgAutomatonHandler::kAS_SAVE) {
- handle_struct->SetSuccess(true);
- }
+ LOG_D() << "next state:" << next_state;
// set state and preform action
- handle_struct->SetStatus(next_state);
- GpgAutomatonHandler::Command cmd = handle_struct->Action();
+ handel->SetStatus(next_state);
+ GpgAutomatonHandler::Command cmd = handel->Action();
LOG_D() << "next action, cmd:" << cmd;
@@ -110,32 +106,33 @@ auto InteratorCbFunc(void* handle, const char* status, const char* args,
return GPG_ERR_FALSE;
}
- return 0;
+ return GPG_ERR_NO_ERROR;
}
auto DoInteractImpl(GpgContext& ctx_, const GpgKeyPtr& key, bool card_edit,
const QString& id,
AutomatonNextStateHandler next_state_handler,
- AutomatonActionHandler action_handler, int flags) -> bool {
+ AutomatonActionHandler action_handler,
+ int flags) -> std::tuple<GpgError, bool> {
gpgme_key_t p_key = key == nullptr ? nullptr : static_cast<gpgme_key_t>(*key);
- AutomatonHandelStruct handel_struct(card_edit, id);
- handel_struct.SetHandler(std::move(next_state_handler),
- std::move(action_handler));
+ AutomatonHandelStruct handel(card_edit, id);
+ handel.SetHandler(std::move(next_state_handler), std::move(action_handler));
GpgData data_out;
auto err =
gpgme_op_interact(ctx_.DefaultContext(), p_key, flags, InteratorCbFunc,
- static_cast<void*>(&handel_struct), data_out);
- return CheckGpgError(err) == GPG_ERR_NO_ERROR && handel_struct.Success();
+ static_cast<void*>(&handel), data_out);
+ return {err, handel.Success()};
}
auto GpgAutomatonHandler::DoInteract(
const GpgKeyPtr& key, AutomatonNextStateHandler next_state_handler,
- AutomatonActionHandler action_handler, int flags) -> bool {
+ AutomatonActionHandler action_handler,
+ int flags) -> std::tuple<GpgError, bool> {
assert(key != nullptr);
- if (key == nullptr) return false;
+ if (key == nullptr) return {GPG_ERR_USER_1, false};
return DoInteractImpl(ctx_, key, false, key->ID(),
std::move(next_state_handler),
std::move(action_handler), flags);
@@ -143,7 +140,7 @@ auto GpgAutomatonHandler::DoInteract(
auto GpgAutomatonHandler::DoCardInteract(
const QString& serial_number, AutomatonNextStateHandler next_state_handler,
- AutomatonActionHandler action_handler) -> bool {
+ AutomatonActionHandler action_handler) -> std::tuple<GpgError, bool> {
return DoInteractImpl(ctx_, nullptr, true, serial_number,
std::move(next_state_handler),
std::move(action_handler), GPGME_INTERACT_CARD);