bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "lib.h"
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen#include "net.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "istream.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "message-parser.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "mail-storage.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "mail-namespace.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "imap-url.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "imap-msgpart.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch#include "imap-msgpart-url.h"
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Boschstruct imap_msgpart_url {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch char *mailbox;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch uint32_t uidvalidity;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch uint32_t uid;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch char *section;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch uoff_t partial_offset, partial_size;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch struct imap_msgpart *part;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mail_user *user;
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen struct mailbox *selected_box;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mailbox *box;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mailbox_transaction_context *trans;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mail *mail;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch struct imap_msgpart_open_result result;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool decode_cte_to_binary:1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch};
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschint imap_msgpart_url_create(struct mail_user *user, const struct imap_url *url,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch struct imap_msgpart_url **mpurl_r,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch const char **error_r)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch{
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch const char *section = url->section == NULL ? "" : url->section;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct imap_msgpart_url *mpurl;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch struct imap_msgpart *msgpart;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
18b24aaee29db8f29bf45cae3b1b60c1f1a43758Stephan Bosch if (url->mailbox == NULL || url->uid == 0 ||
18b24aaee29db8f29bf45cae3b1b60c1f1a43758Stephan Bosch url->search_program != NULL) {
18b24aaee29db8f29bf45cae3b1b60c1f1a43758Stephan Bosch *error_r = "Invalid messagepart IMAP URL";
18b24aaee29db8f29bf45cae3b1b60c1f1a43758Stephan Bosch return -1;
18b24aaee29db8f29bf45cae3b1b60c1f1a43758Stephan Bosch }
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (imap_msgpart_parse(section, &msgpart) < 0) {
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *error_r = "Invalid section";
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch return -1;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch }
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl = i_new(struct imap_msgpart_url, 1);
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch mpurl->part = msgpart;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->user = user;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->mailbox = i_strdup(url->mailbox);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->uidvalidity = url->uidvalidity;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->uid = url->uid;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (url->section != NULL)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->section = i_strdup(url->section);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->partial_offset = url->partial_offset;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->partial_size = url->partial_size;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch imap_msgpart_set_partial(msgpart, url->partial_offset,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch url->partial_size == 0 ?
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch (uoff_t)-1 : url->partial_size);
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *mpurl_r = mpurl;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch return 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
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{
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mailbox_status box_status;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct imap_url base_url, *url;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch const char *error;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* build base url */
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&base_url);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (selected_box != NULL) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_get_open_status(selected_box, STATUS_UIDVALIDITY,
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch &box_status);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch base_url.mailbox = mailbox_get_vname(selected_box);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch base_url.uidvalidity = box_status.uidvalidity;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* parse url */
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen if (imap_url_parse(urlstr, &base_url,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen IMAP_URL_PARSE_REQUIRE_RELATIVE, &url, &error) < 0) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = t_strconcat("Invalid IMAP URL: ", error, NULL);
4351efdddced25735d629496f6c68c9d0cfb896aTimo Sirainen return 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (url->mailbox == NULL) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = "Mailbox-relative IMAP URL, but no mailbox selected";
4351efdddced25735d629496f6c68c9d0cfb896aTimo Sirainen return 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (imap_msgpart_url_create(user, url, mpurl_r, error_r) < 0)
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch return -1;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch (*mpurl_r)->selected_box = selected_box;
4351efdddced25735d629496f6c68c9d0cfb896aTimo Sirainen return 1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Boschstruct mailbox *imap_msgpart_url_get_mailbox(struct imap_msgpart_url *mpurl)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch{
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch return mpurl->box;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
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{
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mailbox_status box_status;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch enum mailbox_flags flags = MAILBOX_FLAG_READONLY;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mail_namespace *ns;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mailbox *box;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen if (mpurl->box != NULL) {
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen *box_r = mpurl->box;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *error_code_r = MAIL_ERROR_NONE;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* find mailbox namespace */
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch ns = mail_namespace_find(mpurl->user->namespaces, mpurl->mailbox);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* open mailbox */
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen if (mpurl->selected_box != NULL &&
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen mailbox_equals(mpurl->selected_box, ns, mpurl->mailbox))
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen box = mpurl->selected_box;
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen else {
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen box = mailbox_alloc(ns->list, mpurl->mailbox, flags);
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen mailbox_set_reason(box, "Read IMAP URL");
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (mailbox_open(box) < 0) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = mail_storage_get_last_error(mailbox_get_storage(box),
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch error_code_r);
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen if (box != mpurl->selected_box)
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen mailbox_free(&box);
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch return *error_code_r == MAIL_ERROR_TEMP ? -1 : 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* verify UIDVALIDITY */
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_get_open_status(box, STATUS_UIDVALIDITY, &box_status);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (mpurl->uidvalidity > 0 &&
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch box_status.uidvalidity != mpurl->uidvalidity) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = "Invalid UIDVALIDITY";
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *error_code_r = MAIL_ERROR_EXPUNGED;
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen if (box != mpurl->selected_box)
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen mailbox_free(&box);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->box = box;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen *box_r = box;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_open_mail(struct imap_msgpart_url *mpurl,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen struct mail **mail_r, const char **error_r)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch{
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mailbox_transaction_context *t;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen struct mailbox *box;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch enum mail_error error_code;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct mail *mail;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen int ret;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen if (mpurl->mail != NULL) {
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen *mail_r = mpurl->mail;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* open mailbox if it is not yet open */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if ((ret = imap_msgpart_url_open_mailbox(mpurl, &box, &error_code,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch error_r)) <= 0)
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return ret;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* start transaction */
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi t = mailbox_transaction_begin(box, 0, __func__);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen mail = mail_alloc(t, MAIL_FETCH_MESSAGE_PARTS |
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen MAIL_FETCH_IMAP_BODYSTRUCTURE, NULL);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* find the message */
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (!mail_set_uid(mail, mpurl->uid)) {
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *error_r = "Message not found";
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mail_free(&mail);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_transaction_rollback(&t);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 0;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->trans = t;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mpurl->mail = mail;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen *mail_r = mail;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschstruct imap_msgpart *
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschimap_msgpart_url_get_part(struct imap_msgpart_url *mpurl)
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen{
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch return mpurl->part;
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch}
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Boschvoid imap_msgpart_url_set_decode_to_binary(struct imap_msgpart_url *mpurl)
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch{
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch imap_msgpart_set_decode_to_binary(mpurl->part);
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen}
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_read_part(struct imap_msgpart_url *mpurl,
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch struct imap_msgpart_open_result *result_r,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen const char **error_r)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch{
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen struct mail *mail;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen int ret;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (mpurl->result.input != NULL) {
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch i_stream_seek(mpurl->result.input, 0);
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *result_r = mpurl->result;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen /* open mail if it is not yet open */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen if (ret <= 0)
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return ret;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch /* open the referenced part as a stream */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch ret = imap_msgpart_open(mail, mpurl->part, result_r);
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen if (ret < 0) {
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch *error_r = mailbox_get_last_error(mpurl->box, NULL);
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return ret;
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen }
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch mpurl->result = *result_r;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainenint imap_msgpart_url_verify(struct imap_msgpart_url *mpurl,
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen const char **error_r)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch{
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen struct mail *mail;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen int ret;
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (mpurl->result.input != NULL)
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen return 1;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
639bb36b12b9f9bb54c8bb1be50eac623622f8a0Timo Sirainen /* open mail if it is not yet open */
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
0177594fa5217b02001f4ec8752154fd2b05c545Timo Sirainen return ret;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainenint imap_msgpart_url_get_bodypartstructure(struct imap_msgpart_url *mpurl,
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen const char **bpstruct_r,
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen const char **error_r)
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen{
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen struct mail *mail;
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen int ret;
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen /* open mail if it is not yet open */
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen ret = imap_msgpart_url_open_mail(mpurl, &mail, error_r);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen if (ret <= 0)
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen return ret;
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen ret = imap_msgpart_bodypartstructure(mail, mpurl->part, bpstruct_r);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen if (ret < 0)
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen *error_r = mailbox_get_last_error(mpurl->box, NULL);
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen else if (ret == 0)
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen *error_r = "Message part not found";
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen return ret;
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen}
d65184737a0465602db98e103f9097b1a1ea9564Timo Sirainen
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Boschvoid imap_msgpart_url_free(struct imap_msgpart_url **_mpurl)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch{
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch struct imap_msgpart_url *mpurl = *_mpurl;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch *_mpurl = NULL;
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch
204ee6ed414f5e4eeb6f6c10763b55daf56f11acJosef 'Jeff' Sipek i_stream_unref(&mpurl->result.input);
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch if (mpurl->part != NULL)
a22f62ebb03080da4f5d42a551cef872e6fbc658Stephan Bosch imap_msgpart_free(&mpurl->part);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (mpurl->mail != NULL)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mail_free(&mpurl->mail);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (mpurl->trans != NULL)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_transaction_rollback(&mpurl->trans);
d2930bd70866c76fc5a746e8dea82537604f989cTimo Sirainen if (mpurl->box != NULL && mpurl->box != mpurl->selected_box)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch mailbox_free(&mpurl->box);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch if (mpurl->section != NULL)
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch i_free(mpurl->section);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch i_free(mpurl->mailbox);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch i_free(mpurl);
c502126b6e8a5d0c5431f0314b4d6eb0d43ab53dStephan Bosch}