RE: Reply all - issue
[notmuch-archives.git] / 4a / ac7ad1a8d1084c9d3dde3f85b0826581328379
1 Return-Path: <awg@lagos.xvx.ca>\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 2E493431FD6\r
6         for <notmuch@notmuchmail.org>; Tue, 13 Mar 2012 21:30:29 -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: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_NONE=-0.0001] 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 Sf1I+PnQXQGk for <notmuch@notmuchmail.org>;\r
16         Tue, 13 Mar 2012 21:30:26 -0700 (PDT)\r
17 Received: from idcmail-mo2no.shaw.ca (idcmail-mo2no.shaw.ca [64.59.134.9])\r
18         by olra.theworths.org (Postfix) with ESMTP id 3B1A5431FAF\r
19         for <notmuch@notmuchmail.org>; Tue, 13 Mar 2012 21:30:23 -0700 (PDT)\r
20 Received: from lb7f8hsrpno-svcs.dcs.int.inet (HELO pd7ml2no-ssvc.prod.shaw.ca)\r
21         ([10.0.144.222])\r
22         by pd6mo1no-svcs.prod.shaw.ca with ESMTP; 13 Mar 2012 22:30:21 -0600\r
23 X-Cloudmark-SP-Filtered: true\r
24 X-Cloudmark-SP-Result: v=1.1 cv=GZn8e3lTBEeJrlGK3+GUWyR5aYe1SJcDn5uEERMe9yQ=\r
25         c=1 sm=1\r
26         a=Zfi7MXTdCt0A:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17\r
27         a=bz-zy3QpAAAA:8 a=n6QtOu-G53KnuZUlGJ0A:9 a=h7bP-iDue0dEkRzURMAA:7\r
28         a=NFWI-YhVVZsA:10 a=ahd4hhnNlpr_wjV6:21 a=RMfGCvDGy4T3Z2Ik:21\r
29         a=HpAAvcLHHh0Zw7uRqdWCyQ==:117\r
30 Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56])\r
31         by pd7ml2no-dmz.prod.shaw.ca with ESMTP; 13 Mar 2012 22:30:21 -0600\r
32 Received: by lagos.xvx.ca (Postfix, from userid 1000)\r
33         id D98AA8004EB2; Tue, 13 Mar 2012 22:30:20 -0600 (MDT)\r
34 From: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v7.1 04/11] reply: Add a JSON reply format.\r
37 Date: Tue, 13 Mar 2012 22:30:09 -0600\r
38 Message-Id: <1331699416-30775-5-git-send-email-awg+notmuch@xvx.ca>\r
39 X-Mailer: git-send-email 1.7.5.4\r
40 In-Reply-To: <1331699416-30775-1-git-send-email-awg+notmuch@xvx.ca>\r
41 References: <1331525142-30539-1-git-send-email-awg+notmuch@xvx.ca>\r
42         <1331699416-30775-1-git-send-email-awg+notmuch@xvx.ca>\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Wed, 14 Mar 2012 04:30:29 -0000\r
56 \r
57 This new JSON format for replies includes headers generated for a\r
58 reply message as well as the headers of the original message.  Using\r
59 this data, a client can intelligently create a reply. For example, the\r
60 emacs client will be able to create replies with quoted HTML parts by\r
61 parsing the HTML parts.\r
62 ---\r
63  notmuch-client.h |   14 ++++++++++----\r
64  notmuch-reply.c  |   49 +++++++++++++++++++++++++++++++++++++++++++++++++\r
65  notmuch-show.c   |   29 +++++++++++++++++++++--------\r
66  test/multipart   |    1 -\r
67  4 files changed, 80 insertions(+), 13 deletions(-)\r
68 \r
69 diff --git a/notmuch-client.h b/notmuch-client.h\r
70 index f4a62cc..270daff 100644\r
71 --- a/notmuch-client.h\r
72 +++ b/notmuch-client.h\r
73 @@ -62,13 +62,13 @@\r
74  #define STRINGIFY(s) STRINGIFY_(s)\r
75  #define STRINGIFY_(s) #s\r
76  \r
77 -struct mime_node;\r
78 +typedef struct mime_node mime_node_t;\r
79  struct notmuch_show_params;\r
80  \r
81  typedef struct notmuch_show_format {\r
82      const char *message_set_start;\r
83      void (*part) (const void *ctx,\r
84 -                 struct mime_node *node, int indent,\r
85 +                 mime_node_t *node, int indent,\r
86                   const struct notmuch_show_params *params);\r
87      const char *message_start;\r
88      void (*message) (const void *ctx,\r
89 @@ -191,6 +191,12 @@ show_message_body (notmuch_message_t *message,\r
90  notmuch_status_t\r
91  show_one_part (const char *filename, int part);\r
92  \r
93 +void\r
94 +format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first);\r
95 +\r
96 +void\r
97 +format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply);\r
98 +\r
99  char *\r
100  json_quote_chararray (const void *ctx, const char *str, const size_t len);\r
101  \r
102 @@ -288,7 +294,7 @@ debugger_is_active (void);\r
103   * parts.  Message-type parts have one child, multipart-type parts\r
104   * have multiple children, and leaf parts have zero children.\r
105   */\r
106 -typedef struct mime_node {\r
107 +struct mime_node {\r
108      /* The MIME object of this part.  This will be a GMimeMessage,\r
109       * GMimePart, GMimeMultipart, or a subclass of one of these.\r
110       *\r
111 @@ -351,7 +357,7 @@ typedef struct mime_node {\r
112       * number to assign it (or -1 if unknown). */\r
113      int next_child;\r
114      int next_part_num;\r
115 -} mime_node_t;\r
116 +};\r
117  \r
118  /* Construct a new MIME node pointing to the root message part of\r
119   * message.  If cryptoctx is non-NULL, it will be used to verify\r
120 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
121 index f1478cc..e2b6c25 100644\r
122 --- a/notmuch-reply.c\r
123 +++ b/notmuch-reply.c\r
124 @@ -604,6 +604,51 @@ notmuch_reply_format_default(void *ctx,\r
125      return 0;\r
126  }\r
127  \r
128 +static int\r
129 +notmuch_reply_format_json(void *ctx,\r
130 +                         notmuch_config_t *config,\r
131 +                         notmuch_query_t *query,\r
132 +                         notmuch_show_params_t *params,\r
133 +                         notmuch_bool_t reply_all)\r
134 +{\r
135 +    GMimeMessage *reply;\r
136 +    notmuch_messages_t *messages;\r
137 +    notmuch_message_t *message;\r
138 +    mime_node_t *node;\r
139 +\r
140 +    if (notmuch_query_count_messages (query) != 1) {\r
141 +       fprintf (stderr, "Error: search term did not match precisely one message.\n");\r
142 +       return 1;\r
143 +    }\r
144 +\r
145 +    messages = notmuch_query_search_messages (query);\r
146 +    message = notmuch_messages_get (messages);\r
147 +    if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,\r
148 +                       &node) != NOTMUCH_STATUS_SUCCESS)\r
149 +       return 1;\r
150 +\r
151 +    reply = create_reply_message (ctx, config, message, reply_all);\r
152 +    if (!reply)\r
153 +       return 1;\r
154 +\r
155 +    /* The headers of the reply message we've created */\r
156 +    printf ("{\"reply-headers\": ");\r
157 +    format_headers_json (ctx, reply, TRUE);\r
158 +    g_object_unref (G_OBJECT (reply));\r
159 +    reply = NULL;\r
160 +\r
161 +    /* Start the original */\r
162 +    printf (", \"original\": ");\r
163 +\r
164 +    format_part_json (ctx, node, TRUE);\r
165 +\r
166 +    /* End */\r
167 +    printf ("}\n");\r
168 +    notmuch_message_destroy (message);\r
169 +\r
170 +    return 0;\r
171 +}\r
172 +\r
173  /* This format is currently tuned for a git send-email --notmuch hook */\r
174  static int\r
175  notmuch_reply_format_headers_only(void *ctx,\r
176 @@ -666,6 +711,7 @@ notmuch_reply_format_headers_only(void *ctx,\r
177  \r
178  enum {\r
179      FORMAT_DEFAULT,\r
180 +    FORMAT_JSON,\r
181      FORMAT_HEADERS_ONLY,\r
182  };\r
183  \r
184 @@ -685,6 +731,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
185      notmuch_opt_desc_t options[] = {\r
186         { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',\r
187           (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },\r
188 +                                 { "json", FORMAT_JSON },\r
189                                   { "headers-only", FORMAT_HEADERS_ONLY },\r
190                                   { 0, 0 } } },\r
191         { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',\r
192 @@ -703,6 +750,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
193  \r
194      if (format == FORMAT_HEADERS_ONLY)\r
195         reply_format_func = notmuch_reply_format_headers_only;\r
196 +    else if (format == FORMAT_JSON)\r
197 +       reply_format_func = notmuch_reply_format_json;\r
198      else\r
199         reply_format_func = notmuch_reply_format_default;\r
200  \r
201 diff --git a/notmuch-show.c b/notmuch-show.c\r
202 index 05d51b2..cb3a427 100644\r
203 --- a/notmuch-show.c\r
204 +++ b/notmuch-show.c\r
205 @@ -291,8 +291,8 @@ format_headers_message_part_text (GMimeMessage *message)\r
206      printf ("Date: %s\n", g_mime_message_get_date_as_string (message));\r
207  }\r
208  \r
209 -static void\r
210 -format_headers_json (const void *ctx, GMimeMessage *message)\r
211 +void\r
212 +format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)\r
213  {\r
214      void *local = talloc_new (ctx);\r
215      InternetAddressList *recipients;\r
216 @@ -316,9 +316,22 @@ format_headers_json (const void *ctx, GMimeMessage *message)\r
217         printf (", %s: %s",\r
218                 json_quote_str (local, "Cc"),\r
219                 json_quote_str (local, recipients_string));\r
220 -    printf (", %s: %s}",\r
221 -           json_quote_str (local, "Date"),\r
222 -           json_quote_str (local, g_mime_message_get_date_as_string (message)));\r
223 +\r
224 +    if (reply) {\r
225 +       printf (", %s: %s",\r
226 +               json_quote_str (local, "In-reply-to"),\r
227 +               json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));\r
228 +\r
229 +       printf (", %s: %s",\r
230 +               json_quote_str (local, "References"),\r
231 +               json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));\r
232 +    } else {\r
233 +       printf (", %s: %s",\r
234 +               json_quote_str (local, "Date"),\r
235 +               json_quote_str (local, g_mime_message_get_date_as_string (message)));\r
236 +    }\r
237 +\r
238 +    printf ("}");\r
239  \r
240      talloc_free (local);\r
241  }\r
242 @@ -654,7 +667,7 @@ format_part_text (const void *ctx, mime_node_t *node,\r
243      printf ("\f%s}\n", part_type);\r
244  }\r
245  \r
246 -static void\r
247 +void\r
248  format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
249  {\r
250      /* Any changes to the JSON format should be reflected in the file\r
251 @@ -665,7 +678,7 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
252         format_message_json (ctx, node->envelope_file);\r
253  \r
254         printf ("\"headers\": ");\r
255 -       format_headers_json (ctx, GMIME_MESSAGE (node->part));\r
256 +       format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);\r
257  \r
258         printf (", \"body\": [");\r
259         format_part_json (ctx, mime_node_child (node, 0), first);\r
260 @@ -739,7 +752,7 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
261      } else if (GMIME_IS_MESSAGE (node->part)) {\r
262         printf (", \"content\": [{");\r
263         printf ("\"headers\": ");\r
264 -       format_headers_json (local, GMIME_MESSAGE (node->part));\r
265 +       format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);\r
266  \r
267         printf (", \"body\": [");\r
268         terminator = "]}]";\r
269 diff --git a/test/multipart b/test/multipart\r
270 index 80d6e88..2383b9c 100755\r
271 --- a/test/multipart\r
272 +++ b/test/multipart\r
273 @@ -590,7 +590,6 @@ EOF\r
274  test_expect_equal_file OUTPUT EXPECTED\r
275  \r
276  test_begin_subtest "'notmuch reply' to a multipart message with json format"\r
277 -test_subtest_known_broken\r
278  notmuch reply --format=json 'id:87liy5ap00.fsf@yoom.home.cworth.org' | notmuch_json_show_sanitize >OUTPUT\r
279  cat <<EOF >EXPECTED\r
280  {"reply-headers": {"Subject": "Re: Multipart message",\r
281 -- \r
282 1.7.5.4\r
283 \r