Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 14C42431FBF for ; Sat, 7 Jul 2012 08:13:22 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0.201 X-Spam-Level: X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id FCPujwf8XFS6 for ; Sat, 7 Jul 2012 08:13:19 -0700 (PDT) Received: from mail-wi0-f173.google.com (mail-wi0-f173.google.com [209.85.212.173]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 6BB42431FC7 for ; Sat, 7 Jul 2012 08:13:16 -0700 (PDT) Received: by wibhm6 with SMTP id hm6so1274327wib.2 for ; Sat, 07 Jul 2012 08:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=UcaJpKhKMa4xVgDc/o+ti+Bw3tHVMSXEtHTiCQAf1Gw=; b=zjtTzxTBykr33/U5ghILLwISbwGZyccZ1gPmknLk32BwnX5INC4ON/ZUyBYI/0qQyF mqVrKbcvsJe0xQdlna83klcBPjl4njnxRwNGgx7V+DTla2/P6XKRUBkwh2OtIjQNrF79 p7c0nn3mOd58XQM5sKdeS7LvSb/NGLY5AgyIf+Lo5eIxAp2YySapEm5KtbeTtoxkAfRm OnfTFnTJqyzYS7a3bw6Rk01R6qI/+yRmpLI54jvJzF6IxoKDdFvBnhmhkuAC3qyF5iEY sCcMJX/lwo0h8i6bcfMCPpioUo5tjWs/A3/Vd1qaK5+mqau3ZQpPi4mE6KfAFfFYrTMd fJYA== Received: by 10.180.78.35 with SMTP id y3mr15901560wiw.16.1341673993770; Sat, 07 Jul 2012 08:13:13 -0700 (PDT) Received: from localhost (94-192-233-223.zone6.bethere.co.uk. [94.192.233.223]) by mx.google.com with ESMTPS id el6sm12172162wib.8.2012.07.07.08.13.11 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 07 Jul 2012 08:13:12 -0700 (PDT) From: Mark Walters To: notmuch@notmuchmail.org Subject: [PATCH 2/3] cli: show allow the caller to specify the headers output. Date: Sat, 7 Jul 2012 16:12:57 +0100 Message-Id: <1341673978-6094-3-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1341673978-6094-1-git-send-email-markwalters1009@gmail.com> References: <1341673978-6094-1-git-send-email-markwalters1009@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 07 Jul 2012 15:13:22 -0000 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