]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
fts: Parser move try_init arguments to a single structure parser_context.
authorSergey Kitov <sergey.kitov@open-xchange.com>
Mon, 9 Oct 2017 08:23:54 +0000 (11:23 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 11 Dec 2017 08:51:49 +0000 (10:51 +0200)
src/plugins/fts/fts-build-mail.c
src/plugins/fts/fts-parser-html.c
src/plugins/fts/fts-parser-script.c
src/plugins/fts/fts-parser-tika.c
src/plugins/fts/fts-parser.c
src/plugins/fts/fts-parser.h
src/plugins/fts/xml2text.c

index 0766c67d2949b14a604f62875fcd42cb95a891df..6999f1bae4b7bcc3411c6c47dd50464124e32ab7 100644 (file)
@@ -216,7 +216,7 @@ fts_build_body_begin(struct fts_mail_build_context *ctx,
                     struct message_part *part, bool *binary_body_r)
 {
        struct mail_storage *storage;
-       const char *content_type;
+       struct fts_parser_context parser_context;
        struct fts_backend_build_key key;
 
        i_assert(ctx->body_parser == NULL);
@@ -226,23 +226,24 @@ fts_build_body_begin(struct fts_mail_build_context *ctx,
        key.uid = ctx->mail->uid;
        key.part = part;
 
-       content_type = ctx->content_type != NULL ?
+       i_zero(&parser_context);
+       parser_context.content_type = ctx->content_type != NULL ?
                ctx->content_type : "text/plain";
-       if (strncmp(content_type, "multipart/", 10) == 0) {
+       if (strncmp(parser_context.content_type, "multipart/", 10) == 0) {
                /* multiparts are never indexed, only their contents */
                return FALSE;
        }
+       storage = mailbox_get_storage(ctx->mail->box);
+       parser_context.user = mail_storage_get_user(storage);
+       parser_context.content_disposition = ctx->content_disposition;
 
        
-       storage = mailbox_get_storage(ctx->mail->box);
-       if (fts_parser_init(mail_storage_get_user(storage),
-                           content_type, ctx->content_disposition,
-                           &ctx->body_parser)) {
+       if (fts_parser_init(&parser_context, &ctx->body_parser)) {
                /* extract text using the the returned parser */
                *binary_body_r = TRUE;
                key.type = FTS_BACKEND_BUILD_KEY_BODY_PART;
-       } else if (strncmp(content_type, "text/", 5) == 0 ||
-                  strncmp(content_type, "message/", 8) == 0) {
+       } else if (strncmp(parser_context.content_type, "text/", 5) == 0 ||
+                  strncmp(parser_context.content_type, "message/", 8) == 0) {
                /* text body parts */
                key.type = FTS_BACKEND_BUILD_KEY_BODY_PART;
                ctx->body_parser = fts_parser_text_init();
@@ -254,7 +255,7 @@ fts_build_body_begin(struct fts_mail_build_context *ctx,
                *binary_body_r = TRUE;
                key.type = FTS_BACKEND_BUILD_KEY_BODY_PART_BINARY;
        }
-       key.body_content_type = content_type;
+       key.body_content_type = parser_context.content_type;
        key.body_content_disposition = ctx->content_disposition;
        ctx->cur_user_lang = NULL;
        if (!fts_backend_update_set_build_key(ctx->update_ctx, &key)) {
index f85d92bf40fd257f1a3eefa61c15c3da6bb37541..e313fd13fca6a4a3c9b816372e492d172325c0f4 100644 (file)
@@ -13,13 +13,11 @@ struct html_fts_parser {
 };
 
 static struct fts_parser *
-fts_parser_html_try_init(struct mail_user *user ATTR_UNUSED,
-                        const char *content_type,
-                        const char *content_disposition ATTR_UNUSED)
+fts_parser_html_try_init(struct fts_parser_context *parser_context)
 {
        struct html_fts_parser *parser;
 
-       if (!mail_html2text_content_type_match(content_type))
+       if (!mail_html2text_content_type_match(parser_context->content_type))
                return NULL;
 
        parser = i_new(struct html_fts_parser, 1);
index e8b1afe5c353d34c602294d9b7e379b67520a026..4238d737fb541998f16c91befecd39bbab1a53e9 100644 (file)
@@ -194,22 +194,20 @@ static void parse_content_disposition(const char *content_disposition,
 }
 
 static struct fts_parser *
-fts_parser_script_try_init(struct mail_user *user,
-                          const char *content_type,
-                          const char *content_disposition)
+fts_parser_script_try_init(struct fts_parser_context *parser_context)
 {
        struct script_fts_parser *parser;
        const char *filename, *path, *cmd;
        int fd;
 
-       parse_content_disposition(content_disposition, &filename);
-       if (!script_support_content(user, &content_type, filename))
+       parse_content_disposition(parser_context->content_disposition, &filename);
+       if (!script_support_content(parser_context->user, &parser_context->content_type, filename))
                return NULL;
 
-       fd = script_connect(user, &path);
+       fd = script_connect(parser_context->user, &path);
        if (fd == -1)
                return NULL;
-       cmd = t_strdup_printf(SCRIPT_HANDSHAKE"%s\n\n", content_type);
+       cmd = t_strdup_printf(SCRIPT_HANDSHAKE"%s\n\n", parser_context->content_type);
        if (write_full(fd, cmd, strlen(cmd)) < 0) {
                i_error("write(%s) failed: %m", path);
                i_close_fd(&fd);
index d35a3d6517867f77f8630b220e90f9ee972bc155..9353a2cd872fdb71c34a81e9036ead2cee3f76de 100644 (file)
@@ -138,19 +138,18 @@ fts_tika_parser_response(const struct http_response *response,
 }
 
 static struct fts_parser *
-fts_parser_tika_try_init(struct mail_user *user, const char *content_type,
-                        const char *content_disposition)
+fts_parser_tika_try_init(struct fts_parser_context *parser_context)
 {
        struct tika_fts_parser *parser;
        struct http_url *http_url;
        struct http_client_request *http_req;
 
-       if (tika_get_http_client_url(user, &http_url) < 0)
+       if (tika_get_http_client_url(parser_context->user, &http_url) < 0)
                return NULL;
 
        parser = i_new(struct tika_fts_parser, 1);
        parser->parser.v = fts_parser_tika;
-       parser->user = user;
+       parser->user = parser_context->user;
 
        http_req = http_client_request(tika_http_client, "PUT",
                        http_url->host.name,
@@ -158,12 +157,12 @@ fts_parser_tika_try_init(struct mail_user *user, const char *content_type,
                        fts_tika_parser_response, parser);
        http_client_request_set_port(http_req, http_url->port);
        http_client_request_set_ssl(http_req, http_url->have_ssl);
-       if (content_type != NULL)
+       if (parser_context->content_type != NULL)
                http_client_request_add_header(http_req, "Content-Type",
-                                              content_type);
-       if (content_disposition != NULL)
+                                              parser_context->content_type);
+       if (parser_context->content_disposition != NULL)
                http_client_request_add_header(http_req, "Content-Disposition",
-                                              content_disposition);
+                                              parser_context->content_disposition);
        http_client_request_add_header(http_req, "Accept", "text/plain");
 
        parser->http_req = http_req;
index 59b20b50aeb6051e7b039f99c9f8e390d15771ee..cfb504f41003544f34982c868cecf651eee1240e 100644 (file)
@@ -20,21 +20,21 @@ static const char *plaintext_content_types[] = {
        NULL
 };
 
-bool fts_parser_init(struct mail_user *user,
-                    const char *content_type, const char *content_disposition,
+bool fts_parser_init(struct fts_parser_context *parser_context,
                     struct fts_parser **parser_r)
 {
        unsigned int i;
+       i_assert(parser_context->user != NULL);
+       i_assert(parser_context->content_type != NULL);
 
-       if (str_array_find(plaintext_content_types, content_type)) {
+       if (str_array_find(plaintext_content_types, parser_context->content_type)) {
                /* we probably don't want/need to allow parsers to handle
                   plaintext? */
                return FALSE;
        }
 
        for (i = 0; i < N_ELEMENTS(parsers); i++) {
-               *parser_r = parsers[i]->try_init(user, content_type,
-                                                content_disposition);
+               *parser_r = parsers[i]->try_init(parser_context);
                if (*parser_r != NULL)
                        return TRUE;
        }
index 221b29f1c7ab713cb62511f23897453e7188d60c..b5bca5ede7ae7c744c8b39d055d8549a6e9c1a62 100644 (file)
@@ -4,10 +4,16 @@
 struct message_block;
 struct mail_user;
 
+struct fts_parser_context {
+       /* Can't be NULL */
+       struct mail_user *user;
+       /* Can't be NULL */
+       const char *content_type;
+       const char *content_disposition;
+};
+
 struct fts_parser_vfuncs {
-       struct fts_parser *(*try_init)(struct mail_user *user,
-                                      const char *content_type,
-                                      const char *content_disposition);
+       struct fts_parser *(*try_init)(struct fts_parser_context *parser_context);
        void (*more)(struct fts_parser *parser, struct message_block *block);
        int (*deinit)(struct fts_parser *parser);
        void (*unload)(void);
@@ -22,8 +28,7 @@ extern struct fts_parser_vfuncs fts_parser_html;
 extern struct fts_parser_vfuncs fts_parser_script;
 extern struct fts_parser_vfuncs fts_parser_tika;
 
-bool fts_parser_init(struct mail_user *user,
-                    const char *content_type, const char *content_disposition,
+bool fts_parser_init(struct fts_parser_context *parser_context,
                     struct fts_parser **parser_r);
 struct fts_parser *fts_parser_text_init(void);
 
index d3d77830deaec451fc93333f6cecd9b7cd67bedb..1071570d1c57ad3272e481eabedc47721662fbdb 100644 (file)
@@ -12,10 +12,11 @@ int main(void)
        unsigned char buf[IO_BLOCK_SIZE];
        struct message_block block;
        ssize_t ret;
+       struct fts_parser_context parser_context = {.content_type = "text/html"};
 
        lib_init();
 
-       parser = fts_parser_html.try_init(NULL, "text/html", NULL);
+       parser = fts_parser_html.try_init(&parser_context);
        i_assert(parser != NULL);
 
        i_zero(&block);