bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainenpop3c_mail_alloc(struct mailbox_transaction_context *t,
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen struct mailbox_header_lookup_ctx *wanted_headers)
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen index_mail_init(&mail->imail, t, wanted_fields, wanted_headers);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainenstatic void pop3c_mail_close(struct mail *_mail)
120e61ccce21fd33d6b55b6f27346e1690b64bd8Josef 'Jeff' Sipek struct pop3c_mail *pmail = POP3C_MAIL(_mail);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek struct pop3c_mailbox *mbox = POP3C_MAILBOX(_mail->box);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen /* wait for any prefetch to finish before closing the mail */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic int pop3c_mail_get_received_date(struct mail *_mail, time_t *date_r)
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek struct pop3c_mailbox *mbox = POP3C_MAILBOX(_mail->box);
5f4e547bb810403e8cfb19a49d8fe34713507ffdTimo Sirainen if (mbox->storage->set->pop3c_quick_received_date) {
5f4e547bb810403e8cfb19a49d8fe34713507ffdTimo Sirainen /* we don't care about the date, just return the current date */
d2cf6522779802d0edeab7dcf960ffea2f2e1828Timo Sirainen /* FIXME: we could also parse the first Received: header and get
d2cf6522779802d0edeab7dcf960ffea2f2e1828Timo Sirainen the date from there, but since this code is unlikely to be called
d2cf6522779802d0edeab7dcf960ffea2f2e1828Timo Sirainen except during migration, I don't think it really matters. */
d2cf6522779802d0edeab7dcf960ffea2f2e1828Timo Sirainen return index_mail_get_date(_mail, date_r, &tz);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic int pop3c_mail_get_save_date(struct mail *_mail, time_t *date_r)
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
d2cf6522779802d0edeab7dcf960ffea2f2e1828Timo Sirainen /* FIXME: we could use a value stored in cache */
d2cf6522779802d0edeab7dcf960ffea2f2e1828Timo Sirainen return pop3c_mail_get_received_date(_mail, date_r);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic int pop3c_mail_get_physical_size(struct mail *_mail, uoff_t *size_r)
87490012895b4f371635ded00add04c9107dcfefJosef 'Jeff' Sipek struct index_mail *mail = INDEX_MAIL(_mail);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek struct pop3c_mailbox *mbox = POP3C_MAILBOX(_mail->box);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* virtual size is already known. it's the same as our
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen (correct) physical size */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (index_mail_get_physical_size(_mail, size_r) == 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (_mail->lookup_abort == MAIL_LOOKUP_ABORT_READ_MAIL &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen (_mail->box->flags & MAILBOX_FLAG_POP3_SESSION) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* kludge: we want output for POP3 LIST with
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen pop3_fast_size_lookups=yes. use the remote's LIST values
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen regardless of their correctness */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* slow way: get the whole message body */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (mail_get_stream(_mail, &hdr_size, &body_size, &input) < 0)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_assert(mail->data.physical_size != (uoff_t)-1);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void pop3c_mail_cache_size(struct index_mail *mail)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (i_stream_get_size(mail->data.stream, TRUE, &size) <= 0)
cee43a7b0fe3934efc863f533428989173682e0bTimo Sirainen /* it'll be actually added to index when closing the mail in
cee43a7b0fe3934efc863f533428989173682e0bTimo Sirainen index_mail_cache_sizes() */
5f8d497e88fae77fbeb625246bc18260f6775b83Timo Sirainenpop3c_mail_prefetch_done(enum pop3c_command_state state,
f2686912e0156c04296d6dc306f39d61089a1363Timo Sirainen /* let pop3c_mail_get_stream() figure out the error handling.
f2686912e0156c04296d6dc306f39d61089a1363Timo Sirainen in case of a -ERR a retry might even work. */
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainenstatic bool pop3c_mail_prefetch(struct mail *_mail)
120e61ccce21fd33d6b55b6f27346e1690b64bd8Josef 'Jeff' Sipek struct pop3c_mail *pmail = POP3C_MAIL(_mail);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek struct pop3c_mailbox *mbox = POP3C_MAILBOX(_mail->box);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen capa = pop3c_client_get_capabilities(mbox->client);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen pmail->prefetching_body = (capa & POP3C_CAPABILITY_TOP) == 0 ||
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen (pmail->imail.data.access_part & (READ_BODY | PARSE_BODY)) != 0;
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen cmd = t_strdup_printf("RETR %u\r\n", _mail->seq);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen cmd = t_strdup_printf("TOP %u 0\r\n", _mail->seq);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen pop3c_client_cmd_stream_async(mbox->client, cmd,
84a5175b9768da401404635c9b606264585739bdTimo Sirainen i_stream_set_name(pmail->prefetch_stream, t_strcut(cmd, '\r'));
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenpop3c_mail_get_stream(struct mail *_mail, bool get_body,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_size *body_size, struct istream **stream_r)
120e61ccce21fd33d6b55b6f27346e1690b64bd8Josef 'Jeff' Sipek struct pop3c_mail *pmail = POP3C_MAIL(_mail);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek struct pop3c_mailbox *mbox = POP3C_MAILBOX(_mail->box);
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen if ((mail->data.access_part & (READ_BODY | PARSE_BODY)) != 0)
afc77c5375cdb8f2bf0ab6280d9229ac27c933c6Timo Sirainen /* wait for prefetch to finish */
84a5175b9768da401404635c9b606264585739bdTimo Sirainen if (pmail->prefetch_stream != NULL && mail->data.stream == NULL) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* we've fetched the body */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen /* we've fetched the header, but we need the body
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_panic("Unexpected POP3 stream name: %s", name);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen capa = pop3c_client_get_capabilities(mbox->client);
0d6ae58916bee3452c91d9d81be72227761ec33dTimo Sirainen if (get_body || (capa & POP3C_CAPABILITY_TOP) == 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen cmd = t_strdup_printf("RETR %u\r\n", _mail->seq);
9f0a996c22ebe39dcfe5cb84c8fd2f22ef5ce9d8Timo Sirainen cmd = t_strdup_printf("TOP %u 0\r\n", _mail->seq);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (pop3c_client_cmd_stream(mbox->client, cmd,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MAIL_ERROR_TEMP : MAIL_ERROR_EXPUNGED, error);
dc7a24adeabfc9372a9d5fe34e7f8761e325ed68Timo Sirainen i_stream_set_name(mail->data.stream, t_strcut(cmd, '\r'));
60d1fdf2c17fd0c7020234590dbd73da81c3ce8fTimo Sirainen /* if this stream is used by some filter stream, make the
60d1fdf2c17fd0c7020234590dbd73da81c3ce8fTimo Sirainen filter stream blocking */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen return index_mail_init_stream(mail, hdr_size, body_size, stream_r);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenpop3c_mail_get_special(struct mail *_mail, enum mail_fetch_field field,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen const char **value_r)
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek struct pop3c_mailbox *mbox = POP3C_MAILBOX(_mail->box);