notmuch and "mute" -- useful to anyone?
[notmuch-archives.git] / a1 / 3562b8d317530d73c84c48b1dd7cb1cb20d9f1
1 Return-Path: <amthrax@drake.mit.edu>\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 3CA09429E30\r
6         for <notmuch@notmuchmail.org>; Tue, 14 Feb 2012 09:34:05 -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.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 zLvbql7BoIrV for <notmuch@notmuchmail.org>;\r
16         Tue, 14 Feb 2012 09:34:00 -0800 (PST)\r
17 Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU\r
18         [18.9.25.13])\r
19         by olra.theworths.org (Postfix) with ESMTP id 3CAB8431E82\r
20         for <notmuch@notmuchmail.org>; Tue, 14 Feb 2012 09:33:57 -0800 (PST)\r
21 X-AuditID: 1209190d-b7fbf6d0000008ba-7f-4f3a9b04d1aa\r
22 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
23         by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id 6F.FF.02234.40B9A3F4; Tue, 14 Feb 2012 12:33:56 -0500 (EST)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q1EHXur3019624; \r
27         Tue, 14 Feb 2012 12:33:56 -0500\r
28 Received: from drake.mit.edu\r
29         (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
30         [209.6.116.242]) (authenticated bits=0)\r
31         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
32         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q1EHXsJH016494\r
33         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
34         Tue, 14 Feb 2012 12:33:55 -0500 (EST)\r
35 Received: from amthrax by drake.mit.edu with local (Exim 4.77)\r
36         (envelope-from <amthrax@drake.mit.edu>)\r
37         id 1RxMGI-000277-Od; Tue, 14 Feb 2012 12:33:54 -0500\r
38 From: Austin Clements <amdragon@MIT.EDU>\r
39 To: notmuch@notmuchmail.org\r
40 Subject: [PATCH 2/8] show: Convert JSON format to the new self-recursive style\r
41 Date: Tue, 14 Feb 2012 12:33:37 -0500\r
42 Message-Id: <1329240823-7856-3-git-send-email-amdragon@mit.edu>\r
43 X-Mailer: git-send-email 1.7.7.3\r
44 In-Reply-To: <1329240823-7856-1-git-send-email-amdragon@mit.edu>\r
45 References: <1329240823-7856-1-git-send-email-amdragon@mit.edu>\r
46 X-Brightmail-Tracker:\r
47  H4sIAAAAAAAAA+NgFjrKIsWRmVeSWpSXmKPExsUixG6nrssy28rf4OpRZYsje2axW1zd2s9u\r
48         cf3mTGYHZo+ds+6yezxbdYvZo+nHYtYA5igum5TUnMyy1CJ9uwSujI8nHzMWdARVnJxV1sB4\r
49         27GLkYNDQsBEYs1x5y5GTiBTTOLCvfVsXYxcHEIC+xglni1uZIRwNjBKbDi8ixnCuc8k8ffP\r
50         Z6jMfEaJrx+WsIL0swloSGzbv5wRxBYRkJbYeXc2WJxZIF7iSNt6sLiwgJ/E3dddrCCrWQRU\r
51         JeY8EAcJ8wrYS/w9+IkN4gwFiXOrz7GD2JwCDhL79/0HGyMEVHPp7laWCYz8CxgZVjHKpuRW\r
52         6eYmZuYUpybrFicn5uWlFuka6eVmluilppRuYgSFFqck7w7GdweVDjEKcDAq8fAaWFj6C7Em\r
53         lhVX5h5ilORgUhLlFZ5h5S/El5SfUpmRWJwRX1Sak1p8iFGCg1lJhHfTdKAcb0piZVVqUT5M\r
54         SpqDRUmcV1XrnZ+QQHpiSWp2ampBahFMVoaDQ0mCN2kWUKNgUWp6akVaZk4JQpqJgxNkOA/Q\r
55         8GSQGt7igsTc4sx0iPwpRkUpcV4PkIQASCKjNA+uFxb7rxjFgV4R5g0CqeIBpg247ldAg5mA\r
56         Bm8/bQEyuCQRISXVwMh3x1LPclvHeV3BtLIWwSJ/a5EN681Pxu9i3usQfvH46f3ZOzYlfksL\r
57         q58YK7h46t6EV9IJq/evWTzdkN9a5aKJfdSct686ZBglTapZyp1X38pSfr/478L9W7l+rrx2\r
58         YNLq3Io37cGfDxRkl1jbHH/EzND7YpduTNzh8PPTDoZULVde9FFuwlMlluKMREMt5qLiRABV\r
59         /iDi2AIAAA==\r
60 X-BeenThere: notmuch@notmuchmail.org\r
61 X-Mailman-Version: 2.1.13\r
62 Precedence: list\r
63 List-Id: "Use and development of the notmuch mail system."\r
64         <notmuch.notmuchmail.org>\r
65 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
66         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
67 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
68 List-Post: <mailto:notmuch@notmuchmail.org>\r
69 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
70 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
71         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
72 X-List-Received-Date: Tue, 14 Feb 2012 17:34:05 -0000\r
73 \r
74 As before, this is all code movement and a smidgen of glue.  This\r
75 moves the existing JSON formatter code into one self-recursive\r
76 function, but doesn't change any of the logic to take advantage of the\r
77 new structure.\r
78 \r
79 In general, "leafs" of the JSON structure are left in helper functions\r
80 (most of them untouched), so that it's easy to see the overall\r
81 structure of the format from the main recursive function.\r
82 ---\r
83  notmuch-show.c |  273 ++++++++++++++++++++++++++++----------------------------\r
84  1 files changed, 135 insertions(+), 138 deletions(-)\r
85 \r
86 diff --git a/notmuch-show.c b/notmuch-show.c\r
87 index 93fb16f..868b2cd 100644\r
88 --- a/notmuch-show.c\r
89 +++ b/notmuch-show.c\r
90 @@ -35,52 +35,14 @@ static const notmuch_show_format_t format_text = {\r
91  };\r
92  \r
93  static void\r
94 -format_message_json (const void *ctx,\r
95 -                    notmuch_message_t *message,\r
96 -                    unused (int indent));\r
97 -static void\r
98 -format_headers_json (const void *ctx,\r
99 -                    notmuch_message_t *message);\r
100 +format_part_json_entry (const void *ctx, mime_node_t *node,\r
101 +                       int indent, const notmuch_show_params_t *params);\r
102  \r
103 -static void\r
104 -format_headers_message_part_json (GMimeMessage *message);\r
105 -\r
106 -static void\r
107 -format_part_start_json (unused (GMimeObject *part),\r
108 -                       int *part_count);\r
109 -\r
110 -static void\r
111 -format_part_encstatus_json (int status);\r
112 -\r
113 -static void\r
114 -#ifdef GMIME_ATLEAST_26\r
115 -format_part_sigstatus_json (GMimeSignatureList* siglist);\r
116 -#else\r
117 -format_part_sigstatus_json (const GMimeSignatureValidity* validity);\r
118 -#endif\r
119 -\r
120 -static void\r
121 -format_part_content_json (GMimeObject *part);\r
122 -\r
123 -static void\r
124 -format_part_end_json (GMimeObject *part);\r
125 -\r
126 -/* Any changes to the JSON format should be reflected in the file\r
127 - * devel/schemata. */\r
128  static const notmuch_show_format_t format_json = {\r
129 -    "[", NULL,\r
130 -       "{", format_message_json,\r
131 -           "\"headers\": {", format_headers_json, format_headers_message_part_json, "}",\r
132 -           ", \"body\": [",\r
133 -               format_part_start_json,\r
134 -               format_part_encstatus_json,\r
135 -               format_part_sigstatus_json,\r
136 -               format_part_content_json,\r
137 -               format_part_end_json,\r
138 -               ", ",\r
139 -           "]",\r
140 -       "}", ", ",\r
141 -    "]"\r
142 +    .message_set_start = "[",\r
143 +    .part = format_part_json_entry,\r
144 +    .message_set_sep = ", ",\r
145 +    .message_set_end = "]"\r
146  };\r
147  \r
148  static void\r
149 @@ -170,7 +132,7 @@ _get_one_line_summary (const void *ctx, notmuch_message_t *message)\r
150  }\r
151  \r
152  static void\r
153 -format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))\r
154 +format_message_json (const void *ctx, notmuch_message_t *message)\r
155  {\r
156      notmuch_tags_t *tags;\r
157      int first = 1;\r
158 @@ -471,24 +433,6 @@ signer_status_to_string (GMimeSignerStatus x)\r
159  }\r
160  #endif\r
161  \r
162 -static void\r
163 -format_part_start_json (unused (GMimeObject *part), int *part_count)\r
164 -{\r
165 -    printf ("{\"id\": %d", *part_count);\r
166 -}\r
167 -\r
168 -static void\r
169 -format_part_encstatus_json (int status)\r
170 -{\r
171 -    printf (", \"encstatus\": [{\"status\": ");\r
172 -    if (status) {\r
173 -       printf ("\"good\"");\r
174 -    } else {\r
175 -       printf ("\"bad\"");\r
176 -    }\r
177 -    printf ("}]");\r
178 -}\r
179 -\r
180  #ifdef GMIME_ATLEAST_26\r
181  static void\r
182  format_part_sigstatus_json (GMimeSignatureList *siglist)\r
183 @@ -619,81 +563,6 @@ format_part_sigstatus_json (const GMimeSignatureValidity* validity)\r
184  #endif\r
185  \r
186  static void\r
187 -format_part_content_json (GMimeObject *part)\r
188 -{\r
189 -    GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));\r
190 -    GMimeStream *stream_memory = g_mime_stream_mem_new ();\r
191 -    const char *cid = g_mime_object_get_content_id (part);\r
192 -    void *ctx = talloc_new (NULL);\r
193 -    GByteArray *part_content;\r
194 -\r
195 -    printf (", \"content-type\": %s",\r
196 -           json_quote_str (ctx, g_mime_content_type_to_string (content_type)));\r
197 -\r
198 -    if (cid != NULL)\r
199 -           printf(", \"content-id\": %s", json_quote_str (ctx, cid));\r
200 -\r
201 -    if (GMIME_IS_PART (part))\r
202 -    {\r
203 -       const char *filename = g_mime_part_get_filename (GMIME_PART (part));\r
204 -       if (filename)\r
205 -           printf (", \"filename\": %s", json_quote_str (ctx, filename));\r
206 -    }\r
207 -\r
208 -    if (g_mime_content_type_is_type (content_type, "text", "*"))\r
209 -    {\r
210 -       /* For non-HTML text parts, we include the content in the\r
211 -        * JSON. Since JSON must be Unicode, we handle charset\r
212 -        * decoding here and do not report a charset to the caller.\r
213 -        * For text/html parts, we do not include the content. If a\r
214 -        * caller is interested in text/html parts, it should retrieve\r
215 -        * them separately and they will not be decoded. Since this\r
216 -        * makes charset decoding the responsibility on the caller, we\r
217 -        * report the charset for text/html parts.\r
218 -        */\r
219 -       if (g_mime_content_type_is_type (content_type, "text", "html"))\r
220 -       {\r
221 -           const char *content_charset = g_mime_object_get_content_type_parameter (GMIME_OBJECT (part), "charset");\r
222 -\r
223 -           if (content_charset != NULL)\r
224 -               printf (", \"content-charset\": %s", json_quote_str (ctx, content_charset));\r
225 -       }\r
226 -       else\r
227 -       {\r
228 -           show_text_part_content (part, stream_memory);\r
229 -           part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));\r
230 -\r
231 -           printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));\r
232 -       }\r
233 -    }\r
234 -    else if (g_mime_content_type_is_type (content_type, "multipart", "*"))\r
235 -    {\r
236 -       printf (", \"content\": [");\r
237 -    }\r
238 -    else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))\r
239 -    {\r
240 -       printf (", \"content\": [{");\r
241 -    }\r
242 -\r
243 -    talloc_free (ctx);\r
244 -    if (stream_memory)\r
245 -       g_object_unref (stream_memory);\r
246 -}\r
247 -\r
248 -static void\r
249 -format_part_end_json (GMimeObject *part)\r
250 -{\r
251 -    GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));\r
252 -\r
253 -    if (g_mime_content_type_is_type (content_type, "multipart", "*"))\r
254 -       printf ("]");\r
255 -    else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))\r
256 -       printf ("}]");\r
257 -\r
258 -    printf ("}");\r
259 -}\r
260 -\r
261 -static void\r
262  format_part_content_raw (GMimeObject *part)\r
263  {\r
264      if (! GMIME_IS_PART (part))\r
265 @@ -811,6 +680,134 @@ format_part_text (const void *ctx, mime_node_t *node,\r
266  }\r
267  \r
268  static void\r
269 +format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
270 +{\r
271 +    /* Any changes to the JSON format should be reflected in the file\r
272 +     * devel/schemata. */\r
273 +\r
274 +    if (node->envelope_file) {\r
275 +       printf ("{");\r
276 +       format_message_json (ctx, node->envelope_file);\r
277 +\r
278 +       printf ("\"headers\": {");\r
279 +       format_headers_json (ctx, node->envelope_file);\r
280 +       printf ("}");\r
281 +\r
282 +       printf (", \"body\": [");\r
283 +       format_part_json (ctx, mime_node_child (node, 0), first);\r
284 +\r
285 +       printf ("]}");\r
286 +       return;\r
287 +    }\r
288 +\r
289 +    void *local = talloc_new (ctx);\r
290 +    /* The disposition and content-type metadata are associated with\r
291 +     * the envelope for message parts */\r
292 +    GMimeObject *meta = node->envelope_part ?\r
293 +       GMIME_OBJECT (node->envelope_part) : node->part;\r
294 +    GMimeContentType *content_type = g_mime_object_get_content_type (meta);\r
295 +    GMimeStream *stream_memory = g_mime_stream_mem_new ();\r
296 +    const char *cid = g_mime_object_get_content_id (meta);\r
297 +    GByteArray *part_content;\r
298 +    int i;\r
299 +\r
300 +    if (!first)\r
301 +       printf (", ");\r
302 +\r
303 +    printf ("{\"id\": %d", node->part_num);\r
304 +\r
305 +    if (node->decrypt_attempted) {\r
306 +       printf (", \"encstatus\": [{\"status\": ");\r
307 +       if (node->decrypt_success) {\r
308 +           printf ("\"good\"");\r
309 +       } else {\r
310 +           printf ("\"bad\"");\r
311 +       }\r
312 +       printf ("}]");\r
313 +    }\r
314 +\r
315 +    if (node->verify_attempted) {\r
316 +#ifdef GMIME_ATLEAST_26\r
317 +       format_part_sigstatus_json (node->sig_list);\r
318 +#else\r
319 +       format_part_sigstatus_json (node->sig_validity);\r
320 +#endif\r
321 +    }\r
322 +\r
323 +    printf (", \"content-type\": %s",\r
324 +           json_quote_str (local, g_mime_content_type_to_string (content_type)));\r
325 +\r
326 +    if (cid != NULL)\r
327 +           printf(", \"content-id\": %s", json_quote_str (local, cid));\r
328 +\r
329 +    if (GMIME_IS_PART (node->part)) {\r
330 +       const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));\r
331 +       if (filename)\r
332 +           printf (", \"filename\": %s", json_quote_str (local, filename));\r
333 +    }\r
334 +\r
335 +    if (g_mime_content_type_is_type (content_type, "text", "*")) {\r
336 +       /* For non-HTML text parts, we include the content in the\r
337 +        * JSON. Since JSON must be Unicode, we handle charset\r
338 +        * decoding here and do not report a charset to the caller.\r
339 +        * For text/html parts, we do not include the content. If a\r
340 +        * caller is interested in text/html parts, it should retrieve\r
341 +        * them separately and they will not be decoded. Since this\r
342 +        * makes charset decoding the responsibility on the caller, we\r
343 +        * report the charset for text/html parts.\r
344 +        */\r
345 +       if (g_mime_content_type_is_type (content_type, "text", "html")) {\r
346 +           const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");\r
347 +\r
348 +           if (content_charset != NULL)\r
349 +               printf (", \"content-charset\": %s", json_quote_str (local, content_charset));\r
350 +       } else {\r
351 +           show_text_part_content (node->part, stream_memory);\r
352 +           part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));\r
353 +\r
354 +           printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));\r
355 +       }\r
356 +    } else if (g_mime_content_type_is_type (content_type, "multipart", "*")) {\r
357 +       printf (", \"content\": [");\r
358 +    } else if (g_mime_content_type_is_type (content_type, "message", "rfc822")) {\r
359 +       printf (", \"content\": [{");\r
360 +    }\r
361 +\r
362 +    if (stream_memory)\r
363 +       g_object_unref (stream_memory);\r
364 +\r
365 +    if (GMIME_IS_MESSAGE (node->part)) {\r
366 +       printf ("\"headers\": {");\r
367 +       format_headers_message_part_json (GMIME_MESSAGE (node->part));\r
368 +       printf ("}");\r
369 +\r
370 +       printf (", \"body\": [");\r
371 +    }\r
372 +\r
373 +    for (i = 0; i < node->nchildren; i++)\r
374 +       format_part_json (ctx, mime_node_child (node, i), i == 0);\r
375 +\r
376 +    if (GMIME_IS_MESSAGE (node->part))\r
377 +       printf ("]");\r
378 +\r
379 +    if (g_mime_content_type_is_type (content_type, "multipart", "*"))\r
380 +       printf ("]");\r
381 +    else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))\r
382 +       printf ("}]");\r
383 +\r
384 +    printf ("}");\r
385 +\r
386 +    talloc_free (local);\r
387 +}\r
388 +\r
389 +static void\r
390 +format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),\r
391 +                       unused (const notmuch_show_params_t *params))\r
392 +{\r
393 +    format_part_json (ctx, node, TRUE);\r
394 +}\r
395 +\r
396 +static void\r
397  show_message (void *ctx,\r
398               const notmuch_show_format_t *format,\r
399               notmuch_message_t *message,\r
400 -- \r
401 1.7.7.3\r
402 \r