Don't do SIP contact/route DNS if we're not using the result.
[asterisk/asterisk.git] / apps / app_minivm.c
index 3ffe061..e37346f 100644 (file)
@@ -26,7 +26,6 @@
  * based on the Comedian Mail voicemail system (app_voicemail.c).
  * 
  * \par See also
- * \arg \ref Config_minivm
  * \arg \ref Config_minivm_examples
  * \arg \ref App_minivm
  *
@@ -1229,6 +1228,7 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
        char dur[PATH_MAX];
        char tmp[80] = "/tmp/astmail-XXXXXX";
        char tmp2[PATH_MAX];
+       char newtmp[PATH_MAX]; /* Only used with volgain */
        struct timeval now;
        struct ast_tm tm;
        struct minivm_zone *the_zone = NULL;
@@ -1255,6 +1255,8 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
 
        if (ast_strlen_zero(email)) {
                ast_log(LOG_WARNING, "No address to send message to.\n");
+               ast_free(str1);
+               ast_free(str2);
                return -1;      
        }
 
@@ -1266,26 +1268,23 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
 
        /* If we have a gain option, process it now with sox */
        if (type == MVM_MESSAGE_EMAIL && (vmu->volgain < -.001 || vmu->volgain > .001) ) {
-               char newtmp[PATH_MAX];
                char tmpcmd[PATH_MAX];
                int tmpfd;
 
-               /**
-                * XXX
-                * /bug tmpfd is a leaked fd.  The file is also never unlinked.
-                *      See app_voicemail.c for how the code works there that
-                *      doesn't have this bug.
-                */
-
                ast_copy_string(newtmp, "/tmp/XXXXXX", sizeof(newtmp));
                ast_debug(3, "newtmp: %s\n", newtmp);
                tmpfd = mkstemp(newtmp);
-               if (tmpfd > -1) {
-                       snprintf(tmpcmd, sizeof(tmpcmd), "sox -v %.4f %s.%s %s.%s", vmu->volgain, filename, format, newtmp, format);
-                       ast_safe_system(tmpcmd);
-                       finalfilename = newtmp;
-                       ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", filename, format, vmu->volgain, vmu->username);
+               if (tmpfd < 0) {
+                       ast_log(LOG_WARNING, "Failed to create temporary file for volgain: %d\n", errno);
+                       ast_free(str1);
+                       ast_free(str2);
+                       return -1;
                }
+               snprintf(tmpcmd, sizeof(tmpcmd), "sox -v %.4f %s.%s %s.%s", vmu->volgain, filename, format, newtmp, format);
+               ast_safe_system(tmpcmd);
+               close(tmpfd);
+               finalfilename = newtmp;
+               ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", filename, format, vmu->volgain, vmu->username);
        } else {
                finalfilename = ast_strdupa(filename);
        }
@@ -1309,11 +1308,15 @@ static int sendmail(struct minivm_template *template, struct minivm_account *vmu
        }
        if (!p) {
                ast_log(LOG_WARNING, "Unable to open temporary file '%s'\n", tmp);
+               ast_free(str1);
+               ast_free(str2);
                return -1;
        }
        /* Allocate channel used for chanvar substitution */
        ast = ast_dummy_channel_alloc();
        if (!ast) {
+               ast_free(str1);
+               ast_free(str2);
                return -1;
        }
 
@@ -1746,10 +1749,10 @@ static void run_externnotify(struct ast_channel *chan, struct minivm_account *vm
        snprintf(arguments, sizeof(arguments), "%s %s@%s %s %s&", 
                ast_strlen_zero(vmu->externnotify) ? global_externnotify : vmu->externnotify, 
                vmu->username, vmu->domain,
-               (chan->caller.id.name.valid && chan->caller.id.name.str)
-                       ? chan->caller.id.name.str : "",
-               (chan->caller.id.number.valid && chan->caller.id.number.str)
-                       ? chan->caller.id.number.str : "");
+               (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str)
+                       ? ast_channel_caller(chan)->id.name.str : "",
+               (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str)
+                       ? ast_channel_caller(chan)->id.number.str : "");
 
        ast_debug(1, "Executing: %s\n", arguments);
        ast_safe_system(arguments);
@@ -1935,17 +1938,17 @@ static int leave_voicemail(struct ast_channel *chan, char *username, struct leav
                ast_strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm);
 
                ast_callerid_merge(callerid, sizeof(callerid),
-                       S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL),
-                       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
+                       S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL),
+                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
                        "Unknown");
                snprintf(logbuf, sizeof(logbuf),
                        /* "Mailbox:domain:macrocontext:exten:priority:callerchan:callerid:origdate:origtime:duration:durationstatus:accountcode" */
                        "%s:%s:%s:%s:%d:%s:%s:%s:%s:%d:%s:%s\n",
                        username,
-                       chan->context,
-                       chan->macrocontext, 
-                       chan->exten,
-                       chan->priority,
+                       ast_channel_context(chan),
+                       ast_channel_macrocontext(chan), 
+                       ast_channel_exten(chan),
+                       ast_channel_priority(chan),
                        ast_channel_name(chan),
                        callerid,
                        date, 
@@ -2048,10 +2051,6 @@ static int minivm_mwi_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
        tmpptr = ast_strdupa((char *)data);
-       if (!tmpptr) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               return -1;
-       }
        argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
        if (argc < 4) {
                ast_log(LOG_ERROR, "%d arguments passed to MiniVM_MWI, need 4.\n", argc);
@@ -2085,7 +2084,7 @@ static int minivm_notify_exec(struct ast_channel *chan, const char *data)
        char *domain;
        char *tmpptr;
        struct minivm_account *vmu;
-       char *username = argv[0];
+       char *username;
        const char *template = "";
        const char *filename;
        const char *format;
@@ -2096,10 +2095,6 @@ static int minivm_notify_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
        tmpptr = ast_strdupa((char *)data);
-       if (!tmpptr) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               return -1;
-       }
        argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
 
        if (argc == 2 && !ast_strlen_zero(argv[1]))
@@ -2141,8 +2136,8 @@ static int minivm_notify_exec(struct ast_channel *chan, const char *data)
                ast_channel_unlock(chan);
                res = notify_new_message(chan, template, vmu, filename, atoi(duration_string),
                        format,
-                       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
-                       S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL));
+                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
+                       S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL));
        }
 
        pbx_builtin_setvar_helper(chan, "MVM_NOTIFY_STATUS", res == 0 ? "SUCCESS" : "FAILED");
@@ -2172,7 +2167,7 @@ static int minivm_record_exec(struct ast_channel *chan, const char *data)
        memset(&leave_options, 0, sizeof(leave_options));
 
        /* Answer channel if it's not already answered */
-       if (chan->_state != AST_STATE_UP)
+       if (ast_channel_state(chan) != AST_STATE_UP)
                ast_answer(chan);
 
        if (ast_strlen_zero(data))  {
@@ -2180,10 +2175,6 @@ static int minivm_record_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
        tmp = ast_strdupa((char *)data);
-       if (!tmp) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               return -1;
-       }
        argc = ast_app_separate_args(tmp, ',', argv, ARRAY_LEN(argv));
        if (argc == 2) {
                if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1])) {
@@ -2243,10 +2234,6 @@ static int minivm_greet_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
        tmpptr = ast_strdupa((char *)data);
-       if (!tmpptr) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               return -1;
-       }
        argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
 
        if (argc == 2) {
@@ -2274,7 +2261,7 @@ static int minivm_greet_exec(struct ast_channel *chan, const char *data)
        }
 
        /* Answer channel if it's not already answered */
-       if (chan->_state != AST_STATE_UP)
+       if (ast_channel_state(chan) != AST_STATE_UP)
                ast_answer(chan);
 
        /* Setup pre-file if appropriate */
@@ -2304,18 +2291,18 @@ static int minivm_greet_exec(struct ast_channel *chan, const char *data)
        if (ast_test_flag(vmu, MVM_OPERATOR)) {
                if (!ast_strlen_zero(vmu->exit)) {
                        if (ast_exists_extension(chan, vmu->exit, "o", 1,
-                               S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
+                               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                                strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
                                ouseexten = 1;
                        }
-               } else if (ast_exists_extension(chan, chan->context, "o", 1,
-                       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
+               } else if (ast_exists_extension(chan, ast_channel_context(chan), "o", 1,
+                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                        strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
                        ouseexten = 1;
                }
-               else if (!ast_strlen_zero(chan->macrocontext)
-                       && ast_exists_extension(chan, chan->macrocontext, "o", 1,
-                               S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
+               else if (!ast_strlen_zero(ast_channel_macrocontext(chan))
+                       && ast_exists_extension(chan, ast_channel_macrocontext(chan), "o", 1,
+                               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                        strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1);
                        ousemacro = 1;
                }
@@ -2323,15 +2310,15 @@ static int minivm_greet_exec(struct ast_channel *chan, const char *data)
 
        if (!ast_strlen_zero(vmu->exit)) {
                if (ast_exists_extension(chan, vmu->exit, "a", 1,
-                       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
+                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                        strncat(ecodes, "*", sizeof(ecodes) -  strlen(ecodes) - 1);
                }
-       } else if (ast_exists_extension(chan, chan->context, "a", 1,
-               S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
+       } else if (ast_exists_extension(chan, ast_channel_context(chan), "a", 1,
+               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                strncat(ecodes, "*", sizeof(ecodes) -  strlen(ecodes) - 1);
-       } else if (!ast_strlen_zero(chan->macrocontext)
-               && ast_exists_extension(chan, chan->macrocontext, "a", 1,
-                       S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
+       } else if (!ast_strlen_zero(ast_channel_macrocontext(chan))
+               && ast_exists_extension(chan, ast_channel_macrocontext(chan), "a", 1,
+                       S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
                strncat(ecodes, "*", sizeof(ecodes) -  strlen(ecodes) - 1);
                ausemacro = 1;
        }
@@ -2371,27 +2358,25 @@ static int minivm_greet_exec(struct ast_channel *chan, const char *data)
        /* Check for a '*' here in case the caller wants to escape from voicemail to something
           other than the operator -- an automated attendant or mailbox login for example */
        if (res == '*') {
-               chan->exten[0] = 'a';
-               chan->exten[1] = '\0';
+               ast_channel_exten_set(chan, "a");
                if (!ast_strlen_zero(vmu->exit)) {
-                       ast_copy_string(chan->context, vmu->exit, sizeof(chan->context));
-               } else if (ausemacro && !ast_strlen_zero(chan->macrocontext)) {
-                       ast_copy_string(chan->context, chan->macrocontext, sizeof(chan->context));
+                       ast_channel_context_set(chan, vmu->exit);
+               } else if (ausemacro && !ast_strlen_zero(ast_channel_macrocontext(chan))) {
+                       ast_channel_context_set(chan, ast_channel_macrocontext(chan));
                }
-               chan->priority = 0;
+               ast_channel_priority_set(chan, 0);
                pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "USEREXIT");
                res = 0;
        } else if (res == '0') { /* Check for a '0' here */
                if(ouseexten || ousemacro) {
-                       chan->exten[0] = 'o';
-                       chan->exten[1] = '\0';
+                       ast_channel_exten_set(chan, "o");
                        if (!ast_strlen_zero(vmu->exit)) {
-                               ast_copy_string(chan->context, vmu->exit, sizeof(chan->context));
-                       } else if (ousemacro && !ast_strlen_zero(chan->macrocontext)) {
-                               ast_copy_string(chan->context, chan->macrocontext, sizeof(chan->context));
+                               ast_channel_context_set(chan, vmu->exit);
+                       } else if (ousemacro && !ast_strlen_zero(ast_channel_macrocontext(chan))) {
+                               ast_channel_context_set(chan, ast_channel_macrocontext(chan));
                        }
                        ast_play_and_wait(chan, "transfer");
-                       chan->priority = 0;
+                       ast_channel_priority_set(chan, 0);
                        pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "USEREXIT");
                }
                res =  0;
@@ -2459,7 +2444,7 @@ static int minivm_accmess_exec(struct ast_channel *chan, const char *data)
        char *domain;
        char *tmpptr = NULL;
        struct minivm_account *vmu;
-       char *username = argv[0];
+       char *username;
        struct ast_flags flags = { 0 };
        char *opts[OPT_ARG_ARRAY_SIZE];
        int error = FALSE;
@@ -2470,14 +2455,9 @@ static int minivm_accmess_exec(struct ast_channel *chan, const char *data)
        if (ast_strlen_zero(data))  {
                ast_log(LOG_ERROR, "MinivmAccmess needs at least two arguments: account and option\n");
                error = TRUE;
-       } else 
+       } else {
                tmpptr = ast_strdupa((char *)data);
-       if (!error) {
-               if (!tmpptr) {
-                       ast_log(LOG_ERROR, "Out of memory\n");
-                       error = TRUE;
-               } else
-                       argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
+               argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv));
        }
 
        if (argc <=1) {
@@ -2520,7 +2500,7 @@ static int minivm_accmess_exec(struct ast_channel *chan, const char *data)
        }
 
        /* Answer channel if it's not already answered */
-       if (chan->_state != AST_STATE_UP)
+       if (ast_channel_state(chan) != AST_STATE_UP)
                ast_answer(chan);
        
        /* Here's where the action is */
@@ -2617,7 +2597,7 @@ static int create_vmaccount(char *name, struct ast_variable *var, int realtime)
                        char *varname = ast_strdupa(var->value);
                        struct ast_variable *tmpvar;
 
-                       if (varname && (varval = strchr(varname, '='))) {
+                       if ((varval = strchr(varname, '='))) {
                                *varval = '\0';
                                varval++;
                                if ((tmpvar = ast_variable_new(varname, varval, ""))) {
@@ -2675,11 +2655,6 @@ static int timezone_add(const char *zonename, const char *config)
                return 0;
 
        msg_format = ast_strdupa(config);
-       if (msg_format == NULL) {
-               ast_log(LOG_WARNING, "Out of memory.\n");
-               ast_free(newzone);
-               return 0;
-       }
 
        timezone_str = strsep(&msg_format, "|");
        if (!msg_format) {
@@ -3195,10 +3170,7 @@ static int minivm_account_func_read(struct ast_channel *chan, const char *cmd, c
        struct minivm_account *vmu;
        char *username, *domain, *colname;
 
-       if (!(username = ast_strdupa(data))) {
-               ast_log(LOG_ERROR, "Memory Error!\n");
-               return -1;
-       }
+       username = ast_strdupa(data);
 
        if ((colname = strchr(username, ':'))) {
                *colname = '\0';
@@ -3348,10 +3320,8 @@ static int minivm_counter_func_read(struct ast_channel *chan, const char *cmd, c
 
        *buf = '\0';
 
-       if (!(username = ast_strdupa(data))) {  /* Copy indata to local buffer */
-               ast_log(LOG_WARNING, "Memory error!\n");
-               return -1;
-       }
+       username = ast_strdupa(data);
+
        if ((countername = strchr(username, ':'))) {
                *countername = '\0';
                countername++;
@@ -3399,7 +3369,6 @@ static int minivm_counter_func_write(struct ast_channel *chan, const char *cmd,
 {
        char *username, *domain, *countername, *operand;
        char userpath[BUFSIZ];
-       struct minivm_account *vmu;
        int change = 0;
        int operation = 0;
 
@@ -3407,10 +3376,7 @@ static int minivm_counter_func_write(struct ast_channel *chan, const char *cmd,
                return -1;
        change = atoi(value);
 
-       if (!(username = ast_strdupa(data))) {  /* Copy indata to local buffer */
-               ast_log(LOG_WARNING, "Memory error!\n");
-               return -1;
-       }
+       username = ast_strdupa(data);
 
        if ((countername = strchr(username, ':'))) {
                *countername = '\0';
@@ -3444,7 +3410,7 @@ static int minivm_counter_func_write(struct ast_channel *chan, const char *cmd,
        }
 
        /* If we can't find account or if the account is temporary, return. */
-       if (!ast_strlen_zero(username) && !(vmu = find_account(domain, username, FALSE))) {
+       if (!ast_strlen_zero(username) && !find_account(domain, username, FALSE)) {
                ast_log(LOG_ERROR, "Minivm account does not exist: %s@%s\n", username, domain);
                return 0;
        }