Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 2a / 682711dcd3db730de3a41e1e4b09177203d84e
1 Return-Path: <jani@nikula.org>\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 826FD431FD0\r
6         for <notmuch@notmuchmail.org>; Sun,  3 Mar 2013 06:10:24 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 RYEOZXK7aNPN for <notmuch@notmuchmail.org>;\r
17         Sun,  3 Mar 2013 06:10:22 -0800 (PST)\r
18 Received: from mail-lb0-f175.google.com (mail-lb0-f175.google.com\r
19         [209.85.217.175]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 92605431FBF\r
22         for <notmuch@notmuchmail.org>; Sun,  3 Mar 2013 06:10:18 -0800 (PST)\r
23 Received: by mail-lb0-f175.google.com with SMTP id n3so3268560lbo.6\r
24         for <notmuch@notmuchmail.org>; Sun, 03 Mar 2013 06:10:17 -0800 (PST)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
28         :references:in-reply-to:references:x-gm-message-state;\r
29         bh=u5nYV6lPQeWzSD0ohAho2tviQoGMdjNz+oNnkID1qjE=;\r
30         b=QaPpZMsfDZDvt2WokOapjegeIO37dWo0G0s4t6hihiw7af6MoRhOAxg2viyA8hBTu1\r
31         KcZao7CnLoVrbPGj5uUouFsgsATr0sQwLkoqS/Wxs8mHk5YimalaFFKAg5/PuGf3Zkmi\r
32         x+c2IEdn5VIhbBWiaJEQ7GTEzEKulcw5z8YmZmiHMidm/1xmjiBYKdtjlfpiO/C1E2WY\r
33         e4oonriXNxei+IAjrdWw6Qta5yeL3LFsM+NHQt3lNV/QuFgCCdVI4Zjflg1NGg/af9Tg\r
34         77TFAJsOPoAePPPLG53hG5oKqkfHqGs2maBfcfeh2gr32jFJ8FoHrbJYx9YvvuLqw50I\r
35         2vHg==\r
36 X-Received: by 10.112.10.138 with SMTP id i10mr3278034lbb.24.1362319816975;\r
37         Sun, 03 Mar 2013 06:10:16 -0800 (PST)\r
38 Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
39         [80.223.81.27])\r
40         by mx.google.com with ESMTPS id fz16sm10111409lab.5.2013.03.03.06.10.15\r
41         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
42         Sun, 03 Mar 2013 06:10:16 -0800 (PST)\r
43 From: Jani Nikula <jani@nikula.org>\r
44 To: notmuch@notmuchmail.org\r
45 Subject: [PATCH 2/2] cli: mime node: abstract decryption and signature\r
46         verification\r
47 Date: Sun,  3 Mar 2013 16:10:11 +0200\r
48 Message-Id:\r
49  <d250faaac8aae1f6383b7a77e84e4b4144791688.1362319765.git.jani@nikula.org>\r
50 X-Mailer: git-send-email 1.7.10.4\r
51 In-Reply-To:\r
52  <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>\r
53 References:\r
54  <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>\r
55 In-Reply-To:\r
56  <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>\r
57 References:\r
58  <a9f12ba474fc51df71e2fd2b7a20a8d101729c6e.1362319765.git.jani@nikula.org>\r
59 X-Gm-Message-State:\r
60  ALoCoQlIbNTVORhZRgaPwImmcXNybkmI21zLix5fPWHMku2hhN+YinpCRaCTlRXXff3XTmWjQuvP\r
61 X-BeenThere: notmuch@notmuchmail.org\r
62 X-Mailman-Version: 2.1.13\r
63 Precedence: list\r
64 List-Id: "Use and development of the notmuch mail system."\r
65         <notmuch.notmuchmail.org>\r
66 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
68 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
69 List-Post: <mailto:notmuch@notmuchmail.org>\r
70 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
71 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
72         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
73 X-List-Received-Date: Sun, 03 Mar 2013 14:10:24 -0000\r
74 \r
75 The code filled with #ifdef GMIME_ATLEAST_26 is difficult to\r
76 read. Abstract the decryption and signature verification into\r
77 functions, with separate implementations for GMime 2.4 and 2.6, to\r
78 clarify the code.\r
79 \r
80 There should be no functional changes.\r
81 ---\r
82  mime-node.c |  210 ++++++++++++++++++++++++++++++++++++++---------------------\r
83  1 file changed, 134 insertions(+), 76 deletions(-)\r
84 \r
85 diff --git a/mime-node.c b/mime-node.c\r
86 index 839737a..ba7f709 100644\r
87 --- a/mime-node.c\r
88 +++ b/mime-node.c\r
89 @@ -130,26 +130,155 @@ DONE:\r
90  }\r
91  \r
92  #ifdef GMIME_ATLEAST_26\r
93 +\r
94  static int\r
95  _signature_list_free (GMimeSignatureList **proxy)\r
96  {\r
97      g_object_unref (*proxy);\r
98      return 0;\r
99  }\r
100 -#else\r
101 +\r
102 +static void\r
103 +set_signature_list_destructor (mime_node_t *node)\r
104 +{\r
105 +    GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);\r
106 +    if (proxy) {\r
107 +       *proxy = node->sig_list;\r
108 +       talloc_set_destructor (proxy, _signature_list_free);\r
109 +    }\r
110 +}\r
111 +\r
112 +static void\r
113 +node_verify (mime_node_t *node, GMimeObject *part,\r
114 +            notmuch_crypto_context_t *cryptoctx)\r
115 +{\r
116 +    GError *err = NULL;\r
117 +\r
118 +    node->verify_attempted = TRUE;\r
119 +    node->sig_list = g_mime_multipart_signed_verify\r
120 +       (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);\r
121 +\r
122 +    if (node->sig_list)\r
123 +       set_signature_list_destructor (node);\r
124 +    else\r
125 +       fprintf (stderr, "Failed to verify signed part: %s\n",\r
126 +                err ? err->message : "no error explanation given");\r
127 +\r
128 +    if (err)\r
129 +       g_error_free (err);\r
130 +}\r
131 +\r
132 +static void\r
133 +node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,\r
134 +                        notmuch_crypto_context_t *cryptoctx)\r
135 +{\r
136 +    GError *err = NULL;\r
137 +    GMimeDecryptResult *decrypt_result = NULL;\r
138 +    GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);\r
139 +\r
140 +    node->decrypt_attempted = TRUE;\r
141 +    node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
142 +       (encrypteddata, cryptoctx, &decrypt_result, &err);\r
143 +    if (! node->decrypted_child) {\r
144 +       fprintf (stderr, "Failed to decrypt part: %s\n",\r
145 +                err ? err->message : "no error explanation given");\r
146 +       goto DONE;\r
147 +    }\r
148 +\r
149 +    node->decrypt_success = TRUE;\r
150 +    node->verify_attempted = TRUE;\r
151 +\r
152 +    /* This may be NULL if the part is not signed. */\r
153 +    node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);\r
154 +    if (node->sig_list) {\r
155 +       g_object_ref (node->sig_list);\r
156 +       set_signature_list_destructor (node);\r
157 +    }\r
158 +    g_object_unref (decrypt_result);\r
159 +\r
160 + DONE:\r
161 +    if (err)\r
162 +       g_error_free (err);\r
163 +}\r
164 +\r
165 +#else /* GMIME_ATLEAST_26 */\r
166 +\r
167  static int\r
168  _signature_validity_free (GMimeSignatureValidity **proxy)\r
169  {\r
170      g_mime_signature_validity_free (*proxy);\r
171      return 0;\r
172  }\r
173 -#endif\r
174 +\r
175 +static void\r
176 +set_signature_validity_destructor (mime_node_t *node)\r
177 +{\r
178 +    GMimeSignatureValidity **proxy = talloc (node, GMimeSignatureValidity *);\r
179 +    if (proxy) {\r
180 +       *proxy = node->sig_validity;\r
181 +       talloc_set_destructor (proxy, _signature_validity_free);\r
182 +    }\r
183 +}\r
184 +\r
185 +static void\r
186 +node_verify (mime_node_t *node, GMimeObject *part,\r
187 +            notmuch_crypto_context_t *cryptoctx)\r
188 +{\r
189 +    GError *err = NULL;\r
190 +\r
191 +    node->verify_attempted = TRUE;\r
192 +    node->sig_validity = g_mime_multipart_signed_verify\r
193 +       (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);\r
194 +    if (node->sig_validity) {\r
195 +       set_signature_validity_destructor (node);\r
196 +    } else {\r
197 +       fprintf (stderr, "Failed to verify signed part: %s\n",\r
198 +                err ? err->message : "no error explanation given");\r
199 +    }\r
200 +\r
201 +    if (err)\r
202 +       g_error_free (err);\r
203 +}\r
204 +\r
205 +static void\r
206 +node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,\r
207 +                        notmuch_crypto_context_t *cryptoctx)\r
208 +{\r
209 +    GError *err = NULL;\r
210 +    GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);\r
211 +\r
212 +    node->decrypt_attempted = TRUE;\r
213 +    node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
214 +       (encrypteddata, cryptoctx, &err);\r
215 +    if (! node->decrypted_child) {\r
216 +       fprintf (stderr, "Failed to decrypt part: %s\n",\r
217 +                err ? err->message : "no error explanation given");\r
218 +       goto DONE;\r
219 +    }\r
220 +\r
221 +    node->decrypt_success = TRUE;\r
222 +    node->verify_attempted = TRUE;\r
223 +\r
224 +    /* The GMimeSignatureValidity returned here is a const, unlike the\r
225 +     * one returned by g_mime_multipart_signed_verify() in\r
226 +     * node_verify() above, so the destructor is not needed.\r
227 +     */\r
228 +    node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);\r
229 +    if (! node->sig_validity)\r
230 +       fprintf (stderr, "Failed to verify encrypted signed part: %s\n",\r
231 +                err ? err->message : "no error explanation given");\r
232 +\r
233 + DONE:\r
234 +    if (err)\r
235 +       g_error_free (err);\r
236 +}\r
237 +\r
238 +#endif  /* GMIME_ATLEAST_26 */\r
239  \r
240  static mime_node_t *\r
241  _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
242  {\r
243      mime_node_t *node = talloc_zero (parent, mime_node_t);\r
244 -    GError *err = NULL;\r
245      notmuch_crypto_context_t *cryptoctx = NULL;\r
246  \r
247      /* Set basic node properties */\r
248 @@ -198,32 +327,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
249                      "message (must be exactly 2)\n",\r
250                      node->nchildren);\r
251         } else {\r
252 -           GMimeMultipartEncrypted *encrypteddata =\r
253 -               GMIME_MULTIPART_ENCRYPTED (part);\r
254 -           node->decrypt_attempted = TRUE;\r
255 -#ifdef GMIME_ATLEAST_26\r
256 -           GMimeDecryptResult *decrypt_result = NULL;\r
257 -           node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
258 -               (encrypteddata, cryptoctx, &decrypt_result, &err);\r
259 -#else\r
260 -           node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
261 -               (encrypteddata, cryptoctx, &err);\r
262 -#endif\r
263 -           if (node->decrypted_child) {\r
264 -               node->decrypt_success = node->verify_attempted = TRUE;\r
265 -#ifdef GMIME_ATLEAST_26\r
266 -               /* This may be NULL if the part is not signed. */\r
267 -               node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);\r
268 -               if (node->sig_list)\r
269 -                   g_object_ref (node->sig_list);\r
270 -               g_object_unref (decrypt_result);\r
271 -#else\r
272 -               node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);\r
273 -#endif\r
274 -           } else {\r
275 -               fprintf (stderr, "Failed to decrypt part: %s\n",\r
276 -                        (err ? err->message : "no error explanation given"));\r
277 -           }\r
278 +           node_decrypt_and_verify (node, part, cryptoctx);\r
279         }\r
280      } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify && cryptoctx) {\r
281         if (node->nchildren != 2) {\r
282 @@ -232,56 +336,10 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
283                      "(must be exactly 2)\n",\r
284                      node->nchildren);\r
285         } else {\r
286 -#ifdef GMIME_ATLEAST_26\r
287 -           node->sig_list = g_mime_multipart_signed_verify\r
288 -               (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);\r
289 -           node->verify_attempted = TRUE;\r
290 -\r
291 -           if (!node->sig_list)\r
292 -               fprintf (stderr, "Failed to verify signed part: %s\n",\r
293 -                        (err ? err->message : "no error explanation given"));\r
294 -#else\r
295 -           /* For some reason the GMimeSignatureValidity returned\r
296 -            * here is not a const (inconsistent with that\r
297 -            * returned by\r
298 -            * g_mime_multipart_encrypted_get_signature_validity,\r
299 -            * and therefore needs to be properly disposed of.\r
300 -            *\r
301 -            * In GMime 2.6, they're both non-const, so we'll be able\r
302 -            * to clean up this asymmetry. */\r
303 -           GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify\r
304 -               (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);\r
305 -           node->verify_attempted = TRUE;\r
306 -           node->sig_validity = sig_validity;\r
307 -           if (sig_validity) {\r
308 -               GMimeSignatureValidity **proxy =\r
309 -                   talloc (node, GMimeSignatureValidity *);\r
310 -               *proxy = sig_validity;\r
311 -               talloc_set_destructor (proxy, _signature_validity_free);\r
312 -           }\r
313 -#endif\r
314 +           node_verify (node, part, cryptoctx);\r
315         }\r
316      }\r
317  \r
318 -#ifdef GMIME_ATLEAST_26\r
319 -    /* sig_list may be created in both above cases, so we need to\r
320 -     * cleanly handle it here. */\r
321 -    if (node->sig_list) {\r
322 -       GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);\r
323 -       *proxy = node->sig_list;\r
324 -       talloc_set_destructor (proxy, _signature_list_free);\r
325 -    }\r
326 -#endif\r
327 -\r
328 -#ifndef GMIME_ATLEAST_26\r
329 -    if (node->verify_attempted && !node->sig_validity)\r
330 -       fprintf (stderr, "Failed to verify signed part: %s\n",\r
331 -                (err ? err->message : "no error explanation given"));\r
332 -#endif\r
333 -\r
334 -    if (err)\r
335 -       g_error_free (err);\r
336 -\r
337      return node;\r
338  }\r
339  \r
340 -- \r
341 1.7.10.4\r
342 \r