Index: apps/app_voicemail.c =================================================================== --- apps/app_voicemail.c (revision 81681) +++ apps/app_voicemail.c (working copy) @@ -131,7 +131,7 @@ static void copy_msgArray(struct vm_state *dst, struct vm_state *src); static int save_body(BODY *body, struct vm_state *vms, char *section, char *format); static int make_gsm_file(char *dest, char *imapuser, char *dir, int num); -static void get_mailbox_delimiter(MAILSTREAM *stream); +static void get_mailbox_delimiter(struct vm_state *vms); static void mm_parsequota (MAILSTREAM *stream, unsigned char *msg, QUOTALIST *pquota); static void imap_mailbox_name(char *spec, struct vm_state *vms, int box, int target); static int imap_store_file(char *dir, char *mailboxuser, char *mailboxcontext, int msgnum, struct ast_channel *chan, struct ast_vm_user *vmu, char *fmt, int duration, struct vm_state *vms); @@ -358,6 +358,7 @@ int starting; int repeats; #ifdef IMAP_STORAGE + ast_mutex_t lock; /*Lock used around c-client mail_* calls so there are no races*/ int updated; /* decremented on each mail check until 1 -allows delay */ long msgArray[256]; MAILSTREAM *mailstream; @@ -890,7 +891,9 @@ ast_log(LOG_DEBUG, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum); /* delete message */ sprintf (arg,"%lu",messageNum); + ast_mutex_lock(&vms->lock); mail_setflag (vms->mailstream,arg,"\\DELETED"); + ast_mutex_unlock(&vms->lock); } #endif @@ -2346,8 +2349,10 @@ INIT(&str, mail_string, buf, len); init_mailstream(vms, 0); imap_mailbox_name(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); @@ -2430,6 +2435,7 @@ return -1; } if (ret == 0) { + ast_mutex_lock(&vms_p->lock); pgm = mail_newsearchpgm (); hdr = mail_newsearchheader ("X-Asterisk-VM-Extension", (char *)mailbox); pgm->header = hdr; @@ -2456,9 +2462,12 @@ /*Freeing the searchpgm also frees the searchhdr*/ mail_free_searchpgm(&pgm); vms_p->updated = 0; + ast_mutex_unlock(&vms_p->lock); return vms_p->vmArrayIndex; } else { + ast_mutex_lock(&vms_p->lock); mail_ping(vms_p->mailstream); + ast_mutex_unlock(&vms_p->lock); } return 0; } @@ -2557,8 +2566,12 @@ } imap_mailbox_name(dest, destvms, imbox, 1); snprintf(messagestring, sizeof(messagestring), "%ld", sendvms->msgArray[msgnum]); - if((mail_copy(sendvms->mailstream, messagestring, dest) == T)) + ast_mutex_lock(&sendvms->lock); + if((mail_copy(sendvms->mailstream, messagestring, dest) == 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; } @@ -3200,7 +3213,9 @@ imap_mailbox_name(dbox, vms, box, 1); if(option_debug > 2) ast_log(LOG_DEBUG, "Copying sequence %s to mailbox %s\n",sequence,dbox); + ast_mutex_lock(&vms->lock); res = mail_copy(vms->mailstream,sequence,dbox); + ast_mutex_unlock(&vms->lock); if (res == 1) return 0; return 1; #else @@ -4080,7 +4095,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]); @@ -4113,7 +4130,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); @@ -4409,16 +4428,19 @@ } /* This will only work for new messages... */ + ast_mutex_lock(&vms->lock); header_content = mail_fetchheader (vms->mailstream, vms->msgArray[vms->curmsg]); /* 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]); + ast_mutex_unlock(&vms->lock); return -1; } snprintf(todir, sizeof(todir), "%s%s/%s/tmp", VM_SPOOL_DIR, vmu->context, vmu->mailbox); make_gsm_file(vms->fn, vms->imapuser, todir, vms->curmsg); mail_fetchstructure (vms->mailstream,vms->msgArray[vms->curmsg],&body); + ast_mutex_unlock(&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) { @@ -4677,12 +4699,14 @@ #include "linkage.c" /* Connect to INBOX first to get folders delimiter */ imap_mailbox_name(tmp, vms, 0, 0); + ast_mutex_lock(&vms->lock); stream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL); + ast_mutex_unlock(&vms->lock); if (stream == NIL) { ast_log (LOG_ERROR, "Can't connect to imap server %s\n", tmp); return NIL; } - get_mailbox_delimiter(stream); + get_mailbox_delimiter(vms); /* update delimiter in imapfolder */ for(cp = imapfolder; *cp; cp++) if(*cp == '/') @@ -4692,7 +4716,9 @@ imap_mailbox_name(tmp, vms, box, 1); if(option_debug > 2) ast_log (LOG_DEBUG,"Before mail_open, server: %s, box:%d\n", tmp, box); + ast_mutex_lock(&vms->lock); vms->mailstream = mail_open (stream, tmp, debug ? OP_DEBUG : NIL); + ast_mutex_unlock(&vms->lock); if (vms->mailstream == NIL) { return -1; } else { @@ -4717,10 +4743,13 @@ } /* Check Quota (here for now to test) */ + ast_mutex_lock(&vms->lock); mail_parameters(NULL, SET_QUOTA, (void *) mm_parsequota); + ast_mutex_unlock(&vms->lock); imap_mailbox_name(dbox, vms, box, 1); imap_getquotaroot(vms->mailstream, dbox); + ast_mutex_lock(&vms->lock); pgm = mail_newsearchpgm(); /* Check IMAP folder for Asterisk messages only... */ @@ -4747,6 +4776,7 @@ vms->lastmsg = vms->vmArrayIndex - 1; mail_free_searchpgm(&pgm); + ast_mutex_unlock(&vms->lock); return 0; } #else @@ -6868,12 +6898,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 */ @@ -7922,7 +7954,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]); @@ -8958,6 +8992,7 @@ if (!vf) { ast_log(LOG_ERROR, "No vmstate found for user:%s, mailbox %s\n",vms->imapuser,vms->username); } else { + ast_mutex_destroy(&vf->vms->lock); free(vf); } ast_mutex_unlock(&vmstate_lock); @@ -8989,6 +9024,7 @@ for (x = 0; x < 256; x++) { vms->msgArray[x] = 0; } + ast_mutex_init(&vms->lock); } static void check_msgArray(struct vm_state *vms) @@ -9021,7 +9057,9 @@ if (!body || body == NIL) return -1; display_body (body, NIL, (long) 0); + 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) { sprintf(filename,"%s.%s", vms->fn, format); /* ast_log (LOG_DEBUG,body_content); */ @@ -9032,10 +9070,12 @@ } /* get delimiter via mm_list callback */ -static void get_mailbox_delimiter(MAILSTREAM *stream) { +static void get_mailbox_delimiter(struct vm_state *vms) { char tmp[50]; sprintf(tmp, "{%s}", imapserver); - mail_list(stream, tmp, "*"); + ast_mutex_lock(&vms->lock); + mail_list(vms->mailstream, tmp, "*"); + ast_mutex_unlock(&vms->lock); } #endif /* IMAP_STORAGE */