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