Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / 3f / dd48ad2e0c4c3e767dd0ace601dd014162cea7
1 Return-Path: <bremner@tethera.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5  by arlo.cworth.org (Postfix) with ESMTP id F3AE46DE17A0\r
6  for <notmuch@notmuchmail.org>; Fri, 11 Dec 2015 05:55:07 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.319\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.319 tagged_above=-999 required=5 tests=[AWL=0.232,\r
12   RP_MATCHES_RCVD=-0.55, SPF_PASS=-0.001] autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id g5cwXdcAEN1d for <notmuch@notmuchmail.org>;\r
16  Fri, 11 Dec 2015 05:55:06 -0800 (PST)\r
17 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
18  by arlo.cworth.org (Postfix) with ESMTPS id AC90B6DE00DD\r
19  for <notmuch@notmuchmail.org>; Fri, 11 Dec 2015 05:54:58 -0800 (PST)\r
20 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
21  (envelope-from <bremner@tethera.net>)\r
22  id 1a7O9u-0000Rs-DA; Fri, 11 Dec 2015 08:54:54 -0500\r
23 Received: (nullmailer pid 11204 invoked by uid 1000);\r
24  Fri, 11 Dec 2015 13:54:52 -0000\r
25 From: David Bremner <david@tethera.net>\r
26 To: notmuch@notmuchmail.org\r
27 Subject: [Patch v2 2/8] crypto: make crypto ctx initialization an array\r
28 Date: Fri, 11 Dec 2015 09:54:41 -0400\r
29 Message-Id: <1449842087-10972-3-git-send-email-david@tethera.net>\r
30 X-Mailer: git-send-email 2.6.2\r
31 In-Reply-To: <1449842087-10972-1-git-send-email-david@tethera.net>\r
32 References: <1449842087-10972-1-git-send-email-david@tethera.net>\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.20\r
35 Precedence: list\r
36 List-Id: "Use and development of the notmuch mail system."\r
37  <notmuch.notmuchmail.org>\r
38 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
39  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
41 List-Post: <mailto:notmuch@notmuchmail.org>\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
43 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
44  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Fri, 11 Dec 2015 13:55:08 -0000\r
46 \r
47 From: Jani Nikula <jani@nikula.org>\r
48 \r
49 Make it trivial to add handlers for new protocols without duplicating\r
50 code. No functional changes.\r
51 ---\r
52  crypto.c | 27 +++++++++++++++++++++------\r
53  1 file changed, 21 insertions(+), 6 deletions(-)\r
54 \r
55 diff --git a/crypto.c b/crypto.c\r
56 index 1187ad7..da0289d 100644\r
57 --- a/crypto.c\r
58 +++ b/crypto.c\r
59 @@ -43,12 +43,27 @@ create_gpg_context (notmuch_crypto_t *crypto)\r
60      return gpgctx;\r
61  }\r
62  \r
63 +static const struct {\r
64 +    const char *protocol;\r
65 +    notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto);\r
66 +} protocols[] = {\r
67 +    {\r
68 +       .protocol = "application/pgp-signature",\r
69 +       .get_context = create_gpg_context,\r
70 +    },\r
71 +    {\r
72 +       .protocol = "application/pgp-encrypted",\r
73 +       .get_context = create_gpg_context,\r
74 +    },\r
75 +};\r
76 +\r
77  /* for the specified protocol return the context pointer (initializing\r
78   * if needed) */\r
79  notmuch_crypto_context_t *\r
80  notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)\r
81  {\r
82      notmuch_crypto_context_t *cryptoctx = NULL;\r
83 +    size_t i;\r
84  \r
85      if (! protocol) {\r
86         fprintf (stderr, "Cryptographic protocol is empty.\n");\r
87 @@ -61,14 +76,14 @@ notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)\r
88       * parameter names as defined in this document are\r
89       * case-insensitive."  Thus, we use strcasecmp for the protocol.\r
90       */\r
91 -    if (strcasecmp (protocol, "application/pgp-signature") == 0 ||\r
92 -       strcasecmp (protocol, "application/pgp-encrypted") == 0) {\r
93 -       cryptoctx = create_gpg_context (crypto);\r
94 -    } else {\r
95 -       fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n");\r
96 +    for (i = 0; i < ARRAY_SIZE (protocols); i++) {\r
97 +       if (strcasecmp (protocol, protocols[i].protocol) == 0)\r
98 +           return protocols[i].get_context (crypto);\r
99      }\r
100  \r
101 -    return cryptoctx;\r
102 +    fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n");\r
103 +\r
104 +    return NULL;\r
105  }\r
106  \r
107  int\r
108 -- \r
109 2.6.2\r
110 \r