Exemplo n.º 1
0
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);
}
Exemplo n.º 2
0
/* Formatted output of single message */
static int
do_show_single (void *ctx,
		notmuch_query_t *query,
		const notmuch_show_format_t *format,
		notmuch_show_params_t *params)
{
    notmuch_messages_t *messages;
    notmuch_message_t *message;

    if (notmuch_query_count_messages (query) != 1) {
	fprintf (stderr, "Error: search term did not match precisely one message.\n");
	return 1;
    }

    messages = notmuch_query_search_messages (query);
    message = notmuch_messages_get (messages);

    if (message == NULL) {
	fprintf (stderr, "Error: Cannot find matching message.\n");
	return 1;
    }

    notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);

    /* Special case for --format=raw of full single message, just cat out file */
    if (params->raw && 0 == params->part) {

	const char *filename;
	FILE *file;
	size_t size;
	char buf[4096];

	filename = notmuch_message_get_filename (message);
	if (filename == NULL) {
	    fprintf (stderr, "Error: Cannot message filename.\n");
	    return 1;
	}

	file = fopen (filename, "r");
	if (file == NULL) {
	    fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
	    return 1;
	}

	while (!feof (file)) {
	    size = fread (buf, 1, sizeof (buf), file);
	    (void) fwrite (buf, size, 1, stdout);
	}

	fclose (file);

    } else {

	show_message (ctx, format, message, 0, params);

    }

    return 0;
}
Exemplo n.º 3
0
static void
format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
{
    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));
}
Exemplo n.º 4
0
/* Print a message in "mboxrd" format as documented, for example,
 * here:
 *
 * http://qmail.org/qmail-manual-html/man5/mbox.html
 */
static notmuch_status_t
format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
		  unused (int indent),
		  unused (const notmuch_show_params_t *params))
{
    notmuch_message_t *message = node->envelope_file;

    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;

    if (!message)
	INTERNAL_ERROR ("format_part_mbox requires a root part");

    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 NOTMUCH_STATUS_FILE_ERROR;
    }

    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);

    return NOTMUCH_STATUS_SUCCESS;
}
Exemplo n.º 5
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);
}
Exemplo n.º 6
0
static int
do_search_messages (const search_format_t *format,
		    notmuch_query_t *query,
		    output_t output)
{
    notmuch_message_t *message;
    notmuch_messages_t *messages;
    int first_message = 1;

    messages = notmuch_query_search_messages (query);
    if (messages == NULL)
	return 1;

    fputs (format->results_start, stdout);

    for (;
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	if (! first_message)
	    fputs (format->item_sep, stdout);

	if (output == OUTPUT_FILES) {
	    format->item_id (message, "",
			     notmuch_message_get_filename (message));
	} else { /* output == OUTPUT_MESSAGES */
	    format->item_id (message, "id:",
			     notmuch_message_get_message_id (message));
	}

	first_message = 0;

	notmuch_message_destroy (message);
    }

    notmuch_messages_destroy (messages);

    fputs (format->results_end, stdout);

    return 0;
}
Exemplo n.º 7
0
/* Emit a sequence of key/value pairs for the metadata of message.
 * The caller should begin a map before calling this. */
static void
format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
{
    /* Any changes to the JSON or S-Expression format should be
     * reflected in the file devel/schemata. */

    void *local = talloc_new (NULL);
    notmuch_tags_t *tags;
    time_t date;
    const char *relative_date;

    sp->map_key (sp, "id");
    sp->string (sp, notmuch_message_get_message_id (message));

    sp->map_key (sp, "match");
    sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));

    sp->map_key (sp, "excluded");
    sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));

    sp->map_key (sp, "filename");
    sp->string (sp, notmuch_message_get_filename (message));

    sp->map_key (sp, "timestamp");
    date = notmuch_message_get_date (message);
    sp->integer (sp, date);

    sp->map_key (sp, "date_relative");
    relative_date = notmuch_time_relative_date (local, date);
    sp->string (sp, relative_date);

    sp->map_key (sp, "tags");
    sp->begin_list (sp);
    for (tags = notmuch_message_get_tags (message);
	 notmuch_tags_valid (tags);
	 notmuch_tags_move_to_next (tags))
	sp->string (sp, notmuch_tags_get (tags));
    sp->end (sp);

    talloc_free (local);
}
Exemplo n.º 8
0
static notmuch_status_t
format_part_raw (unused (const void *ctx), mime_node_t *node,
                 unused (int indent),
                 unused (const notmuch_show_params_t *params))
{
    if (node->envelope_file) {
        /* Special case the entire message to avoid MIME parsing. */
        const char *filename;
        FILE *file;
        size_t size;
        char buf[4096];

        filename = notmuch_message_get_filename (node->envelope_file);
        if (filename == NULL) {
            fprintf (stderr, "Error: Cannot get message filename.\n");
            return NOTMUCH_STATUS_FILE_ERROR;
        }

        file = fopen (filename, "r");
        if (file == NULL) {
            fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
            return NOTMUCH_STATUS_FILE_ERROR;
        }

        while (!feof (file)) {
            size = fread (buf, 1, sizeof (buf), file);
            if (ferror (file)) {
                fprintf (stderr, "Error: Read failed from %s\n", filename);
                fclose (file);
                return NOTMUCH_STATUS_FILE_ERROR;
            }

            if (fwrite (buf, size, 1, stdout) != 1) {
                fprintf (stderr, "Error: Write failed\n");
                fclose (file);
                return NOTMUCH_STATUS_FILE_ERROR;
            }
        }

        fclose (file);
        return NOTMUCH_STATUS_SUCCESS;
    }

    GMimeStream *stream_stdout;
    GMimeStream *stream_filter = NULL;

    stream_stdout = g_mime_stream_file_new (stdout);
    g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);

    stream_filter = g_mime_stream_filter_new (stream_stdout);

    if (GMIME_IS_PART (node->part)) {
        /* For leaf parts, we emit only the transfer-decoded
         * body. */
        GMimeDataWrapper *wrapper;
        wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));

        if (wrapper && stream_filter)
            g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
    } else {
        /* Write out the whole part.  For message parts (the root
         * part and embedded message parts), this will be the
         * message including its headers (but not the
         * encapsulating part's headers).  For multipart parts,
         * this will include the headers. */
        if (stream_filter)
            g_mime_object_write_to_stream (node->part, stream_filter);
    }

    if (stream_filter)
        g_object_unref (stream_filter);

    if (stream_stdout)
        g_object_unref(stream_stdout);

    return NOTMUCH_STATUS_SUCCESS;
}
Exemplo n.º 9
0
static notmuch_status_t
format_part_text (const void *ctx, mime_node_t *node,
                  int indent, const notmuch_show_params_t *params)
{
    /* 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 notmuch_bool_t leaf = GMIME_IS_PART (node->part);
    const char *part_type;
    int i;

    if (node->envelope_file) {
        notmuch_message_t *message = node->envelope_file;

        part_type = "message";
        printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
                part_type,
                notmuch_message_get_message_id (message),
                indent,
                notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
                notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
                notmuch_message_get_filename (message));
    } else {
        GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
        const char *cid = g_mime_object_get_content_id (meta);
        const char *filename = leaf ?
                               g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;

        if (disposition &&
                strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
            part_type = "attachment";
        else
            part_type = "part";

        printf ("\f%s{ ID: %d", part_type, node->part_num);
        if (filename)
            printf (", Filename: %s", filename);
        if (cid)
            printf (", Content-id: %s", cid);
        printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
    }

    if (GMIME_IS_MESSAGE (node->part)) {
        GMimeMessage *message = GMIME_MESSAGE (node->part);
        InternetAddressList *recipients;
        const char *recipients_string;

        printf ("\fheader{\n");
        if (node->envelope_file)
            printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
        printf ("Subject: %s\n", g_mime_message_get_subject (message));
        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 ("Date: %s\n", g_mime_message_get_date_as_string (message));
        printf ("\fheader}\n");

        printf ("\fbody{\n");
    }

    if (leaf) {
        if (g_mime_content_type_is_type (content_type, "text", "*") &&
                !g_mime_content_type_is_type (content_type, "text", "html"))
        {
            GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
            g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
            show_text_part_content (node->part, stream_stdout, 0);
            g_object_unref(stream_stdout);
        } else {
            printf ("Non-text part: %s\n",
                    g_mime_content_type_to_string (content_type));
        }
    }

    for (i = 0; i < node->nchildren; i++)
        format_part_text (ctx, mime_node_child (node, i), indent, params);

    if (GMIME_IS_MESSAGE (node->part))
        printf ("\fbody}\n");

    printf ("\f%s}\n", part_type);

    return NOTMUCH_STATUS_SUCCESS;
}
Exemplo n.º 10
0
static int
notmuch_reply_format_default(void *ctx,
			     notmuch_config_t *config,
			     notmuch_query_t *query,
			     notmuch_show_params_t *params)
{
    GMimeMessage *reply;
    notmuch_messages_t *messages;
    notmuch_message_t *message;
    const char *subject, *from_addr = NULL;
    const char *in_reply_to, *orig_references, *references;
    const notmuch_show_format_t *format = &format_reply;

    for (messages = notmuch_query_search_messages (query);
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	/* The 1 means we want headers in a "pretty" order. */
	reply = g_mime_message_new (1);
	if (reply == NULL) {
	    fprintf (stderr, "Out of memory\n");
	    return 1;
	}

	subject = notmuch_message_get_header (message, "subject");
	if (subject) {
	    if (strncasecmp (subject, "Re:", 3))
		subject = talloc_asprintf (ctx, "Re: %s", subject);
	    g_mime_message_set_subject (reply, subject);
	}

	from_addr = add_recipients_from_message (reply, config, message);

	if (from_addr == NULL)
	    from_addr = guess_from_received_header (config, message);

	if (from_addr == NULL)
	    from_addr = notmuch_config_get_user_primary_email (config);

	from_addr = talloc_asprintf (ctx, "%s <%s>",
				     notmuch_config_get_user_name (config),
				     from_addr);
	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "From", from_addr);

	in_reply_to = talloc_asprintf (ctx, "<%s>",
			     notmuch_message_get_message_id (message));

	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "In-Reply-To", in_reply_to);

	orig_references = notmuch_message_get_header (message, "references");
	references = talloc_asprintf (ctx, "%s%s%s",
				      orig_references ? orig_references : "",
				      orig_references ? " " : "",
				      in_reply_to);
	g_mime_object_set_header (GMIME_OBJECT (reply),
				  "References", references);

	show_reply_headers (reply);

	g_object_unref (G_OBJECT (reply));
	reply = NULL;

	printf ("On %s, %s wrote:\n",
		notmuch_message_get_header (message, "date"),
		notmuch_message_get_header (message, "from"));

	show_message_body (notmuch_message_get_filename (message),
			   format, params);

	notmuch_message_destroy (message);
    }
    return 0;
}
int main (int argc, char** argv) {
  GFile              *db_dir, *db_file;
  notmuch_status_t    status;
  notmuch_database_t *db   = NULL;
  notmuch_query_t *query = NULL;
  notmuch_messages_t *messages = NULL;
  notmuch_message_t *message = NULL;
  GMainLoop          *loop = NULL;

  const char *query_string = "date:2014-02-01..";

  if (argc != 2)
  {
    g_warning ("Usage: %s EVOLUTION_MAILDIR", argv[0]);
    return 1;
  }

  db_dir = g_file_new_for_path (argv[1]);
  db_file = g_file_get_child (db_dir, ".notmuch");

  if (!g_file_query_exists (db_dir, NULL))
  {
    g_object_unref (db_dir);
    g_object_unref (db_file);
    g_error ("directory %s does not exists");
    return 2;
  }

  if (!g_file_query_exists (db_file, NULL))
    status = notmuch_database_create (argv[1], &db);
  else
    status = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);

  if (status)
  {
    g_error ("Could not open database: %d", status);
    g_object_unref (db_dir);
    g_object_unref (db_file);
    notmuch_database_destroy (db);
    return 3;
  }


  scan_directory (db, db_dir);
  //loop = g_main_loop_new (NULL, FALSE);
  //g_main_loop_run (loop);

  query = notmuch_query_create (db, query_string);

  if (!query) {
    g_error ("Could not create query from string = \"%s\"", query_string);
    notmuch_database_destroy (db);
    g_object_unref (db_file);
    g_object_unref (db_dir);
    return 4;
  }

  g_message ("Query results -\n\n");

  for (messages = notmuch_query_search_messages (query);
         notmuch_messages_valid (messages);
         notmuch_messages_move_to_next (messages)) {

    message = notmuch_messages_get (messages);

    g_message ("Message file: %s", notmuch_message_get_filename (message));
    g_message ("Message ID: %s", notmuch_message_get_message_id (message));
    g_message ("Message Sender: %s", notmuch_message_get_header (message, "from"));
    g_message ("Message Recipients: %s", notmuch_message_get_header (message, "to"));
    g_message ("Message Subject: %s", notmuch_message_get_header (message, "subject"));
    g_message ("Message date: %s\n", notmuch_message_get_header (message, "date"));

    notmuch_message_destroy (message);
  }

  notmuch_query_destroy (query);

  notmuch_database_destroy (db);
  g_object_unref (db_file);
  g_object_unref (db_dir);
  return 0;
}