[PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / b7 / 6a1c53290ee9b1a206911e80942d370562a30d
1 Return-Path: <dkg@fifthhorseman.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 393B76DE1B66\r
6  for <notmuch@notmuchmail.org>; Tue, 19 Jan 2016 18:53:21 -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.023\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.023 tagged_above=-999 required=5\r
12  tests=[AWL=-0.023] 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 2X5Y4l47KP1u for <notmuch@notmuchmail.org>;\r
16  Tue, 19 Jan 2016 18:53:18 -0800 (PST)\r
17 Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
18  by arlo.cworth.org (Postfix) with ESMTP id 622956DE1416\r
19  for <notmuch@notmuchmail.org>; Tue, 19 Jan 2016 18:53:15 -0800 (PST)\r
20 Received: from fifthhorseman.net (unknown [38.109.115.130])\r
21  by che.mayfirst.org (Postfix) with ESMTPSA id 307E8F984\r
22  for <notmuch@notmuchmail.org>; Tue, 19 Jan 2016 21:53:11 -0500 (EST)\r
23 Received: by fifthhorseman.net (Postfix, from userid 1000)\r
24  id CD9E02024F; Tue, 19 Jan 2016 18:53:10 -0800 (PST)\r
25 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
26 To: Notmuch Mail <notmuch@notmuchmail.org>\r
27 Subject: [PATCH v2 02/16] Move crypto.c into libutil\r
28 Date: Tue, 19 Jan 2016 21:52:35 -0500\r
29 Message-Id: <1453258369-7366-3-git-send-email-dkg@fifthhorseman.net>\r
30 X-Mailer: git-send-email 2.7.0.rc3\r
31 In-Reply-To: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net>\r
32 References: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net>\r
33 MIME-Version: 1.0\r
34 Content-Type: text/plain; charset=UTF-8\r
35 Content-Transfer-Encoding: 8bit\r
36 X-BeenThere: notmuch@notmuchmail.org\r
37 X-Mailman-Version: 2.1.20\r
38 Precedence: list\r
39 List-Id: "Use and development of the notmuch mail system."\r
40  <notmuch.notmuchmail.org>\r
41 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
42  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
44 List-Post: <mailto:notmuch@notmuchmail.org>\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
46 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
47  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
48 X-List-Received-Date: Wed, 20 Jan 2016 02:53:21 -0000\r
49 \r
50 This prepares us for using the crypto object in both the library and\r
51 the client.\r
52 \r
53 i've prefixed notmuch_crypto with _ to indicate that while this can be\r
54 built into the library when needed, it's not something to be exported\r
55 or used externally.\r
56 ---\r
57  Makefile.local      |   1 -\r
58  crypto.c            |  99 --------------------------------------------------\r
59  mime-node.c         |  12 +++---\r
60  notmuch-client.h    |  20 ++--------\r
61  notmuch-reply.c     |   2 +-\r
62  notmuch-show.c      |   2 +-\r
63  util/Makefile.local |   2 +-\r
64  util/crypto.c       | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
65  util/crypto.h       |  23 ++++++++++++\r
66  9 files changed, 138 insertions(+), 126 deletions(-)\r
67  delete mode 100644 crypto.c\r
68  create mode 100644 util/crypto.c\r
69  create mode 100644 util/crypto.h\r
70 \r
71 diff --git a/Makefile.local b/Makefile.local\r
72 index 066ecf2..6206771 100644\r
73 --- a/Makefile.local\r
74 +++ b/Makefile.local\r
75 @@ -293,7 +293,6 @@ notmuch_client_srcs =               \\r
76         sprinter-text.c         \\r
77         query-string.c          \\r
78         mime-node.c             \\r
79 -       crypto.c                \\r
80         tag-util.c\r
81  \r
82  notmuch_client_modules = $(notmuch_client_srcs:.c=.o)\r
83 diff --git a/crypto.c b/crypto.c\r
84 deleted file mode 100644\r
85 index feae949..0000000\r
86 --- a/crypto.c\r
87 +++ /dev/null\r
88 @@ -1,99 +0,0 @@\r
89 -/* notmuch - Not much of an email program, (just index and search)\r
90 - *\r
91 - * Copyright © 2012 Jameson Rollins\r
92 - *\r
93 - * This program is free software: you can redistribute it and/or modify\r
94 - * it under the terms of the GNU General Public License as published by\r
95 - * the Free Software Foundation, either version 3 of the License, or\r
96 - * (at your option) any later version.\r
97 - *\r
98 - * This program is distributed in the hope that it will be useful,\r
99 - * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
100 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
101 - * GNU General Public License for more details.\r
102 - *\r
103 - * You should have received a copy of the GNU General Public License\r
104 - * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
105 - *\r
106 - * Authors: Jameson Rollins <jrollins@finestructure.net>\r
107 - */\r
108 -\r
109 -#include "notmuch-client.h"\r
110 -\r
111 -/* Create a GPG context (GMime 2.6) */\r
112 -static notmuch_crypto_context_t *\r
113 -create_gpg_context (notmuch_crypto_t *crypto)\r
114 -{\r
115 -    notmuch_crypto_context_t *gpgctx;\r
116 -\r
117 -    if (crypto->gpgctx)\r
118 -       return crypto->gpgctx;\r
119 -\r
120 -    /* TODO: GMimePasswordRequestFunc */\r
121 -    gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");\r
122 -    if (! gpgctx) {\r
123 -       fprintf (stderr, "Failed to construct gpg context.\n");\r
124 -       return NULL;\r
125 -    }\r
126 -    crypto->gpgctx = gpgctx;\r
127 -\r
128 -    g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);\r
129 -    g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);\r
130 -\r
131 -    return gpgctx;\r
132 -}\r
133 -\r
134 -static const struct {\r
135 -    const char *protocol;\r
136 -    notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto);\r
137 -} protocols[] = {\r
138 -    {\r
139 -       .protocol = "application/pgp-signature",\r
140 -       .get_context = create_gpg_context,\r
141 -    },\r
142 -    {\r
143 -       .protocol = "application/pgp-encrypted",\r
144 -       .get_context = create_gpg_context,\r
145 -    },\r
146 -};\r
147 -\r
148 -/* for the specified protocol return the context pointer (initializing\r
149 - * if needed) */\r
150 -notmuch_crypto_context_t *\r
151 -notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)\r
152 -{\r
153 -    notmuch_crypto_context_t *cryptoctx = NULL;\r
154 -    size_t i;\r
155 -\r
156 -    if (! protocol) {\r
157 -       fprintf (stderr, "Cryptographic protocol is empty.\n");\r
158 -       return cryptoctx;\r
159 -    }\r
160 -\r
161 -    /* As per RFC 1847 section 2.1: "the [protocol] value token is\r
162 -     * comprised of the type and sub-type tokens of the Content-Type".\r
163 -     * As per RFC 1521 section 2: "Content-Type values, subtypes, and\r
164 -     * parameter names as defined in this document are\r
165 -     * case-insensitive."  Thus, we use strcasecmp for the protocol.\r
166 -     */\r
167 -    for (i = 0; i < ARRAY_SIZE (protocols); i++) {\r
168 -       if (strcasecmp (protocol, protocols[i].protocol) == 0)\r
169 -           return protocols[i].get_context (crypto);\r
170 -    }\r
171 -\r
172 -    fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",\r
173 -            protocol);\r
174 -\r
175 -    return NULL;\r
176 -}\r
177 -\r
178 -int\r
179 -notmuch_crypto_cleanup (notmuch_crypto_t *crypto)\r
180 -{\r
181 -    if (crypto->gpgctx) {\r
182 -       g_object_unref (crypto->gpgctx);\r
183 -       crypto->gpgctx = NULL;\r
184 -    }\r
185 -\r
186 -    return 0;\r
187 -}\r
188 diff --git a/mime-node.c b/mime-node.c\r
189 index e96e663..a8f5670 100644\r
190 --- a/mime-node.c\r
191 +++ b/mime-node.c\r
192 @@ -33,7 +33,7 @@ typedef struct mime_node_context {\r
193      GMimeMessage *mime_message;\r
194  \r
195      /* Context provided by the caller. */\r
196 -    notmuch_crypto_t *crypto;\r
197 +    _notmuch_crypto_t *crypto;\r
198  } mime_node_context_t;\r
199  \r
200  static int\r
201 @@ -56,7 +56,7 @@ _mime_node_context_free (mime_node_context_t *res)\r
202  \r
203  notmuch_status_t\r
204  mime_node_open (const void *ctx, notmuch_message_t *message,\r
205 -               notmuch_crypto_t *crypto, mime_node_t **root_out)\r
206 +               _notmuch_crypto_t *crypto, mime_node_t **root_out)\r
207  {\r
208      const char *filename = notmuch_message_get_filename (message);\r
209      mime_node_context_t *mctx;\r
210 @@ -151,7 +151,7 @@ set_signature_list_destructor (mime_node_t *node)\r
211  /* Verify a signed mime node (GMime 2.6) */\r
212  static void\r
213  node_verify (mime_node_t *node, GMimeObject *part,\r
214 -            notmuch_crypto_context_t *cryptoctx)\r
215 +            GMimeCryptoContext *cryptoctx)\r
216  {\r
217      GError *err = NULL;\r
218  \r
219 @@ -172,7 +172,7 @@ node_verify (mime_node_t *node, GMimeObject *part,\r
220  /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */\r
221  static void\r
222  node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,\r
223 -                        notmuch_crypto_context_t *cryptoctx)\r
224 +                        GMimeCryptoContext *cryptoctx)\r
225  {\r
226      GError *err = NULL;\r
227      GMimeDecryptResult *decrypt_result = NULL;\r
228 @@ -207,7 +207,7 @@ static mime_node_t *\r
229  _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
230  {\r
231      mime_node_t *node = talloc_zero (parent, mime_node_t);\r
232 -    notmuch_crypto_context_t *cryptoctx = NULL;\r
233 +    GMimeCryptoContext *cryptoctx = NULL;\r
234  \r
235      /* Set basic node properties */\r
236      node->part = part;\r
237 @@ -244,7 +244,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
238         || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {\r
239         GMimeContentType *content_type = g_mime_object_get_content_type (part);\r
240         const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");\r
241 -       cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);\r
242 +       cryptoctx = _notmuch_crypto_get_gmime_context (node->ctx->crypto, protocol);\r
243      }\r
244  \r
245      /* Handle PGP/MIME parts */\r
246 diff --git a/notmuch-client.h b/notmuch-client.h\r
247 index 3bd2903..6157cd9 100644\r
248 --- a/notmuch-client.h\r
249 +++ b/notmuch-client.h\r
250 @@ -30,8 +30,6 @@\r
251  \r
252  #include <gmime/gmime.h>\r
253  \r
254 -typedef GMimeCryptoContext notmuch_crypto_context_t;\r
255 -\r
256  #include "notmuch.h"\r
257  \r
258  /* This is separate from notmuch-private.h because we're trying to\r
259 @@ -51,6 +49,7 @@ typedef GMimeCryptoContext notmuch_crypto_context_t;\r
260  #include <ctype.h>\r
261  \r
262  #include "talloc-extra.h"\r
263 +#include "crypto.h"\r
264  \r
265  #define unused(x) x __attribute__ ((unused))\r
266  \r
267 @@ -68,20 +67,13 @@ typedef struct notmuch_show_format {\r
268                               const struct notmuch_show_params *params);\r
269  } notmuch_show_format_t;\r
270  \r
271 -typedef struct notmuch_crypto {\r
272 -    notmuch_crypto_context_t* gpgctx;\r
273 -    notmuch_bool_t verify;\r
274 -    notmuch_bool_t decrypt;\r
275 -    const char *gpgpath;\r
276 -} notmuch_crypto_t;\r
277 -\r
278  typedef struct notmuch_show_params {\r
279      notmuch_bool_t entire_thread;\r
280      notmuch_bool_t omit_excluded;\r
281      notmuch_bool_t output_body;\r
282      notmuch_bool_t raw;\r
283      int part;\r
284 -    notmuch_crypto_t crypto;\r
285 +    _notmuch_crypto_t crypto;\r
286      notmuch_bool_t include_html;\r
287  } notmuch_show_params_t;\r
288  \r
289 @@ -164,12 +156,6 @@ typedef struct _notmuch_config notmuch_config_t;\r
290  void\r
291  notmuch_exit_if_unsupported_format (void);\r
292  \r
293 -notmuch_crypto_context_t *\r
294 -notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);\r
295 -\r
296 -int\r
297 -notmuch_crypto_cleanup (notmuch_crypto_t *crypto);\r
298 -\r
299  int\r
300  notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);\r
301  \r
302 @@ -420,7 +406,7 @@ struct mime_node {\r
303   */\r
304  notmuch_status_t\r
305  mime_node_open (const void *ctx, notmuch_message_t *message,\r
306 -               notmuch_crypto_t *crypto, mime_node_t **node_out);\r
307 +               _notmuch_crypto_t *crypto, mime_node_t **node_out);\r
308  \r
309  /* Return a new MIME node for the requested child part of parent.\r
310   * parent will be used as the talloc context for the returned child\r
311 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
312 index 6df54fc..eccfb32 100644\r
313 --- a/notmuch-reply.c\r
314 +++ b/notmuch-reply.c\r
315 @@ -862,7 +862,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
316      if (reply_format_func (config, config, query, &params, reply_all, sp) != 0)\r
317         return EXIT_FAILURE;\r
318  \r
319 -    notmuch_crypto_cleanup (&params.crypto);\r
320 +    _notmuch_crypto_cleanup (&params.crypto);\r
321      notmuch_query_destroy (query);\r
322      notmuch_database_destroy (notmuch);\r
323  \r
324 diff --git a/notmuch-show.c b/notmuch-show.c\r
325 index 87e52bb..3c91ece 100644\r
326 --- a/notmuch-show.c\r
327 +++ b/notmuch-show.c\r
328 @@ -1171,7 +1171,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
329         ret = do_show (config, query, format, sprinter, &params);\r
330      }\r
331  \r
332 -    notmuch_crypto_cleanup (&params.crypto);\r
333 +    _notmuch_crypto_cleanup (&params.crypto);\r
334      notmuch_query_destroy (query);\r
335      notmuch_database_destroy (notmuch);\r
336  \r
337 diff --git a/util/Makefile.local b/util/Makefile.local\r
338 index 8b2b91b..7590618 100644\r
339 --- a/util/Makefile.local\r
340 +++ b/util/Makefile.local\r
341 @@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir)\r
342  \r
343  libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
344                   $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \\r
345 -               $(dir)/util.c $(dir)/search-path.c\r
346 +               $(dir)/util.c $(dir)/search-path.c $(dir)/crypto.c\r
347  \r
348  libutil_modules := $(libutil_c_srcs:.c=.o)\r
349  \r
350 diff --git a/util/crypto.c b/util/crypto.c\r
351 new file mode 100644\r
352 index 0000000..1712347\r
353 --- /dev/null\r
354 +++ b/util/crypto.c\r
355 @@ -0,0 +1,103 @@\r
356 +/* notmuch - Not much of an email program, (just index and search)\r
357 + *\r
358 + * Copyright © 2012 Jameson Rollins\r
359 + *\r
360 + * This program is free software: you can redistribute it and/or modify\r
361 + * it under the terms of the GNU General Public License as published by\r
362 + * the Free Software Foundation, either version 3 of the License, or\r
363 + * (at your option) any later version.\r
364 + *\r
365 + * This program is distributed in the hope that it will be useful,\r
366 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
367 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
368 + * GNU General Public License for more details.\r
369 + *\r
370 + * You should have received a copy of the GNU General Public License\r
371 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
372 + *\r
373 + * Authors: Jameson Rollins <jrollins@finestructure.net>\r
374 + *          Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
375 + */\r
376 +\r
377 +#include "notmuch.h"\r
378 +#include "crypto.h"\r
379 +#include <string.h>\r
380 +\r
381 +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
382 +\r
383 +/* Create a GPG context (GMime 2.6) */\r
384 +static GMimeCryptoContext*\r
385 +create_gpg_context (_notmuch_crypto_t *crypto)\r
386 +{\r
387 +    GMimeCryptoContext *gpgctx;\r
388 +\r
389 +    if (crypto->gpgctx) {\r
390 +       return crypto->gpgctx;\r
391 +    }\r
392 +\r
393 +    /* TODO: GMimePasswordRequestFunc */\r
394 +    gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");\r
395 +    if (! gpgctx) {\r
396 +       fprintf (stderr, "Failed to construct gpg context.\n");\r
397 +       return NULL;\r
398 +    }\r
399 +    crypto->gpgctx = gpgctx;\r
400 +\r
401 +    g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);\r
402 +    g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);\r
403 +\r
404 +    return crypto->gpgctx;\r
405 +}\r
406 +\r
407 +static const struct {\r
408 +    const char *protocol;\r
409 +    GMimeCryptoContext *(*get_context) (_notmuch_crypto_t *crypto);\r
410 +} protocols[] = {\r
411 +    {\r
412 +       .protocol = "application/pgp-signature",\r
413 +       .get_context = create_gpg_context,\r
414 +    },\r
415 +    {\r
416 +       .protocol = "application/pgp-encrypted",\r
417 +       .get_context = create_gpg_context,\r
418 +    },\r
419 +};\r
420 +\r
421 +/* for the specified protocol return the context pointer (initializing\r
422 + * if needed) */\r
423 +GMimeCryptoContext *\r
424 +_notmuch_crypto_get_gmime_context (_notmuch_crypto_t *crypto, const char *protocol)\r
425 +{\r
426 +    GMimeCryptoContext *cryptoctx = NULL;\r
427 +    size_t i;\r
428 +\r
429 +    if (! protocol) {\r
430 +       fprintf (stderr, "Cryptographic protocol is empty.\n");\r
431 +       return cryptoctx;\r
432 +    }\r
433 +\r
434 +    /* As per RFC 1847 section 2.1: "the [protocol] value token is\r
435 +     * comprised of the type and sub-type tokens of the Content-Type".\r
436 +     * As per RFC 1521 section 2: "Content-Type values, subtypes, and\r
437 +     * parameter names as defined in this document are\r
438 +     * case-insensitive."  Thus, we use strcasecmp for the protocol.\r
439 +     */\r
440 +    for (i = 0; i < ARRAY_SIZE (protocols); i++) {\r
441 +       if (strcasecmp (protocol, protocols[i].protocol) == 0)\r
442 +           return protocols[i].get_context (crypto);\r
443 +    }\r
444 +\r
445 +    fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",\r
446 +            protocol);\r
447 +\r
448 +    return NULL;\r
449 +}\r
450 +\r
451 +void\r
452 +_notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)\r
453 +{\r
454 +    if (crypto->gpgctx) {\r
455 +       g_object_unref (crypto->gpgctx);\r
456 +       crypto->gpgctx = NULL;\r
457 +    }\r
458 +}\r
459 diff --git a/util/crypto.h b/util/crypto.h\r
460 new file mode 100644\r
461 index 0000000..0e65472\r
462 --- /dev/null\r
463 +++ b/util/crypto.h\r
464 @@ -0,0 +1,23 @@\r
465 +#ifndef _CRYPTO_H\r
466 +#define _CRYPTO_H\r
467 +\r
468 +#include "notmuch.h"\r
469 +#include <gmime/gmime.h>\r
470 +\r
471 +typedef struct _notmuch_crypto {\r
472 +    GMimeCryptoContext* gpgctx;\r
473 +    notmuch_bool_t verify;\r
474 +    notmuch_bool_t decrypt;\r
475 +    const char *gpgpath;\r
476 +} _notmuch_crypto_t;\r
477 +\r
478 +\r
479 +GMimeCryptoContext *\r
480 +_notmuch_crypto_get_gmime_context (_notmuch_crypto_t *crypto, const char *protocol);\r
481 +\r
482 +void\r
483 +_notmuch_crypto_cleanup (_notmuch_crypto_t *crypto);\r
484 +\r
485 +\r
486 +\r
487 +#endif\r
488 -- \r
489 2.7.0.rc3\r
490 \r