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