CLI: print extra headers in structured output

This is based on a patch from Johan Parin [1], which is in turn
responding to a bug report / feature requiest from Jan Malkhovski.

The update to the structured output documented in schemata is intended
to be upward compatible, so the format version stays the same

[1]: id:20191116162723.18343-1-johan.parin@gmail.com
[2]: id:87h8sdemnr.fsf@oxij.org
This commit is contained in:
David Bremner 2022-01-01 08:01:36 -04:00
parent 79936ac93e
commit c5cf92aa35
4 changed files with 85 additions and 1 deletions

View file

@ -145,9 +145,11 @@ headers = {
Cc?: string, Cc?: string,
Bcc?: string, Bcc?: string,
Reply-To?: string, Reply-To?: string,
Date: string Date: string,
extra_header_pair*
} }
extra_header_pair= (header_name: string)
# Encryption status (format_part_sprinter) # Encryption status (format_part_sprinter)
encstatus = [{status: "good"|"bad"}] encstatus = [{status: "good"|"bad"}]

View file

@ -209,6 +209,30 @@ _is_from_line (const char *line)
return 0; return 0;
} }
/* Output extra headers if configured with the `show.extra_headers'
* configuration option
*/
static void
format_extra_headers_sprinter (sprinter_t *sp, GMimeMessage *message)
{
GMimeHeaderList *header_list = g_mime_object_get_header_list (GMIME_OBJECT (message));
for (notmuch_config_values_t *extra_headers = notmuch_config_get_values (
sp->notmuch, NOTMUCH_CONFIG_EXTRA_HEADERS);
notmuch_config_values_valid (extra_headers);
notmuch_config_values_move_to_next (extra_headers)) {
GMimeHeader *header;
const char *header_name = notmuch_config_values_get (extra_headers);
header = g_mime_header_list_get_header (header_list, header_name);
if (header == NULL)
continue;
sp->map_key (sp, g_mime_header_get_name (header));
sp->string (sp, g_mime_header_get_value (header));
}
}
void void
format_headers_sprinter (sprinter_t *sp, GMimeMessage *message, format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
bool reply, const _notmuch_message_crypto_t *msg_crypto) bool reply, const _notmuch_message_crypto_t *msg_crypto)
@ -269,6 +293,8 @@ format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
sp->string (sp, g_mime_message_get_date_string (sp, message)); sp->string (sp, g_mime_message_get_date_string (sp, message));
} }
/* Output extra headers the user has configured, if any */
format_extra_headers_sprinter (sp, message);
sp->end (sp); sp->end (sp);
talloc_free (local); talloc_free (local);
} }

View file

@ -156,4 +156,46 @@ EOF
output=$(notmuch show --format=json --body=false --format-version=2 id:message-id@example.com) output=$(notmuch show --format=json --body=false --format-version=2 id:message-id@example.com)
test_expect_equal_json "$output" "$(cat EXPECTED)" test_expect_equal_json "$output" "$(cat EXPECTED)"
test_begin_subtest "show extra headers"
add_message "[subject]=\"extra-headers\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[in-reply-to]=\"<parent@notmuch-test-suite>\"" "[body]=\"extra-headers test\""\
"[header]=\"Received: from mail.example.com (mail.example.com [1.1.1.1])
by mail.notmuchmail.org (some MTA) with ESMTP id 12345678
for <test_suite_other@notmuchmail.org>; Sat, 10 Apr 2010 07:54:51 -0400 (EDT)\"" \
notmuch config set show.extra_headers "in-reply-to;received"
output=$(notmuch show --format=json --body=false id:${gen_msg_id} | notmuch_json_show_sanitize)
cat <<EOF > EXPECTED
[
[
[
{
"crypto": {},
"date_relative": "2000-01-01",
"excluded": false,
"filename": [
"YYYYY"
],
"headers": {
"Date": "Sat, 01 Jan 2000 12:00:00 +0000",
"From": "Notmuch Test Suite <test_suite@notmuchmail.org>",
"In-Reply-To": "<parent@notmuch-test-suite>",
"Received": "from mail.example.com (mail.example.com [1.1.1.1])\tby mail.notmuchmail.org (some MTA) with ESMTP id 12345678\tfor <test_suite_other@notmuchmail.org>; Sat, 10 Apr 2010 07:54:51 -0400 (EDT)",
"Subject": "extra-headers",
"To": "Notmuch Test Suite <test_suite@notmuchmail.org>"
},
"id": "XXXXX",
"match": true,
"tags": [
"inbox",
"unread"
],
"timestamp": 946728000
},
[]
]
]
]
EOF
test_expect_equal_json "${output}" "$(cat EXPECTED)"
test_done test_done

View file

@ -47,4 +47,18 @@ filename=$(notmuch search --output=files "id:$id")
attachment_length=$(( $(base64 $NOTMUCH_SRCDIR/test/README | wc -c) - 1 )) attachment_length=$(( $(base64 $NOTMUCH_SRCDIR/test/README | wc -c) - 1 ))
test_expect_equal "$output" "((((:id \"$id\" :match t :excluded nil :filename (\"$filename\") :timestamp 946728000 :date_relative \"2000-01-01\" :tags (\"inbox\") :body ((:id 1 :content-type \"multipart/mixed\" :content ((:id 2 :content-type \"text/plain\" :content \"This is a test message with inline attachment with a filename\") (:id 3 :content-type \"application/octet-stream\" :content-disposition \"inline\" :filename \"README\" :content-transfer-encoding \"base64\" :content-length $attachment_length)))) :crypto () :headers (:Subject \"sexp-show-inline-attachment-filename\" :From \"Notmuch Test Suite <test_suite@notmuchmail.org>\" :To \"test_suite@notmuchmail.org\" :Date \"Sat, 01 Jan 2000 12:00:00 +0000\")) ())))" test_expect_equal "$output" "((((:id \"$id\" :match t :excluded nil :filename (\"$filename\") :timestamp 946728000 :date_relative \"2000-01-01\" :tags (\"inbox\") :body ((:id 1 :content-type \"multipart/mixed\" :content ((:id 2 :content-type \"text/plain\" :content \"This is a test message with inline attachment with a filename\") (:id 3 :content-type \"application/octet-stream\" :content-disposition \"inline\" :filename \"README\" :content-transfer-encoding \"base64\" :content-length $attachment_length)))) :crypto () :headers (:Subject \"sexp-show-inline-attachment-filename\" :From \"Notmuch Test Suite <test_suite@notmuchmail.org>\" :To \"test_suite@notmuchmail.org\" :Date \"Sat, 01 Jan 2000 12:00:00 +0000\")) ())))"
test_begin_subtest "show extra headers"
add_message "[subject]=\"extra-headers\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[in-reply-to]=\"<parent@notmuch-test-suite>\"" "[body]=\"extra-headers test\""\
"[header]=\"Received: from mail.example.com (mail.example.com [1.1.1.1])
by mail.notmuchmail.org (some MTA) with ESMTP id 12345678
for <test_suite_other@notmuchmail.org>; Sat, 10 Apr 2010 07:54:51 -0400 (EDT)\"" \
notmuch config set show.extra_headers "in-reply-to;received"
notmuch show --format=sexp --body=false id:${gen_msg_id} | \
notmuch_dir_sanitize | sed 's/msg-[0-9]*/MSG/g'> OUTPUT
cat <<EOF > EXPECTED
((((:id "MSG@notmuch-test-suite" :match t :excluded nil :filename ("MAIL_DIR/MSG") :timestamp 946728000 :date_relative "2000-01-01" :tags ("inbox" "unread") :crypto () :headers (:Subject "extra-headers" :From "Notmuch Test Suite <test_suite@notmuchmail.org>" :To "Notmuch Test Suite <test_suite@notmuchmail.org>" :Date "Sat, 01 Jan 2000 12:00:00 +0000" :In-Reply-To "<parent@notmuch-test-suite>" :Received "from mail.example.com (mail.example.com [1.1.1.1])\011by mail.notmuchmail.org (some MTA) with ESMTP id 12345678\011for <test_suite_other@notmuchmail.org>; Sat, 10 Apr 2010 07:54:51 -0400 (EDT)")) ())))
EOF
test_expect_equal_file EXPECTED OUTPUT
test_done test_done