pop3c-mail.c revision afc77c5375cdb8f2bf0ab6280d9229ac27c933c6
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
de754cb78f75e8b3b994cddafe41c9ed1467c33dTimo Sirainenpop3c_mail_alloc(struct mailbox_transaction_context *t,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mailbox_header_lookup_ctx *wanted_headers)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen index_mail_init(&mail->imail, t, wanted_fields, wanted_headers);
2e78f05b11df23ec2731afaf8f19d5b5240cb29fTimo Sirainenstatic void pop3c_mail_close(struct mail *_mail)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mail *pmail = (struct pop3c_mail *)_mail;
d1e7425048c61d71f41f737ba947687198842dc2Timo Sirainen struct pop3c_mailbox *mbox = (struct pop3c_mailbox *)_mail->box;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* wait for any prefetch to finish before closing the mail */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic int pop3c_mail_get_received_date(struct mail *_mail, time_t *date_r)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mailbox *mbox = (struct pop3c_mailbox *)_mail->box;
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen if (mbox->storage->set->pop3c_quick_received_date) {
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen /* we don't care about the date, just return the current date */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* FIXME: we could also parse the first Received: header and get
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen the date from there, but since this code is unlikely to be called
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen except during migration, I don't think it really matters. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return index_mail_get_date(_mail, date_r, &tz);
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainenstatic int pop3c_mail_get_save_date(struct mail *_mail, time_t *date_r)
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* FIXME: we could use a value stored in cache */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return pop3c_mail_get_received_date(_mail, date_r);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic int pop3c_mail_get_physical_size(struct mail *_mail, uoff_t *size_r)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct index_mail *mail = (struct index_mail *)_mail;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mailbox *mbox = (struct pop3c_mailbox *)_mail->box;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* virtual size is already known. it's the same as our
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen (correct) physical size */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (index_mail_get_physical_size(_mail, size_r) == 0) {
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen if (_mail->lookup_abort == MAIL_LOOKUP_ABORT_READ_MAIL &&
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen (_mail->box->flags & MAILBOX_FLAG_POP3_SESSION) != 0) {
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen /* kludge: we want output for POP3 LIST with
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen pop3_fast_size_lookups=yes. use the remote's LIST values
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen regardless of their correctness */
009217abb57a24a4076092e8e4e165545747839eStephan Bosch /* slow way: get the whole message body */
009217abb57a24a4076092e8e4e165545747839eStephan Bosch if (mail_get_stream(_mail, &hdr_size, &body_size, &input) < 0)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_assert(mail->data.physical_size != (uoff_t)-1);
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Boschstatic void pop3c_mail_cache_size(struct index_mail *mail)
c93aca832ee532010ead91b85fa9f614132e1be2Stephan Bosch if (i_stream_get_size(mail->data.stream, TRUE, &size) <= 0)
1093de32efb2a231949566d4bd8aa55a8f43fb70Timo Sirainen cache_idx = mail->ibox->cache_fields[MAIL_CACHE_VIRTUAL_FULL_SIZE].idx;
ab281fc992907b6cf6c730f672dc3aa4c6685015Timo Sirainen if (mail_cache_field_exists(_mail->transaction->cache_view,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen index_mail_cache_add_idx(mail, cache_idx, &size, sizeof(size));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* make sure it's not cached twice */
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainenstatic void pop3c_mail_prefetch_done(enum pop3c_command_state state,
2e2a1d720ed53490e8e5c5031e773d395bd5683dTimo Sirainen i_stream_create_error_str(EIO, "%s failed: %s",
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainen pmail->prefetching_body ? "RETR" : "TOP", reply);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstatic bool pop3c_mail_prefetch(struct mail *_mail)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mail *pmail = (struct pop3c_mail *)_mail;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mailbox *mbox = (struct pop3c_mailbox *)_mail->box;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *cmd;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen capa = pop3c_client_get_capabilities(mbox->client);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen pmail->prefetching_body = (capa & POP3C_CAPABILITY_TOP) == 0 ||
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen (pmail->imail.data.access_part & (READ_BODY | PARSE_BODY)) != 0;
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen cmd = t_strdup_printf("RETR %u\r\n", _mail->seq);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen cmd = t_strdup_printf("TOP %u 0\r\n", _mail->seq);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen pop3c_client_cmd_stream_async(mbox->client, cmd,
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen i_stream_set_name(pmail->imail.data.stream, t_strcut(cmd, '\r'));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenpop3c_mail_get_stream(struct mail *_mail, bool get_body,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct message_size *body_size, struct istream **stream_r)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mail *pmail = (struct pop3c_mail *)_mail;
ec23e16ed879e289d12c6e1a5f9745dd3979004aTimo Sirainen struct pop3c_mailbox *mbox = (struct pop3c_mailbox *)_mail->box;
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen if ((mail->data.access_part & (READ_BODY | PARSE_BODY)) != 0)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* wait for prefetch to finish */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we've fetched the body */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* we've fetched the header, but we need the body
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen i_panic("Unexpected POP3 stream name: %s", name);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen capa = pop3c_client_get_capabilities(mbox->client);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (get_body || (capa & POP3C_CAPABILITY_TOP) == 0) {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cmd = t_strdup_printf("RETR %u\r\n", _mail->seq);
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen cmd = t_strdup_printf("TOP %u 0\r\n", _mail->seq);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (pop3c_client_cmd_stream(mbox->client, cmd,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen MAIL_ERROR_TEMP : MAIL_ERROR_EXPUNGED, error);
e911b23f3e05308df9b98b1a3fdaf72e4302d8fdTimo Sirainen i_stream_set_name(mail->data.stream, t_strcut(cmd, '\r'));
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* if this stream is used by some filter stream, make the
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen filter stream blocking */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen return index_mail_init_stream(mail, hdr_size, body_size, stream_r);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenpop3c_mail_get_special(struct mail *_mail, enum mail_fetch_field field,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char **value_r)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct pop3c_mailbox *mbox = (struct pop3c_mailbox *)_mail->box;