unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/2] cli: clean up user address matching code in guess_from_received_header()
Date: Fri, 11 May 2012 17:33:05 +0300	[thread overview]
Message-ID: <cd73ec6368b3743b0d84ed1696bfa00960124645.1336746187.git.jani@nikula.org> (raw)
In-Reply-To: <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>
In-Reply-To: <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>

Get rid of user address matching code duplication in
guess_from_received_header() by using the new address matching
helpers.

No functional changes.

Signed-off-by: Jani Nikula <jani@nikula.org>
---
 notmuch-reply.c |   64 +++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 45 deletions(-)

diff --git a/notmuch-reply.c b/notmuch-reply.c
index 0c82755..51cb6de 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -377,20 +377,15 @@ add_recipients_from_message (GMimeMessage *reply,
 static const char *
 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
 {
-    const char *received,*primary,*by;
-    const char **other;
-    char *tohdr;
+    const char *addr, *received, *by;
     char *mta,*ptr,*token;
     char *domain=NULL;
     char *tld=NULL;
     const char *delim=". \t";
-    size_t i,j,other_len;
+    size_t i;
 
     const char *to_headers[] = {"Envelope-to", "X-Original-To"};
 
-    primary = notmuch_config_get_user_primary_email (config);
-    other = notmuch_config_get_user_other_email (config, &other_len);
-
     /* sadly, there is no standard way to find out to which email
      * address a mail was delivered - what is in the headers depends
      * on the MTAs used along the way. So we are trying a number of
@@ -405,23 +400,13 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message
      *    'by' part of Received headers
      * If none of these work, we give up and return NULL
      */
-    for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
-	tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
-	if (tohdr && *tohdr) {
-	    /* tohdr is potentialy a list of email addresses, so here we
-	     * check if one of the email addresses is a substring of tohdr
-	     */
-	    if (strcasestr(tohdr, primary)) {
-		free(tohdr);
-		return primary;
-	    }
-	    for (j = 0; j < other_len; j++)
-		if (strcasestr (tohdr, other[j])) {
-		    free(tohdr);
-		    return other[j];
-		}
-	    free(tohdr);
-	}
+    for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
+	const char *tohdr = notmuch_message_get_header (message, to_headers[i]);
+
+	/* Note: tohdr potentially contains a list of email addresses. */
+	addr = user_address_in_string (tohdr, config);
+	if (addr)
+	    return addr;
     }
 
     /* We get the concatenated Received: headers and search from the
@@ -439,19 +424,12 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message
      * header
      */
     ptr = strstr (received, " for ");
-    if (ptr) {
-	/* the text following is potentialy a list of email addresses,
-	 * so again we check if one of the email addresses is a
-	 * substring of ptr
-	 */
-	if (strcasestr(ptr, primary)) {
-	    return primary;
-	}
-	for (i = 0; i < other_len; i++)
-	    if (strcasestr (ptr, other[i])) {
-		return other[i];
-	    }
-    }
+
+    /* Note: ptr potentially contains a list of email addresses. */
+    addr = user_address_in_string (ptr, config);
+    if (addr)
+	return addr;
+
     /* Finally, we parse all the " by MTA ..." headers to guess the
      * email address that this was originally delivered to.
      * We extract just the MTA here by removing leading whitespace and
@@ -492,15 +470,11 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message
 	     */
 	    *(tld-1) = '.';
 
-	    if (strcasestr(primary, domain)) {
-		free(mta);
-		return primary;
+	    addr = string_in_user_address (domain, config);
+	    if (addr) {
+		free (mta);
+		return addr;
 	    }
-	    for (i = 0; i < other_len; i++)
-		if (strcasestr (other[i],domain)) {
-		    free(mta);
-		    return other[i];
-		}
 	}
 	free (mta);
     }
-- 
1.7.9.5

  reply	other threads:[~2012-05-11 14:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-11 14:33 [PATCH 1/2] cli: add user address matching helpers for notmuch reply Jani Nikula
2012-05-11 14:33 ` Jani Nikula [this message]
2012-05-12 15:48   ` [PATCH 2/2] cli: clean up user address matching code in guess_from_received_header() Austin Clements
2012-05-12 17:23     ` Jani Nikula
2012-05-24  1:54 ` [PATCH 1/2] cli: add user address matching helpers for notmuch reply David Bremner

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=cd73ec6368b3743b0d84ed1696bfa00960124645.1336746187.git.jani@nikula.org \
    --to=jani@nikula.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).