From 3b17e4c33af5e8ffeb636dd853053e51cb28463b Mon Sep 17 00:00:00 2001 From: Stephan Bosch Date: Thu, 29 Dec 2016 14:02:05 +0100 Subject: [PATCH] lib-imap: imap-envelope: Added function for parsing ENVELOPE from string. --- src/lib-imap/imap-envelope.c | 42 ++++++++++++++++++++++++++++++++++++ src/lib-imap/imap-envelope.h | 4 ++++ 2 files changed, 46 insertions(+) diff --git a/src/lib-imap/imap-envelope.c b/src/lib-imap/imap-envelope.c index 44c4440f7e..c23f1bbaef 100644 --- a/src/lib-imap/imap-envelope.c +++ b/src/lib-imap/imap-envelope.c @@ -214,3 +214,45 @@ bool imap_envelope_parse_args(const struct imap_arg *args, *envlp_r = envlp; return TRUE; } + +bool imap_envelope_parse(const char *envelope, + pool_t pool, struct message_part_envelope **envlp_r, + const char **error_r) +{ + struct istream *input; + struct imap_parser *parser; + const struct imap_arg *args; + char *error; + int ret; + + input = i_stream_create_from_data(envelope, strlen(envelope)); + (void)i_stream_read(input); + + parser = imap_parser_create(input, NULL, (size_t)-1); + ret = imap_parser_finish_line(parser, 0, + IMAP_PARSE_FLAG_LITERAL_TYPE, &args); + if (ret < 0) { + *error_r = t_strdup_printf("IMAP parser failed: %s", + imap_parser_get_error(parser, NULL)); + } else if (ret == 0) { + *error_r = "Empty envelope"; + ret = -1; + } else { + T_BEGIN { + if (!imap_envelope_parse_args + (args, pool, envlp_r, error_r)) { + error = i_strdup(*error_r); + ret = -1; + } + } T_END; + + if (ret < 0) { + *error_r = t_strdup(error); + i_free(error); + } + } + + imap_parser_unref(&parser); + i_stream_destroy(&input); + return (ret >= 0); +} \ No newline at end of file diff --git a/src/lib-imap/imap-envelope.h b/src/lib-imap/imap-envelope.h index 4f9425ea55..f9e4c0bdd2 100644 --- a/src/lib-imap/imap-envelope.h +++ b/src/lib-imap/imap-envelope.h @@ -12,5 +12,9 @@ void imap_envelope_write(struct message_part_envelope *data, bool imap_envelope_parse_args(const struct imap_arg *args, pool_t pool, struct message_part_envelope **envlp_r, const char **error_r); +/* Parse envelope from string */ +bool imap_envelope_parse(const char *envelope, + pool_t pool, struct message_part_envelope **envlp_r, + const char **error_r); #endif