+ {
+ 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_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);
+}
+
+/* Write a MIME text part out to the given stream.
+ *
+ * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
+ * UTF-8) will be performed, so it is inappropriate to call this
+ * function with a non-text part. Doing so will trigger an internal
+ * error.
+ */
+static void
+show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
+{
+ GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
+ GMimeStream *stream_filter = NULL;
+ GMimeDataWrapper *wrapper;
+ const char *charset;
+
+ if (! g_mime_content_type_is_type (content_type, "text", "*"))
+ INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
+ g_mime_content_type_to_string (content_type));
+
+ if (stream_out == NULL)
+ return;
+
+ stream_filter = g_mime_stream_filter_new (stream_out);
+ g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
+ g_mime_filter_crlf_new (FALSE, FALSE));
+
+ charset = g_mime_object_get_content_type_parameter (part, "charset");
+ if (charset) {
+ GMimeFilter *charset_filter;
+ charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
+ /* This result can be NULL for things like "unknown-8bit".
+ * Don't set a NULL filter as that makes GMime print
+ * annoying assertion-failure messages on stderr. */
+ if (charset_filter) {
+ g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
+ charset_filter);
+ g_object_unref (charset_filter);
+ }
+
+ }
+
+ wrapper = g_mime_part_get_content_object (GMIME_PART (part));
+ if (wrapper && stream_filter)
+ g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
+ if (stream_filter)
+ g_object_unref(stream_filter);
+}
+
+#ifdef GMIME_ATLEAST_26
+static const char*
+signature_status_to_string (GMimeSignatureStatus x)
+{
+ switch (x) {
+ case GMIME_SIGNATURE_STATUS_GOOD:
+ return "good";
+ case GMIME_SIGNATURE_STATUS_BAD:
+ return "bad";
+ case GMIME_SIGNATURE_STATUS_ERROR:
+ return "error";
+ }
+ return "unknown";
+}
+#else
+static const char*
+signer_status_to_string (GMimeSignerStatus x)
+{
+ switch (x) {
+ case GMIME_SIGNER_STATUS_NONE:
+ return "none";
+ case GMIME_SIGNER_STATUS_GOOD:
+ return "good";
+ case GMIME_SIGNER_STATUS_BAD:
+ return "bad";
+ case GMIME_SIGNER_STATUS_ERROR:
+ return "error";
+ }
+ return "unknown";
+}
+#endif
+
+static void
+format_part_start_json (unused (GMimeObject *part), int *part_count)
+{
+ printf ("{\"id\": %d", *part_count);
+}
+
+static void
+format_part_encstatus_json (int status)
+{
+ printf (", \"encstatus\": [{\"status\": ");
+ if (status) {
+ printf ("\"good\"");
+ } else {
+ printf ("\"bad\"");
+ }
+ printf ("}]");
+}
+
+#ifdef GMIME_ATLEAST_26
+static void
+format_part_sigstatus_json (GMimeSignatureList *siglist)
+{
+ printf (", \"sigstatus\": [");
+
+ 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);
+}
+#else
+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\": %d", signer->errors);
+ }
+
+ printf ("}");
+ signer = signer->next;
+ }
+
+ printf ("]");
+
+ talloc_free (ctx_quote);
+}
+#endif
+
+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));