[PATCH 1/6] cli: new crypto structure to store crypto contexts and parameters
[notmuch-archives.git] / ba / f44ad7e7fb25197abd496a57ec0ab999a4f45f
1 Return-Path: <jrollins@finestructure.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 olra.theworths.org (Postfix) with ESMTP id C8052431FBC\r
6         for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:11 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id XOxnH2QWlbQ9 for <notmuch@notmuchmail.org>;\r
16         Wed, 16 May 2012 14:56:08 -0700 (PDT)\r
17 Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
18         [131.215.239.19])\r
19         by olra.theworths.org (Postfix) with ESMTP id 67A1E431FAE\r
20         for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:08 -0700 (PDT)\r
21 Received: from earth-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
22         by earth-doxen-postvirus (Postfix) with ESMTP id 4B98466E0189\r
23         for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:06 -0700 (PDT)\r
24 X-Spam-Scanned: at Caltech-IMSS on earth-doxen by amavisd-new\r
25 Received: from finestructure.net (rrcs-184-74-199-219.nyc.biz.rr.com\r
26         [184.74.199.219]) (Authenticated sender: jrollins)\r
27         by earth-doxen-submit (Postfix) with ESMTP id A91CC66E0194\r
28         for <notmuch@notmuchmail.org>; Wed, 16 May 2012 14:56:03 -0700 (PDT)\r
29 Received: by finestructure.net (Postfix, from userid 1000)\r
30         id 150703C4; Wed, 16 May 2012 14:56:02 -0700 (PDT)\r
31 From: Jameson Graef Rollins <jrollins@finestructure.net>\r
32 To: Notmuch Mail <notmuch@notmuchmail.org>\r
33 Subject: [PATCH 1/6] cli: new crypto structure to store crypto contexts and\r
34         parameters\r
35 Date: Wed, 16 May 2012 14:55:54 -0700\r
36 Message-Id: <1337205359-2444-2-git-send-email-jrollins@finestructure.net>\r
37 X-Mailer: git-send-email 1.7.10\r
38 In-Reply-To: <1337205359-2444-1-git-send-email-jrollins@finestructure.net>\r
39 References: <1337205359-2444-1-git-send-email-jrollins@finestructure.net>\r
40 X-BeenThere: notmuch@notmuchmail.org\r
41 X-Mailman-Version: 2.1.13\r
42 Precedence: list\r
43 List-Id: "Use and development of the notmuch mail system."\r
44         <notmuch.notmuchmail.org>\r
45 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
47 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
48 List-Post: <mailto:notmuch@notmuchmail.org>\r
49 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
50 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
51         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
52 X-List-Received-Date: Wed, 16 May 2012 21:56:12 -0000\r
53 \r
54 The main point here is to keep track of the crypto stuff together in\r
55 one place.  In notmuch-show the crypto struct is a sub structure of\r
56 the parameters struct.  In notmuch-reply, which had been using a\r
57 notmuch_show_params_t to store the crypto parameters, we can now just\r
58 use the general crypto struct.\r
59 \r
60 I slip in a name change of the crypto context itself to better reflect\r
61 what the context is specifically for: it's actually a GPG context,\r
62 which is a sub type of Crypto context.  There are other types of\r
63 Crypto contexts (Pkcs7 in particular) so we want to be clear.\r
64 \r
65 The following patches will use this to simplify some function\r
66 interfaces.\r
67 ---\r
68  notmuch-client.h |   16 ++++++++++------\r
69  notmuch-reply.c  |   34 +++++++++++++++++-----------------\r
70  notmuch-show.c   |   22 +++++++++++-----------\r
71  3 files changed, 38 insertions(+), 34 deletions(-)\r
72 \r
73 diff --git a/notmuch-client.h b/notmuch-client.h\r
74 index 19b7f01..2ad24cf 100644\r
75 --- a/notmuch-client.h\r
76 +++ b/notmuch-client.h\r
77 @@ -74,17 +74,21 @@ typedef struct notmuch_show_format {\r
78      const char *message_set_end;\r
79  } notmuch_show_format_t;\r
80  \r
81 +typedef struct notmuch_crypto {\r
82 +#ifdef GMIME_ATLEAST_26\r
83 +    GMimeCryptoContext* gpgctx;\r
84 +#else\r
85 +    GMimeCipherContext* gpgctx;\r
86 +#endif\r
87 +    notmuch_bool_t decrypt;\r
88 +} notmuch_crypto_t;\r
89 +\r
90  typedef struct notmuch_show_params {\r
91      notmuch_bool_t entire_thread;\r
92      notmuch_bool_t omit_excluded;\r
93      notmuch_bool_t raw;\r
94      int part;\r
95 -#ifdef GMIME_ATLEAST_26\r
96 -    GMimeCryptoContext* cryptoctx;\r
97 -#else\r
98 -    GMimeCipherContext* cryptoctx;\r
99 -#endif\r
100 -    notmuch_bool_t decrypt;\r
101 +    notmuch_crypto_t crypto;\r
102  } notmuch_show_params_t;\r
103  \r
104  /* There's no point in continuing when we've detected that we've done\r
105 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
106 index 7184a5d..ed87899 100644\r
107 --- a/notmuch-reply.c\r
108 +++ b/notmuch-reply.c\r
109 @@ -515,7 +515,7 @@ static int\r
110  notmuch_reply_format_default(void *ctx,\r
111                              notmuch_config_t *config,\r
112                              notmuch_query_t *query,\r
113 -                            notmuch_show_params_t *params,\r
114 +                            notmuch_crypto_t *crypto,\r
115                              notmuch_bool_t reply_all)\r
116  {\r
117      GMimeMessage *reply;\r
118 @@ -544,7 +544,7 @@ notmuch_reply_format_default(void *ctx,\r
119         g_object_unref (G_OBJECT (reply));\r
120         reply = NULL;\r
121  \r
122 -       if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,\r
123 +       if (mime_node_open (ctx, message, crypto->gpgctx, crypto->decrypt,\r
124                             &root) == NOTMUCH_STATUS_SUCCESS) {\r
125             format_part_reply (root);\r
126             talloc_free (root);\r
127 @@ -559,7 +559,7 @@ static int\r
128  notmuch_reply_format_json(void *ctx,\r
129                           notmuch_config_t *config,\r
130                           notmuch_query_t *query,\r
131 -                         notmuch_show_params_t *params,\r
132 +                         notmuch_crypto_t *crypto,\r
133                           notmuch_bool_t reply_all)\r
134  {\r
135      GMimeMessage *reply;\r
136 @@ -574,7 +574,7 @@ notmuch_reply_format_json(void *ctx,\r
137  \r
138      messages = notmuch_query_search_messages (query);\r
139      message = notmuch_messages_get (messages);\r
140 -    if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,\r
141 +    if (mime_node_open (ctx, message, crypto->gpgctx, crypto->decrypt,\r
142                         &node) != NOTMUCH_STATUS_SUCCESS)\r
143         return 1;\r
144  \r
145 @@ -605,7 +605,7 @@ static int\r
146  notmuch_reply_format_headers_only(void *ctx,\r
147                                   notmuch_config_t *config,\r
148                                   notmuch_query_t *query,\r
149 -                                 unused (notmuch_show_params_t *params),\r
150 +                                 unused (notmuch_crypto_t *crypto),\r
151                                   notmuch_bool_t reply_all)\r
152  {\r
153      GMimeMessage *reply;\r
154 @@ -674,8 +674,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
155      notmuch_query_t *query;\r
156      char *query_string;\r
157      int opt_index, ret = 0;\r
158 -    int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params, notmuch_bool_t reply_all);\r
159 -    notmuch_show_params_t params = { .part = -1 };\r
160 +    int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_crypto_t *crypto, notmuch_bool_t reply_all);\r
161 +    notmuch_crypto_t crypto = { .decrypt = FALSE };\r
162      int format = FORMAT_DEFAULT;\r
163      int reply_all = TRUE;\r
164  \r
165 @@ -689,7 +689,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
166           (notmuch_keyword_t []){ { "all", TRUE },\r
167                                   { "sender", FALSE },\r
168                                   { 0, 0 } } },\r
169 -       { NOTMUCH_OPT_BOOLEAN, &params.decrypt, "decrypt", 'd', 0 },\r
170 +       { NOTMUCH_OPT_BOOLEAN, &crypto.decrypt, "decrypt", 'd', 0 },\r
171         { 0, 0, 0, 0, 0 }\r
172      };\r
173  \r
174 @@ -706,18 +706,18 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
175      else\r
176         reply_format_func = notmuch_reply_format_default;\r
177  \r
178 -    if (params.decrypt) {\r
179 +    if (crypto.decrypt) {\r
180  #ifdef GMIME_ATLEAST_26\r
181         /* TODO: GMimePasswordRequestFunc */\r
182 -       params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");\r
183 +       crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
184  #else\r
185         GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
186 -       params.cryptoctx = g_mime_gpg_context_new (session, "gpg");\r
187 +       crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
188  #endif\r
189 -       if (params.cryptoctx) {\r
190 -           g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);\r
191 +       if (crypto.gpgctx) {\r
192 +           g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) crypto.gpgctx, FALSE);\r
193         } else {\r
194 -           params.decrypt = FALSE;\r
195 +           crypto.decrypt = FALSE;\r
196             fprintf (stderr, "Failed to construct gpg context.\n");\r
197         }\r
198  #ifndef GMIME_ATLEAST_26\r
199 @@ -750,14 +750,14 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
200         return 1;\r
201      }\r
202  \r
203 -    if (reply_format_func (ctx, config, query, &params, reply_all) != 0)\r
204 +    if (reply_format_func (ctx, config, query, &crypto, reply_all) != 0)\r
205         return 1;\r
206  \r
207      notmuch_query_destroy (query);\r
208      notmuch_database_destroy (notmuch);\r
209  \r
210 -    if (params.cryptoctx)\r
211 -       g_object_unref(params.cryptoctx);\r
212 +    if (crypto.gpgctx)\r
213 +       g_object_unref(crypto.gpgctx);\r
214  \r
215      return ret;\r
216  }\r
217 diff --git a/notmuch-show.c b/notmuch-show.c\r
218 index 95427d4..d254179 100644\r
219 --- a/notmuch-show.c\r
220 +++ b/notmuch-show.c\r
221 @@ -810,8 +810,8 @@ show_message (void *ctx,\r
222      mime_node_t *root, *part;\r
223      notmuch_status_t status;\r
224  \r
225 -    status = mime_node_open (local, message, params->cryptoctx,\r
226 -                            params->decrypt, &root);\r
227 +    status = mime_node_open (local, message, params->crypto.gpgctx,\r
228 +                            params->crypto.decrypt, &root);\r
229      if (status)\r
230         goto DONE;\r
231      part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));\r
232 @@ -1002,7 +1002,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
233                                    { 0, 0 } } },\r
234         { NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },\r
235         { NOTMUCH_OPT_BOOLEAN, &params.entire_thread, "entire-thread", 't', 0 },\r
236 -       { NOTMUCH_OPT_BOOLEAN, &params.decrypt, "decrypt", 'd', 0 },\r
237 +       { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },\r
238         { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },\r
239         { 0, 0, 0, 0, 0 }\r
240      };\r
241 @@ -1047,18 +1047,18 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
242         break;\r
243      }\r
244  \r
245 -    if (params.decrypt || verify) {\r
246 +    if (params.crypto.decrypt || verify) {\r
247  #ifdef GMIME_ATLEAST_26\r
248         /* TODO: GMimePasswordRequestFunc */\r
249 -       params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");\r
250 +       params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
251  #else\r
252         GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
253 -       params.cryptoctx = g_mime_gpg_context_new (session, "gpg");\r
254 +       params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
255  #endif\r
256 -       if (params.cryptoctx) {\r
257 -           g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);\r
258 +       if (params.crypto.gpgctx) {\r
259 +           g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE);\r
260         } else {\r
261 -           params.decrypt = FALSE;\r
262 +           params.crypto.decrypt = FALSE;\r
263             fprintf (stderr, "Failed to construct gpg context.\n");\r
264         }\r
265  #ifndef GMIME_ATLEAST_26\r
266 @@ -1118,8 +1118,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
267      notmuch_query_destroy (query);\r
268      notmuch_database_destroy (notmuch);\r
269  \r
270 -    if (params.cryptoctx)\r
271 -       g_object_unref(params.cryptoctx);\r
272 +    if (params.crypto.gpgctx)\r
273 +       g_object_unref(params.crypto.gpgctx);\r
274  \r
275      return ret;\r
276  }\r
277 -- \r
278 1.7.10\r
279 \r