Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 13 / 7214f5db16b144d3bb9f467790fe98db984ae5
1 Return-Path: <schnouki@schnouki.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 30542431FC3\r
6         for <notmuch@notmuchmail.org>; Fri, 20 Jan 2012 01:39:40 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.1\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.1 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1]\r
13         autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 1PZ2x86XCt99 for <notmuch@notmuchmail.org>;\r
17         Fri, 20 Jan 2012 01:39:36 -0800 (PST)\r
18 Received: from ks3536.kimsufi.com (schnouki.net [87.98.217.222])\r
19         by olra.theworths.org (Postfix) with ESMTP id 1C8A1431FAF\r
20         for <notmuch@notmuchmail.org>; Fri, 20 Jan 2012 01:39:36 -0800 (PST)\r
21 Received: from thor.loria.fr (thor.loria.fr [152.81.12.250])\r
22         by ks3536.kimsufi.com (Postfix) with ESMTPSA id B64516C000B;\r
23         Fri, 20 Jan 2012 10:39:23 +0100 (CET)\r
24 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=schnouki.net;\r
25         s=key-schnouki; t=1327052363;\r
26         bh=aYkJin9xklD3p00j4qOwXl+UclLO329MeZytu7vdktM=;\r
27         h=From:To:Subject:Date:Message-Id:In-Reply-To:References;\r
28         b=MHRIV9bfordwhlt+qVPjp3XbdZjhd8NM/3GlrNkMb60r7l1TqveMbnkUx7zKby+xG\r
29         UQm9T1V0j5QJ2C01JB8NFiJfX+i0QaVtaVywrIgmaNHVgKDYxepDROXxbfsMrWCkz7\r
30         4cACGpkzifBc04dE3vTlXfdGDCfhJxABuR+Nc3UA=\r
31 From: Thomas Jost <schnouki@schnouki.net>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v4 2/3] Add compatibility with gmime 2.6\r
34 Date: Fri, 20 Jan 2012 10:39:24 +0100\r
35 Message-Id: <1327052365-20012-2-git-send-email-schnouki@schnouki.net>\r
36 X-Mailer: git-send-email 1.7.8.4\r
37 In-Reply-To: <1327052365-20012-1-git-send-email-schnouki@schnouki.net>\r
38 References: <87fwfaemuc.fsf@thor.loria.fr>\r
39         <1327052365-20012-1-git-send-email-schnouki@schnouki.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: Fri, 20 Jan 2012 09:39:40 -0000\r
53 \r
54 There are lots of API changes in gmime 2.6 crypto handling. By adding\r
55 preprocessor directives, it is however possible to add gmime 2.6 compatibility\r
56 while preserving compatibility with gmime 2.4 too.\r
57 \r
58 This is mostly based on id:"8762i8hrb9.fsf@bookbinder.fernseed.info".\r
59 \r
60 This was tested against both gmime 2.6.4 and 2.4.31. With gmime 2.4.31, the\r
61 crypto tests all work fine (as expected). With gmime 2.6.4, one crypto test is\r
62 currently broken (signature verification with signer key unavailable), most\r
63 likely because of a bug in gmime which will hopefully be fixed in a future\r
64 version.\r
65 ---\r
66  mime-node.c      |   57 +++++++++++++++++++++++++++++++-\r
67  notmuch-client.h |   30 ++++++++++++++++-\r
68  notmuch-reply.c  |    7 ++++\r
69  notmuch-show.c   |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
70  show-message.c   |    4 ++\r
71  test/crypto      |    2 +\r
72  6 files changed, 191 insertions(+), 4 deletions(-)\r
73 \r
74 diff --git a/mime-node.c b/mime-node.c\r
75 index d26bb44..27077f7 100644\r
76 --- a/mime-node.c\r
77 +++ b/mime-node.c\r
78 @@ -33,7 +33,11 @@ typedef struct mime_node_context {\r
79      GMimeMessage *mime_message;\r
80  \r
81      /* Context provided by the caller. */\r
82 +#ifdef GMIME_ATLEAST_26\r
83 +    GMimeCryptoContext *cryptoctx;\r
84 +#else\r
85      GMimeCipherContext *cryptoctx;\r
86 +#endif\r
87      notmuch_bool_t decrypt;\r
88  } mime_node_context_t;\r
89  \r
90 @@ -57,8 +61,12 @@ _mime_node_context_free (mime_node_context_t *res)\r
91  \r
92  notmuch_status_t\r
93  mime_node_open (const void *ctx, notmuch_message_t *message,\r
94 -               GMimeCipherContext *cryptoctx, notmuch_bool_t decrypt,\r
95 -               mime_node_t **root_out)\r
96 +#ifdef GMIME_ATLEAST_26\r
97 +               GMimeCryptoContext *cryptoctx,\r
98 +#else\r
99 +               GMimeCipherContext *cryptoctx,\r
100 +#endif\r
101 +               notmuch_bool_t decrypt, mime_node_t **root_out)\r
102  {\r
103      const char *filename = notmuch_message_get_filename (message);\r
104      mime_node_context_t *mctx;\r
105 @@ -112,12 +120,21 @@ DONE:\r
106      return status;\r
107  }\r
108  \r
109 +#ifdef GMIME_ATLEAST_26\r
110 +static int\r
111 +_signature_list_free (GMimeSignatureList **proxy)\r
112 +{\r
113 +    g_object_unref (*proxy);\r
114 +    return 0;\r
115 +}\r
116 +#else\r
117  static int\r
118  _signature_validity_free (GMimeSignatureValidity **proxy)\r
119  {\r
120      g_mime_signature_validity_free (*proxy);\r
121      return 0;\r
122  }\r
123 +#endif\r
124  \r
125  static mime_node_t *\r
126  _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
127 @@ -165,11 +182,25 @@ _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
128             GMimeMultipartEncrypted *encrypteddata =\r
129                 GMIME_MULTIPART_ENCRYPTED (part);\r
130             node->decrypt_attempted = TRUE;\r
131 +#ifdef GMIME_ATLEAST_26\r
132 +           GMimeDecryptResult *decrypt_result = NULL;\r
133 +           node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
134 +               (encrypteddata, node->ctx->cryptoctx, &decrypt_result, &err);\r
135 +#else\r
136             node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
137                 (encrypteddata, node->ctx->cryptoctx, &err);\r
138 +#endif\r
139             if (node->decrypted_child) {\r
140                 node->decrypt_success = node->verify_attempted = TRUE;\r
141 +#ifdef GMIME_ATLEAST_26\r
142 +               /* This may be NULL if the part is not signed. */\r
143 +               node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);\r
144 +               if (node->sig_list)\r
145 +                   g_object_ref (node->sig_list);\r
146 +               g_object_unref (decrypt_result);\r
147 +#else\r
148                 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);\r
149 +#endif\r
150             } else {\r
151                 fprintf (stderr, "Failed to decrypt part: %s\n",\r
152                          (err ? err->message : "no error explanation given"));\r
153 @@ -182,6 +213,15 @@ _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
154                      "(must be exactly 2)\n",\r
155                      node->nchildren);\r
156         } else {\r
157 +#ifdef GMIME_ATLEAST_26\r
158 +           node->sig_list = g_mime_multipart_signed_verify\r
159 +               (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);\r
160 +           node->verify_attempted = TRUE;\r
161 +\r
162 +           if (!node->sig_list)\r
163 +               fprintf (stderr, "Failed to verify signed part: %s\n",\r
164 +                        (err ? err->message : "no error explanation given"));\r
165 +#else\r
166             /* For some reason the GMimeSignatureValidity returned\r
167              * here is not a const (inconsistent with that\r
168              * returned by\r
169 @@ -200,12 +240,25 @@ _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
170                 *proxy = sig_validity;\r
171                 talloc_set_destructor (proxy, _signature_validity_free);\r
172             }\r
173 +#endif\r
174         }\r
175      }\r
176  \r
177 +#ifdef GMIME_ATLEAST_26\r
178 +    /* sig_list may be created in both above cases, so we need to\r
179 +     * cleanly handle it here. */\r
180 +    if (node->sig_list) {\r
181 +       GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);\r
182 +       *proxy = node->sig_list;\r
183 +       talloc_set_destructor (proxy, _signature_list_free);\r
184 +    }\r
185 +#endif\r
186 +\r
187 +#ifndef GMIME_ATLEAST_26\r
188      if (node->verify_attempted && !node->sig_validity)\r
189         fprintf (stderr, "Failed to verify signed part: %s\n",\r
190                  (err ? err->message : "no error explanation given"));\r
191 +#endif\r
192  \r
193      if (err)\r
194         g_error_free (err);\r
195 diff --git a/notmuch-client.h b/notmuch-client.h\r
196 index 62ede28..9c1d383 100644\r
197 --- a/notmuch-client.h\r
198 +++ b/notmuch-client.h\r
199 @@ -30,6 +30,14 @@\r
200  \r
201  #include <gmime/gmime.h>\r
202  \r
203 +/* GMIME_CHECK_VERSION in gmime 2.4 is not usable from the\r
204 + * preprocessor (it calls a runtime function). But since\r
205 + * GMIME_MAJOR_VERSION and friends were added in gmime 2.6, we can use\r
206 + * these to check the version number. */\r
207 +#ifdef GMIME_MAJOR_VERSION\r
208 +#define GMIME_ATLEAST_26\r
209 +#endif\r
210 +\r
211  #include "notmuch.h"\r
212  \r
213  /* This is separate from notmuch-private.h because we're trying to\r
214 @@ -69,7 +77,11 @@ typedef struct notmuch_show_format {\r
215      void (*part_start) (GMimeObject *part,\r
216                         int *part_count);\r
217      void (*part_encstatus) (int status);\r
218 +#ifdef GMIME_ATLEAST_26\r
219 +    void (*part_sigstatus) (GMimeSignatureList* siglist);\r
220 +#else\r
221      void (*part_sigstatus) (const GMimeSignatureValidity* validity);\r
222 +#endif\r
223      void (*part_content) (GMimeObject *part);\r
224      void (*part_end) (GMimeObject *part);\r
225      const char *part_sep;\r
226 @@ -83,7 +95,11 @@ typedef struct notmuch_show_params {\r
227      int entire_thread;\r
228      int raw;\r
229      int part;\r
230 +#ifdef GMIME_ATLEAST_26\r
231 +    GMimeCryptoContext* cryptoctx;\r
232 +#else\r
233      GMimeCipherContext* cryptoctx;\r
234 +#endif\r
235      int decrypt;\r
236  } notmuch_show_params_t;\r
237  \r
238 @@ -290,11 +306,17 @@ typedef struct mime_node {\r
239  \r
240      /* True if signature verification on this part was attempted. */\r
241      notmuch_bool_t verify_attempted;\r
242 +#ifdef GMIME_ATLEAST_26\r
243 +    /* The list of signatures for signed or encrypted containers. If\r
244 +     * there are no signatures, this will be NULL. */\r
245 +    GMimeSignatureList* sig_list;\r
246 +#else\r
247      /* For signed or encrypted containers, the validity of the\r
248       * signature.  May be NULL if signature verification failed.  If\r
249       * there are simply no signatures, this will be non-NULL with an\r
250       * empty signers list. */\r
251      const GMimeSignatureValidity *sig_validity;\r
252 +#endif\r
253  \r
254      /* Internal: Context inherited from the root iterator. */\r
255      struct mime_node_context *ctx;\r
256 @@ -319,8 +341,12 @@ typedef struct mime_node {\r
257   */\r
258  notmuch_status_t\r
259  mime_node_open (const void *ctx, notmuch_message_t *message,\r
260 -               GMimeCipherContext *cryptoctx, notmuch_bool_t decrypt,\r
261 -               mime_node_t **node_out);\r
262 +#ifdef GMIME_ATLEAST_26\r
263 +               GMimeCryptoContext *cryptoctx,\r
264 +#else\r
265 +               GMimeCipherContext *cryptoctx,\r
266 +#endif\r
267 +               notmuch_bool_t decrypt, mime_node_t **node_out);\r
268  \r
269  /* Return a new MIME node for the requested child part of parent.\r
270   * parent will be used as the talloc context for the returned child\r
271 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
272 index 0f682db..bf67960 100644\r
273 --- a/notmuch-reply.c\r
274 +++ b/notmuch-reply.c\r
275 @@ -688,15 +688,22 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
276         reply_format_func = notmuch_reply_format_default;\r
277  \r
278      if (decrypt) {\r
279 +#ifdef GMIME_ATLEAST_26\r
280 +       /* TODO: GMimePasswordRequestFunc */\r
281 +       params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");\r
282 +#else\r
283         GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
284         params.cryptoctx = g_mime_gpg_context_new (session, "gpg");\r
285 +#endif\r
286         if (params.cryptoctx) {\r
287             g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);\r
288             params.decrypt = TRUE;\r
289         } else {\r
290             fprintf (stderr, "Failed to construct gpg context.\n");\r
291         }\r
292 +#ifndef GMIME_ATLEAST_26\r
293         g_object_unref (session);\r
294 +#endif\r
295      }\r
296  \r
297      config = notmuch_config_open (ctx, NULL, NULL);\r
298 diff --git a/notmuch-show.c b/notmuch-show.c\r
299 index 91f566c..190210c 100644\r
300 --- a/notmuch-show.c\r
301 +++ b/notmuch-show.c\r
302 @@ -76,7 +76,11 @@ static void\r
303  format_part_encstatus_json (int status);\r
304  \r
305  static void\r
306 +#ifdef GMIME_ATLEAST_26\r
307 +format_part_sigstatus_json (GMimeSignatureList* siglist);\r
308 +#else\r
309  format_part_sigstatus_json (const GMimeSignatureValidity* validity);\r
310 +#endif\r
311  \r
312  static void\r
313  format_part_content_json (GMimeObject *part);\r
314 @@ -486,6 +490,21 @@ show_text_part_content (GMimeObject *part, GMimeStream *stream_out)\r
315         g_object_unref(stream_filter);\r
316  }\r
317  \r
318 +#ifdef GMIME_ATLEAST_26\r
319 +static const char*\r
320 +signature_status_to_string (GMimeSignatureStatus x)\r
321 +{\r
322 +    switch (x) {\r
323 +    case GMIME_SIGNATURE_STATUS_GOOD:\r
324 +       return "good";\r
325 +    case GMIME_SIGNATURE_STATUS_BAD:\r
326 +       return "bad";\r
327 +    case GMIME_SIGNATURE_STATUS_ERROR:\r
328 +       return "error";\r
329 +    }\r
330 +    return "unknown";\r
331 +}\r
332 +#else\r
333  static const char*\r
334  signer_status_to_string (GMimeSignerStatus x)\r
335  {\r
336 @@ -501,6 +520,7 @@ signer_status_to_string (GMimeSignerStatus x)\r
337      }\r
338      return "unknown";\r
339  }\r
340 +#endif\r
341  \r
342  static void\r
343  format_part_start_text (GMimeObject *part, int *part_count)\r
344 @@ -592,6 +612,73 @@ format_part_encstatus_json (int status)\r
345      printf ("}]");\r
346  }\r
347  \r
348 +#ifdef GMIME_ATLEAST_26\r
349 +static void\r
350 +format_part_sigstatus_json (GMimeSignatureList *siglist)\r
351 +{\r
352 +    printf (", \"sigstatus\": [");\r
353 +\r
354 +    if (!siglist) {\r
355 +       printf ("]");\r
356 +       return;\r
357 +    }\r
358 +\r
359 +    void *ctx_quote = talloc_new (NULL);\r
360 +    int i;\r
361 +    for (i = 0; i < g_mime_signature_list_length (siglist); i++) {\r
362 +       GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);\r
363 +\r
364 +       if (i > 0)\r
365 +           printf (", ");\r
366 +\r
367 +       printf ("{");\r
368 +\r
369 +       /* status */\r
370 +       GMimeSignatureStatus status = g_mime_signature_get_status (signature);\r
371 +       printf ("\"status\": %s",\r
372 +               json_quote_str (ctx_quote,\r
373 +                               signature_status_to_string (status)));\r
374 +\r
375 +       GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);\r
376 +       if (status == GMIME_SIGNATURE_STATUS_GOOD) {\r
377 +           if (certificate)\r
378 +               printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));\r
379 +           /* these dates are seconds since the epoch; should we\r
380 +            * provide a more human-readable format string? */\r
381 +           time_t created = g_mime_signature_get_created (signature);\r
382 +           if (created != -1)\r
383 +               printf (", \"created\": %d", (int) created);\r
384 +           time_t expires = g_mime_signature_get_expires (signature);\r
385 +           if (expires > 0)\r
386 +               printf (", \"expires\": %d", (int) expires);\r
387 +           /* output user id only if validity is FULL or ULTIMATE. */\r
388 +           /* note that gmime is using the term "trust" here, which\r
389 +            * is WRONG.  It's actually user id "validity". */\r
390 +           if (certificate) {\r
391 +               const char *name = g_mime_certificate_get_name (certificate);\r
392 +               GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);\r
393 +               if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))\r
394 +                   printf (", \"userid\": %s", json_quote_str (ctx_quote, name));\r
395 +           }\r
396 +       } else if (certificate) {\r
397 +           const char *key_id = g_mime_certificate_get_key_id (certificate);\r
398 +           if (key_id)\r
399 +               printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));\r
400 +       }\r
401 +\r
402 +       GMimeSignatureError errors = g_mime_signature_get_errors (signature);\r
403 +       if (errors != GMIME_SIGNATURE_ERROR_NONE) {\r
404 +           printf (", \"errors\": %d", errors);\r
405 +       }\r
406 +\r
407 +       printf ("}");\r
408 +     }\r
409 +\r
410 +    printf ("]");\r
411 +\r
412 +    talloc_free (ctx_quote);\r
413 +}\r
414 +#else\r
415  static void\r
416  format_part_sigstatus_json (const GMimeSignatureValidity* validity)\r
417  {\r
418 @@ -652,6 +739,7 @@ format_part_sigstatus_json (const GMimeSignatureValidity* validity)\r
419  \r
420      talloc_free (ctx_quote);\r
421  }\r
422 +#endif\r
423  \r
424  static void\r
425  format_part_content_json (GMimeObject *part)\r
426 @@ -990,13 +1078,20 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
427         } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||\r
428                    (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {\r
429             if (params.cryptoctx == NULL) {\r
430 +#ifdef GMIME_ATLEAST_26\r
431 +               /* TODO: GMimePasswordRequestFunc */\r
432 +               if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))\r
433 +#else\r
434                 GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);\r
435                 if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))\r
436 +#endif\r
437                     fprintf (stderr, "Failed to construct gpg context.\n");\r
438                 else\r
439                     g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);\r
440 +#ifndef GMIME_ATLEAST_26\r
441                 g_object_unref (session);\r
442                 session = NULL;\r
443 +#endif\r
444             }\r
445             if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)\r
446                 params.decrypt = 1;\r
447 diff --git a/show-message.c b/show-message.c\r
448 index 8768889..83ecf81 100644\r
449 --- a/show-message.c\r
450 +++ b/show-message.c\r
451 @@ -48,7 +48,11 @@ show_message_part (mime_node_t *node,\r
452         format->part_encstatus (node->decrypt_success);\r
453  \r
454      if (node->verify_attempted && format->part_sigstatus)\r
455 +#ifdef GMIME_ATLEAST_26\r
456 +       format->part_sigstatus (node->sig_list);\r
457 +#else\r
458         format->part_sigstatus (node->sig_validity);\r
459 +#endif\r
460  \r
461      format->part_content (part);\r
462  \r
463 diff --git a/test/crypto b/test/crypto\r
464 index 0af4aa8..446a58b 100755\r
465 --- a/test/crypto\r
466 +++ b/test/crypto\r
467 @@ -104,6 +104,8 @@ test_expect_equal \\r
468      "$expected"\r
469  \r
470  test_begin_subtest "signature verification with signer key unavailable"\r
471 +# this is broken with current versions of gmime-2.6\r
472 +(ldd $(which notmuch) | grep -Fq gmime-2.6) && test_subtest_known_broken\r
473  # move the gnupghome temporarily out of the way\r
474  mv "${GNUPGHOME}"{,.bak}\r
475  output=$(notmuch show --format=json --verify subject:"test signed message 001" \\r
476 -- \r
477 1.7.8.4\r
478 \r