[PATCH v7.1 02/11] reply: Factor out reply creation
authorAdam Wolfe Gordon <awg+notmuch@xvx.ca>
Wed, 14 Mar 2012 04:30:07 +0000 (22:30 +1800)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:28 +0000 (09:45 -0800)
e1/6d9971de696fa5fe6eb278a7bd0aae692eaa8c [new file with mode: 0644]

diff --git a/e1/6d9971de696fa5fe6eb278a7bd0aae692eaa8c b/e1/6d9971de696fa5fe6eb278a7bd0aae692eaa8c
new file mode 100644 (file)
index 0000000..60d5fa0
--- /dev/null
@@ -0,0 +1,195 @@
+Return-Path: <awg@lagos.xvx.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 99FA4429E29\r
+       for <notmuch@notmuchmail.org>; Tue, 13 Mar 2012 21:30:24 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id cr8n64vqBgNa for <notmuch@notmuchmail.org>;\r
+       Tue, 13 Mar 2012 21:30:23 -0700 (PDT)\r
+Received: from idcmail-mo1so.shaw.ca (idcmail-mo1so.shaw.ca [24.71.223.10])\r
+       by olra.theworths.org (Postfix) with ESMTP id 54F22431FCF\r
+       for <notmuch@notmuchmail.org>; Tue, 13 Mar 2012 21:30:22 -0700 (PDT)\r
+Received: from pd2ml3so-ssvc.prod.shaw.ca ([10.0.141.148])\r
+       by pd4mo1so-svcs.prod.shaw.ca with ESMTP; 13 Mar 2012 22:30:20 -0600\r
+X-Cloudmark-SP-Filtered: true\r
+X-Cloudmark-SP-Result: v=1.1 cv=W+ZrOcpwHTeQLvZQ9vbIxXRdvbK5p6H2/T6SuQCN924=\r
+       c=1 sm=1\r
+       a=hY5Esnnl3dQA:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17\r
+       a=Ip1p9aMYQwLSyBp_cu4A:9 a=VU9I-PVEt86hzLs7d4sA:7\r
+       a=HpAAvcLHHh0Zw7uRqdWCyQ==:117\r
+Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56])\r
+       by pd2ml3so-dmz.prod.shaw.ca with ESMTP; 13 Mar 2012 22:30:21 -0600\r
+Received: by lagos.xvx.ca (Postfix, from userid 1000)\r
+       id BF6948004C20; Tue, 13 Mar 2012 22:30:20 -0600 (MDT)\r
+From: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7.1 02/11] reply: Factor out reply creation\r
+Date: Tue, 13 Mar 2012 22:30:07 -0600\r
+Message-Id: <1331699416-30775-3-git-send-email-awg+notmuch@xvx.ca>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1331699416-30775-1-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1331525142-30539-1-git-send-email-awg+notmuch@xvx.ca>\r
+       <1331699416-30775-1-git-send-email-awg+notmuch@xvx.ca>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 14 Mar 2012 04:30:25 -0000\r
+\r
+Factor out the creation of a reply message based on an original\r
+message so it can be shared by different reply formats.\r
+---\r
+ notmuch-reply.c |  104 ++++++++++++++++++++++++++++++++----------------------\r
+ 1 files changed, 62 insertions(+), 42 deletions(-)\r
+\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 6b244e6..f1478cc 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -505,6 +505,61 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
+     return NULL;\r
+ }\r
\r
++static GMimeMessage *\r
++create_reply_message(void *ctx,\r
++                   notmuch_config_t *config,\r
++                   notmuch_message_t *message,\r
++                   notmuch_bool_t reply_all)\r
++{\r
++    const char *subject, *from_addr = NULL;\r
++    const char *in_reply_to, *orig_references, *references;\r
++\r
++    /* The 1 means we want headers in a "pretty" order. */\r
++    GMimeMessage *reply = g_mime_message_new (1);\r
++    if (reply == NULL) {\r
++      fprintf (stderr, "Out of memory\n");\r
++      return NULL;\r
++    }\r
++\r
++    subject = notmuch_message_get_header (message, "subject");\r
++    if (subject) {\r
++      if (strncasecmp (subject, "Re:", 3))\r
++          subject = talloc_asprintf (ctx, "Re: %s", subject);\r
++      g_mime_message_set_subject (reply, subject);\r
++    }\r
++\r
++    from_addr = add_recipients_from_message (reply, config,\r
++                                           message, reply_all);\r
++\r
++    if (from_addr == NULL)\r
++      from_addr = guess_from_received_header (config, message);\r
++\r
++    if (from_addr == NULL)\r
++      from_addr = notmuch_config_get_user_primary_email (config);\r
++\r
++    from_addr = talloc_asprintf (ctx, "%s <%s>",\r
++                               notmuch_config_get_user_name (config),\r
++                               from_addr);\r
++    g_mime_object_set_header (GMIME_OBJECT (reply),\r
++                            "From", from_addr);\r
++\r
++    in_reply_to = talloc_asprintf (ctx, "<%s>",\r
++                                 notmuch_message_get_message_id (message));\r
++\r
++    g_mime_object_set_header (GMIME_OBJECT (reply),\r
++                            "In-Reply-To", in_reply_to);\r
++\r
++    orig_references = notmuch_message_get_header (message, "references");\r
++    references = talloc_asprintf (ctx, "%s%s%s",\r
++                                orig_references ? orig_references : "",\r
++                                orig_references ? " " : "",\r
++                                in_reply_to);\r
++    g_mime_object_set_header (GMIME_OBJECT (reply),\r
++                            "References", references);\r
++\r
++    return reply;\r
++}\r
++\r
+ static int\r
+ notmuch_reply_format_default(void *ctx,\r
+                            notmuch_config_t *config,\r
+@@ -515,8 +570,6 @@ notmuch_reply_format_default(void *ctx,\r
+     GMimeMessage *reply;\r
+     notmuch_messages_t *messages;\r
+     notmuch_message_t *message;\r
+-    const char *subject, *from_addr = NULL;\r
+-    const char *in_reply_to, *orig_references, *references;\r
+     const notmuch_show_format_t *format = &format_reply;\r
\r
+     for (messages = notmuch_query_search_messages (query);\r
+@@ -525,49 +578,16 @@ notmuch_reply_format_default(void *ctx,\r
+     {\r
+       message = notmuch_messages_get (messages);\r
\r
+-      /* The 1 means we want headers in a "pretty" order. */\r
+-      reply = g_mime_message_new (1);\r
+-      if (reply == NULL) {\r
+-          fprintf (stderr, "Out of memory\n");\r
+-          return 1;\r
+-      }\r
++      reply = create_reply_message (ctx, config, message, reply_all);\r
\r
+-      subject = notmuch_message_get_header (message, "subject");\r
+-      if (subject) {\r
+-          if (strncasecmp (subject, "Re:", 3))\r
+-              subject = talloc_asprintf (ctx, "Re: %s", subject);\r
+-          g_mime_message_set_subject (reply, subject);\r
++      /* If reply creation failed, we're out of memory, so don't\r
++       * bother trying any more messages.\r
++       */\r
++      if (!reply) {\r
++          notmuch_message_destroy (message);\r
++          return 1;\r
+       }\r
\r
+-      from_addr = add_recipients_from_message (reply, config, message,\r
+-                                               reply_all);\r
+-\r
+-      if (from_addr == NULL)\r
+-          from_addr = guess_from_received_header (config, message);\r
+-\r
+-      if (from_addr == NULL)\r
+-          from_addr = notmuch_config_get_user_primary_email (config);\r
+-\r
+-      from_addr = talloc_asprintf (ctx, "%s <%s>",\r
+-                                   notmuch_config_get_user_name (config),\r
+-                                   from_addr);\r
+-      g_mime_object_set_header (GMIME_OBJECT (reply),\r
+-                                "From", from_addr);\r
+-\r
+-      in_reply_to = talloc_asprintf (ctx, "<%s>",\r
+-                           notmuch_message_get_message_id (message));\r
+-\r
+-      g_mime_object_set_header (GMIME_OBJECT (reply),\r
+-                                "In-Reply-To", in_reply_to);\r
+-\r
+-      orig_references = notmuch_message_get_header (message, "references");\r
+-      references = talloc_asprintf (ctx, "%s%s%s",\r
+-                                    orig_references ? orig_references : "",\r
+-                                    orig_references ? " " : "",\r
+-                                    in_reply_to);\r
+-      g_mime_object_set_header (GMIME_OBJECT (reply),\r
+-                                "References", references);\r
+-\r
+       show_reply_headers (reply);\r
\r
+       g_object_unref (G_OBJECT (reply));\r
+-- \r
+1.7.5.4\r
+\r