Skip to content

Commit

Permalink
lib-mail: Add message_detail_address_parse
Browse files Browse the repository at this point in the history
Parses e.g. username+foo@domain into
username@domain with detail foo, when
delimiter is set to +
  • Loading branch information
cmouse committed Jul 4, 2017
1 parent e1777cb commit 145e47d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib-mail/message-address.c
Expand Up @@ -527,3 +527,33 @@ bool message_header_is_address(const char *hdr_name)
}
return FALSE;
}

void message_detail_address_parse(const char *delimiters, const char *address,
const char **username_r, char *delim_r,
const char **detail_r)
{
const char *p, *domain;
size_t idx;

*username_r = address;
*detail_r = "";
*delim_r = '\0';

domain = strchr(address, '@');
/* first character that matches the recipient_delimiter */
idx = strcspn(address, delimiters);
p = address[idx] != '\0' ? address + idx : NULL;

if (p != NULL && (domain == NULL || p < domain)) {
*delim_r = *p;
/* user+detail@domain */
*username_r = t_strdup_until(*username_r, p);
if (domain == NULL)
*detail_r = p+1;
else {
*detail_r = t_strdup_until(p+1, domain);
*username_r = t_strconcat(*username_r, domain, NULL);
}
}
}

6 changes: 6 additions & 0 deletions src/lib-mail/message-address.h
Expand Up @@ -33,4 +33,10 @@ void message_address_write(string_t *str, const struct message_address *addr);
/* Returns TRUE if header is known to be an address */
bool message_header_is_address(const char *hdr_name);

/* Parse address+detail@domain into address@domain and detail
using given delimiters. Returns used delimiter. */
void message_detail_address_parse(const char *delimiters, const char *address,
const char **username_r, char *delim_r,
const char **detail_r);

#endif

0 comments on commit 145e47d

Please sign in to comment.