unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: notmuch@notmuchmail.org
Cc: Felipe Contreras <felipe.contreras@ngmail.com>
Subject: [PATCH v2 1/3] Add 'compose' command
Date: Wed, 18 Apr 2012 15:39:11 +0300	[thread overview]
Message-ID: <1334752753-23970-2-git-send-email-felipe.contreras@gmail.com> (raw)
In-Reply-To: <1334752753-23970-1-git-send-email-felipe.contreras@gmail.com>

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

  reply	other threads:[~2012-04-18 12:39 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-18 12:39 [PATCH v2 0/3] composing patches Felipe Contreras
2012-04-18 12:39 ` Felipe Contreras [this message]
2012-04-18 13:06   ` [PATCH v2 1/3] Add 'compose' command Jani Nikula
2012-04-18 13:34     ` Felipe Contreras
2012-04-18 13:43       ` Dmitry Kurochkin
2012-04-18 14:11         ` Felipe Contreras
2012-04-18 14:41           ` Dmitry Kurochkin
2012-04-18 15:45             ` Felipe Contreras
2012-04-18 14:20       ` Jani Nikula
2012-04-18 15:39         ` Felipe Contreras
2012-04-18 16:00           ` Tomi Ollila
2012-04-18 16:10             ` Dmitry Kurochkin
2012-04-18 16:34           ` Jani Nikula
2012-04-19  9:31             ` Tomi Ollila
2012-04-19 14:03               ` Felipe Contreras
2012-04-18 12:39 ` [PATCH v2 2/3] reply: add message-id header Felipe Contreras
2012-04-18 13:09   ` Jani Nikula
2012-04-18 17:27     ` Adam Wolfe Gordon
2012-04-18 19:44     ` Felipe Contreras
2012-04-18 12:39 ` [PATCH v2 3/3] reply: add user-agent field Felipe Contreras
2012-04-18 17:36 ` [PATCH v2 0/3] composing patches Adam Wolfe Gordon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1334752753-23970-2-git-send-email-felipe.contreras@gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=felipe.contreras@ngmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).