Index: app_voicemail.c =================================================================== --- app_voicemail.c (revision 432422) +++ app_voicemail.c (working copy) @@ -2206,6 +2206,10 @@ *vms_p->introfn = '\0'; ast_mutex_lock(&vms_p->lock); + + // get the current mailbox so that we can point the mailstream back to it later + int curr_mbox = get_folder_by_name(vms_p->curbox); + if (init_mailstream(vms_p, GREETINGS_FOLDER) || !vms_p->mailstream) { ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL or can't init_mailstream\n"); ast_mutex_unlock(&vms_p->lock); @@ -2212,6 +2216,7 @@ return -1; } + int ret = 0; /*XXX Yuck, this could probably be done a lot better */ for (i = 0; i < vms_p->mailstream->nmsgs; i++) { mail_fetchstructure(vms_p->mailstream, i + 1, &body); @@ -2220,8 +2225,8 @@ attachment = ast_strdupa(body->nested.part->next->body.parameter->value); } else { ast_log(AST_LOG_ERROR, "There is no file attached to this IMAP message.\n"); - ast_mutex_unlock(&vms_p->lock); - return -1; + ret = -1; + break; } filename = strsep(&attachment, "."); if (!strcmp(filename, file)) { @@ -2228,13 +2233,21 @@ ast_copy_string(vms_p->fn, dir, sizeof(vms_p->fn)); vms_p->msgArray[vms_p->curmsg] = i + 1; save_body(body, vms_p, "2", attachment, 0); - ast_mutex_unlock(&vms_p->lock); - return 0; + ret = 0; + break; } } + + if (curr_mbox != -1) + { + // restore previous mbox stream + if (init_mailstream(vms_p, curr_mbox) || !vms_p->mailstream) { + ast_log(AST_LOG_ERROR, "IMAP mailstream is NULL or can't init_mailstream\n"); + ret = -1; + } + } ast_mutex_unlock(&vms_p->lock); - - return -1; + return ret; } static int imap_retrieve_file(const char *dir, const int msgnum, const char *mailbox, const char *context) @@ -2280,6 +2293,20 @@ goto exit; } + /* Ensure we have the correct mailbox open and have a valid mailstream for it + */ + int curr_mbox = get_folder_by_name(vms->curbox); + if (curr_mbox < 0) { + ast_debug(3, "Mailbox folder curbox not set, defaulting to Inbox\n"); + curr_mbox = 0; + } + init_mailstream(vms, curr_mbox); + if (!vms->mailstream) { + ast_log(AST_LOG_ERROR, "IMAP mailstream for %s is NULL\n", vmu->mailbox); + res = -1; + goto exit; + } + make_file(vms->fn, sizeof(vms->fn), dir, msgnum); snprintf(vms->introfn, sizeof(vms->introfn), "%sintro", vms->fn);