diff -ruN asterisk-1.4.21.2-save/apps/app_voicemail.c asterisk-1.4.21.2/apps/app_voicemail.c --- asterisk-1.4.21.2-save/apps/app_voicemail.c 2008-05-27 18:17:20.000000000 +0100 +++ asterisk-1.4.21.2/apps/app_voicemail.c 2008-10-09 14:01:57.000000000 +0100 @@ -344,6 +344,7 @@ #ifdef IMAP_STORAGE char imapuser[80]; /* IMAP server login */ char imappassword[80]; /* IMAP server password if authpassword not defined */ + char imapmbxid[80]; /* Shared mailbox ID to use rather than the dial in one */ #endif double volgain; /*!< Volume gain for voicemails sent via email */ AST_LIST_ENTRY(ast_vm_user) list; @@ -603,6 +604,8 @@ ast_copy_string(vmu->imapuser, value, sizeof(vmu->imapuser)); } else if (!strcasecmp(var, "imappassword")) { ast_copy_string(vmu->imappassword, value, sizeof(vmu->imappassword)); + } else if (!strcasecmp(var, "imapmbxid")) { + ast_copy_string(vmu->imapmbxid, value, sizeof(vmu->imapmbxid)); #endif } else if (!strcasecmp(var, "delete") || !strcasecmp(var, "deletevoicemail")) { ast_set2_flag(vmu, ast_true(value), VM_DELETE); @@ -707,6 +710,8 @@ ast_copy_string(retval->imapuser, tmp->value, sizeof(retval->imapuser)); } else if (!strcasecmp(tmp->name, "imappassword")) { ast_copy_string(retval->imappassword, tmp->value, sizeof(retval->imappassword)); + } else if (!strcasecmp(tmp->name, "imapmbxid")) { + ast_copy_string(retval->imapmbxid, tmp->value, sizeof(retval->imapmbxid)); #endif } else apply_option(retval, tmp->name, tmp->value); @@ -908,7 +913,9 @@ ast_log(LOG_DEBUG, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum); /* delete message */ snprintf (arg, sizeof(arg), "%lu",messageNum); + ast_mutex_lock(&vms->lock); mail_setflag (vms->mailstream,arg,"\\DELETED"); + ast_mutex_unlock(&vms->lock); } #endif @@ -1864,7 +1871,11 @@ /* fprintf(p, "X-Asterisk-VM-Orig-Mailbox: %s" ENDL, ext); */ fprintf(p, "X-Asterisk-VM-Server-Name: %s" ENDL, fromstring); fprintf(p, "X-Asterisk-VM-Context: %s" ENDL, context); +#ifdef IMAP_STORAGE + fprintf(p, "X-Asterisk-VM-Extension: %s" ENDL, ((vmu->imapmbxid[0] != '\0')?vmu->imapmbxid:mailbox)); +#else fprintf(p, "X-Asterisk-VM-Extension: %s" ENDL, mailbox); +#endif fprintf(p, "X-Asterisk-VM-Priority: %d" ENDL, chan->priority); fprintf(p, "X-Asterisk-VM-Caller-channel: %s" ENDL, chan->name); fprintf(p, "X-Asterisk-VM-Caller-ID-Num: %s" ENDL, cidnum); @@ -2401,8 +2412,10 @@ INIT(&str, mail_string, buf, len); init_mailstream(vms, 0); imap_mailbox_name(mailbox, sizeof(mailbox), vms, 0, 1); + ast_mutex_lock(&vms->lock); if (!mail_append(vms->mailstream, mailbox, &str)) ast_log(LOG_ERROR, "Error while sending the message to %s\n", mailbox); + ast_mutex_unlock(&vms->lock); fclose(p); unlink(tmp); ast_free(buf); @@ -2487,8 +2500,9 @@ return -1; } if (ret == 0) { + ast_mutex_lock(&vms_p->lock); pgm = mail_newsearchpgm (); - hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)mailbox); + hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)((vmu->imapmbxid[0] != '\0')?vmu->imapmbxid:mailbox)); pgm->header = hdr; if (fold != 1) { pgm->unseen = 1; @@ -2512,10 +2526,13 @@ vms_p->oldmessages = vms_p->vmArrayIndex; /*Freeing the searchpgm also frees the searchhdr*/ mail_free_searchpgm(&pgm); + ast_mutex_unlock(&vms_p->lock); vms_p->updated = 0; return vms_p->vmArrayIndex; - } else { + } else { + ast_mutex_lock(&vms_p->lock); mail_ping(vms_p->mailstream); + ast_mutex_unlock(&vms_p->lock); } return 0; } @@ -2614,8 +2631,12 @@ return -1; } snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]); - if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(imbox)) == T)) + ast_mutex_lock(&sendvms->lock); + if ((mail_copy(sendvms->mailstream, messagestring, (char *) mbox(imbox)) == T)) { + ast_mutex_unlock(&sendvms->lock); return 0; + } + ast_mutex_unlock(&sendvms->lock); ast_log(LOG_WARNING, "Unable to copy message from mailbox %s to mailbox %s\n", vmu->mailbox, recip->mailbox); return -1; } @@ -3260,7 +3281,9 @@ snprintf(sequence, sizeof(sequence), "%ld", vms->msgArray[msg]); if (option_debug > 2) ast_log(LOG_DEBUG, "Copying sequence %s to mailbox %s\n",sequence,(char *) mbox(box)); - res = mail_copy(vms->mailstream,sequence,(char *) mbox(box)); + ast_mutex_lock(&vms->lock); + res = mail_copy(vms->mailstream,sequence,(char *) mbox(box)); + ast_mutex_unlock(&vms->lock); if (res == 1) return 0; return 1; #else @@ -4174,7 +4197,9 @@ } /* This will only work for new messages... */ + ast_mutex_lock(&vms->lock); header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]); + ast_mutex_unlock(&vms->lock); /* empty string means no valid header */ if (ast_strlen_zero(header_content)) { ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[vms->curmsg]); @@ -4207,7 +4232,9 @@ if (option_debug > 2) ast_log (LOG_DEBUG,"Before mail_fetchstructure, message number is %ld, filename is:%s\n",vms->msgArray[vms->curmsg], vms->fn); /*mail_fetchstructure (mailstream, vmArray[0], &body); */ + ast_mutex_lock(&vms->lock); mail_fetchstructure (vms->mailstream, vms->msgArray[vms->curmsg], &body); + ast_mutex_unlock(&vms->lock); save_body(body,vms,"3","gsm"); /* should not assume "fmt" here! */ save_body(body,vms,"2",fmt); @@ -4506,7 +4533,9 @@ } /* This will only work for new messages... */ + ast_mutex_lock(&vms->lock); header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]); + ast_mutex_unlock(&vms->lock); /* empty string means no valid header */ if (ast_strlen_zero(header_content)) { ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[vms->curmsg]); @@ -4515,8 +4544,9 @@ snprintf(todir, sizeof(todir), "%s%s/%s/tmp", VM_SPOOL_DIR, vmu->context, vmu->mailbox); make_gsm_file(vms->fn, sizeof(vms->fn), vms->imapuser, todir, vms->curmsg); + ast_mutex_lock(&vms->lock); mail_fetchstructure (vms->mailstream,vms->msgArray[vms->curmsg],&body); - + ast_mutex_lock(&vms->lock); /* We have the body, now we extract the file name of the first attachment. */ if (body->nested.part && body->nested.part->next && body->nested.part->next->body.parameter->value) { attachedfilefmt = ast_strdupa(body->nested.part->next->body.parameter->value); @@ -4832,10 +4862,11 @@ check_quota(vms,(char *)mbox(box)); } + ast_mutex_lock(&vms->lock); pgm = mail_newsearchpgm(); /* Check IMAP folder for Asterisk messages only... */ - hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", vmu->mailbox); + hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", ((vmu->imapmbxid[0] != '\0')?vmu->imapmbxid:vmu->mailbox)); pgm->header = hdr; pgm->deleted = 0; pgm->undeleted = 1; @@ -4858,6 +4889,7 @@ vms->lastmsg = vms->vmArrayIndex - 1; mail_free_searchpgm(&pgm); + ast_mutex_unlock(&vms->lock); return 0; } #else @@ -6989,12 +7021,14 @@ if (option_debug > 2) ast_log(LOG_DEBUG, "*** Checking if we can expunge, deleted set to %d, expungeonhangup set to %d\n",deleted,expungeonhangup); if (vmu && deleted == 1 && expungeonhangup == 1) { + ast_mutex_lock(&vms.lock); #ifdef HAVE_IMAP_TK2006 if (LEVELUIDPLUS (vms.mailstream)) { mail_expunge_full(vms.mailstream,NIL,EX_UID); } else #endif mail_expunge(vms.mailstream); + ast_mutex_unlock(&vms.lock); } /* before we delete the state, we should copy pertinent info * back to the persistent model */ @@ -8052,7 +8086,9 @@ } /* This will only work for new messages... */ + ast_mutex_lock(&vms->lock); header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]); + ast_mutex_unlock(&vms->lock); /* empty string means no valid header */ if (ast_strlen_zero(header_content)) { ast_log (LOG_ERROR,"Could not fetch header for message number %ld\n",vms->msgArray[vms->curmsg]); @@ -9102,7 +9138,9 @@ if (!body || body == NIL) return -1; + ast_mutex_lock(&vms->lock); body_content = mail_fetchbody (vms->mailstream, vms->msgArray[vms->curmsg], section, &len); + ast_mutex_unlock(&vms->lock); if (body_content != NIL) { snprintf(filename, sizeof(filename), "%s.%s", vms->fn, format); /* ast_log (LOG_DEBUG,body_content); */ @@ -9113,6 +9151,7 @@ } /* get delimiter via mm_list callback */ +/* MUTEX should already be held */ static void get_mailbox_delimiter(MAILSTREAM *stream) { char tmp[50]; snprintf(tmp, sizeof(tmp), "{%s}", imapserver); @@ -9121,6 +9160,7 @@ /* Check Quota for user */ static void check_quota(struct vm_state *vms, char *mailbox) { + ast_mutex_lock(&vms->lock); mail_parameters(NULL, SET_QUOTA, (void *) mm_parsequota); if (option_debug > 2) ast_log(LOG_DEBUG, "Mailbox name set to: %s, about to check quotas\n", mailbox); @@ -9129,6 +9169,7 @@ } else { ast_log(LOG_WARNING,"Mailstream not available for mailbox: %s\n",mailbox); } + ast_mutex_unlock(&vms->lock); } #endif /* IMAP_STORAGE */