unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jed Brown <jed@59A2.org>
To: notmuch@notmuchmail.org
Subject: [PATCH 1/2] notmuch-reply.c: accept the --format=default default option.
Date: Wed, 25 Nov 2009 02:35:13 +0100	[thread overview]
Message-ID: <1259112914-19806-2-git-send-email-jed@59A2.org> (raw)
In-Reply-To: <1259112914-19806-1-git-send-email-jed@59A2.org>

This factors actual generation of the reply out of notmuch_reply_command
into notmuch_reply_format_default(), in preparation for other --format=
options.

Signed-off-by: Jed Brown <jed@59A2.org>
---
 notmuch-reply.c |  121 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 78 insertions(+), 43 deletions(-)

diff --git a/notmuch-reply.c b/notmuch-reply.c
index 65bd356..17eb38d 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -23,6 +23,17 @@
 #include "notmuch-client.h"
 #include "gmime-filter-reply.h"
 
+static const struct {
+    const char *header;
+    const char *fallback;
+    GMimeRecipientType recipient_type;
+} reply_to_map[] = {
+    { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
+    { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
+    { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
+    { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
+};
+
 static void
 reply_part_content (GMimeObject *part)
 {
@@ -182,58 +193,17 @@ add_recipients_for_string (GMimeMessage *message,
     return add_recipients_for_address_list (message, config, type, list);
 }
 
-int
-notmuch_reply_command (void *ctx, int argc, char *argv[])
+static int
+notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)
 {
-    notmuch_config_t *config;
-    notmuch_database_t *notmuch;
-    notmuch_query_t *query;
     GMimeMessage *reply;
-    char *query_string;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
-    int ret = 0;
     const char *subject, *recipients, *from_addr = NULL;
     const char *in_reply_to, *orig_references, *references;
     char *reply_headers;
-    struct {
-	const char *header;
-	const char *fallback;
-	GMimeRecipientType recipient_type;
-    } reply_to_map[] = {
-	{ "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
-	{ "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
-	{ "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
-	{ "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
-    };
     unsigned int i;
 
-    config = notmuch_config_open (ctx, NULL, NULL);
-    if (config == NULL)
-	return 1;
-
-    query_string = query_string_from_args (ctx, argc, argv);
-    if (query_string == NULL) {
-	fprintf (stderr, "Out of memory\n");
-	return 1;
-    }
-
-    if (*query_string == '\0') {
-	fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
-	return 1;
-    }
-
-    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
-				     NOTMUCH_DATABASE_MODE_READ_ONLY);
-    if (notmuch == NULL)
-	return 1;
-
-    query = notmuch_query_create (notmuch, query_string);
-    if (query == NULL) {
-	fprintf (stderr, "Out of memory\n");
-	return 1;
-    }
-
     for (messages = notmuch_query_search_messages (query);
 	 notmuch_messages_has_more (messages);
 	 notmuch_messages_advance (messages))
@@ -310,6 +280,71 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 
 	notmuch_message_destroy (message);
     }
+    return 0;
+}
+
+int
+notmuch_reply_command (void *ctx, int argc, char *argv[])
+{
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_query_t *query;
+    char *opt, *query_string;
+    int i, ret = 0;
+    int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query);
+
+    reply_format_func = notmuch_reply_format_default;
+
+    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+	if (strcmp (argv[i], "--") == 0) {
+	    i++;
+	    break;
+	}
+        if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
+	    opt = argv[i] + sizeof ("--format=") - 1;
+	    if (strcmp (opt, "default") == 0) {
+		reply_format_func = notmuch_reply_format_default;
+	    } else {
+		fprintf (stderr, "Invalid value for --format: %s\n", opt);
+		return 1;
+	    }
+	} else {
+	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+	    return 1;
+	}
+    }
+
+    argc -= i;
+    argv += i;
+
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+	return 1;
+
+    query_string = query_string_from_args (ctx, argc, argv);
+    if (query_string == NULL) {
+	fprintf (stderr, "Out of memory\n");
+	return 1;
+    }
+
+    if (*query_string == '\0') {
+	fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
+	return 1;
+    }
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    if (notmuch == NULL)
+	return 1;
+
+    query = notmuch_query_create (notmuch, query_string);
+    if (query == NULL) {
+	fprintf (stderr, "Out of memory\n");
+	return 1;
+    }
+
+    if (reply_format_func (ctx, config, query) != 0)
+	return 1;
 
     notmuch_query_destroy (query);
     notmuch_database_close (notmuch);
-- 
1.6.5.3

  reply	other threads:[~2009-11-25  1:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-25  1:35 Notmuch-side support for git send-email --notmuch id:<MESSAGE-ID> Jed Brown
2009-11-25  1:35 ` Jed Brown [this message]
2009-11-25  1:35   ` [PATCH 2/2] notmuch-reply.c: implement notmuch_reply_format_headers_only Jed Brown
2009-11-25 18:02   ` [PATCH 1/2] reply --format=headers-only: set In-Reply-To header, with ID *last* in References Jed Brown
2009-11-25 18:02     ` [PATCH 2/2] Recognize reply --format=headers-only-git Jed Brown
2009-11-28  1:01       ` Carl Worth
2009-11-28  1:00 ` Notmuch-side support for git send-email --notmuch id:<MESSAGE-ID> Carl Worth
2009-11-28 17:56   ` [PATCH] Documentation for notmuch reply --format=(default|headers-only) Jed Brown
2009-11-28 20:14     ` Carl Worth

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=1259112914-19806-2-git-send-email-jed@59A2.org \
    --to=jed@59a2.org \
    --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).