[PATCH 1/2] cli: crypto: abstract gpg context creation for clarity
authorJani Nikula <jani@nikula.org>
Sun, 3 Mar 2013 14:10:10 +0000 (16:10 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:52 +0000 (09:53 -0800)
14/a22e866072b6aee3c3dbcf9046471053061f02 [new file with mode: 0644]

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