From ac023b74a709875dcb75b05e15e7a567d15060ae Mon Sep 17 00:00:00 2001 From: djcb Date: Sat, 11 Oct 2014 14:05:08 +0300 Subject: [PATCH] mu-msg-crypto: give better error when gpg is not found also fixes some other warnings --- lib/mu-msg-crypto.c | 52 ++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/mu-msg-crypto.c b/lib/mu-msg-crypto.c index a4db5fbc..b77fc90c 100644 --- a/lib/mu-msg-crypto.c +++ b/lib/mu-msg-crypto.c @@ -99,26 +99,46 @@ dummy_password_func (const char *user_id, const char *prompt_ctx, } +static char* +get_gpg (GError **err) +{ + char *path; + const char *envpath; + + if ((envpath = g_getenv ("MU_GPG_PATH"))) { + if (access (envpath, X_OK) != 0) { + mu_util_g_set_error ( + err, MU_ERROR, + "'%s': not a valid gpg path: %s", + envpath, strerror (errno)); + return NULL; + } + return g_strdup (envpath); + } + + if (!(path = g_find_program_in_path ("gpg")) && + !(path = g_find_program_in_path ("gpg2"))) { + mu_util_g_set_error (err, MU_ERROR, "gpg/gpg2 not found"); + return NULL; + } else + return path; +} + + static GMimeCryptoContext* get_gpg_crypto_context (MuMsgOptions opts, GError **err) { - GMimeCryptoContext *cctx; - const char *prog; + GMimeCryptoContext *cctx; + char *gpg; cctx = NULL; + if (!(gpg = get_gpg (err))) + return NULL; + + cctx = g_mime_gpg_context_new ( + (GMimePasswordRequestFunc)password_requester, gpg); + g_free (gpg); - prog = g_getenv ("MU_GPG_PATH"); - if (prog) - cctx = g_mime_gpg_context_new ( - (GMimePasswordRequestFunc)password_requester, prog); - else { - char *path; - path = g_find_program_in_path ("gpg"); - if (path) - cctx = g_mime_gpg_context_new ( - password_requester, path); - g_free (path); - } if (!cctx) { mu_util_g_set_error (err, MU_ERROR, "failed to get GPG crypto context"); @@ -143,7 +163,9 @@ get_crypto_context (MuMsgOptions opts, MuMsgPartPasswordFunc password_func, GMimeCryptoContext *cctx; cctx = get_gpg_crypto_context (opts, err); - + if (!cctx) + return NULL; + /* use gobject to pass data to the callback func */ cbdata = g_new0 (CallbackData, 1); cbdata->pw_func = password_func ? password_func : dummy_password_func;