From a2090250829fe8989be2afc8cf41ba2a022072fc Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 23 Feb 2017 20:01:30 +0100 Subject: [PATCH] wks: New callback for the mime parser. * tools/mime-parser.c (mime_parser_context_s): New field 't2body'. (parse_message_cb): Call that callback. (mime_parser_set_t2body): New. -- This callback allows to process header values even on the outer level. Signed-off-by: Werner Koch --- tools/mime-parser.c | 25 +++++++++++++++++++++++++ tools/mime-parser.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/tools/mime-parser.c b/tools/mime-parser.c index 264353c19..169ea2b1e 100644 --- a/tools/mime-parser.c +++ b/tools/mime-parser.c @@ -49,6 +49,9 @@ struct mime_parser_context_s { void *cookie; /* Cookie passed to all callbacks. */ + /* The callback to announce the transation from header to body. */ + gpg_error_t (*t2body) (void *cookie, int level); + /* The callback to announce a new part. */ gpg_error_t (*new_part) (void *cookie, const char *mediatype, @@ -224,6 +227,14 @@ parse_message_cb (void *opaque, rfc822parse_event_t event, rfc822parse_t msg) ctx->want_part = 0; ctx->decode_part = 0; + + if (ctx->t2body) + { + rc = ctx->t2body (ctx->cookie, ctx->nesting_level); + if (rc) + goto t2body_leave; + } + field = rfc822parse_parse_field (msg, "Content-Type", -1); if (field) { @@ -412,6 +423,7 @@ parse_message_cb (void *opaque, rfc822parse_event_t event, rfc822parse_t msg) } } + t2body_leave: ctx->show.header = 0; ctx->show.data = 1; ctx->show.n_skip = 1; @@ -541,6 +553,19 @@ mime_parser_set_verbose (mime_parser_t ctx, int level) } +/* Set a callback for the transition from header to body. LEVEL is + * the current nesting level, starting with 0. This callback can be + * used to evaluate headers before any other action is done. Note + * that if a new NEW_PART callback needs to be called it is done after + * this T2BODY callback. */ +void +mime_parser_set_t2body (mime_parser_t ctx, + gpg_error_t (*fnc) (void *cookie, int level)) +{ + ctx->t2body = fnc; +} + + /* Set the callback used to announce a new part. It will be called * with the media type and media subtype of the part. If no * Content-type header was given both values are the empty string. diff --git a/tools/mime-parser.h b/tools/mime-parser.h index 37a74a153..b9bb46575 100644 --- a/tools/mime-parser.h +++ b/tools/mime-parser.h @@ -27,6 +27,8 @@ gpg_error_t mime_parser_new (mime_parser_t *r_ctx, void *cookie); void mime_parser_release (mime_parser_t ctx); void mime_parser_set_verbose (mime_parser_t ctx, int level); +void mime_parser_set_t2body (mime_parser_t ctx, + gpg_error_t (*fnc) (void *cookie, int level)); void mime_parser_set_new_part (mime_parser_t ctx, gpg_error_t (*fnc) (void *cookie, const char *mediatype,