--- /dev/null
+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 AC8E5431FD0\r
+ for <notmuch@notmuchmail.org>; Sat, 30 Mar 2013 06:53:31 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\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 Vl88RcfBsffd for <notmuch@notmuchmail.org>;\r
+ Sat, 30 Mar 2013 06:53:30 -0700 (PDT)\r
+Received: from mail-la0-f51.google.com (mail-la0-f51.google.com\r
+ [209.85.215.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id C7B97431FCB\r
+ for <notmuch@notmuchmail.org>; Sat, 30 Mar 2013 06:53:25 -0700 (PDT)\r
+Received: by mail-la0-f51.google.com with SMTP id fo13so1102591lab.24\r
+ for <notmuch@notmuchmail.org>; Sat, 30 Mar 2013 06:53:24 -0700 (PDT)\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:in-reply-to\r
+ :references:in-reply-to:references:x-gm-message-state;\r
+ bh=SwrNsEqSmwXHzGNbU4XbN6z6+yPRyKFVMjubIs0Arhw=;\r
+ b=isfFVhxRgnhT9J2hys1r4yehLciJJmnv3+EeboACGMazxf/xsfD0m1mDvCySp8wpkH\r
+ jyrXzAnrczDr5Wjhxfya09MRCZ3BuGkZKwZBEmMlR8BZ7h92jL1Ngz7Ev5x9lXSXerdD\r
+ MFREfeOiGdB8QEsYhUKZrDgni4+sMnUzizzOCF8d8V0LkB6D3zAJUmQTkgYQ1qwhNUW6\r
+ TA/wPds9C32A3IajIp/1/H/ujmnfQMJ6rQRDasqBhYF0un58HpQLVxAHwo+3B8PbB6bO\r
+ MgWBNs7HbL1KTIq5ZkfxCcw8+hwQ1L8LuzzZLOQ56oRw/j2ZG5YWlkyZZaWhox2Ko+to\r
+ gdSg==\r
+X-Received: by 10.112.3.198 with SMTP id e6mr2889294lbe.118.1364651602924;\r
+ Sat, 30 Mar 2013 06:53:22 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
+ [80.223.81.27])\r
+ by mx.google.com with ESMTPS id m2sm2639033lbz.7.2013.03.30.06.53.21\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 30 Mar 2013 06:53:22 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/3] cli: crypto: abstract gpg context creation for clarity\r
+Date: Sat, 30 Mar 2013 15:53:15 +0200\r
+Message-Id:\r
+ <2dab662da71c9f5740a21a2c148ecf9c855cc81e.1364651359.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <cover.1364651359.git.jani@nikula.org>\r
+References: <cover.1364651359.git.jani@nikula.org>\r
+In-Reply-To: <cover.1364651359.git.jani@nikula.org>\r
+References: <cover.1364651359.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQn3Kmj2G0nGehcJjA93FXRvUpvoRmAn6piRYeapx6sRVLkVc0KpZhO6Pe5TB2uWzkP46EsG\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: Sat, 30 Mar 2013 13:53:32 -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 | 66 ++++++++++++++++++++++++++++++++++++++++++++------------------\r
+ 1 file changed, 47 insertions(+), 19 deletions(-)\r
+\r
+diff --git a/crypto.c b/crypto.c\r
+index cb361e1..9736517 100644\r
+--- a/crypto.c\r
++++ b/crypto.c\r
+@@ -20,6 +20,48 @@\r
+ \r
+ #include "notmuch-client.h"\r
+ \r
++#ifdef GMIME_ATLEAST_26\r
++\r
++/* Create a GPG context (GMime 2.6) */\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
++\r
++#else /* GMIME_ATLEAST_26 */\r
++\r
++/* Create a GPG context (GMime 2.4) */\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
++\r
++#endif /* GMIME_ATLEAST_26 */\r
++\r
+ /* for the specified protocol return the context pointer (initializing\r
+ * if needed) */\r
+ notmuch_crypto_context_t *\r
+@@ -33,28 +75,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