app_playback.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Trivial application to playback a sound file
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/app.h"
  35. /* This file provides config-file based 'say' functions, and implenents
  36. * some CLI commands.
  37. */
  38. #include "asterisk/say.h" /*!< provides config-file based 'say' functions */
  39. #include "asterisk/cli.h"
  40. /*** DOCUMENTATION
  41. <application name="Playback" language="en_US">
  42. <synopsis>
  43. Play a file.
  44. </synopsis>
  45. <syntax>
  46. <parameter name="filenames" required="true" argsep="&amp;">
  47. <argument name="filename" required="true" />
  48. <argument name="filename2" multiple="true" />
  49. </parameter>
  50. <parameter name="options">
  51. <para>Comma separated list of options</para>
  52. <optionlist>
  53. <option name="skip">
  54. <para>Do not play if not answered</para>
  55. </option>
  56. <option name="noanswer">
  57. <para>Playback without answering, otherwise the channel will
  58. be answered before the sound is played.</para>
  59. <note><para>Not all channel types support playing messages while still on hook.</para></note>
  60. </option>
  61. </optionlist>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>Plays back given filenames (do not put extension of wav/alaw etc).
  66. The playback command answer the channel if no options are specified.
  67. If the file is non-existant it will fail</para>
  68. <para>This application sets the following channel variable upon completion:</para>
  69. <variablelist>
  70. <variable name="PLAYBACKSTATUS">
  71. <para>The status of the playback attempt as a text string.</para>
  72. <value name="SUCCESS"/>
  73. <value name="FAILED"/>
  74. </variable>
  75. </variablelist>
  76. <para>See Also: Background (application) -- for playing sound files that are interruptible</para>
  77. <para>WaitExten (application) -- wait for digits from caller, optionally play music on hold</para>
  78. </description>
  79. <see-also>
  80. <ref type="application">Background</ref>
  81. <ref type="application">WaitExten</ref>
  82. <ref type="application">ControlPlayback</ref>
  83. <ref type="agi">stream file</ref>
  84. <ref type="agi">control stream file</ref>
  85. <ref type="manager">ControlPlayback</ref>
  86. </see-also>
  87. </application>
  88. ***/
  89. static char *app = "Playback";
  90. static struct ast_config *say_cfg = NULL;
  91. /*! \brief save the say' api calls.
  92. * The first entry is NULL if we have the standard source,
  93. * otherwise we are sourcing from here.
  94. * 'say load [new|old]' will enable the new or old method, or report status
  95. */
  96. static const void *say_api_buf[40];
  97. static const char * const say_old = "old";
  98. static const char * const say_new = "new";
  99. static void save_say_mode(const void *arg)
  100. {
  101. int i = 0;
  102. say_api_buf[i++] = arg;
  103. say_api_buf[i++] = ast_say_number_full;
  104. say_api_buf[i++] = ast_say_enumeration_full;
  105. say_api_buf[i++] = ast_say_digit_str_full;
  106. say_api_buf[i++] = ast_say_character_str_full;
  107. say_api_buf[i++] = ast_say_phonetic_str_full;
  108. say_api_buf[i++] = ast_say_datetime;
  109. say_api_buf[i++] = ast_say_time;
  110. say_api_buf[i++] = ast_say_date;
  111. say_api_buf[i++] = ast_say_datetime_from_now;
  112. say_api_buf[i++] = ast_say_date_with_format;
  113. }
  114. static void restore_say_mode(void *arg)
  115. {
  116. int i = 0;
  117. say_api_buf[i++] = arg;
  118. ast_say_number_full = say_api_buf[i++];
  119. ast_say_enumeration_full = say_api_buf[i++];
  120. ast_say_digit_str_full = say_api_buf[i++];
  121. ast_say_character_str_full = say_api_buf[i++];
  122. ast_say_phonetic_str_full = say_api_buf[i++];
  123. ast_say_datetime = say_api_buf[i++];
  124. ast_say_time = say_api_buf[i++];
  125. ast_say_date = say_api_buf[i++];
  126. ast_say_datetime_from_now = say_api_buf[i++];
  127. ast_say_date_with_format = say_api_buf[i++];
  128. }
  129. /*! \brief
  130. * Typical 'say' arguments in addition to the date or number or string
  131. * to say. We do not include 'options' because they may be different
  132. * in recursive calls, and so they are better left as an external
  133. * parameter.
  134. */
  135. typedef struct {
  136. struct ast_channel *chan;
  137. const char *ints;
  138. const char *language;
  139. int audiofd;
  140. int ctrlfd;
  141. } say_args_t;
  142. static int s_streamwait3(const say_args_t *a, const char *fn)
  143. {
  144. int res = ast_streamfile(a->chan, fn, a->language);
  145. if (res) {
  146. ast_log(LOG_WARNING, "Unable to play message %s\n", fn);
  147. return res;
  148. }
  149. res = (a->audiofd > -1 && a->ctrlfd > -1) ?
  150. ast_waitstream_full(a->chan, a->ints, a->audiofd, a->ctrlfd) :
  151. ast_waitstream(a->chan, a->ints);
  152. ast_stopstream(a->chan);
  153. return res;
  154. }
  155. /*! \brief
  156. * the string is 'prefix:data' or prefix:fmt:data'
  157. * with ':' being invalid in strings.
  158. */
  159. static int do_say(say_args_t *a, const char *s, const char *options, int depth)
  160. {
  161. struct ast_variable *v;
  162. char *lang, *x, *rule = NULL;
  163. int ret = 0;
  164. struct varshead head = { .first = NULL, .last = NULL };
  165. struct ast_var_t *n;
  166. ast_debug(2, "string <%s> depth <%d>\n", s, depth);
  167. if (depth++ > 10) {
  168. ast_log(LOG_WARNING, "recursion too deep, exiting\n");
  169. return -1;
  170. } else if (!say_cfg) {
  171. ast_log(LOG_WARNING, "no say.conf, cannot spell '%s'\n", s);
  172. return -1;
  173. }
  174. /* scan languages same as in file.c */
  175. if (a->language == NULL)
  176. a->language = "en"; /* default */
  177. ast_debug(2, "try <%s> in <%s>\n", s, a->language);
  178. lang = ast_strdupa(a->language);
  179. for (;;) {
  180. for (v = ast_variable_browse(say_cfg, lang); v ; v = v->next) {
  181. if (ast_extension_match(v->name, s)) {
  182. rule = ast_strdupa(v->value);
  183. break;
  184. }
  185. }
  186. if (rule)
  187. break;
  188. if ( (x = strchr(lang, '_')) )
  189. *x = '\0'; /* try without suffix */
  190. else if (strcmp(lang, "en"))
  191. lang = "en"; /* last resort, try 'en' if not done yet */
  192. else
  193. break;
  194. }
  195. if (!rule)
  196. return 0;
  197. /* skip up to two prefixes to get the value */
  198. if ( (x = strchr(s, ':')) )
  199. s = x + 1;
  200. if ( (x = strchr(s, ':')) )
  201. s = x + 1;
  202. ast_debug(2, "value is <%s>\n", s);
  203. n = ast_var_assign("SAY", s);
  204. if (!n) {
  205. ast_log(LOG_ERROR, "Memory allocation error in do_say\n");
  206. return -1;
  207. }
  208. AST_LIST_INSERT_HEAD(&head, n, entries);
  209. /* scan the body, one piece at a time */
  210. while ( !ret && (x = strsep(&rule, ",")) ) { /* exit on key */
  211. char fn[128];
  212. const char *p, *fmt, *data; /* format and data pointers */
  213. /* prepare a decent file name */
  214. x = ast_skip_blanks(x);
  215. ast_trim_blanks(x);
  216. /* replace variables */
  217. pbx_substitute_variables_varshead(&head, x, fn, sizeof(fn));
  218. ast_debug(2, "doing [%s]\n", fn);
  219. /* locate prefix and data, if any */
  220. fmt = strchr(fn, ':');
  221. if (!fmt || fmt == fn) { /* regular filename */
  222. ret = s_streamwait3(a, fn);
  223. continue;
  224. }
  225. fmt++;
  226. data = strchr(fmt, ':'); /* colon before data */
  227. if (!data || data == fmt) { /* simple prefix-fmt */
  228. ret = do_say(a, fn, options, depth);
  229. continue;
  230. }
  231. /* prefix:fmt:data */
  232. for (p = fmt; p < data && ret <= 0; p++) {
  233. char fn2[sizeof(fn)];
  234. if (*p == ' ' || *p == '\t') /* skip blanks */
  235. continue;
  236. if (*p == '\'') {/* file name - we trim them */
  237. char *y;
  238. strcpy(fn2, ast_skip_blanks(p+1)); /* make a full copy */
  239. y = strchr(fn2, '\'');
  240. if (!y) {
  241. p = data; /* invalid. prepare to end */
  242. break;
  243. }
  244. *y = '\0';
  245. ast_trim_blanks(fn2);
  246. p = strchr(p+1, '\'');
  247. ret = s_streamwait3(a, fn2);
  248. } else {
  249. int l = fmt-fn;
  250. strcpy(fn2, fn); /* copy everything */
  251. /* after prefix, append the format */
  252. fn2[l++] = *p;
  253. strcpy(fn2 + l, data);
  254. ret = do_say(a, fn2, options, depth);
  255. }
  256. if (ret) {
  257. break;
  258. }
  259. }
  260. }
  261. ast_var_delete(n);
  262. return ret;
  263. }
  264. static int say_full(struct ast_channel *chan, const char *string,
  265. const char *ints, const char *lang, const char *options,
  266. int audiofd, int ctrlfd)
  267. {
  268. say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
  269. return do_say(&a, string, options, 0);
  270. }
  271. static int say_number_full(struct ast_channel *chan, int num,
  272. const char *ints, const char *lang, const char *options,
  273. int audiofd, int ctrlfd)
  274. {
  275. char buf[64];
  276. say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
  277. snprintf(buf, sizeof(buf), "num:%d", num);
  278. return do_say(&a, buf, options, 0);
  279. }
  280. static int say_enumeration_full(struct ast_channel *chan, int num,
  281. const char *ints, const char *lang, const char *options,
  282. int audiofd, int ctrlfd)
  283. {
  284. char buf[64];
  285. say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
  286. snprintf(buf, sizeof(buf), "enum:%d", num);
  287. return do_say(&a, buf, options, 0);
  288. }
  289. static int say_date_generic(struct ast_channel *chan, time_t t,
  290. const char *ints, const char *lang, const char *format, const char *timezonename, const char *prefix)
  291. {
  292. char buf[128];
  293. struct ast_tm tm;
  294. struct timeval when = { t, 0 };
  295. say_args_t a = { chan, ints, lang, -1, -1 };
  296. if (format == NULL)
  297. format = "";
  298. ast_localtime(&when, &tm, timezonename);
  299. snprintf(buf, sizeof(buf), "%s:%s:%04d%02d%02d%02d%02d.%02d-%d-%3d",
  300. prefix,
  301. format,
  302. tm.tm_year+1900,
  303. tm.tm_mon+1,
  304. tm.tm_mday,
  305. tm.tm_hour,
  306. tm.tm_min,
  307. tm.tm_sec,
  308. tm.tm_wday,
  309. tm.tm_yday);
  310. return do_say(&a, buf, NULL, 0);
  311. }
  312. static int say_date_with_format(struct ast_channel *chan, time_t t,
  313. const char *ints, const char *lang, const char *format, const char *timezonename)
  314. {
  315. return say_date_generic(chan, t, ints, lang, format, timezonename, "datetime");
  316. }
  317. static int say_date(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
  318. {
  319. return say_date_generic(chan, t, ints, lang, "", NULL, "date");
  320. }
  321. static int say_time(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
  322. {
  323. return say_date_generic(chan, t, ints, lang, "", NULL, "time");
  324. }
  325. static int say_datetime(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
  326. {
  327. return say_date_generic(chan, t, ints, lang, "", NULL, "datetime");
  328. }
  329. /*! \brief
  330. * remap the 'say' functions to use those in this file
  331. */
  332. static int say_init_mode(const char *mode) {
  333. if (!strcmp(mode, say_new)) {
  334. if (say_cfg == NULL) {
  335. ast_log(LOG_ERROR, "There is no say.conf file to use new mode\n");
  336. return -1;
  337. }
  338. save_say_mode(say_new);
  339. ast_say_number_full = say_number_full;
  340. ast_say_enumeration_full = say_enumeration_full;
  341. #if 0
  342. /*! \todo XXX
  343. These functions doesn't exist.
  344. say.conf.sample indicates this is working...
  345. */
  346. ast_say_digits_full = say_digits_full;
  347. ast_say_digit_str_full = say_digit_str_full;
  348. ast_say_character_str_full = say_character_str_full;
  349. ast_say_phonetic_str_full = say_phonetic_str_full;
  350. ast_say_datetime_from_now = say_datetime_from_now;
  351. #endif
  352. ast_say_datetime = say_datetime;
  353. ast_say_time = say_time;
  354. ast_say_date = say_date;
  355. ast_say_date_with_format = say_date_with_format;
  356. } else if (!strcmp(mode, say_old) && say_api_buf[0] == say_new) {
  357. restore_say_mode(NULL);
  358. } else if (strcmp(mode, say_old)) {
  359. ast_log(LOG_WARNING, "unrecognized mode %s\n", mode);
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. static char *__say_cli_init(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  365. {
  366. const char *old_mode = say_api_buf[0] ? say_new : say_old;
  367. const char *mode;
  368. switch (cmd) {
  369. case CLI_INIT:
  370. e->command = "say load [new|old]";
  371. e->usage =
  372. "Usage: say load [new|old]\n"
  373. " say load\n"
  374. " Report status of current say mode\n"
  375. " say load new\n"
  376. " Set say method, configured in say.conf\n"
  377. " say load old\n"
  378. " Set old say method, coded in asterisk core\n";
  379. return NULL;
  380. case CLI_GENERATE:
  381. return NULL;
  382. }
  383. if (a->argc == 2) {
  384. ast_cli(a->fd, "say mode is [%s]\n", old_mode);
  385. return CLI_SUCCESS;
  386. } else if (a->argc != e->args)
  387. return CLI_SHOWUSAGE;
  388. mode = a->argv[2];
  389. if (!strcmp(mode, old_mode))
  390. ast_cli(a->fd, "say mode is %s already\n", mode);
  391. else
  392. if (say_init_mode(mode) == 0)
  393. ast_cli(a->fd, "setting say mode from %s to %s\n", old_mode, mode);
  394. return CLI_SUCCESS;
  395. }
  396. static struct ast_cli_entry cli_playback[] = {
  397. AST_CLI_DEFINE(__say_cli_init, "Set or show the say mode"),
  398. };
  399. static int playback_exec(struct ast_channel *chan, const char *data)
  400. {
  401. int res = 0;
  402. int mres = 0;
  403. char *tmp;
  404. int option_skip=0;
  405. int option_say=0;
  406. int option_noanswer = 0;
  407. AST_DECLARE_APP_ARGS(args,
  408. AST_APP_ARG(filenames);
  409. AST_APP_ARG(options);
  410. );
  411. if (ast_strlen_zero(data)) {
  412. ast_log(LOG_WARNING, "Playback requires an argument (filename)\n");
  413. return -1;
  414. }
  415. tmp = ast_strdupa(data);
  416. AST_STANDARD_APP_ARGS(args, tmp);
  417. if (args.options) {
  418. if (strcasestr(args.options, "skip"))
  419. option_skip = 1;
  420. if (strcasestr(args.options, "say"))
  421. option_say = 1;
  422. if (strcasestr(args.options, "noanswer"))
  423. option_noanswer = 1;
  424. }
  425. if (ast_channel_state(chan) != AST_STATE_UP) {
  426. if (option_skip) {
  427. /* At the user's option, skip if the line is not up */
  428. goto done;
  429. } else if (!option_noanswer) {
  430. /* Otherwise answer unless we're supposed to send this while on-hook */
  431. res = ast_answer(chan);
  432. }
  433. }
  434. if (!res) {
  435. char *back = args.filenames;
  436. char *front;
  437. ast_stopstream(chan);
  438. while (!res && (front = strsep(&back, "&"))) {
  439. if (option_say)
  440. res = say_full(chan, front, "", ast_channel_language(chan), NULL, -1, -1);
  441. else
  442. res = ast_streamfile(chan, front, ast_channel_language(chan));
  443. if (!res) {
  444. res = ast_waitstream(chan, "");
  445. ast_stopstream(chan);
  446. }
  447. if (res) {
  448. if (!ast_check_hangup(chan)) {
  449. ast_log(LOG_WARNING, "Playback failed on %s for %s\n", ast_channel_name(chan), (char *)data);
  450. }
  451. res = 0;
  452. mres = 1;
  453. }
  454. }
  455. }
  456. done:
  457. pbx_builtin_setvar_helper(chan, "PLAYBACKSTATUS", mres ? "FAILED" : "SUCCESS");
  458. return res;
  459. }
  460. static int reload(void)
  461. {
  462. struct ast_variable *v;
  463. struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
  464. struct ast_config *newcfg;
  465. if ((newcfg = ast_config_load("say.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
  466. return 0;
  467. } else if (newcfg == CONFIG_STATUS_FILEINVALID) {
  468. ast_log(LOG_ERROR, "Config file say.conf is in an invalid format. Aborting.\n");
  469. return 0;
  470. }
  471. if (say_cfg) {
  472. ast_config_destroy(say_cfg);
  473. ast_log(LOG_NOTICE, "Reloading say.conf\n");
  474. }
  475. say_cfg = newcfg;
  476. if (say_cfg) {
  477. for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
  478. if (ast_extension_match(v->name, "mode")) {
  479. say_init_mode(v->value);
  480. break;
  481. }
  482. }
  483. }
  484. /*! \todo
  485. * XXX here we should sort rules according to the same order
  486. * we have in pbx.c so we have the same matching behaviour.
  487. */
  488. return 0;
  489. }
  490. static int unload_module(void)
  491. {
  492. int res;
  493. res = ast_unregister_application(app);
  494. ast_cli_unregister_multiple(cli_playback, ARRAY_LEN(cli_playback));
  495. if (say_cfg)
  496. ast_config_destroy(say_cfg);
  497. return res;
  498. }
  499. static int load_module(void)
  500. {
  501. struct ast_variable *v;
  502. struct ast_flags config_flags = { 0 };
  503. say_cfg = ast_config_load("say.conf", config_flags);
  504. if (say_cfg && say_cfg != CONFIG_STATUS_FILEINVALID) {
  505. for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
  506. if (ast_extension_match(v->name, "mode")) {
  507. say_init_mode(v->value);
  508. break;
  509. }
  510. }
  511. }
  512. ast_cli_register_multiple(cli_playback, ARRAY_LEN(cli_playback));
  513. return ast_register_application_xml(app, playback_exec);
  514. }
  515. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Sound File Playback Application",
  516. .support_level = AST_MODULE_SUPPORT_CORE,
  517. .load = load_module,
  518. .unload = unload_module,
  519. .reload = reload,
  520. );