Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[asterisk/asterisk.git] / apps / app_playback.c
index 54e90ff..5303bb4 100644 (file)
  * \brief Trivial application to playback a sound file
  *
  * \author Mark Spencer <markster@digium.com>
- * 
+ *
  * \ingroup applications
  */
-#include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
 
 #include "asterisk/file.h"
 #include "asterisk/pbx.h"
@@ -36,7 +38,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 /* This file provides config-file based 'say' functions, and implenents
  * some CLI commands.
  */
-#include "asterisk/say.h"      /* provides config-file based 'say' functions */
+#include "asterisk/say.h"      /*!< provides config-file based 'say' functions */
 #include "asterisk/cli.h"
 
 /*** DOCUMENTATION
@@ -78,13 +80,22 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <para>See Also: Background (application) -- for playing sound files that are interruptible</para>
                        <para>WaitExten (application) -- wait for digits from caller, optionally play music on hold</para>
                </description>
+               <see-also>
+                       <ref type="application">Background</ref>
+                       <ref type="application">WaitExten</ref>
+                       <ref type="application">ControlPlayback</ref>
+                       <ref type="agi">stream file</ref>
+                       <ref type="agi">control stream file</ref>
+                       <ref type="manager">ControlPlayback</ref>
+               </see-also>
        </application>
  ***/
 
 static char *app = "Playback";
 
 static struct ast_config *say_cfg = NULL;
-/* save the say' api calls.
+
+/*! \brief save the say' api calls.
  * The first entry is NULL if we have the standard source,
  * otherwise we are sourcing from here.
  * 'say load [new|old]' will enable the new or old method, or report status
@@ -127,7 +138,7 @@ static void restore_say_mode(void *arg)
        ast_say_date_with_format = say_api_buf[i++];
 }
 
-/* 
+/*! \brief
  * Typical 'say' arguments in addition to the date or number or string
  * to say. We do not include 'options' because they may be different
  * in recursive calls, and so they are better left as an external
@@ -152,10 +163,10 @@ static int s_streamwait3(const say_args_t *a, const char *fn)
        ast_waitstream_full(a->chan, a->ints, a->audiofd, a->ctrlfd) :
        ast_waitstream(a->chan, a->ints);
        ast_stopstream(a->chan);
-       return res;  
+       return res;
 }
 
-/*
+/*! \brief
  * the string is 'prefix:data' or prefix:fmt:data'
  * with ':' being invalid in strings.
  */
@@ -163,7 +174,7 @@ static int do_say(say_args_t *a, const char *s, const char *options, int depth)
 {
        struct ast_variable *v;
        char *lang, *x, *rule = NULL;
-       int ret = 0;   
+       int ret = 0;
        struct varshead head = { .first = NULL, .last = NULL };
        struct ast_var_t *n;
 
@@ -207,6 +218,10 @@ static int do_say(say_args_t *a, const char *s, const char *options, int depth)
                s = x + 1;
        ast_debug(2, "value is <%s>\n", s);
        n = ast_var_assign("SAY", s);
+       if (!n) {
+               ast_log(LOG_ERROR, "Memory allocation error in do_say\n");
+               return -1;
+       }
        AST_LIST_INSERT_HEAD(&head, n, entries);
 
        /* scan the body, one piece at a time */
@@ -259,7 +274,7 @@ static int do_say(say_args_t *a, const char *s, const char *options, int depth)
                                strcpy(fn2 + l, data);
                                ret = do_say(a, fn2, options, depth);
                        }
-                       
+
                        if (ret) {
                                break;
                        }
@@ -307,7 +322,7 @@ static int say_date_generic(struct ast_channel *chan, time_t t,
        if (format == NULL)
                format = "";
 
-       ast_localtime(&when, &tm, NULL);
+       ast_localtime(&when, &tm, timezonename);
        snprintf(buf, sizeof(buf), "%s:%s:%04d%02d%02d%02d%02d.%02d-%d-%3d",
                prefix,
                format,
@@ -343,7 +358,7 @@ static int say_datetime(struct ast_channel *chan, time_t t, const char *ints, co
        return say_date_generic(chan, t, ints, lang, "", NULL, "datetime");
 }
 
-/*
+/*! \brief
  * remap the 'say' functions to use those in this file
  */
 static int say_init_mode(const char *mode) {
@@ -357,6 +372,10 @@ static int say_init_mode(const char *mode) {
 
                ast_say_enumeration_full = say_enumeration_full;
 #if 0
+               /*! \todo XXX
+                  These functions doesn't exist.
+                  say.conf.sample indicates this is working...
+               */
                ast_say_digits_full = say_digits_full;
                ast_say_digit_str_full = say_digit_str_full;
                ast_say_character_str_full = say_character_str_full;
@@ -373,7 +392,7 @@ static int say_init_mode(const char *mode) {
                ast_log(LOG_WARNING, "unrecognized mode %s\n", mode);
                return -1;
        }
-       
+
        return 0;
 }
 
@@ -384,7 +403,7 @@ static char *__say_cli_init(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
        switch (cmd) {
        case CLI_INIT:
                e->command = "say load [new|old]";
-               e->usage = 
+               e->usage =
                        "Usage: say load [new|old]\n"
                        "       say load\n"
                        "           Report status of current say mode\n"
@@ -428,7 +447,7 @@ static int playback_exec(struct ast_channel *chan, const char *data)
                AST_APP_ARG(filenames);
                AST_APP_ARG(options);
        );
-       
+
        if (ast_strlen_zero(data)) {
                ast_log(LOG_WARNING, "Playback requires an argument (filename)\n");
                return -1;
@@ -444,14 +463,15 @@ static int playback_exec(struct ast_channel *chan, const char *data)
                        option_say = 1;
                if (strcasestr(args.options, "noanswer"))
                        option_noanswer = 1;
-       } 
-       if (chan->_state != AST_STATE_UP) {
+       }
+       if (ast_channel_state(chan) != AST_STATE_UP) {
                if (option_skip) {
                        /* At the user's option, skip if the line is not up */
                        goto done;
-               } else if (!option_noanswer)
+               } else if (!option_noanswer) {
                        /* Otherwise answer unless we're supposed to send this while on-hook */
                        res = ast_answer(chan);
+               }
        }
        if (!res) {
                char *back = args.filenames;
@@ -460,14 +480,17 @@ static int playback_exec(struct ast_channel *chan, const char *data)
                ast_stopstream(chan);
                while (!res && (front = strsep(&back, "&"))) {
                        if (option_say)
-                               res = say_full(chan, front, "", chan->language, NULL, -1, -1);
+                               res = say_full(chan, front, "", ast_channel_language(chan), NULL, -1, -1);
                        else
-                               res = ast_streamfile(chan, front, chan->language);
-                       if (!res) { 
-                               res = ast_waitstream(chan, ""); 
+                               res = ast_streamfile(chan, front, ast_channel_language(chan));
+                       if (!res) {
+                               res = ast_waitstream(chan, "");
                                ast_stopstream(chan);
-                       } else {
-                               ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
+                       }
+                       if (res) {
+                               if (!ast_check_hangup(chan)) {
+                                       ast_log(LOG_WARNING, "Playback failed on %s for %s\n", ast_channel_name(chan), (char *)data);
+                               }
                                res = 0;
                                mres = 1;
                        }
@@ -494,8 +517,8 @@ static int reload(void)
        if (say_cfg) {
                ast_config_destroy(say_cfg);
                ast_log(LOG_NOTICE, "Reloading say.conf\n");
-               say_cfg = newcfg;
        }
+       say_cfg = newcfg;
 
        if (say_cfg) {
                for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
@@ -505,8 +528,8 @@ static int reload(void)
                        }
                }
        }
-       
-       /*
+
+       /*! \todo
         * XXX here we should sort rules according to the same order
         * we have in pbx.c so we have the same matching behaviour.
         */
@@ -524,7 +547,7 @@ static int unload_module(void)
        if (say_cfg)
                ast_config_destroy(say_cfg);
 
-       return res;     
+       return res;
 }
 
 static int load_module(void)
@@ -547,7 +570,8 @@ static int load_module(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Sound File Playback Application",
-               .load = load_module,
-               .unload = unload_module,
-               .reload = reload,
-              );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .reload = reload,
+);