Fix HasVoicemail so it can parse all arguments correctly (bug 2559)
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
index ff660dc..5891e67 100755 (executable)
 #include <asterisk/channel.h>
 #include <asterisk/pbx.h>
 #include <asterisk/module.h>
+#include <asterisk/lock.h>
+#include <asterisk/utils.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
 #include <dirent.h>
 
-#include <pthread.h>
-
 #include "../astconf.h"
 
 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
@@ -71,7 +71,7 @@ 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";
+       char vmpath[256], *temps, *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default";
        DIR *vmdir;
        struct dirent *vment;
        int vmcount = 0;
@@ -84,21 +84,22 @@ static int hasvoicemail_exec(struct ast_channel *chan, void *data)
 
        input = ast_strdupa((char *)data);
        if (input) {
-               if ((vmbox = strsep(&input,":")))
-                       if ((vmfolder = strsep(&input,"|")))
+               temps = input;
+               if ((temps = strsep(&input, "|"))) {
+                       if (input && !ast_strlen_zero(input))
                                varname = input;
-                       else
+                       input = temps;
+               }
+               if ((temps = strsep(&input, ":"))) {
+                       if (input && !ast_strlen_zero(input))
                                vmfolder = input;
-               else
-                       if ((vmbox = strsep(&input,"|")))
-                               varname = input;
-                       else
-                               vmbox = input;
-
-               if (index(vmbox,'@')) {
-                       context = vmbox;
-                       vmbox = strsep(&context,"@");
+                       input = temps;
                }
+               if ((vmbox = strsep(&input, "@")))
+                       if (input && !ast_strlen_zero(input))
+                               context = input;
+               if (!vmbox)
+                       vmbox = input;
 
                snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
                if (!(vmdir = opendir(vmpath))) {
@@ -114,13 +115,13 @@ static int hasvoicemail_exec(struct ast_channel *chan, void *data)
                /* Set the count in the channel variable */
                if (varname) {
                        char tmp[12];
-                       snprintf(tmp, sizeof(tmp) - 1, "%d", vmcount);
+                       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)) {
+                       if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
                                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);