app_playback.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. </application>
  80. ***/
  81. static char *app = "Playback";
  82. static struct ast_config *say_cfg = NULL;
  83. /* save the say' api calls.
  84. * The first entry is NULL if we have the standard source,
  85. * otherwise we are sourcing from here.
  86. * 'say load [new|old]' will enable the new or old method, or report status
  87. */
  88. static const void *say_api_buf[40];
  89. static const char * const say_old = "old";
  90. static const char * const say_new = "new";
  91. static void save_say_mode(const void *arg)
  92. {
  93. int i = 0;
  94. say_api_buf[i++] = arg;
  95. say_api_buf[i++] = ast_say_number_full;
  96. say_api_buf[i++] = ast_say_enumeration_full;
  97. say_api_buf[i++] = ast_say_digit_str_full;
  98. say_api_buf[i++] = ast_say_character_str_full;
  99. say_api_buf[i++] = ast_say_phonetic_str_full;
  100. say_api_buf[i++] = ast_say_datetime;
  101. say_api_buf[i++] = ast_say_time;
  102. say_api_buf[i++] = ast_say_date;
  103. say_api_buf[i++] = ast_say_datetime_from_now;
  104. say_api_buf[i++] = ast_say_date_with_format;
  105. }
  106. static void restore_say_mode(void *arg)
  107. {
  108. int i = 0;
  109. say_api_buf[i++] = arg;
  110. ast_say_number_full = say_api_buf[i++];
  111. ast_say_enumeration_full = say_api_buf[i++];
  112. ast_say_digit_str_full = say_api_buf[i++];
  113. ast_say_character_str_full = say_api_buf[i++];
  114. ast_say_phonetic_str_full = say_api_buf[i++];
  115. ast_say_datetime = say_api_buf[i++];
  116. ast_say_time = say_api_buf[i++];
  117. ast_say_date = say_api_buf[i++];
  118. ast_say_datetime_from_now = say_api_buf[i++];
  119. ast_say_date_with_format = say_api_buf[i++];
  120. }
  121. /*
  122. * Typical 'say' arguments in addition to the date or number or string
  123. * to say. We do not include 'options' because they may be different
  124. * in recursive calls, and so they are better left as an external
  125. * parameter.
  126. */
  127. typedef struct {
  128. struct ast_channel *chan;
  129. const char *ints;
  130. const char *language;
  131. int audiofd;
  132. int ctrlfd;
  133. } say_args_t;
  134. static int s_streamwait3(const say_args_t *a, const char *fn)
  135. {
  136. int res = ast_streamfile(a->chan, fn, a->language);
  137. if (res) {
  138. ast_log(LOG_WARNING, "Unable to play message %s\n", fn);
  139. return res;
  140. }
  141. res = (a->audiofd > -1 && a->ctrlfd > -1) ?
  142. ast_waitstream_full(a->chan, a->ints, a->audiofd, a->ctrlfd) :
  143. ast_waitstream(a->chan, a->ints);
  144. ast_stopstream(a->chan);
  145. return res;
  146. }
  147. /*
  148. * the string is 'prefix:data' or prefix:fmt:data'
  149. * with ':' being invalid in strings.
  150. */
  151. static int do_say(say_args_t *a, const char *s, const char *options, int depth)
  152. {
  153. struct ast_variable *v;
  154. char *lang, *x, *rule = NULL;
  155. int ret = 0;
  156. struct varshead head = { .first = NULL, .last = NULL };
  157. struct ast_var_t *n;
  158. ast_debug(2, "string <%s> depth <%d>\n", s, depth);
  159. if (depth++ > 10) {
  160. ast_log(LOG_WARNING, "recursion too deep, exiting\n");
  161. return -1;
  162. } else if (!say_cfg) {
  163. ast_log(LOG_WARNING, "no say.conf, cannot spell '%s'\n", s);
  164. return -1;
  165. }
  166. /* scan languages same as in file.c */
  167. if (a->language == NULL)
  168. a->language = "en"; /* default */
  169. ast_debug(2, "try <%s> in <%s>\n", s, a->language);
  170. lang = ast_strdupa(a->language);
  171. for (;;) {
  172. for (v = ast_variable_browse(say_cfg, lang); v ; v = v->next) {
  173. if (ast_extension_match(v->name, s)) {
  174. rule = ast_strdupa(v->value);
  175. break;
  176. }
  177. }
  178. if (rule)
  179. break;
  180. if ( (x = strchr(lang, '_')) )
  181. *x = '\0'; /* try without suffix */
  182. else if (strcmp(lang, "en"))
  183. lang = "en"; /* last resort, try 'en' if not done yet */
  184. else
  185. break;
  186. }
  187. if (!rule)
  188. return 0;
  189. /* skip up to two prefixes to get the value */
  190. if ( (x = strchr(s, ':')) )
  191. s = x + 1;
  192. if ( (x = strchr(s, ':')) )
  193. s = x + 1;
  194. ast_debug(2, "value is <%s>\n", s);
  195. n = ast_var_assign("SAY", s);
  196. AST_LIST_INSERT_HEAD(&head, n, entries);
  197. /* scan the body, one piece at a time */
  198. while ( !ret && (x = strsep(&rule, ",")) ) { /* exit on key */
  199. char fn[128];
  200. const char *p, *fmt, *data; /* format and data pointers */
  201. /* prepare a decent file name */
  202. x = ast_skip_blanks(x);
  203. ast_trim_blanks(x);
  204. /* replace variables */
  205. pbx_substitute_variables_varshead(&head, x, fn, sizeof(fn));
  206. ast_debug(2, "doing [%s]\n", fn);
  207. /* locate prefix and data, if any */
  208. fmt = strchr(fn, ':');
  209. if (!fmt || fmt == fn) { /* regular filename */
  210. ret = s_streamwait3(a, fn);
  211. continue;
  212. }
  213. fmt++;
  214. data = strchr(fmt, ':'); /* colon before data */
  215. if (!data || data == fmt) { /* simple prefix-fmt */
  216. ret = do_say(a, fn, options, depth);
  217. continue;
  218. }
  219. /* prefix:fmt:data */
  220. for (p = fmt; p < data && ret <= 0; p++) {
  221. char fn2[sizeof(fn)];
  222. if (*p == ' ' || *p == '\t') /* skip blanks */
  223. continue;
  224. if (*p == '\'') {/* file name - we trim them */
  225. char *y;
  226. strcpy(fn2, ast_skip_blanks(p+1)); /* make a full copy */
  227. y = strchr(fn2, '\'');
  228. if (!y) {
  229. p = data; /* invalid. prepare to end */
  230. break;
  231. }
  232. *y = '\0';
  233. ast_trim_blanks(fn2);
  234. p = strchr(p+1, '\'');
  235. ret = s_streamwait3(a, fn2);
  236. } else {
  237. int l = fmt-fn;
  238. strcpy(fn2, fn); /* copy everything */
  239. /* after prefix, append the format */
  240. fn2[l++] = *p;
  241. strcpy(fn2 + l, data);
  242. ret = do_say(a, fn2, options, depth);
  243. }
  244. if (ret) {
  245. break;
  246. }
  247. }
  248. }
  249. ast_var_delete(n);
  250. return ret;
  251. }
  252. static int say_full(struct ast_channel *chan, const char *string,
  253. const char *ints, const char *lang, const char *options,
  254. int audiofd, int ctrlfd)
  255. {
  256. say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
  257. return do_say(&a, string, options, 0);
  258. }
  259. static int say_number_full(struct ast_channel *chan, int num,
  260. const char *ints, const char *lang, const char *options,
  261. int audiofd, int ctrlfd)
  262. {
  263. char buf[64];
  264. say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
  265. snprintf(buf, sizeof(buf), "num:%d", num);
  266. return do_say(&a, buf, options, 0);
  267. }
  268. static int say_enumeration_full(struct ast_channel *chan, int num,
  269. const char *ints, const char *lang, const char *options,
  270. int audiofd, int ctrlfd)
  271. {
  272. char buf[64];
  273. say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
  274. snprintf(buf, sizeof(buf), "enum:%d", num);
  275. return do_say(&a, buf, options, 0);
  276. }
  277. static int say_date_generic(struct ast_channel *chan, time_t t,
  278. const char *ints, const char *lang, const char *format, const char *timezonename, const char *prefix)
  279. {
  280. char buf[128];
  281. struct ast_tm tm;
  282. struct timeval when = { t, 0 };
  283. say_args_t a = { chan, ints, lang, -1, -1 };
  284. if (format == NULL)
  285. format = "";
  286. ast_localtime(&when, &tm, NULL);
  287. snprintf(buf, sizeof(buf), "%s:%s:%04d%02d%02d%02d%02d.%02d-%d-%3d",
  288. prefix,
  289. format,
  290. tm.tm_year+1900,
  291. tm.tm_mon+1,
  292. tm.tm_mday,
  293. tm.tm_hour,
  294. tm.tm_min,
  295. tm.tm_sec,
  296. tm.tm_wday,
  297. tm.tm_yday);
  298. return do_say(&a, buf, NULL, 0);
  299. }
  300. static int say_date_with_format(struct ast_channel *chan, time_t t,
  301. const char *ints, const char *lang, const char *format, const char *timezonename)
  302. {
  303. return say_date_generic(chan, t, ints, lang, format, timezonename, "datetime");
  304. }
  305. static int say_date(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
  306. {
  307. return say_date_generic(chan, t, ints, lang, "", NULL, "date");
  308. }
  309. static int say_time(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
  310. {
  311. return say_date_generic(chan, t, ints, lang, "", NULL, "time");
  312. }
  313. static int say_datetime(struct ast_channel *chan, time_t t, const char *ints, const char *lang)
  314. {
  315. return say_date_generic(chan, t, ints, lang, "", NULL, "datetime");
  316. }
  317. /*
  318. * remap the 'say' functions to use those in this file
  319. */
  320. static int say_init_mode(const char *mode) {
  321. if (!strcmp(mode, say_new)) {
  322. if (say_cfg == NULL) {
  323. ast_log(LOG_ERROR, "There is no say.conf file to use new mode\n");
  324. return -1;
  325. }
  326. save_say_mode(say_new);
  327. ast_say_number_full = say_number_full;
  328. ast_say_enumeration_full = say_enumeration_full;
  329. #if 0
  330. ast_say_digits_full = say_digits_full;
  331. ast_say_digit_str_full = say_digit_str_full;
  332. ast_say_character_str_full = say_character_str_full;
  333. ast_say_phonetic_str_full = say_phonetic_str_full;
  334. ast_say_datetime_from_now = say_datetime_from_now;
  335. #endif
  336. ast_say_datetime = say_datetime;
  337. ast_say_time = say_time;
  338. ast_say_date = say_date;
  339. ast_say_date_with_format = say_date_with_format;
  340. } else if (!strcmp(mode, say_old) && say_api_buf[0] == say_new) {
  341. restore_say_mode(NULL);
  342. } else if (strcmp(mode, say_old)) {
  343. ast_log(LOG_WARNING, "unrecognized mode %s\n", mode);
  344. return -1;
  345. }
  346. return 0;
  347. }
  348. static char *__say_cli_init(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  349. {
  350. const char *old_mode = say_api_buf[0] ? say_new : say_old;
  351. const char *mode;
  352. switch (cmd) {
  353. case CLI_INIT:
  354. e->command = "say load [new|old]";
  355. e->usage =
  356. "Usage: say load [new|old]\n"
  357. " say load\n"
  358. " Report status of current say mode\n"
  359. " say load new\n"
  360. " Set say method, configured in say.conf\n"
  361. " say load old\n"
  362. " Set old say method, coded in asterisk core\n";
  363. return NULL;
  364. case CLI_GENERATE:
  365. return NULL;
  366. }
  367. if (a->argc == 2) {
  368. ast_cli(a->fd, "say mode is [%s]\n", old_mode);
  369. return CLI_SUCCESS;
  370. } else if (a->argc != e->args)
  371. return CLI_SHOWUSAGE;
  372. mode = a->argv[2];
  373. if (!strcmp(mode, old_mode))
  374. ast_cli(a->fd, "say mode is %s already\n", mode);
  375. else
  376. if (say_init_mode(mode) == 0)
  377. ast_cli(a->fd, "setting say mode from %s to %s\n", old_mode, mode);
  378. return CLI_SUCCESS;
  379. }
  380. static struct ast_cli_entry cli_playback[] = {
  381. AST_CLI_DEFINE(__say_cli_init, "Set or show the say mode"),
  382. };
  383. static int playback_exec(struct ast_channel *chan, const char *data)
  384. {
  385. int res = 0;
  386. int mres = 0;
  387. char *tmp;
  388. int option_skip=0;
  389. int option_say=0;
  390. int option_noanswer = 0;
  391. AST_DECLARE_APP_ARGS(args,
  392. AST_APP_ARG(filenames);
  393. AST_APP_ARG(options);
  394. );
  395. if (ast_strlen_zero(data)) {
  396. ast_log(LOG_WARNING, "Playback requires an argument (filename)\n");
  397. return -1;
  398. }
  399. tmp = ast_strdupa(data);
  400. AST_STANDARD_APP_ARGS(args, tmp);
  401. if (args.options) {
  402. if (strcasestr(args.options, "skip"))
  403. option_skip = 1;
  404. if (strcasestr(args.options, "say"))
  405. option_say = 1;
  406. if (strcasestr(args.options, "noanswer"))
  407. option_noanswer = 1;
  408. }
  409. if (chan->_state != AST_STATE_UP) {
  410. if (option_skip) {
  411. /* At the user's option, skip if the line is not up */
  412. goto done;
  413. } else if (!option_noanswer) {
  414. /* Otherwise answer unless we're supposed to send this while on-hook */
  415. res = ast_answer(chan);
  416. }
  417. }
  418. if (!res) {
  419. char *back = args.filenames;
  420. char *front;
  421. ast_stopstream(chan);
  422. while (!res && (front = strsep(&back, "&"))) {
  423. if (option_say)
  424. res = say_full(chan, front, "", chan->language, NULL, -1, -1);
  425. else
  426. res = ast_streamfile(chan, front, chan->language);
  427. if (!res) {
  428. res = ast_waitstream(chan, "");
  429. ast_stopstream(chan);
  430. } else {
  431. ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
  432. res = 0;
  433. mres = 1;
  434. }
  435. }
  436. }
  437. done:
  438. pbx_builtin_setvar_helper(chan, "PLAYBACKSTATUS", mres ? "FAILED" : "SUCCESS");
  439. return res;
  440. }
  441. static int reload(void)
  442. {
  443. struct ast_variable *v;
  444. struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
  445. struct ast_config *newcfg;
  446. if ((newcfg = ast_config_load("say.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
  447. return 0;
  448. } else if (newcfg == CONFIG_STATUS_FILEINVALID) {
  449. ast_log(LOG_ERROR, "Config file say.conf is in an invalid format. Aborting.\n");
  450. return 0;
  451. }
  452. if (say_cfg) {
  453. ast_config_destroy(say_cfg);
  454. ast_log(LOG_NOTICE, "Reloading say.conf\n");
  455. say_cfg = newcfg;
  456. }
  457. if (say_cfg) {
  458. for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
  459. if (ast_extension_match(v->name, "mode")) {
  460. say_init_mode(v->value);
  461. break;
  462. }
  463. }
  464. }
  465. /*
  466. * XXX here we should sort rules according to the same order
  467. * we have in pbx.c so we have the same matching behaviour.
  468. */
  469. return 0;
  470. }
  471. static int unload_module(void)
  472. {
  473. int res;
  474. res = ast_unregister_application(app);
  475. ast_cli_unregister_multiple(cli_playback, ARRAY_LEN(cli_playback));
  476. if (say_cfg)
  477. ast_config_destroy(say_cfg);
  478. return res;
  479. }
  480. static int load_module(void)
  481. {
  482. struct ast_variable *v;
  483. struct ast_flags config_flags = { 0 };
  484. say_cfg = ast_config_load("say.conf", config_flags);
  485. if (say_cfg && say_cfg != CONFIG_STATUS_FILEINVALID) {
  486. for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
  487. if (ast_extension_match(v->name, "mode")) {
  488. say_init_mode(v->value);
  489. break;
  490. }
  491. }
  492. }
  493. ast_cli_register_multiple(cli_playback, ARRAY_LEN(cli_playback));
  494. return ast_register_application_xml(app, playback_exec);
  495. }
  496. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Sound File Playback Application",
  497. .load = load_module,
  498. .unload = unload_module,
  499. .reload = reload,
  500. );