[PATCH 2/3] cli: show allow the caller to specify the headers output.
Mark Walters
markwalters1009 at gmail.com
Sat Jul 7 08:12:57 PDT 2012
This uses the new parsing functionality to allow the caller of notmuch
show to specify which headers they want. Callers of format_part_json
should pass a bitfield specifiying the headers they want.
Since the functionality includes that contained in the previous
"reply" boolean passed notmuch-reply.c is converted to use this new
style.
---
notmuch-client.h | 24 +++++++++++++-
notmuch-reply.c | 12 ++++++-
notmuch-show.c | 90 ++++++++++++++++++++++++++++++++++++-----------------
3 files changed, 93 insertions(+), 33 deletions(-)
diff --git a/notmuch-client.h b/notmuch-client.h
index 0c17b79..c241a7d 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -68,6 +68,24 @@ typedef GMimeCipherContext notmuch_crypto_context_t;
typedef struct mime_node mime_node_t;
struct notmuch_show_params;
+
+typedef enum {
+ NOTMUCH_SHOW_OUTPUT_HDR_DATE = 0x1,
+ NOTMUCH_SHOW_OUTPUT_HDR_SUBJECT = 0x2,
+ NOTMUCH_SHOW_OUTPUT_HDR_FROM = 0x4,
+ NOTMUCH_SHOW_OUTPUT_HDR_TO = 0x8,
+ NOTMUCH_SHOW_OUTPUT_HDR_CC = 0x10,
+ NOTMUCH_SHOW_OUTPUT_HDR_REPLY_TO = 0x20,
+ NOTMUCH_SHOW_OUTPUT_HDR_IN_REPLY_TO = 0x40,
+ NOTMUCH_SHOW_OUTPUT_HDR_REFERENCES = 0x80,
+ NOTMUCH_SHOW_OUTPUT_DEFAULT =
+ NOTMUCH_SHOW_OUTPUT_HDR_SUBJECT |
+ NOTMUCH_SHOW_OUTPUT_HDR_FROM |
+ NOTMUCH_SHOW_OUTPUT_HDR_TO |
+ NOTMUCH_SHOW_OUTPUT_HDR_CC |
+ NOTMUCH_SHOW_OUTPUT_HDR_DATE
+} notmuch_show_output_t;
+
typedef struct notmuch_show_format {
const char *message_set_start;
notmuch_status_t (*part) (const void *ctx,
@@ -90,6 +108,7 @@ typedef struct notmuch_show_params {
notmuch_bool_t raw;
int part;
notmuch_crypto_t crypto;
+ notmuch_show_output_t output;
} notmuch_show_params_t;
/* There's no point in continuing when we've detected that we've done
@@ -176,10 +195,11 @@ notmuch_status_t
show_one_part (const char *filename, int part);
void
-format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first);
+format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first,
+ notmuch_show_output_t output);
void
-format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply);
+format_headers_json (const void *ctx, GMimeMessage *message, notmuch_show_output_t output);
typedef enum {
NOTMUCH_SHOW_TEXT_PART_REPLY = 1 << 0,
diff --git a/notmuch-reply.c b/notmuch-reply.c
index 3a038ed..f4bf70d 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -611,16 +611,24 @@ notmuch_reply_format_json(void *ctx,
if (!reply)
return 1;
+ notmuch_show_output_t NOTMUCH_REPLY_OUTPUT =
+ NOTMUCH_SHOW_OUTPUT_HDR_SUBJECT |
+ NOTMUCH_SHOW_OUTPUT_HDR_FROM |
+ NOTMUCH_SHOW_OUTPUT_HDR_TO |
+ NOTMUCH_SHOW_OUTPUT_HDR_CC |
+ NOTMUCH_SHOW_OUTPUT_HDR_IN_REPLY_TO |
+ NOTMUCH_SHOW_OUTPUT_HDR_REFERENCES;
+
/* The headers of the reply message we've created */
printf ("{\"reply-headers\": ");
- format_headers_json (ctx, reply, TRUE);
+ format_headers_json (ctx, reply, NOTMUCH_REPLY_OUTPUT);
g_object_unref (G_OBJECT (reply));
reply = NULL;
/* Start the original */
printf (", \"original\": ");
- format_part_json (ctx, node, TRUE);
+ format_part_json (ctx, node, TRUE, NOTMUCH_SHOW_OUTPUT_DEFAULT);
/* End */
printf ("}\n");
diff --git a/notmuch-show.c b/notmuch-show.c
index 8f3c60e..242e8e0 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -194,44 +194,58 @@ _is_from_line (const char *line)
}
void
-format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)
+format_headers_json (const void *ctx, GMimeMessage *message, notmuch_show_output_t output)
{
void *local = talloc_new (ctx);
InternetAddressList *recipients;
const char *recipients_string;
+ const char *reply_to_string;
- printf ("{%s: %s",
- json_quote_str (local, "Subject"),
- json_quote_str (local, g_mime_message_get_subject (message)));
- printf (", %s: %s",
- json_quote_str (local, "From"),
- json_quote_str (local, g_mime_message_get_sender (message)));
- recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
- recipients_string = internet_address_list_to_string (recipients, 0);
- if (recipients_string)
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_SUBJECT)
+ printf ("{%s: %s",
+ json_quote_str (local, "Subject"),
+ json_quote_str (local, g_mime_message_get_subject (message)));
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_FROM)
printf (", %s: %s",
- json_quote_str (local, "To"),
- json_quote_str (local, recipients_string));
- recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
- recipients_string = internet_address_list_to_string (recipients, 0);
- if (recipients_string)
- printf (", %s: %s",
- json_quote_str (local, "Cc"),
- json_quote_str (local, recipients_string));
-
- if (reply) {
+ json_quote_str (local, "From"),
+ json_quote_str (local, g_mime_message_get_sender (message)));
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_TO) {
+ recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
+ recipients_string = internet_address_list_to_string (recipients, 0);
+ if (recipients_string)
+ printf (", %s: %s",
+ json_quote_str (local, "To"),
+ json_quote_str (local, recipients_string));
+ }
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_CC) {
+ recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
+ recipients_string = internet_address_list_to_string (recipients, 0);
+ if (recipients_string)
+ printf (", %s: %s",
+ json_quote_str (local, "Cc"),
+ json_quote_str (local, recipients_string));
+ }
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_REPLY_TO) {
+ reply_to_string = g_mime_message_get_reply_to (message);
+ if (reply_to_string)
+ printf (", %s: %s",
+ json_quote_str (local, "Reply-To"),
+ json_quote_str (local, reply_to_string));
+ }
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_IN_REPLY_TO)
printf (", %s: %s",
json_quote_str (local, "In-reply-to"),
json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_REFERENCES)
printf (", %s: %s",
json_quote_str (local, "References"),
json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));
- } else {
+
+ if (output & NOTMUCH_SHOW_OUTPUT_HDR_DATE)
printf (", %s: %s",
json_quote_str (local, "Date"),
json_quote_str (local, g_mime_message_get_date_as_string (message)));
- }
printf ("}");
@@ -559,7 +573,7 @@ format_part_text (const void *ctx, mime_node_t *node,
}
void
-format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
+format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first, notmuch_show_output_t output)
{
/* Any changes to the JSON format should be reflected in the file
* devel/schemata. */
@@ -569,10 +583,10 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
format_message_json (ctx, node->envelope_file);
printf ("\"headers\": ");
- format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);
+ format_headers_json (ctx, GMIME_MESSAGE (node->part), output);
printf (", \"body\": [");
- format_part_json (ctx, mime_node_child (node, 0), first);
+ format_part_json (ctx, mime_node_child (node, 0), first, output);
printf ("]}");
return;
@@ -643,7 +657,7 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
} else if (GMIME_IS_MESSAGE (node->part)) {
printf (", \"content\": [{");
printf ("\"headers\": ");
- format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);
+ format_headers_json (local, GMIME_MESSAGE (node->part), output);
printf (", \"body\": [");
terminator = "]}]";
@@ -652,16 +666,16 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
talloc_free (local);
for (i = 0; i < node->nchildren; i++)
- format_part_json (ctx, mime_node_child (node, i), i == 0);
+ format_part_json (ctx, mime_node_child (node, i), i == 0, output);
printf ("%s}", terminator);
}
static notmuch_status_t
format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
- unused (const notmuch_show_params_t *params))
+ const notmuch_show_params_t *params)
{
- format_part_json (ctx, node, TRUE);
+ format_part_json (ctx, node, TRUE, params->output);
return NOTMUCH_STATUS_SUCCESS;
}
@@ -1020,6 +1034,17 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
{ "mbox", NOTMUCH_FORMAT_MBOX },
{ "raw", NOTMUCH_FORMAT_RAW },
{ 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD_LIST, ¶ms.output, "output", 'o',
+ (notmuch_keyword_t []){ { "default", NOTMUCH_SHOW_OUTPUT_DEFAULT },
+ { "date", NOTMUCH_SHOW_OUTPUT_HDR_DATE },
+ { "subject", NOTMUCH_SHOW_OUTPUT_HDR_SUBJECT },
+ { "from", NOTMUCH_SHOW_OUTPUT_HDR_FROM },
+ { "to", NOTMUCH_SHOW_OUTPUT_HDR_TO },
+ { "cc", NOTMUCH_SHOW_OUTPUT_HDR_CC },
+ { "reply-to", NOTMUCH_SHOW_OUTPUT_HDR_REPLY_TO },
+ { "in-reply-to", NOTMUCH_SHOW_OUTPUT_HDR_IN_REPLY_TO },
+ { "references", NOTMUCH_SHOW_OUTPUT_HDR_REFERENCES },
+ { 0, 0 } } },
{ NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
(notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
{ "false", EXCLUDE_FALSE },
@@ -1053,6 +1078,13 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
format_sel = NOTMUCH_FORMAT_TEXT;
}
+ if (params.output) {
+ if (format_sel != NOTMUCH_FORMAT_JSON)
+ fprintf (stderr, "Error: specifying output is only implemented for json format.\n");
+ } else {
+ params.output = NOTMUCH_SHOW_OUTPUT_DEFAULT;
+ }
+
switch (format_sel) {
case NOTMUCH_FORMAT_JSON:
format = &format_json;
--
1.7.9.1
More information about the notmuch
mailing list