Index: app.c =================================================================== RCS file: /usr/cvsroot/asterisk/app.c,v retrieving revision 1.31 diff -u -r1.31 app.c --- app.c 23 Sep 2004 16:58:09 -0000 1.31 +++ app.c 3 Oct 2004 13:50:15 -0000 @@ -148,7 +148,7 @@ return 0; } -int ast_app_has_voicemail(const char *mailbox) +int ast_app_has_voicemail2(const char *user, const char *mailbox) { DIR *dir; struct dirent *de; @@ -158,7 +158,7 @@ char *context; int ret; /* If no mailbox, return immediately */ - if (ast_strlen_zero(mailbox)) + if (ast_strlen_zero(user) || ast_strlen_zero(mailbox)) return 0; if (strchr(mailbox, ',')) { strncpy(tmp, mailbox, sizeof(tmp) - 1); @@ -172,14 +172,14 @@ } return 0; } - strncpy(tmp, mailbox, sizeof(tmp) - 1); + strncpy(tmp, user, sizeof(tmp) - 1); context = strchr(tmp, '@'); if (context) { *context = '\0'; context++; } else context = "default"; - snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp); + snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, tmp, mailbox); dir = opendir(fn); if (!dir) return 0; @@ -193,15 +193,13 @@ return 0; } +int ast_app_has_voicemail(const char *mailbox) +{ + return ast_app_has_voicemail2(mailbox, "INBOX"); +} + int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs) { - DIR *dir; - struct dirent *de; - char fn[256]; - char tmp[256]=""; - char *mb, *cur; - char *context; - int ret; if (newmsgs) *newmsgs = 0; if (oldmsgs) @@ -211,9 +209,9 @@ return 0; if (strchr(mailbox, ',')) { int tmpnew, tmpold; + char tmp[256], *mb, *cur; strncpy(tmp, mailbox, sizeof(tmp) - 1); mb = tmp; - ret = 0; while((cur = strsep(&mb, ", "))) { if (!ast_strlen_zero(cur)) { if (ast_app_messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL)) @@ -228,39 +226,8 @@ } return 0; } - strncpy(tmp, mailbox, sizeof(tmp) - 1); - context = strchr(tmp, '@'); - if (context) { - *context = '\0'; - context++; - } else - context = "default"; - if (newmsgs) { - snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp); - dir = opendir(fn); - if (dir) { - while ((de = readdir(dir))) { - if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) && - !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt")) - (*newmsgs)++; - - } - closedir(dir); - } - } - if (oldmsgs) { - snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/Old", (char *)ast_config_AST_SPOOL_DIR, context, tmp); - dir = opendir(fn); - if (dir) { - while ((de = readdir(dir))) { - if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) && - !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt")) - (*oldmsgs)++; - - } - closedir(dir); - } - } + *oldmsgs = ast_app_has_voicemail2(mailbox, "INBOX"); + *newmsgs = ast_app_has_voicemail2(mailbox, "Old"); return 0; } Index: apps/app_hasnewvoicemail.c =================================================================== RCS file: /usr/cvsroot/asterisk/apps/app_hasnewvoicemail.c,v retrieving revision 1.7 diff -u -r1.7 app_hasnewvoicemail.c --- apps/app_hasnewvoicemail.c 14 Jul 2004 07:22:30 -0000 1.7 +++ apps/app_hasnewvoicemail.c 3 Oct 2004 13:50:15 -0000 @@ -1,7 +1,11 @@ /* * Asterisk -- A telephony toolkit for Linux. * - * HasVoicemail application + * HasNewVoicemail application + * + * CHANGELOG: + * 2004-10-03 TL Fix bug # 2559 (correctly parsing arguments) + * * Changes Copyright (c) 2004 Todd Freeman * * 95% based on HasNewVoicemail by: @@ -38,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -59,70 +64,94 @@ static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101"; static char *hasnewvoicemail_descrip = "HasNewVoicemail(vmbox[@context][|varname])\n" -" Branches to priority + 101, if there is voicemail in folder INBOX." -" Optionally sets to the number of messages in that folder.\n"; +" Branches to priority + 101, if there is new voicemail." +" Optionally sets to the number of new messages.\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; +static int hasnewvoicemail_exec(struct ast_channel *chan, void *data) +{ + int res=0; + struct localuser *u; + char *input, *varname = NULL, *vmbox; + int vmcount = 0; + + if (!data) { + ast_log(LOG_WARNING, "HasNewVoicemail requires an argument (vmbox[@context][|varname])\n"); + return -1; + } + LOCAL_USER_ADD(u); + + input = ast_strdupa((char *)data); + if (input) { + vmbox = strsep(&input, "|"); + varname = input; + + vmcount = ast_app_has_voicemail2(vmbox, "INBOX"); + + /* Set the count in the channel variable */ + if (varname) { + char tmp[12]; + snprintf(tmp, sizeof(tmp), "%d", vmcount); + pbx_builtin_setvar_helper(chan, varname, tmp); + } + if (vmcount > 0) { + /* Branch to the next extension */ + if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) { + chan->priority += 100; + } else + ast_log(LOG_WARNING, "VM box %s has new voicemail, but extension %s, priority %d doesn't exist\n", vmbox, chan->exten, chan->priority + 101); + } + } else { + ast_log(LOG_ERROR, "Out of memory error\n"); + } + + LOCAL_USER_REMOVE(u); + return res; +} + static int hasvoicemail_exec(struct ast_channel *chan, void *data) { int res=0; struct localuser *u; - char vmpath[256], *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default"; - DIR *vmdir; - struct dirent *vment; + char *input, *varname = NULL, *vmbox, *vmfolder = "INBOX"; int vmcount = 0; if (!data) { - ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n"); + ast_log(LOG_WARNING, "HasVoicemail requires an argument (vmbox[@context][:folder]|varname)\n"); return -1; } LOCAL_USER_ADD(u); input = ast_strdupa((char *)data); if (input) { - if ((vmbox = strsep(&input,":"))) - if ((vmfolder = strsep(&input,"|"))) - varname = input; - else - vmfolder = input; - else - if ((vmbox = strsep(&input,"|"))) - varname = input; - else - vmbox = input; - - if (index(vmbox,'@')) { - context = vmbox; - vmbox = strsep(&context,"@"); - } - - snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder); - if (!(vmdir = opendir(vmpath))) { - ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath); + if (index(input, ':')) { + vmbox = strsep(&input, ":"); } else { + vmbox = strsep(&input, "|"); + } - /* No matter what the format of VM, there will always be a .txt file for each message. */ - while ((vment = readdir(vmdir))) - if (!strncmp(vment->d_name + 7,".txt",4)) - vmcount++; - closedir(vmdir); + if (input) { + vmfolder = strsep(&input, "|"); + varname = input; } + + vmcount = ast_app_has_voicemail2(vmbox, vmfolder); + /* Set the count in the channel variable */ if (varname) { char tmp[12]; snprintf(tmp, sizeof(tmp), "%d", vmcount); pbx_builtin_setvar_helper(chan, varname, tmp); } - if (vmcount > 0) { /* Branch to the next extension */ if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) { chan->priority += 100; } else - ast_log(LOG_WARNING, "VM box %s@%s has new voicemail, but extension %s, priority %d doesn't exist\n", vmbox, context, chan->exten, chan->priority + 101); + ast_log(LOG_WARNING, "VM box %s folder %s has voicemail, but extension %s, priority %d doesn't exist\n", vmbox, vmfolder, chan->exten, chan->priority + 101); } } else { ast_log(LOG_ERROR, "Out of memory error\n"); @@ -145,7 +174,7 @@ { int res; res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip); - res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip); + res |= ast_register_application(app_hasnewvoicemail, hasnewvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip); return res; } Index: include/asterisk/app.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/app.h,v retrieving revision 1.15 diff -u -r1.15 app.h --- include/asterisk/app.h 18 Sep 2004 14:01:35 -0000 1.15 +++ include/asterisk/app.h 3 Oct 2004 13:50:15 -0000 @@ -41,6 +41,7 @@ //! Determine if a given mailbox has any voicemail extern int ast_app_has_voicemail(const char *mailbox); +extern int ast_app_has_voicemail2(const char *user, const char *mailbox); //! Determine number of new/old messages in a mailbox extern int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs);