bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschint imap_msgpart_url_create(struct mail_user *user, const struct imap_url *url,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch const char **error_r)
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch const char *section = url->section == NULL ? "" : url->section;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (imap_msgpart_parse(section, &msgpart) < 0) {
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch imap_msgpart_set_partial(msgpart, url->partial_offset,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_parse(struct mail_user *user, struct mailbox *selected_box,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch const char *urlstr, struct imap_msgpart_url **mpurl_r,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen const char **error_r)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* build base url */
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_get_open_status(selected_box, STATUS_UIDVALIDITY,
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch base_url.mailbox = mailbox_get_vname(selected_box);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch base_url.uidvalidity = box_status.uidvalidity;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* parse url */
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen IMAP_URL_PARSE_REQUIRE_RELATIVE, &url, &error) < 0) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = t_strconcat("Invalid IMAP URL: ", error, NULL);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = "Mailbox-relative IMAP URL, but no mailbox selected";
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (imap_msgpart_url_create(user, url, mpurl_r, error_r) < 0)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Boschstruct mailbox *imap_msgpart_url_get_mailbox(struct imap_msgpart_url *mpurl)
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_open_mailbox(struct imap_msgpart_url *mpurl,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch struct mailbox **box_r, enum mail_error *error_code_r,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch const char **error_r)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch enum mailbox_flags flags = MAILBOX_FLAG_READONLY;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* find mailbox namespace */
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch ns = mail_namespace_find(mpurl->user->namespaces, mpurl->mailbox);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* open mailbox */
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen mailbox_equals(mpurl->selected_box, ns, mpurl->mailbox))
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen box = mailbox_alloc(ns->list, mpurl->mailbox, flags);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = mail_storage_get_last_error(mailbox_get_storage(box),
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch return *error_code_r == MAIL_ERROR_TEMP ? -1 : 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* verify UIDVALIDITY */
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_get_open_status(box, STATUS_UIDVALIDITY, &box_status);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch box_status.uidvalidity != mpurl->uidvalidity) {
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_open_mail(struct imap_msgpart_url *mpurl,
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* open mailbox if it is not yet open */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if ((ret = imap_msgpart_url_open_mailbox(mpurl, &box, &error_code,
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* start transaction */
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi t = mailbox_transaction_begin(box, 0, __func__);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen mail = mail_alloc(t, MAIL_FETCH_MESSAGE_PARTS |
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* find the message */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschimap_msgpart_url_get_part(struct imap_msgpart_url *mpurl)
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschvoid imap_msgpart_url_set_decode_to_binary(struct imap_msgpart_url *mpurl)
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch imap_msgpart_set_decode_to_binary(mpurl->part);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_read_part(struct imap_msgpart_url *mpurl,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen const char **error_r)
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen /* open mail if it is not yet open */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* open the referenced part as a stream */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch ret = imap_msgpart_open(mail, mpurl->part, result_r);
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *error_r = mailbox_get_last_error(mpurl->box, NULL);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_verify(struct imap_msgpart_url *mpurl,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen const char **error_r)
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen /* open mail if it is not yet open */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainenint imap_msgpart_url_get_bodypartstructure(struct imap_msgpart_url *mpurl,
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen const char **error_r)
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen /* open mail if it is not yet open */
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen ret = imap_msgpart_bodypartstructure(mail, mpurl->part, bpstruct_r);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen *error_r = mailbox_get_last_error(mpurl->box, NULL);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen else if (ret == 0)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Boschvoid imap_msgpart_url_free(struct imap_msgpart_url **_mpurl)
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen if (mpurl->box != NULL && mpurl->box != mpurl->selected_box)