Re: [PATCH v2 1/3] Add 'compose' command

Subject: Re: [PATCH v2 1/3] Add 'compose' command

Date: Wed, 18 Apr 2012 13:06:32 +0000

To: Felipe Contreras, notmuch@notmuchmail.org

Cc: Felipe Contreras

From: Jani Nikula


On Wed, 18 Apr 2012 15:39:11 +0300, Felipe Contreras <felipe.contreras@gmail.com> wrote:
> Signed-off-by: Felipe Contreras <felipe.contreras@ngmail.com>
> ---
>  Makefile.local    |    1 +
>  notmuch-client.h  |    3 ++
>  notmuch-compose.c |  111 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  notmuch.c         |    5 +++
>  4 files changed, 120 insertions(+)
>  create mode 100644 notmuch-compose.c
> 
> diff --git a/Makefile.local b/Makefile.local
> index 53b4a0d..2c15ec2 100644
> --- a/Makefile.local
> +++ b/Makefile.local
> @@ -279,6 +279,7 @@ notmuch_client_srcs =		\
>  	gmime-filter-headers.c	\
>  	hooks.c			\
>  	notmuch.c		\
> +	notmuch-compose.c	\
>  	notmuch-config.c	\
>  	notmuch-count.c		\
>  	notmuch-dump.c		\
> diff --git a/notmuch-client.h b/notmuch-client.h
> index 19b7f01..1146cd1 100644
> --- a/notmuch-client.h
> +++ b/notmuch-client.h
> @@ -126,6 +126,9 @@ int
>  notmuch_reply_command (void *ctx, int argc, char *argv[]);
>  
>  int
> +notmuch_compose_command (void *ctx, int argc, char *argv[]);
> +
> +int
>  notmuch_restore_command (void *ctx, int argc, char *argv[]);
>  
>  int
> diff --git a/notmuch-compose.c b/notmuch-compose.c
> new file mode 100644
> index 0000000..ac5ea95
> --- /dev/null
> +++ b/notmuch-compose.c
> @@ -0,0 +1,111 @@
> +/* notmuch - Not much of an email program, (just index and search)
> + *
> + * Copyright © 2009 Carl Worth
> + * Copyright © 2009 Keith Packard
> + *
> + * This program is free software: you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 3 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program.  If not, see http://www.gnu.org/licenses/ .
> + *
> + * Authors: Carl Worth <cworth@cworth.org>
> + *	    Keith Packard <keithp@keithp.com>
> + *	    Felipe Contreras <felipe.contreras@gmail.com>
> + */
> +
> +#include "notmuch-client.h"
> +#include "gmime-filter-headers.h"
> +
> +static void
> +show_message_headers (GMimeMessage *message)
> +{
> +    GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
> +
> +    stream_stdout = g_mime_stream_file_new (stdout);
> +    if (stream_stdout) {
> +	g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
> +	stream_filter = g_mime_stream_filter_new(stream_stdout);
> +	if (stream_filter) {
> +		g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
> +					 g_mime_filter_headers_new());
> +		g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
> +		g_object_unref(stream_filter);
> +	}
> +	g_object_unref(stream_stdout);
> +    }
> +}
> +
> +static int
> +notmuch_compose (void *ctx, notmuch_config_t *config)
> +{
> +    GMimeMessage *msg;
> +    const char *from_addr = NULL;
> +    const char *message_id, *user_agent;
> +    char *simple_from;
> +
> +    /* The 1 means we want headers in a "pretty" order. */
> +    msg = g_mime_message_new (1);
> +    if (msg == NULL) {
> +	fprintf (stderr, "Out of memory\n");
> +	return 1;
> +    }
> +
> +    g_mime_message_set_subject (msg, "");
> +
> +    g_mime_object_set_header (GMIME_OBJECT (msg), "To", "");
> +
> +    if (from_addr == NULL)
> +	from_addr = notmuch_config_get_user_primary_email (config);
> +
> +    simple_from = talloc_strdup (ctx, from_addr);
> +
> +    from_addr = talloc_asprintf (ctx, "%s <%s>",
> +				 notmuch_config_get_user_name (config),
> +				 from_addr);
> +    g_mime_object_set_header (GMIME_OBJECT (msg),
> +			      "From", from_addr);
> +
> +    g_mime_object_set_header (GMIME_OBJECT (msg), "Bcc",
> +			      notmuch_config_get_user_primary_email (config));
> +
> +    user_agent = talloc_asprintf (ctx, "notmuch %s",
> +				  STRINGIFY(NOTMUCH_VERSION));
> +    g_mime_object_set_header (GMIME_OBJECT (msg),
> +			      "User-Agent", user_agent);
> +
> +    message_id = talloc_asprintf (ctx, "<%lu-notmuch-%s>",
> +				  time(NULL),
> +				  simple_from);

Running "notmuch compose" more than once within a second would result in
identical message ids for the messages, which is not a good idea. That's
not likely in interactive use, but the notmuch cli is highly scriptable,
so someone is bound to hit this.

Some paranoid might also be worried about "leaking" the time you run
"notmuch compose"... which may be different from the actual time you
send the message.


BR,
Jani.

> +    g_mime_object_set_header (GMIME_OBJECT (msg),
> +			      "Message-ID", message_id);
> +    talloc_free (simple_from);
> +
> +    show_message_headers (msg);
> +
> +    g_object_unref (G_OBJECT (msg));
> +
> +    return 0;
> +}
> +
> +int
> +notmuch_compose_command (void *ctx, unused (int argc), unused (char *argv[]))
> +{
> +    notmuch_config_t *config;
> +    int ret = 0;
> +
> +    config = notmuch_config_open (ctx, NULL, NULL);
> +    if (config == NULL)
> +	return 1;
> +
> +    ret = notmuch_compose (ctx, config);
> +
> +    return ret;
> +}
> diff --git a/notmuch.c b/notmuch.c
> index 477a09c..2b500d7 100644
> --- a/notmuch.c
> +++ b/notmuch.c
> @@ -65,6 +65,11 @@ static command_t commands[] = {
>      { "reply", notmuch_reply_command,
>        "[options...] <search-terms> [...]",
>        "Construct a reply template for a set of messages." },
> +    { "compose", notmuch_compose_command,
> +	NULL,
> +	"Constructs an empty message.",
> +	"\tConstructs a new empty message filling basic headers such as\n"
> +	"\tFrom:, User-Agent: and Message-ID:." },
>      { "tag", notmuch_tag_command,
>        "+<tag>|-<tag> [...] [--] <search-terms> [...]" ,
>        "Add/remove tags for all messages matching the search terms." },
> -- 
> 1.7.10
> 
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch

Thread: