imap-fetch-body.c revision defeb23b40f1c1af0535a84529383825e5ef8dfe
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2002-2015 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *section; /* NOTE: always uppercased */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void fetch_read_error(struct imap_fetch_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (state->cur_input->stream_errno == ENOENT) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen *disconnect_reason_r = "Mail expunged while it was being FETCHed";
7fc0ca6c1f664de6506afa816200d115f9f80391Timo Sirainen mail_storage_set_critical(state->cur_mail->box->storage,
7fc0ca6c1f664de6506afa816200d115f9f80391Timo Sirainen "read(%s) failed: %s (FETCH %s for mailbox %s UID %u)",
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_get_vname(state->cur_mail->box), state->cur_mail->uid);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainenstatic const char *get_body_name(const struct imap_fetch_body_data *body)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen imap_msgpart_get_partial_offset(body->msgpart));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic string_t *get_prefix(struct imap_fetch_state *state,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_printfa(str, " ~{%"PRIuUOFF_T"}\r\n", size);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen str_printfa(str, " {%"PRIuUOFF_T"}\r\n", size);
7fc0ca6c1f664de6506afa816200d115f9f80391Timo Sirainenstatic int fetch_stream_continue(struct imap_fetch_context *ctx)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen o_stream_set_max_buffer_size(ctx->client->output, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = o_stream_send_istream(ctx->client->output, state->cur_input);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen o_stream_set_max_buffer_size(ctx->client->output, (size_t)-1);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* unfinished */
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen client_disconnect(ctx->client, disconnect_reason);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (!i_stream_have_bytes_left(state->cur_input)) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen /* Input stream gave less data than expected */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("read(%s): FETCH %s for mailbox %s UID %u "
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "got too little data: "
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen client_disconnect(ctx->client, "FETCH failed");
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainen /* client probably disconnected */
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainen o_stream_set_flush_pending(ctx->client->output, TRUE);
8f90ef65db62946aabe1969755edcdefb4eb430aTimo Sirainenstatic const char *
8f90ef65db62946aabe1969755edcdefb4eb430aTimo Sirainenget_body_human_name(pool_t pool, struct imap_fetch_body_data *body)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen partial_offset = imap_msgpart_get_partial_offset(body->msgpart);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen partial_size = imap_msgpart_get_partial_size(body->msgpart);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (partial_offset != 0 || partial_size != (uoff_t)-1) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen str_printfa(str, "<%"PRIuUOFF_T, partial_offset);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen str_printfa(str, ".%"PRIuUOFF_T, partial_size);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic void fetch_state_update_stats(struct imap_fetch_context *ctx,
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainen ctx->state.cur_stats_sizep = &ctx->client->fetch_hdr_bytes;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->state.cur_stats_sizep = &ctx->client->fetch_body_bytes;
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainenstatic int fetch_body_msgpart(struct imap_fetch_context *ctx, struct mail *mail,
825b0e819a7c48a366ddca23ec78b87e8c30e9b4Aki Tuomi if (imap_msgpart_open(mail, body->msgpart, &result) < 0)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->state.cur_size_field = result.size_field;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->state.cur_human_name = get_body_human_name(ctx->ctx_pool, body);
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen str = get_prefix(&ctx->state, body, ctx->state.cur_size,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen o_stream_nsend(ctx->client->output, str_data(str), str_len(str));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->state.cont_handler = fetch_stream_continue;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic int fetch_binary_size(struct imap_fetch_context *ctx, struct mail *mail,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (imap_msgpart_size(mail, body->msgpart, &size) < 0)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen str_printfa(str, "%s %"PRIuUOFF_T, get_body_name(body), size);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (o_stream_send(ctx->client->output, str_data(str), str_len(str)) < 0)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen/* Parse next digits in string into integer. Returns -1 if the integer
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainen becomes too big and wraps. */
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstatic int read_uoff_t(const char **p, uoff_t *value)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenbody_header_fields_parse(struct imap_fetch_init_context *ctx,
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct imap_fetch_body_data *body, const char *prefix,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct imap_arg *args, unsigned int args_count)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < args_count; i++) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (!imap_arg_get_astring(&args[i], &value)) {
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainen "Header list contains non-strings";
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenstatic int body_parse_partial(struct imap_fetch_body_data *body,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *p, const char **error_r)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (*p == '\0')
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* <start.end> */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (*p != '<') {
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch if (read_uoff_t(&p, &offset) < 0 || offset > OFF_T_MAX) {
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch /* wrapped */
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen if (*p == '.') {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (read_uoff_t(&p, &size) < 0 || size > OFF_T_MAX) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* wrapped */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (*p != '>') {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen imap_msgpart_set_partial(body->msgpart, offset, size);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenbool imap_fetch_body_section_init(struct imap_fetch_init_context *ctx)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen body = p_new(ctx->pool, struct imap_fetch_body_data, 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (*p != '[') {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->error = "Invalid BODY[..] parameter: Missing '['";
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (imap_arg_get_list_full(&ctx->args[0], &list_args, &list_count)) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen /* BODY[HEADER.FIELDS.. (headers list)] */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (!imap_arg_get_atom(&ctx->args[1], &str) ||
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ctx->error = "Invalid BODY[..] parameter: Missing ']'";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* no headers list */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->error = "Invalid BODY[..] parameter: Missing ']'";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen body->section = p_strdup_until(ctx->pool, body->section, p);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (imap_msgpart_parse(body->section, &body->msgpart) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen imap_msgpart_get_wanted_headers(body->msgpart, &ctx->fetch_ctx->all_headers);
a406615fbcef37b1d12f0be95a70abf23238c5efTimo Sirainen if (body_parse_partial(body, p, &error) < 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* update the section name for the imap_fetch_add_handler() */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->name = p_strdup(ctx->pool, get_body_name(body));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen imap_fetch_add_handler(ctx, IMAP_FETCH_HANDLER_FLAG_WANT_DEINIT,
a406615fbcef37b1d12f0be95a70abf23238c5efTimo Sirainenbool imap_fetch_binary_init(struct imap_fetch_init_context *ctx)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_assert(strncmp(ctx->name, "BINARY", 6) == 0);
42270b2d8d9bb0d04b16e0ead727154b32399a3fTimo Sirainen body = p_new(ctx->pool, struct imap_fetch_body_data, 1);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* fetch decoded size of the section */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (*p != '[') {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->error = "Invalid BINARY[..] parameter: Missing '['";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (imap_arg_get_list_full(&ctx->args[0], &list_args, &list_count)) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* BINARY[HEADER.FIELDS.. (headers list)] */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!imap_arg_get_atom(&ctx->args[1], &str) ||
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->error = "Invalid BINARY[..] parameter: Missing ']'";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* no headers list */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->error = "Invalid BINARY[..] parameter: Missing ']'";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen body->section = p_strdup_until(ctx->pool, body->section, p);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (imap_msgpart_parse(body->section, &body->msgpart) < 0) {
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen imap_msgpart_set_decode_to_binary(body->msgpart);
a406615fbcef37b1d12f0be95a70abf23238c5efTimo Sirainen if (body_parse_partial(body, p, &error) < 0) {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen /* update the section name for the imap_fetch_add_handler() */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->name = p_strdup(ctx->pool, get_body_name(body));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen imap_fetch_add_handler(ctx, IMAP_FETCH_HANDLER_FLAG_WANT_DEINIT,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen imap_fetch_add_handler(ctx, IMAP_FETCH_HANDLER_FLAG_WANT_DEINIT,
817d027593510c3ba70ad542ce0011f5f6916d1eTimo Sirainenfetch_rfc822_size(struct imap_fetch_context *ctx, struct mail *mail,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen str_printfa(ctx->state.cur_str, "RFC822.SIZE %"PRIuUOFF_T" ", size);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenfetch_and_free_msgpart(struct imap_fetch_context *ctx,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail *mail, struct imap_msgpart **_msgpart)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = imap_msgpart_open(mail, *_msgpart, &result);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ctx->state.cur_size_field = result.size_field;
3e1d94509fd76c56f2eb54f98ea02ade9ac2d44aTimo Sirainen ctx->state.cont_handler = fetch_stream_continue;
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainenfetch_rfc822(struct imap_fetch_context *ctx, struct mail *mail,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *str;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (fetch_and_free_msgpart(ctx, mail, &msgpart) < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str = t_strdup_printf(" RFC822 {%"PRIuUOFF_T"}\r\n",
fde0b1793a2842da00eaa105d5e13fec465f0443Timo Sirainenfetch_rfc822_header(struct imap_fetch_context *ctx,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *str;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (fetch_and_free_msgpart(ctx, mail, &msgpart) < 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen str = t_strdup_printf(" RFC822.HEADER {%"PRIuUOFF_T"}\r\n",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenfetch_rfc822_text(struct imap_fetch_context *ctx, struct mail *mail,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *str;
e3736b5d480878031c386ac55d201fcf08e68766Timo Sirainen if (fetch_and_free_msgpart(ctx, mail, &msgpart) < 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen str = t_strdup_printf(" RFC822.TEXT {%"PRIuUOFF_T"}\r\n",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainenbool imap_fetch_rfc822_init(struct imap_fetch_init_context *ctx)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->fetch_ctx->fetch_data |= MAIL_FETCH_STREAM_HEADER |
a21823d90cee6a18aeab0378637472c7e3fbbab2Timo Sirainen ctx->fetch_ctx->fetch_data |= MAIL_FETCH_VIRTUAL_SIZE;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen imap_fetch_add_handler(ctx, IMAP_FETCH_HANDLER_FLAG_BUFFERED,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->fetch_ctx->fetch_data |= MAIL_FETCH_STREAM_HEADER;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->fetch_ctx->fetch_data |= MAIL_FETCH_STREAM_BODY;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ctx->error = t_strconcat("Unknown parameter ", name, NULL);