Skip to content

Commit 0eb259b

Browse files
committed
lib: consolidate duplicated EMAIL_ADDR construction logic
Fixes: gromox-2.22-19-gf13994811 Fixes: gromox-2.28-27-g60c949541 References: GXF-1673
1 parent a7ba581 commit 0eb259b

File tree

7 files changed

+37
-50
lines changed

7 files changed

+37
-50
lines changed

exch/midb/mail_engine.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,6 @@ static void mail_engine_extract_digest_fields(const Json::Value &digest, char *s
13311331
size_t out_len;
13321332
char temp_buff[64*1024];
13331333
char temp_buff1[64*1024];
1334-
EMAIL_ADDR temp_address;
13351334

13361335
subject[0] = '\0';
13371336
if (get_digest(digest, "subject", temp_buff, std::size(temp_buff)) &&
@@ -1342,8 +1341,7 @@ static void mail_engine_extract_digest_fields(const Json::Value &digest, char *s
13421341
if (get_digest(digest, "from", temp_buff, std::size(temp_buff)) &&
13431342
decode64(temp_buff, strlen(temp_buff), temp_buff1,
13441343
std::size(temp_buff1), &out_len) == 0) {
1345-
memset(&temp_address, 0, sizeof(temp_address));
1346-
parse_mime_addr(&temp_address, temp_buff1);
1344+
EMAIL_ADDR temp_address(temp_buff1);
13471345
gx_strlcpy(from, temp_address.addr, fromsize);
13481346
}
13491347
rcpt[0] = '\0';
@@ -1358,8 +1356,7 @@ static void mail_engine_extract_digest_fields(const Json::Value &digest, char *s
13581356
}
13591357
}
13601358
HX_strrtrim(temp_buff1);
1361-
memset(&temp_address, 0, sizeof(temp_address));
1362-
parse_mime_addr(&temp_address, temp_buff1);
1359+
EMAIL_ADDR temp_address(temp_buff1);
13631360
gx_strlcpy(rcpt, temp_address.addr, rcptsize);
13641361
}
13651362
*psize = 0;

include/gromox/mail_func.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ctime>
77
#include <string>
88
#include <vector>
9+
#include <vmime/mailbox.hpp>
910
#include <vmime/message.hpp>
1011
#include <gromox/mapi_types.hpp>
1112
#define MIME_NAME_LEN 80U
@@ -17,6 +18,12 @@ struct attachment_list;
1718
* All fields are always UTF-8 for consistency.
1819
*/
1920
struct GX_EXPORT EMAIL_ADDR {
21+
EMAIL_ADDR() = default;
22+
EMAIL_ADDR(const char *x) { parse(x); }
23+
EMAIL_ADDR(const vmime::mailbox &x) { set(x); }
24+
void clear();
25+
void set(const vmime::mailbox &);
26+
void parse(const char *);
2027
inline bool has_dispname() const { return *display_name != '\0'; }
2128
inline bool has_addr() const { return *local_part != '\0' && *domain != '\0'; }
2229
inline bool has_value() const { return has_dispname() || has_addr(); }
@@ -36,7 +43,6 @@ struct ENCODE_STRING {
3643
};
3744

3845
struct MAIL;
39-
extern GX_EXPORT void parse_mime_addr(EMAIL_ADDR *e_addr, const char *email);
4046
extern GX_EXPORT BOOL parse_uri(const char *uri_buff, char *parsed_uri);
4147
extern GX_EXPORT size_t parse_mime_field(const char *, size_t, MIME_FIELD *);
4248
extern GX_EXPORT void parse_field_value(const char *in_buff, long buff_len, char *value, long val_len, std::vector<kvpair> &);

lib/email/mjson.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,10 @@ static int mjson_convert_address(const char *address, const char *charset,
652652
{
653653
int offset;
654654
size_t ecode_len;
655-
EMAIL_ADDR email_addr;
656655
char temp_buff[2048];
657656

658657
offset = 0;
659-
parse_mime_addr(&email_addr, address);
658+
EMAIL_ADDR email_addr(address);
660659
if (*email_addr.display_name == '\0') {
661660
memcpy(buff + offset, "(NIL", 4);
662661
offset += 4;

lib/mail_func.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,30 +255,40 @@ BOOL parse_uri(const char *uri_buff, char *parsed_uri)
255255
return TRUE;
256256
}
257257

258-
/*
259-
* parse email address in mime field into e_addr
260-
* @param
261-
* email [in] string contain the address
262-
* e_addr [out] for retrieving parsed address
263-
*/
264-
void parse_mime_addr(EMAIL_ADDR *e_addr, const char *input) try
258+
void EMAIL_ADDR::clear()
265259
{
266-
vmime::mailbox mb("");
267-
mb.parse(input);
260+
memset(this, 0, sizeof(*this));
261+
}
262+
263+
void EMAIL_ADDR::set(const vmime::mailbox &mb) try
264+
{
265+
auto e_addr = this;
268266

269267
gx_strlcpy(e_addr->display_name, mb.getName().getConvertedText("utf-8").c_str(), std::size(e_addr->display_name));
270268
auto emp = mb.getEmail().generate();
271269
gx_strlcpy(e_addr->addr, emp.c_str(), std::size(e_addr->addr));
272270
auto at = emp.find('@');
273271
if (at == emp.npos) {
274-
*e_addr = {};
272+
clear();
275273
return;
276274
}
277275
emp[at] = '\0';
278276
gx_strlcpy(e_addr->local_part, &emp[0], std::size(e_addr->local_part));
279277
gx_strlcpy(e_addr->domain, &emp[at+1], std::size(e_addr->domain));
280278
} catch (const std::bad_alloc &) {
281-
*e_addr = {};
279+
clear();
280+
}
281+
282+
/**
283+
* @input: whatever is normally allowed in the From header value
284+
*/
285+
void EMAIL_ADDR::parse(const char *input) try
286+
{
287+
vmime::mailbox mb("");
288+
mb.parse(input);
289+
set(mb);
290+
} catch (const std::bad_alloc &) {
291+
clear();
282292
}
283293

284294
/*

lib/mapi/oxcmail.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,6 @@ static BOOL oxcmail_parse_recipient(const char *charset,
448448
static BOOL oxcmail_parse_addresses(const char *charset, const char *field,
449449
uint32_t rcpt_type, TARRAY_SET *pset) try
450450
{
451-
EMAIL_ADDR email_addr;
452-
453451
vmime::mailboxList mblist;
454452
try {
455453
mblist.parse(field);
@@ -461,16 +459,7 @@ static BOOL oxcmail_parse_addresses(const char *charset, const char *field,
461459
auto mb = vmime::dynamicCast<vmime::mailbox>(compo);
462460
if (mb == nullptr)
463461
continue;
464-
gx_strlcpy(email_addr.display_name, mb->getName().getConvertedText("utf-8").c_str(), std::size(email_addr.display_name));
465-
auto emp = mb->getEmail().generate();
466-
gx_strlcpy(email_addr.addr, emp.c_str(), std::size(email_addr.addr));
467-
auto at = emp.find('@');
468-
if (at == emp.npos)
469-
continue;
470-
emp[at] = '\0';
471-
gx_strlcpy(email_addr.local_part, &emp[0], std::size(email_addr.local_part));
472-
gx_strlcpy(email_addr.domain, &emp[at+1], std::size(email_addr.domain));
473-
462+
EMAIL_ADDR email_addr(*mb);
474463
if (!email_addr.has_addr())
475464
continue;
476465
if (!oxcmail_parse_recipient(charset,
@@ -487,8 +476,7 @@ static BOOL oxcmail_parse_address(const char *field, uint32_t pr_name,
487476
uint32_t pr_addrtype, uint32_t pr_emaddr, uint32_t pr_smtpaddr,
488477
uint32_t pr_searchkey, uint32_t pr_entryid, TPROPVAL_ARRAY *pproplist) try
489478
{
490-
EMAIL_ADDR email_addr, *paddr = &email_addr;
491-
parse_mime_addr(&email_addr, field);
479+
EMAIL_ADDR email_addr(field), *paddr = &email_addr;
492480

493481
if (paddr->has_dispname()) {
494482
if (pproplist->set(pr_name, paddr->display_name) != 0)
@@ -566,16 +554,7 @@ static BOOL oxcmail_parse_reply_to(const char *charset, const char *field,
566554
auto mb = vmime::dynamicCast<vmime::mailbox>(compo);
567555
if (mb == nullptr)
568556
continue;
569-
gx_strlcpy(email_addr.display_name, mb->getName().getConvertedText("utf-8").c_str(), std::size(email_addr.display_name));
570-
auto emp = mb->getEmail().generate();
571-
gx_strlcpy(email_addr.addr, emp.c_str(), std::size(email_addr.addr));
572-
auto at = emp.find('@');
573-
if (at == emp.npos)
574-
continue;
575-
emp[at] = '\0';
576-
gx_strlcpy(email_addr.local_part, &emp[0], std::size(email_addr.local_part));
577-
gx_strlcpy(email_addr.domain, &emp[at+1], std::size(email_addr.domain));
578-
557+
email_addr.set(*mb);
579558
if (!email_addr.has_addr())
580559
continue;
581560
snprintf(tmp_buff, std::size(tmp_buff), "%s@%s",

mda/smtp/smtp_cmd_handler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ int smtp_cmd_handler_mail(const char* cmd_line, int line_length,
127127
size_t string_length = 0;
128128
const char *smtp_reply_str, *smtp_reply_str2;
129129
char buff[1024], buff2[1024];
130-
EMAIL_ADDR email_addr;
131130

132131
if (line_length <= 10 || 0 != strncasecmp(cmd_line + 4, " FROM:", 6))
133132
/* syntax error or arguments error*/
@@ -141,7 +140,7 @@ int smtp_cmd_handler_mail(const char* cmd_line, int line_length,
141140
if (g_param.support_starttls && g_param.force_starttls &&
142141
pcontext->connection.ssl == nullptr)
143142
return 520;
144-
parse_mime_addr(&email_addr, buff);
143+
EMAIL_ADDR email_addr(buff);
145144
if (!email_addr.has_addr()) {
146145
/* 550 invalid user - <email_addr> */
147146
smtp_reply_str = resource_get_smtp_code(516, 1, &string_length);
@@ -175,7 +174,6 @@ int smtp_cmd_handler_rcpt(const char* cmd_line, int line_length,
175174
size_t string_length = 0;
176175
const char*smtp_reply_str, *smtp_reply_str2;
177176
char buff[1024], reason[1024], path[256];
178-
EMAIL_ADDR email_addr;
179177

180178
if (line_length <= 8 || 0 != strncasecmp(cmd_line + 4, " TO:", 4))
181179
/* syntax error or arguments error*/
@@ -185,7 +183,7 @@ int smtp_cmd_handler_rcpt(const char* cmd_line, int line_length,
185183
return 520;
186184
memcpy(buff, cmd_line + 8, line_length - 8);
187185
buff[line_length - 8] = '\0';
188-
parse_mime_addr(&email_addr, buff);
186+
EMAIL_ADDR email_addr(buff);
189187
if (!email_addr.has_addr()) {
190188
/* 550 invalid user - <email_addr> */
191189
smtp_reply_str = resource_get_smtp_code(516, 1, &string_length);

tests/utiltest.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ static int t_convert()
7878

7979
static int t_emailaddr()
8080
{
81-
EMAIL_ADDR em;
8281
for (const auto s : {"[email protected]", "<[email protected]>", "\"[email protected]\"", "U D <[email protected]>",
8382
"\"U D\" <[email protected]>", "\"U\\\"D\" <[email protected]>",
8483
"=?utf-8?Q?=C3=A5 D?= <[email protected]>", "\"U D\"", "\"U D\" <>"}) {
8584
printf("%s:\n", s);
86-
em = {};
87-
parse_mime_addr(&em, s);
85+
EMAIL_ADDR em(s);
8886
printf("\tmime: <%s> <%s> <%s>\n", em.display_name, em.local_part, em.domain);
8987
}
9088
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)