imapc-mail-fetch.c revision 86520ddddf88bce4bb27b3c6e3e5a2375d7b2bdb
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen/* Copyright (c) 2011 Dovecot authors, see the included COPYING file */
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainenimapc_mail_prefetch_callback(const struct imapc_command_reply *reply,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen (struct imapc_mailbox *)mail->imail.mail.mail.box;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen unsigned int i, count;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen fetch_mails = array_get(&mbox->fetch_mails, &count);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen for (i = 0; i < count; i++) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen else if (reply->state == IMAPC_COMMAND_STATE_NO) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen imapc_copy_error_from_reply(mbox->storage, MAIL_ERROR_PARAMS,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen mail_storage_set_critical(&mbox->storage->storage,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen "imapc: Command failed: %s", reply->text_full);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainenimapc_mail_send_fetch(struct mail *_mail, enum mail_fetch_field fields)
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen struct imapc_mail *mail = (struct imapc_mail *)_mail;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen struct imapc_mailbox *mbox = (struct imapc_mailbox *)_mail->box;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen if (_mail->lookup_abort != MAIL_LOOKUP_ABORT_NEVER)
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen /* drop any fields that we may already be fetching currently */
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen str_printfa(str, "UID FETCH %u (", _mail->uid);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen else if ((fields & MAIL_FETCH_STREAM_HEADER) != 0)
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen struct imapc_mail *mail = (struct imapc_mail *)_mail;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen struct index_mail_data *data = &mail->imail.data;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen if ((mail->imail.wanted_fields & MAIL_FETCH_RECEIVED_DATE) != 0 &&
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen if (data->stream == NULL && data->access_part != 0) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen if ((data->access_part & (READ_BODY | PARSE_BODY)) != 0)
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainenimapc_mail_have_fields(struct imapc_mail *imail, enum mail_fetch_field fields)
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainen if ((fields & MAIL_FETCH_RECEIVED_DATE) != 0) {
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainen if (imail->imail.data.received_date == (time_t)-1)
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainen fields &= ~(MAIL_FETCH_STREAM_HEADER | MAIL_FETCH_STREAM_BODY);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainenint imapc_mail_fetch(struct mail *_mail, enum mail_fetch_field fields)
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen struct imapc_mail *imail = (struct imapc_mail *)_mail;
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainen /* we'll continue waiting until we've got all the fields we wanted,
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainen or until all FETCH replies have been received (i.e. some FETCHes
9ed77dd00248e88d731ec129116db5dddc0dd3b5Timo Sirainen while (!imapc_mail_have_fields(imail, fields) && imail->fetch_count > 0)
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainenstatic bool imapc_find_lfile_arg(const struct imapc_untagged_reply *reply,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen unsigned int i, count;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen for (i = 0; i < reply->file_args_count; i++) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen const struct imapc_arg_file *farg = &reply->file_args[i];
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen imap_arg_get_list_full(arg->parent, &list, &count) &&
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen farg->list_idx < count && &list[farg->list_idx] == arg) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen /* maybe the existing stream has no body. replace it. */
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen imail->data.stream = i_stream_create_fd(fd, 0, TRUE);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen mail->body = buffer_create_dynamic(default_pool,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen buffer_append(mail->body, value, arg->str_len);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen imail->data.stream = i_stream_create_from_data(mail->body->data,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen t_strdup_printf("imapc mail uid=%u", _mail->uid));
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen index_mail_set_read_buffer_size(_mail, imail->data.stream);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen } else if (body) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen ret = i_stream_get_size(imail->data.stream, TRUE, &size);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen /* we'll assume that the remote server is working properly and
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen sending CRLF linefeeds */
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen if (index_mail_init_stream(imail, NULL, NULL, &input) < 0)
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainenvoid imapc_mail_fetch_update(struct imapc_mail *mail,
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen (struct imapc_mailbox *)mail->imail.mail.mail.box;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen unsigned int i;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen for (i = 0; args[i].type != IMAP_ARG_EOL; i += 2) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen imapc_fetch_stream(mail, reply, &args[i+1], TRUE);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen } else if (strcasecmp(key, "BODY[HEADER]") == 0) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen imapc_fetch_stream(mail, reply, &args[i+1], FALSE);
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen } else if (strcasecmp(key, "INTERNALDATE") == 0) {
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen if (imap_arg_get_astring(&args[i+1], &value) &&
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen /* this is only a FETCH FLAGS update for the wanted mail */