]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-imap: imap-envelope: Added function for parsing ENVELOPE from string.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Thu, 29 Dec 2016 13:02:05 +0000 (14:02 +0100)
committerGitLab <gitlab@git.dovecot.net>
Mon, 30 Jan 2017 16:00:26 +0000 (18:00 +0200)
src/lib-imap/imap-envelope.c
src/lib-imap/imap-envelope.h

index 44c4440f7e40329015389e0f74ede1af02a759e3..c23f1bbaefafa5dccf2c3269e6aa95c94e8531a4 100644 (file)
@@ -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
index 4f9425ea554aa368a336b3399d41addef20977ac..f9e4c0bdd2d29c7cb6e60976fc3052d2d3005eca 100644 (file)
@@ -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