cli: clean up user address matching code in guess_from_received_header()

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>
This commit is contained in:
Jani Nikula 2012-05-11 17:33:05 +03:00 committed by David Bremner
parent e03ed64c8f
commit 4c526fe351

View file

@ -377,20 +377,15 @@ add_recipients_from_message (GMimeMessage *reply,
static const char * static const char *
guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message) guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
{ {
const char *received,*primary,*by; const char *addr, *received, *by;
const char **other;
char *tohdr;
char *mta,*ptr,*token; char *mta,*ptr,*token;
char *domain=NULL; char *domain=NULL;
char *tld=NULL; char *tld=NULL;
const char *delim=". \t"; const char *delim=". \t";
size_t i,j,other_len; size_t i;
const char *to_headers[] = {"Envelope-to", "X-Original-To"}; 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 /* sadly, there is no standard way to find out to which email
* address a mail was delivered - what is in the headers depends * 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 * 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 * 'by' part of Received headers
* If none of these work, we give up and return NULL * If none of these work, we give up and return NULL
*/ */
for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) { for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i])); const char *tohdr = notmuch_message_get_header (message, to_headers[i]);
if (tohdr && *tohdr) {
/* tohdr is potentialy a list of email addresses, so here we /* Note: tohdr potentially contains a list of email addresses. */
* check if one of the email addresses is a substring of tohdr addr = user_address_in_string (tohdr, config);
*/ if (addr)
if (strcasestr(tohdr, primary)) { return addr;
free(tohdr);
return primary;
}
for (j = 0; j < other_len; j++)
if (strcasestr (tohdr, other[j])) {
free(tohdr);
return other[j];
}
free(tohdr);
}
} }
/* We get the concatenated Received: headers and search from the /* 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 * header
*/ */
ptr = strstr (received, " for "); ptr = strstr (received, " for ");
if (ptr) {
/* the text following is potentialy a list of email addresses, /* Note: ptr potentially contains a list of email addresses. */
* so again we check if one of the email addresses is a addr = user_address_in_string (ptr, config);
* substring of ptr if (addr)
*/ return addr;
if (strcasestr(ptr, primary)) {
return primary;
}
for (i = 0; i < other_len; i++)
if (strcasestr (ptr, other[i])) {
return other[i];
}
}
/* Finally, we parse all the " by MTA ..." headers to guess the /* Finally, we parse all the " by MTA ..." headers to guess the
* email address that this was originally delivered to. * email address that this was originally delivered to.
* We extract just the MTA here by removing leading whitespace and * We extract just the MTA here by removing leading whitespace and
@ -492,14 +470,10 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message
*/ */
*(tld-1) = '.'; *(tld-1) = '.';
if (strcasestr(primary, domain)) { addr = string_in_user_address (domain, config);
if (addr) {
free (mta); free (mta);
return primary; return addr;
}
for (i = 0; i < other_len; i++)
if (strcasestr (other[i],domain)) {
free(mta);
return other[i];
} }
} }
free (mta); free (mta);