Index: apps/app_voicemail.c =================================================================== --- apps/app_voicemail.c (revision 175304) +++ apps/app_voicemail.c (working copy) @@ -107,6 +107,8 @@ #endif #ifdef IMAP_STORAGE +#include "asterisk/threadstorage.h" + AST_MUTEX_DEFINE_STATIC(imaptemp_lock); static char imaptemp[1024]; static char imapserver[48]; @@ -123,6 +125,8 @@ struct vm_state; struct ast_vm_user; +AST_THREADSTORAGE(ts_vmstate, ts_vmstate_init); + static int init_mailstream (struct vm_state *vms, int box); static void write_file (char *filename, char *buffer, unsigned long len); /*static void status (MAILSTREAM *stream); */ /* No need for this. */ @@ -1897,6 +1901,9 @@ { struct vm_state *vms_p; + if ((vms_p = pthread_getspecific(ts_vmstate.key)) && !strcmp(vms_p->imapuser, vmu->imapuser) && !strcmp(vms_p->username, vmu->mailbox)) { + return vms_p; + } if (option_debug > 4) ast_log(LOG_DEBUG,"Adding new vmstate for %s\n",vmu->imapuser); if (!(vms_p = ast_calloc(1, sizeof(*vms_p)))) @@ -1919,6 +1926,12 @@ { struct vmstate *vlist = NULL; + if (interactive) { + struct vm_state *vms; + vms = pthread_getspecific(ts_vmstate.key); + return vms; + } + ast_mutex_lock(&vmstate_lock); vlist = vmstates; while (vlist) { @@ -1954,6 +1967,12 @@ struct vmstate *vlist = NULL; const char *local_context = S_OR(context, "default"); + if (interactive) { + struct vm_state *vms; + vms = pthread_getspecific(ts_vmstate.key); + return vms; + } + ast_mutex_lock(&vmstate_lock); vlist = vmstates; if (option_debug > 2) @@ -1963,7 +1982,7 @@ if (vlist->vms->username && vlist->vms->context) { if (option_debug > 2) ast_log(LOG_DEBUG, " comparing mailbox %s (i=%d) to vmstate mailbox %s (i=%d)\n",mailbox,interactive,vlist->vms->username,vlist->vms->interactive); - if (!strcmp(vlist->vms->username,mailbox) && !(strcmp(vlist->vms->context, local_context)) && vlist->vms->interactive == interactive) { + if (!strcmp(vlist->vms->username,mailbox) && !(strcmp(vlist->vms->context, local_context))) { if (option_debug > 2) ast_log(LOG_DEBUG, " Found it!\n"); ast_mutex_unlock(&vmstate_lock); @@ -2008,9 +2027,13 @@ /* get a pointer to the persistent store */ vms->persist_vms = altvms; /* Reuse the mailstream? */ +#ifdef REALLY_FAST_EVEN_IF_IT_MEANS_RESOURCE_LEAKS vms->mailstream = altvms->mailstream; - /* vms->mailstream = NIL; */ +#else + vms->mailstream = NIL; +#endif } + return; } v = (struct vmstate *)malloc(sizeof(struct vmstate)); @@ -2042,6 +2065,10 @@ altvms->oldmessages = vms->oldmessages; altvms->updated = 1; } + vms->mailstream = mail_close(vms->mailstream); + + /* Interactive states are not stored within the persistent list */ + return; } ast_mutex_lock(&vmstate_lock); @@ -7324,6 +7351,9 @@ adsi_begin(chan, &useadsi); #ifdef IMAP_STORAGE + pthread_once(&ts_vmstate.once, ts_vmstate.key_init); + pthread_setspecific(ts_vmstate.key, &vms); + vms.interactive = 1; vms.updated = 1; ast_copy_string(vms.context, vmu->context, sizeof(vms.context)); @@ -7755,8 +7785,11 @@ free(vms.deleted); if (vms.heard) free(vms.heard); + +#ifdef IMAP_STORAGE + pthread_setspecific(ts_vmstate.key, NULL); +#endif ast_module_user_remove(u); - return res; }