--- asterisk-1.6.2.0/apps/app_voicemail.c 2009-12-04 10:36:21.000000000 -0700 +++ voicemail.c 2010-01-13 15:40:20.000000000 -0700 @@ -111,6 +111,7 @@ #include "asterisk/utils.h" #include "asterisk/stringfields.h" #include "asterisk/smdi.h" +#include "asterisk/astobj2.h" #include "asterisk/event.h" #include "asterisk/taskprocessor.h" @@ -828,6 +829,57 @@ static int add_email_attachment(FILE *p, struct ast_vm_user *vmu, char *format, char *attach, char *greeting_attachment, char *mailbox, char *bound, char *filename, int last, int msgnum); static int is_valid_dtmf(const char *key); +struct ao2_container *inprocess_container; + +struct inprocess { + int count; + char *context; + char mailbox[0]; +}; + +static int inprocess_hash_fn(const void *obj, const int flags) +{ + const struct inprocess *i = obj; + return atoi(i->mailbox); +} + +static int inprocess_cmp_fn(void *obj, void *arg, int flags) +{ + struct inprocess *i = obj, *j = arg; + if (!strcmp(i->mailbox, j->mailbox)) { + return 0; + } + return !strcmp(i->context, j->context) ? CMP_MATCH : 0; +} + +static int inprocess_count(const char *context, const char *mailbox, int delta) +{ + struct inprocess *i, *arg = alloca(sizeof(*arg) + strlen(context) + strlen(mailbox) + 2); + arg->context = arg->mailbox + strlen(mailbox) + 1; + strcpy(arg->mailbox, mailbox); /* SAFE */ + strcpy(arg->context, context); /* SAFE */ + ao2_lock(inprocess_container); + if ((i = ao2_find(inprocess_container, &arg, 0))) { + int ret = ast_atomic_fetchadd_int(&i->count, delta); + ao2_unlock(inprocess_container); + ao2_ref(i, -1); + return ret + delta; + } + if (!(i = ao2_alloc(sizeof(*i) + strlen(context) + strlen(mailbox) + 2, NULL))) { + ao2_unlock(inprocess_container); + return 0; + } + i->context = i->mailbox + strlen(mailbox) + 1; + strcpy(i->mailbox, mailbox); /* SAFE */ + strcpy(i->context, context); /* SAFE */ + i->count = delta; + ao2_link(inprocess_container, i); + ao2_unlock(inprocess_container); + ao2_ref(i, -1); + return delta; +} + + #if !(defined(ODBC_STORAGE) || defined(IMAP_STORAGE)) static int __has_voicemail(const char *context, const char *mailbox, const char *folder, int shortcircuit); #endif @@ -4846,7 +4898,7 @@ return ERROR_LOCK_PATH; recipmsgnum = last_message_index(recip, todir) + 1; - if (recipmsgnum < recip->maxmsg) { + if (recipmsgnum < recip->maxmsg - (imbox ? 0 : inprocess_count(vmu->mailbox, vmu->context, 0))) { make_file(topath, sizeof(topath), todir, recipmsgnum); if (EXISTS(fromdir, msgnum, frompath, chan->language)) { COPY(fromdir, msgnum, todir, recipmsgnum, recip->mailbox, recip->context, frompath, topath); @@ -5353,19 +5405,21 @@ } /* Check if we have exceeded maxmsg */ - if (msgnum >= vmu->maxmsg) { + if (msgnum >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) { ast_log(AST_LOG_WARNING, "Unable to leave message since we will exceed the maximum number of messages allowed (%u > %u)\n", msgnum, vmu->maxmsg); ast_play_and_wait(chan, "vm-mailboxfull"); + inprocess_count(vmu->mailbox, vmu->context, -1); ast_free(tmp); return -1; } #else - if (count_messages(vmu, dir) >= vmu->maxmsg) { + if (count_messages(vmu, dir) >= vmu->maxmsg - inprocess_count(vmu->mailbox, vmu->context, +1)) { res = ast_streamfile(chan, "vm-mailboxfull", chan->language); if (!res) res = ast_waitstream(chan, ""); ast_log(AST_LOG_WARNING, "No more messages possible\n"); pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED"); + inprocess_count(vmu->mailbox, vmu->context, -1); goto leave_vm_out; } @@ -5379,6 +5433,7 @@ res = ast_waitstream(chan, ""); ast_log(AST_LOG_ERROR, "Unable to create message file: %s\n", strerror(errno)); pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED"); + inprocess_count(vmu->mailbox, vmu->context, -1); goto leave_vm_out; } @@ -5436,6 +5491,7 @@ ast_verbose( VERBOSE_PREFIX_3 "Recording was %d seconds long but needs to be at least %d - abandoning\n", duration, vmminsecs); ast_filedelete(tmptxtfile, NULL); unlink(tmptxtfile); + inprocess_count(vmu->mailbox, vmu->context, -1); if (ast_check_realtime("voicemail_data")) { ast_destroy_realtime("voicemail_data", "filename", tmptxtfile, SENTINEL); } @@ -5447,6 +5503,7 @@ /* Delete files */ ast_filedelete(tmptxtfile, NULL); unlink(tmptxtfile); + inprocess_count(vmu->mailbox, vmu->context, -1); } else if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) { ast_debug(1, "The recorded media file is gone, so we should remove the .txt file too!\n"); unlink(tmptxtfile); @@ -5470,6 +5527,7 @@ snprintf(txtfile, sizeof(txtfile), "%s.txt", fn); ast_filerename(tmptxtfile, fn, NULL); rename(tmptxtfile, txtfile); + inprocess_count(vmu->mailbox, vmu->context, -1); /* Properly set permissions on voicemail text descriptor file. Unfortunately mkstemp() makes this file 0600 on most unix systems. */ @@ -5534,6 +5592,8 @@ } } } + } else { + inprocess_count(vmu->mailbox, vmu->context, -1); } if (res == '0') { goto transfer; @@ -7198,7 +7258,7 @@ if (vm_lock_path(vms->curdir)) return ERROR_LOCK_PATH; - for (x = 0; x < vmu->maxmsg; x++) { + for (x = 0; x < vmu->maxmsg + 10; x++) { if (!vms->deleted[x] && ((strcasecmp(vms->curbox, "INBOX") && strcasecmp(vms->curbox, "Urgent")) || !vms->heard[x] || (vms->heard[x] && !ast_test_flag(vmu, VM_MOVEHEARD)))) { /* Save this message. It's not in INBOX or hasn't been heard */ make_file(vms->fn, sizeof(vms->fn), vms->curdir, x); @@ -11177,6 +11237,7 @@ res |= ast_manager_unregister("VoicemailUsersList"); ast_cli_unregister_multiple(cli_voicemail, ARRAY_LEN(cli_voicemail)); ast_uninstall_vm_functions(); + ao2_ref(inprocess_container, -1); if (poll_thread != AST_PTHREADT_NULL) stop_poll_thread(); @@ -11193,6 +11254,10 @@ static int load_module(void) { int res; + if (!(inprocess_container = ao2_container_alloc(573, inprocess_hash_fn, inprocess_cmp_fn))) { + return res; + } + my_umask = umask(0); umask(my_umask);