From 61ade73332d9c915779e6e68c369e3dee8a42d6d Mon Sep 17 00:00:00 2001 From: djcb Date: Thu, 13 Sep 2012 22:57:04 +0300 Subject: [PATCH] * lib: add support for asking user for password from console --- lib/mu-msg-part.c | 29 ++++++++++++++++++++++++++++- lib/mu-msg-priv.h | 26 +++++--------------------- lib/mu-msg.h | 3 +++ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/lib/mu-msg-part.c b/lib/mu-msg-part.c index 915251fc..a0e6c799 100644 --- a/lib/mu-msg-part.c +++ b/lib/mu-msg-part.c @@ -327,6 +327,26 @@ static gboolean handle_mime_object (MuMsg *msg, unsigned index, MuMsgPartForeachFunc func, gpointer user_data); + +#ifdef BUILD_CRYPTO +static gchar* +get_console_pw (const char* user_id, const char *prompt_ctx, + gboolean reprompt, gpointer user_data) +{ + char *prompt, *pass; + + if (reprompt) + g_print ("Authentication failed. Please try again\n"); + + prompt = g_strdup_printf ("Password for %s: ", user_id); + + pass = mu_util_read_password (prompt); + g_free (prompt); + + return pass; +} +#endif /*BUILD_CRYPTO*/ + /* call 'func' with information about this MIME-part */ static gboolean handle_encrypted_part (MuMsg *msg, @@ -337,9 +357,16 @@ handle_encrypted_part (MuMsg *msg, #ifdef BUILD_CRYPTO GError *err; GMimeObject *dec; + MuMsgPartPasswordFunc pw_func; + + if (opts & MU_MSG_OPTION_CONSOLE_PASSWORD) + pw_func = (MuMsgPartPasswordFunc)get_console_pw; + else + pw_func = NULL; + err = NULL; - dec = mu_msg_crypto_decrypt_part (part, opts, NULL, NULL, &err); + dec = mu_msg_crypto_decrypt_part (part, opts, pw_func, NULL, &err); if (err) { g_warning ("error decrypting part: %s", err->message); g_clear_error (&err); diff --git a/lib/mu-msg-priv.h b/lib/mu-msg-priv.h index 59e37647..ffb93b8b 100644 --- a/lib/mu-msg-priv.h +++ b/lib/mu-msg-priv.h @@ -1,7 +1,7 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by @@ -68,28 +68,13 @@ struct _MuMsg { * convert a GMimePart to a string * * @param part a GMimePart - * @param err will receive TRUE if there was an error, FALSE otherwise. Must NOT be NULL. + * @param err will receive TRUE if there was an error, FALSE + * otherwise. Must NOT be NULL. * * @return utf8 string for this MIME part, to be freed by caller */ -gchar* mu_msg_mime_part_to_string (GMimePart *part, gboolean *err); - - -/* /\** */ -/* * write a GMimeObject to a file */ -/* * */ -/* * @param obj a GMimeObject */ -/* * @param fullpath full file path */ -/* * @param overwrite allow overwriting existing file */ -/* * @param if file already exist, don't bother to write */ -/* * @param err receives error information */ -/* * */ -/* * @return TRUE if writing succeeded, FALSE otherwise. */ -/* *\/ */ -/* gboolean mu_msg_part_mime_save_object (GMimeObject *obj, const char *fullpath, */ -/* gboolean overwrite, gboolean use_existing, */ -/* GError **err); */ - +gchar* mu_msg_mime_part_to_string (GMimePart *part, gboolean *err) + G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; /** @@ -108,7 +93,6 @@ void mu_mime_message_foreach (GMimeMessage *msg, gboolean decrypt, gpointer user_data); #ifdef BUILD_CRYPTO - /** * callback function to retrieve a password from the user * diff --git a/lib/mu-msg.h b/lib/mu-msg.h index e9596be5..f28a51c3 100644 --- a/lib/mu-msg.h +++ b/lib/mu-msg.h @@ -48,6 +48,9 @@ enum _MuMsgOptions { MU_MSG_OPTION_USE_AGENT = 1 << 6, /* MU_MSG_OPTION_USE_PKCS7 = 1 << 7, /\* gpg is the default *\/ */ + /* get password from console if needed */ + MU_MSG_OPTION_CONSOLE_PASSWORD = 1 << 7, + MU_MSG_OPTION_DECRYPT = 1 << 8, /* misc */