+ printf ("id:%s depth:%d match:%d filename:%s\n",
+ notmuch_message_get_message_id (message),
+ indent,
+ notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
+ notmuch_message_get_filename (message));
+}
+
+static void
+format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
+{
+ 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, \"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",
+ 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);
+}
+
+/* Extract just the email address from the contents of a From:
+ * header. */
+static const char *
+_extract_email_address (const void *ctx, const char *from)
+{
+ InternetAddressList *addresses;
+ InternetAddress *address;
+ InternetAddressMailbox *mailbox;
+ const char *email = "MAILER-DAEMON";
+
+ addresses = internet_address_list_parse_string (from);
+
+ /* Bail if there is no address here. */
+ if (addresses == NULL || internet_address_list_length (addresses) < 1)
+ goto DONE;
+
+ /* Otherwise, just use the first address. */
+ address = internet_address_list_get_address (addresses, 0);
+
+ /* The From header should never contain an address group rather
+ * than a mailbox. So bail if it does. */
+ if (! INTERNET_ADDRESS_IS_MAILBOX (address))
+ goto DONE;
+
+ mailbox = INTERNET_ADDRESS_MAILBOX (address);
+ email = internet_address_mailbox_get_addr (mailbox);
+ email = talloc_strdup (ctx, email);
+
+ DONE:
+ /* XXX: How to free addresses here? */
+ return email;
+ }
+
+/* Return 1 if 'line' is an mbox From_ line---that is, a line
+ * beginning with zero or more '>' characters followed by the
+ * characters 'F', 'r', 'o', 'm', and space.
+ *
+ * Any characters at all may appear after that in the line.
+ */
+static int
+_is_from_line (const char *line)
+{
+ const char *s = line;
+
+ if (line == NULL)
+ return 0;
+
+ while (*s == '>')
+ s++;
+
+ if (STRNCMP_LITERAL (s, "From ") == 0)
+ return 1;
+ else
+ return 0;
+}
+
+/* Print a message in "mboxrd" format as documented, for example,
+ * here:
+ *
+ * http://qmail.org/qmail-manual-html/man5/mbox.html
+ */
+static void
+format_message_mbox (const void *ctx,
+ notmuch_message_t *message,
+ unused (int indent))
+{
+ const char *filename;
+ FILE *file;
+ const char *from;
+
+ time_t date;
+ struct tm date_gmtime;
+ char date_asctime[26];
+
+ char *line = NULL;
+ size_t line_size;
+ ssize_t line_len;
+
+ filename = notmuch_message_get_filename (message);
+ file = fopen (filename, "r");
+ if (file == NULL) {
+ fprintf (stderr, "Failed to open %s: %s\n",
+ filename, strerror (errno));
+ return;
+ }
+
+ from = notmuch_message_get_header (message, "from");
+ from = _extract_email_address (ctx, from);
+
+ date = notmuch_message_get_date (message);
+ gmtime_r (&date, &date_gmtime);
+ asctime_r (&date_gmtime, date_asctime);
+
+ printf ("From %s %s", from, date_asctime);
+
+ while ((line_len = getline (&line, &line_size, file)) != -1 ) {
+ if (_is_from_line (line))
+ putchar ('>');
+ printf ("%s", line);
+ }
+
+ printf ("\n");
+
+ fclose (file);
+}
+
+
+static void
+format_headers_text (const void *ctx, notmuch_message_t *message)
+{
+ const char *headers[] = {
+ "Subject", "From", "To", "Cc", "Bcc", "Date"
+ };
+ const char *name, *value;
+ unsigned int i;
+
+ printf ("%s\n", _get_one_line_summary (ctx, message));
+
+ for (i = 0; i < ARRAY_SIZE (headers); i++) {
+ name = headers[i];
+ value = notmuch_message_get_header (message, name);
+ if (value && strlen (value))
+ printf ("%s: %s\n", name, value);
+ }
+}
+
+static void
+format_headers_message_part_text (GMimeMessage *message)
+{
+ InternetAddressList *recipients;
+ const char *recipients_string;
+
+ printf ("From: %s\n", 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 ("To: %s\n",
+ 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 ("Cc: %s\n",
+ recipients_string);
+ printf ("Subject: %s\n", g_mime_message_get_subject (message));
+ printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
+}
+
+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_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;