static void format_thread_json (const void *ctx, const char *thread_id, const time_t date, const int matched, const int total, const char *authors, const char *subject) { void *ctx_quote = talloc_new (ctx); printf ("\"thread\": %s,\n" "\"timestamp\": %ld,\n" "\"matched\": %d,\n" "\"total\": %d,\n" "\"authors\": %s,\n" "\"subject\": %s,\n", json_quote_str (ctx_quote, thread_id), date, matched, total, json_quote_str (ctx_quote, authors), json_quote_str (ctx_quote, subject)); talloc_free (ctx_quote); }
static void format_message_json (const void *ctx, notmuch_message_t *message) { notmuch_tags_t *tags; int first = 1; void *ctx_quote = talloc_new (ctx); time_t date; const char *relative_date; date = notmuch_message_get_date (message); relative_date = notmuch_time_relative_date (ctx, date); printf ("\"id\": %s, \"match\": %s, \"excluded\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [", json_quote_str (ctx_quote, notmuch_message_get_message_id (message)), notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false", notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? "true" : "false", json_quote_str (ctx_quote, notmuch_message_get_filename (message)), date, relative_date); for (tags = notmuch_message_get_tags (message); notmuch_tags_valid (tags); notmuch_tags_move_to_next (tags)) { printf("%s%s", first ? "" : ",", json_quote_str (ctx_quote, notmuch_tags_get (tags))); first = 0; } printf("], "); talloc_free (ctx_quote); }
static void format_part_sigstatus_json (const GMimeSignatureValidity* validity) { printf (", \"sigstatus\": ["); if (!validity) { printf ("]"); return; } const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity); int first = 1; void *ctx_quote = talloc_new (NULL); while (signer) { if (first) first = 0; else printf (", "); printf ("{"); /* status */ printf ("\"status\": %s", json_quote_str (ctx_quote, signer_status_to_string (signer->status))); if (signer->status == GMIME_SIGNER_STATUS_GOOD) { if (signer->fingerprint) printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint)); /* these dates are seconds since the epoch; should we * provide a more human-readable format string? */ if (signer->created) printf (", \"created\": %d", (int) signer->created); if (signer->expires) printf (", \"expires\": %d", (int) signer->expires); /* output user id only if validity is FULL or ULTIMATE. */ /* note that gmime is using the term "trust" here, which * is WRONG. It's actually user id "validity". */ if ((signer->name) && (signer->trust)) { if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE)) printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name)); } } else { if (signer->keyid) printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid)); } if (signer->errors != GMIME_SIGNER_ERROR_NONE) { printf (", \"errors\": %x", signer->errors); } printf ("}"); signer = signer->next; } printf ("]"); talloc_free (ctx_quote); }
static void format_headers_message_part_json (GMimeMessage *message) { void *ctx = talloc_new (NULL); void *ctx_quote = talloc_new (ctx); InternetAddressList *recipients; const char *recipients_string; printf ("%s: %s", json_quote_str (ctx_quote, "From"), json_quote_str (ctx_quote, 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) printf (", %s: %s", json_quote_str (ctx_quote, "To"), json_quote_str (ctx_quote, 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 (ctx_quote, "Cc"), json_quote_str (ctx_quote, recipients_string)); printf (", %s: %s", json_quote_str (ctx_quote, "Subject"), json_quote_str (ctx_quote, g_mime_message_get_subject (message))); printf (", %s: %s", json_quote_str (ctx_quote, "Date"), json_quote_str (ctx_quote, g_mime_message_get_date_as_string (message))); talloc_free (ctx_quote); talloc_free (ctx); }
static void format_headers_json (const void *ctx, notmuch_message_t *message) { const char *headers[] = { "Subject", "From", "To", "Cc", "Bcc", "Date" }; const char *name, *value; unsigned int i; int first_header = 1; void *ctx_quote = talloc_new (ctx); for (i = 0; i < ARRAY_SIZE (headers); i++) { name = headers[i]; value = notmuch_message_get_header (message, name); if (value) { if (!first_header) fputs (", ", stdout); first_header = 0; printf ("%s: %s", json_quote_str (ctx_quote, name), json_quote_str (ctx_quote, value)); } } talloc_free (ctx_quote); }
static void format_part_content_json (GMimeObject *part) { GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); GMimeStream *stream_memory = g_mime_stream_mem_new (); const char *cid = g_mime_object_get_content_id (part); void *ctx = talloc_new (NULL); GByteArray *part_content; printf (", \"content-type\": %s", json_quote_str (ctx, g_mime_content_type_to_string (content_type))); if (cid != NULL) printf(", \"content-id\": %s", json_quote_str (ctx, cid)); if (GMIME_IS_PART (part)) { const char *filename = g_mime_part_get_filename (GMIME_PART (part)); if (filename) printf (", \"filename\": %s", json_quote_str (ctx, filename)); } if (g_mime_content_type_is_type (content_type, "text", "*") && !g_mime_content_type_is_type (content_type, "text", "html")) { show_text_part_content (part, stream_memory); part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory)); printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len)); } else if (g_mime_content_type_is_type (content_type, "multipart", "*")) { printf (", \"content\": ["); } else if (g_mime_content_type_is_type (content_type, "message", "rfc822")) { printf (", \"content\": [{"); } talloc_free (ctx); if (stream_memory) g_object_unref (stream_memory); }
static void format_item_id_json (const void *ctx, unused (const char *item_type), const char *item_id) { void *ctx_quote = talloc_new (ctx); printf ("%s", json_quote_str (ctx_quote, item_id)); talloc_free (ctx_quote); }
void format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first) { /* Any changes to the JSON format should be reflected in the file * devel/schemata. */ if (node->envelope_file) { printf ("{"); format_message_json (ctx, node->envelope_file); printf ("\"headers\": "); format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE); printf (", \"body\": ["); format_part_json (ctx, mime_node_child (node, 0), first); printf ("]}"); return; } void *local = talloc_new (ctx); /* The disposition and content-type metadata are associated with * the envelope for message parts */ GMimeObject *meta = node->envelope_part ? GMIME_OBJECT (node->envelope_part) : node->part; GMimeContentType *content_type = g_mime_object_get_content_type (meta); const char *cid = g_mime_object_get_content_id (meta); const char *filename = GMIME_IS_PART (node->part) ? g_mime_part_get_filename (GMIME_PART (node->part)) : NULL; const char *terminator = ""; int i; if (!first) printf (", "); printf ("{\"id\": %d", node->part_num); if (node->decrypt_attempted) printf (", \"encstatus\": [{\"status\": \"%s\"}]", node->decrypt_success ? "good" : "bad"); if (node->verify_attempted) { printf (", \"sigstatus\": "); format_part_sigstatus_json (node); } printf (", \"content-type\": %s", json_quote_str (local, g_mime_content_type_to_string (content_type))); if (cid) printf (", \"content-id\": %s", json_quote_str (local, cid)); if (filename) printf (", \"filename\": %s", json_quote_str (local, filename)); if (GMIME_IS_PART (node->part)) { /* For non-HTML text parts, we include the content in the * JSON. Since JSON must be Unicode, we handle charset * decoding here and do not report a charset to the caller. * For text/html parts, we do not include the content. If a * caller is interested in text/html parts, it should retrieve * them separately and they will not be decoded. Since this * makes charset decoding the responsibility on the caller, we * report the charset for text/html parts. */ if (g_mime_content_type_is_type (content_type, "text", "html")) { const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset"); if (content_charset != NULL) printf (", \"content-charset\": %s", json_quote_str (local, content_charset)); } else if (g_mime_content_type_is_type (content_type, "text", "*")) { GMimeStream *stream_memory = g_mime_stream_mem_new (); GByteArray *part_content; show_text_part_content (node->part, stream_memory, 0); part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory)); printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len)); g_object_unref (stream_memory); } } else if (GMIME_IS_MULTIPART (node->part)) { printf (", \"content\": ["); terminator = "]"; } else if (GMIME_IS_MESSAGE (node->part)) { printf (", \"content\": [{"); printf ("\"headers\": "); format_headers_json (local, GMIME_MESSAGE (node->part), FALSE); printf (", \"body\": ["); terminator = "]}]"; } talloc_free (local); for (i = 0; i < node->nchildren; i++) format_part_json (ctx, mime_node_child (node, i), i == 0); printf ("%s}", terminator); }
static void format_part_sigstatus_json (mime_node_t *node) { GMimeSignatureList *siglist = node->sig_list; printf ("["); if (!siglist) { printf ("]"); return; } void *ctx_quote = talloc_new (NULL); int i; for (i = 0; i < g_mime_signature_list_length (siglist); i++) { GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i); if (i > 0) printf (", "); printf ("{"); /* status */ GMimeSignatureStatus status = g_mime_signature_get_status (signature); printf ("\"status\": %s", json_quote_str (ctx_quote, signature_status_to_string (status))); GMimeCertificate *certificate = g_mime_signature_get_certificate (signature); if (status == GMIME_SIGNATURE_STATUS_GOOD) { if (certificate) printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate))); /* these dates are seconds since the epoch; should we * provide a more human-readable format string? */ time_t created = g_mime_signature_get_created (signature); if (created != -1) printf (", \"created\": %d", (int) created); time_t expires = g_mime_signature_get_expires (signature); if (expires > 0) printf (", \"expires\": %d", (int) expires); /* output user id only if validity is FULL or ULTIMATE. */ /* note that gmime is using the term "trust" here, which * is WRONG. It's actually user id "validity". */ if (certificate) { const char *name = g_mime_certificate_get_name (certificate); GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate); if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)) printf (", \"userid\": %s", json_quote_str (ctx_quote, name)); } } else if (certificate) { const char *key_id = g_mime_certificate_get_key_id (certificate); if (key_id) printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id)); } GMimeSignatureError errors = g_mime_signature_get_errors (signature); if (errors != GMIME_SIGNATURE_ERROR_NONE) { printf (", \"errors\": %d", errors); } printf ("}"); } printf ("]"); talloc_free (ctx_quote); }
void format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply) { void *local = talloc_new (ctx); InternetAddressList *recipients; const char *recipients_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) 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) { 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"))); printf (", %s: %s", json_quote_str (local, "References"), json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References"))); } else { printf (", %s: %s", json_quote_str (local, "Date"), json_quote_str (local, g_mime_message_get_date_as_string (message))); } printf ("}"); talloc_free (local); }