From: Jani Nikula Date: Sun, 3 Mar 2013 14:10:10 +0000 (+0200) Subject: [PATCH 1/2] cli: crypto: abstract gpg context creation for clarity X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=2f46cc5cf051f752b5ace9de6a5c4512455fbba7;p=notmuch-archives.git [PATCH 1/2] cli: crypto: abstract gpg context creation for clarity --- diff --git a/14/a22e866072b6aee3c3dbcf9046471053061f02 b/14/a22e866072b6aee3c3dbcf9046471053061f02 new file mode 100644 index 000000000..6ba9569ac --- /dev/null +++ b/14/a22e866072b6aee3c3dbcf9046471053061f02 @@ -0,0 +1,157 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 6713A431FAF + for ; Sun, 3 Mar 2013 06:10:16 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id pVAHfcJg8n92 for ; + Sun, 3 Mar 2013 06:10:16 -0800 (PST) +Received: from mail-lb0-f177.google.com (mail-lb0-f177.google.com + [209.85.217.177]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id AC262431FAE + for ; Sun, 3 Mar 2013 06:10:15 -0800 (PST) +Received: by mail-lb0-f177.google.com with SMTP id go11so3260458lbb.22 + for ; Sun, 03 Mar 2013 06:10:14 -0800 (PST) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=google.com; s=20120113; + h=x-received:from:to:cc:subject:date:message-id:x-mailer + :x-gm-message-state; + bh=e83dNHPXuHgeFbJGPujqQUwwzo20YasCYiCl61dPAF4=; + b=gA5eA5FoElm0VkVVGzY/6NUz3zerEotIVC7+WcLg69uYwZEN9hsywNhxzn3fNHGsi9 + 6I7hGiYipiN26ujtSfWXMtRyak68Qx9MOxVyjuFgeQPZz4rI+8gEm896u3CXxb6RmvtJ + Ww6jB3+R0La3oxb7tvE2v9NEJd9vhBRPVzM22VUJZgNgtOXMrYeH/XZ6vTezv3zFL6yo + yuz/hikLum/0LAtm3KPlmJstXR/OXEVTnk7I6n6OjLx94M+aBGZQ3vD7ufkHoA9nRVxL + rh5uq9mRZtB2ydD1Ibsm5H5/KiQaKFFs83mtyChEiItkD1KM+it987imwjxxkg4rW+Do + kqTA== +X-Received: by 10.112.100.41 with SMTP id ev9mr3271963lbb.34.1362319813930; + Sun, 03 Mar 2013 06:10:13 -0800 (PST) +Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi. + [80.223.81.27]) + by mx.google.com with ESMTPS id e9sm6241164lbz.1.2013.03.03.06.10.11 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sun, 03 Mar 2013 06:10:12 -0800 (PST) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH 1/2] cli: crypto: abstract gpg context creation for clarity +Date: Sun, 3 Mar 2013 16:10:10 +0200 +Message-Id: + +X-Mailer: git-send-email 1.7.10.4 +X-Gm-Message-State: + ALoCoQlog9iN+lrzE0gYTv3prokS35bH93ta5TdhOLSwRyvj3fd3xI/usvxVXbrkN6TsMUsj9y1/ +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 03 Mar 2013 14:10:16 -0000 + +The code filled with #ifdef GMIME_ATLEAST_26 is difficult to +read. Abstract gpg context creation into a function, with separate +implementations for GMime 2.4 and 2.6, to clarify the code. + +There should be no functional changes. +--- + crypto.c | 60 +++++++++++++++++++++++++++++++++++++++++------------------- + 1 file changed, 41 insertions(+), 19 deletions(-) + +diff --git a/crypto.c b/crypto.c +index cb361e1..7d1dfe4 100644 +--- a/crypto.c ++++ b/crypto.c +@@ -20,6 +20,42 @@ + + #include "notmuch-client.h" + ++#ifdef GMIME_ATLEAST_26 ++static notmuch_crypto_context_t * ++create_gpg_context (void) ++{ ++ notmuch_crypto_context_t *gpgctx; ++ ++ /* TODO: GMimePasswordRequestFunc */ ++ gpgctx = g_mime_gpg_context_new (NULL, "gpg"); ++ if (! gpgctx) ++ return NULL; ++ ++ g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE); ++ g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE); ++ ++ return gpgctx; ++} ++#else ++static notmuch_crypto_context_t * ++create_gpg_context (void) ++{ ++ GMimeSession *session; ++ notmuch_crypto_context_t *gpgctx; ++ ++ session = g_object_new (g_mime_session_get_type (), NULL); ++ gpgctx = g_mime_gpg_context_new (session, "gpg"); ++ g_object_unref (session); ++ ++ if (! gpgctx) ++ return NULL; ++ ++ g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE); ++ ++ return gpgctx; ++} ++#endif ++ + /* for the specified protocol return the context pointer (initializing + * if needed) */ + notmuch_crypto_context_t * +@@ -33,28 +69,14 @@ notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol) + * parameter names as defined in this document are + * case-insensitive." Thus, we use strcasecmp for the protocol. + */ +- if ((strcasecmp (protocol, "application/pgp-signature") == 0) +- || (strcasecmp (protocol, "application/pgp-encrypted") == 0)) { +- if (!crypto->gpgctx) { +-#ifdef GMIME_ATLEAST_26 +- /* TODO: GMimePasswordRequestFunc */ +- crypto->gpgctx = g_mime_gpg_context_new (NULL, "gpg"); +-#else +- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL); +- crypto->gpgctx = g_mime_gpg_context_new (session, "gpg"); +- g_object_unref (session); +-#endif +- if (crypto->gpgctx) { +-#ifdef GMIME_ATLEAST_26 +- g_mime_gpg_context_set_use_agent ((GMimeGpgContext*) crypto->gpgctx, TRUE); +-#endif +- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) crypto->gpgctx, FALSE); +- } else { ++ if (strcasecmp (protocol, "application/pgp-signature") == 0 || ++ strcasecmp (protocol, "application/pgp-encrypted") == 0) { ++ if (! crypto->gpgctx) { ++ crypto->gpgctx = create_gpg_context (); ++ if (! crypto->gpgctx) + fprintf (stderr, "Failed to construct gpg context.\n"); +- } + } + cryptoctx = crypto->gpgctx; +- + } else { + fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n"); + } +-- +1.7.10.4 +