app_externalivr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Kevin P. Fleming <kpfleming@digium.com>
  7. *
  8. * Portions taken from the file-based music-on-hold work
  9. * created by Anthony Minessale II in res_musiconhold.c
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. * This program is free software, distributed under the terms of
  18. * the GNU General Public License Version 2. See the LICENSE file
  19. * at the top of the source tree.
  20. */
  21. /*! \file
  22. *
  23. * \brief External IVR application interface
  24. *
  25. * \author Kevin P. Fleming <kpfleming@digium.com>
  26. *
  27. * \note Portions taken from the file-based music-on-hold work
  28. * created by Anthony Minessale II in res_musiconhold.c
  29. *
  30. * \ingroup applications
  31. */
  32. /*** MODULEINFO
  33. <support_level>extended</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include <signal.h>
  38. #include "asterisk/lock.h"
  39. #include "asterisk/file.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/pbx.h"
  42. #include "asterisk/module.h"
  43. #include "asterisk/linkedlists.h"
  44. #include "asterisk/app.h"
  45. #include "asterisk/utils.h"
  46. #include "asterisk/tcptls.h"
  47. #include "asterisk/astobj2.h"
  48. /*** DOCUMENTATION
  49. <application name="ExternalIVR" language="en_US">
  50. <synopsis>
  51. Interfaces with an external IVR application.
  52. </synopsis>
  53. <syntax>
  54. <parameter name="command|ivr://host" required="true" hasparams="true">
  55. <argument name="arg1" />
  56. <argument name="arg2" multiple="yes" />
  57. </parameter>
  58. <parameter name="options">
  59. <optionlist>
  60. <option name="n">
  61. <para>Tells ExternalIVR() not to answer the channel.</para>
  62. </option>
  63. <option name="i">
  64. <para>Tells ExternalIVR() not to send a hangup and exit when the
  65. channel receives a hangup, instead it sends an <literal>I</literal>
  66. informative message meaning that the external application MUST hang
  67. up the call with an <literal>H</literal> command.</para>
  68. </option>
  69. <option name="d">
  70. <para>Tells ExternalIVR() to run on a channel that has been hung up
  71. and will not look for hangups. The external application must exit with
  72. an <literal>E</literal> command.</para>
  73. </option>
  74. </optionlist>
  75. </parameter>
  76. </syntax>
  77. <description>
  78. <para>Either forks a process to run given command or makes a socket to connect
  79. to given host and starts a generator on the channel. The generator's play list
  80. is controlled by the external application, which can add and clear entries via
  81. simple commands issued over its stdout. The external application will receive
  82. all DTMF events received on the channel, and notification if the channel is
  83. hung up. The received on the channel, and notification if the channel is hung
  84. up. The application will not be forcibly terminated when the channel is hung up.
  85. For more information see <filename>doc/AST.pdf</filename>.</para>
  86. </description>
  87. </application>
  88. ***/
  89. static const char app[] = "ExternalIVR";
  90. /* XXX the parser in gcc 2.95 gets confused if you don't put a space between 'name' and the comma */
  91. #define ast_chan_log(level, channel, format, ...) ast_log(level, "%s: " format, ast_channel_name(channel) , ## __VA_ARGS__)
  92. /* Commands */
  93. #define EIVR_CMD_APND 'A' /* append to prompt queue */
  94. #define EIVR_CMD_DTMF 'D' /* send DTMF */
  95. #define EIVR_CMD_EXIT 'E' /* exit */
  96. #define EIVR_CMD_GET 'G' /* get channel varable(s) */
  97. #define EIVR_CMD_HGUP 'H' /* hangup */
  98. #define EIVR_CMD_IRPT 'I' /* interrupt */
  99. #define EIVR_CMD_LOG 'L' /* log message */
  100. #define EIVR_CMD_OPT 'O' /* option */
  101. #define EIVR_CMD_PARM 'P' /* return supplied params */
  102. #define EIVR_CMD_SQUE 'S' /* (re)set prompt queue */
  103. #define EIVR_CMD_ANS 'T' /* answer channel */
  104. #define EIVR_CMD_SVAR 'V' /* set channel varable(s) */
  105. #define EIVR_CMD_XIT 'X' /* exit **depricated** */
  106. #define EXTERNALIVR_PORT 2949
  107. enum options_flags {
  108. noanswer = (1 << 0),
  109. ignore_hangup = (1 << 1),
  110. run_dead = (1 << 2),
  111. };
  112. AST_APP_OPTIONS(app_opts, {
  113. AST_APP_OPTION('n', noanswer),
  114. AST_APP_OPTION('i', ignore_hangup),
  115. AST_APP_OPTION('d', run_dead),
  116. });
  117. struct playlist_entry {
  118. AST_LIST_ENTRY(playlist_entry) list;
  119. char filename[1];
  120. };
  121. struct ivr_localuser {
  122. struct ast_channel *chan;
  123. AST_LIST_HEAD(playlist, playlist_entry) playlist;
  124. AST_LIST_HEAD(finishlist, playlist_entry) finishlist;
  125. int abort_current_sound;
  126. int playing_silence;
  127. int option_autoclear;
  128. int gen_active;
  129. };
  130. struct gen_state {
  131. struct ivr_localuser *u;
  132. struct ast_filestream *stream;
  133. struct playlist_entry *current;
  134. int sample_queue;
  135. };
  136. static int eivr_comm(struct ast_channel *chan, struct ivr_localuser *u,
  137. int *eivr_events_fd, int *eivr_commands_fd, int *eivr_errors_fd,
  138. const struct ast_str *args, const struct ast_flags flags);
  139. static void send_eivr_event(FILE *handle, const char event, const char *data,
  140. const struct ast_channel *chan)
  141. {
  142. struct ast_str *tmp = ast_str_create(12);
  143. ast_str_append(&tmp, 0, "%c,%10d", event, (int)time(NULL));
  144. if (data) {
  145. ast_str_append(&tmp, 0, ",%s", data);
  146. }
  147. fprintf(handle, "%s\n", ast_str_buffer(tmp));
  148. ast_debug(1, "sent '%s'\n", ast_str_buffer(tmp));
  149. ast_free(tmp);
  150. }
  151. static void *gen_alloc(struct ast_channel *chan, void *params)
  152. {
  153. struct ivr_localuser *u = params;
  154. struct gen_state *state;
  155. if (!(state = ast_calloc(1, sizeof(*state))))
  156. return NULL;
  157. state->u = u;
  158. return state;
  159. }
  160. static void gen_closestream(struct gen_state *state)
  161. {
  162. if (!state->stream)
  163. return;
  164. ast_closestream(state->stream);
  165. ast_channel_stream_set(state->u->chan, NULL);
  166. state->stream = NULL;
  167. }
  168. static void gen_release(struct ast_channel *chan, void *data)
  169. {
  170. struct gen_state *state = data;
  171. gen_closestream(state);
  172. ast_free(data);
  173. }
  174. /* caller has the playlist locked */
  175. static int gen_nextfile(struct gen_state *state)
  176. {
  177. struct ivr_localuser *u = state->u;
  178. char *file_to_stream;
  179. u->abort_current_sound = 0;
  180. u->playing_silence = 0;
  181. gen_closestream(state);
  182. while (!state->stream) {
  183. state->current = AST_LIST_FIRST(&u->playlist);
  184. if (state->current) {
  185. file_to_stream = state->current->filename;
  186. } else {
  187. file_to_stream = "silence/10";
  188. u->playing_silence = 1;
  189. }
  190. if (!(state->stream = ast_openstream_full(u->chan, file_to_stream, ast_channel_language(u->chan), 1))) {
  191. ast_chan_log(LOG_WARNING, u->chan, "File '%s' could not be opened: %s\n", file_to_stream, strerror(errno));
  192. AST_LIST_LOCK(&u->playlist);
  193. AST_LIST_REMOVE_HEAD(&u->playlist, list);
  194. AST_LIST_UNLOCK(&u->playlist);
  195. if (!u->playing_silence) {
  196. continue;
  197. } else {
  198. break;
  199. }
  200. }
  201. }
  202. return (!state->stream);
  203. }
  204. static struct ast_frame *gen_readframe(struct gen_state *state)
  205. {
  206. struct ast_frame *f = NULL;
  207. struct ivr_localuser *u = state->u;
  208. if (u->abort_current_sound ||
  209. (u->playing_silence && AST_LIST_FIRST(&u->playlist))) {
  210. gen_closestream(state);
  211. AST_LIST_LOCK(&u->playlist);
  212. gen_nextfile(state);
  213. AST_LIST_UNLOCK(&u->playlist);
  214. }
  215. if (!(state->stream && (f = ast_readframe(state->stream)))) {
  216. if (state->current) {
  217. /* remove finished file from playlist */
  218. AST_LIST_LOCK(&u->playlist);
  219. AST_LIST_REMOVE_HEAD(&u->playlist, list);
  220. AST_LIST_UNLOCK(&u->playlist);
  221. /* add finished file to finishlist */
  222. AST_LIST_LOCK(&u->finishlist);
  223. AST_LIST_INSERT_TAIL(&u->finishlist, state->current, list);
  224. AST_LIST_UNLOCK(&u->finishlist);
  225. state->current = NULL;
  226. }
  227. if (!gen_nextfile(state))
  228. f = ast_readframe(state->stream);
  229. }
  230. return f;
  231. }
  232. static int gen_generate(struct ast_channel *chan, void *data, int len, int samples)
  233. {
  234. struct gen_state *state = data;
  235. struct ast_frame *f = NULL;
  236. int res = 0;
  237. state->sample_queue += samples;
  238. while (state->sample_queue > 0) {
  239. if (!(f = gen_readframe(state)))
  240. return -1;
  241. res = ast_write(chan, f);
  242. ast_frfree(f);
  243. if (res < 0) {
  244. ast_chan_log(LOG_WARNING, chan, "Failed to write frame: %s\n", strerror(errno));
  245. return -1;
  246. }
  247. state->sample_queue -= f->samples;
  248. }
  249. return res;
  250. }
  251. static struct ast_generator gen =
  252. {
  253. .alloc = gen_alloc,
  254. .release = gen_release,
  255. .generate = gen_generate,
  256. };
  257. static void ast_eivr_getvariable(struct ast_channel *chan, char *data, char *outbuf, int outbuflen)
  258. {
  259. /* original input data: "G,var1,var2," */
  260. /* data passed as "data": "var1,var2" */
  261. char *inbuf, *variable;
  262. const char *value;
  263. int j;
  264. struct ast_str *newstring = ast_str_alloca(outbuflen);
  265. outbuf[0] = '\0';
  266. for (j = 1, inbuf = data; ; j++) {
  267. variable = strsep(&inbuf, ",");
  268. if (variable == NULL) {
  269. int outstrlen = strlen(outbuf);
  270. if (outstrlen && outbuf[outstrlen - 1] == ',') {
  271. outbuf[outstrlen - 1] = 0;
  272. }
  273. break;
  274. }
  275. ast_channel_lock(chan);
  276. if (!(value = pbx_builtin_getvar_helper(chan, variable))) {
  277. value = "";
  278. }
  279. ast_str_append(&newstring, 0, "%s=%s,", variable, value);
  280. ast_channel_unlock(chan);
  281. ast_copy_string(outbuf, ast_str_buffer(newstring), outbuflen);
  282. }
  283. }
  284. static void ast_eivr_setvariable(struct ast_channel *chan, char *data)
  285. {
  286. char *value;
  287. char *inbuf = ast_strdupa(data), *variable;
  288. for (variable = strsep(&inbuf, ","); variable; variable = strsep(&inbuf, ",")) {
  289. ast_debug(1, "Setting up a variable: %s\n", variable);
  290. /* variable contains "varname=value" */
  291. value = strchr(variable, '=');
  292. if (!value) {
  293. value = "";
  294. } else {
  295. *value++ = '\0';
  296. }
  297. pbx_builtin_setvar_helper(chan, variable, value);
  298. }
  299. }
  300. static void ast_eivr_senddtmf(struct ast_channel *chan, char *vdata)
  301. {
  302. char *data;
  303. int dinterval = 0, duration = 0;
  304. AST_DECLARE_APP_ARGS(args,
  305. AST_APP_ARG(digits);
  306. AST_APP_ARG(dinterval);
  307. AST_APP_ARG(duration);
  308. );
  309. data = ast_strdupa(vdata);
  310. AST_STANDARD_APP_ARGS(args, data);
  311. if (!ast_strlen_zero(args.dinterval)) {
  312. ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS);
  313. }
  314. if (!ast_strlen_zero(args.duration)) {
  315. ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS);
  316. }
  317. ast_verb(4, "Sending DTMF: %s %d %d\n", args.digits, dinterval <= 0 ? 250 : dinterval, duration);
  318. ast_dtmf_stream(chan, NULL, args.digits, dinterval <= 0 ? 250 : dinterval, duration);
  319. }
  320. static struct playlist_entry *make_entry(const char *filename)
  321. {
  322. struct playlist_entry *entry;
  323. if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(filename) + 10))) /* XXX why 10 ? */
  324. return NULL;
  325. strcpy(entry->filename, filename);
  326. return entry;
  327. }
  328. static int app_exec(struct ast_channel *chan, const char *data)
  329. {
  330. struct ast_flags flags = { 0, };
  331. char *opts[0];
  332. struct playlist_entry *entry;
  333. int child_stdin[2] = { -1, -1 };
  334. int child_stdout[2] = { -1, -1 };
  335. int child_stderr[2] = { -1, -1 };
  336. int res = -1;
  337. int pid;
  338. struct ast_tcptls_session_instance *ser = NULL;
  339. struct ivr_localuser foo = {
  340. .playlist = AST_LIST_HEAD_INIT_VALUE,
  341. .finishlist = AST_LIST_HEAD_INIT_VALUE,
  342. .gen_active = 0,
  343. .playing_silence = 1,
  344. };
  345. struct ivr_localuser *u = &foo;
  346. char *buf;
  347. int j;
  348. char *s, **app_args, *e;
  349. struct ast_str *comma_delim_args = ast_str_alloca(100);
  350. AST_DECLARE_APP_ARGS(eivr_args,
  351. AST_APP_ARG(application);
  352. AST_APP_ARG(options);
  353. );
  354. AST_DECLARE_APP_ARGS(application_args,
  355. AST_APP_ARG(cmd)[32];
  356. );
  357. u->abort_current_sound = 0;
  358. u->chan = chan;
  359. if (ast_strlen_zero(data)) {
  360. ast_log(LOG_ERROR, "ExternalIVR requires a command to execute\n");
  361. goto exit;
  362. }
  363. buf = ast_strdupa(data);
  364. AST_STANDARD_APP_ARGS(eivr_args, buf);
  365. ast_verb(4, "ExternalIVR received application and arguments: %s\n", eivr_args.application);
  366. ast_verb(4, "ExternalIVR received options: %s\n", eivr_args.options);
  367. /* Parse out any application arguments */
  368. if ((s = strchr(eivr_args.application, '('))) {
  369. s[0] = ',';
  370. if ((e = strrchr(s, ')'))) {
  371. *e = '\0';
  372. } else {
  373. ast_log(LOG_ERROR, "Parse error, missing closing parenthesis\n");
  374. goto exit;
  375. }
  376. }
  377. AST_STANDARD_APP_ARGS(application_args, eivr_args.application);
  378. app_args = application_args.argv;
  379. /* Put the application + the arguments in a , delimited list */
  380. ast_str_reset(comma_delim_args);
  381. for (j = 0; application_args.cmd[j] != NULL; j++) {
  382. ast_str_append(&comma_delim_args, 0, "%s%s", j == 0 ? "" : ",", application_args.cmd[j]);
  383. }
  384. /* Get rid of any extraneous arguments */
  385. if (eivr_args.options && (s = strchr(eivr_args.options, ','))) {
  386. *s = '\0';
  387. }
  388. /* Parse the ExternalIVR() arguments */
  389. ast_verb(4, "Parsing options from: [%s]\n", eivr_args.options);
  390. ast_app_parse_options(app_opts, &flags, opts, eivr_args.options);
  391. if (ast_test_flag(&flags, noanswer)) {
  392. ast_verb(4, "noanswer is set\n");
  393. }
  394. if (ast_test_flag(&flags, ignore_hangup)) {
  395. ast_verb(4, "ignore_hangup is set\n");
  396. }
  397. if (ast_test_flag(&flags, run_dead)) {
  398. ast_verb(4, "run_dead is set\n");
  399. }
  400. if (!(ast_test_flag(&flags, noanswer))) {
  401. ast_verb(3, "Answering channel and starting generator\n");
  402. if (ast_channel_state(chan) != AST_STATE_UP) {
  403. if (ast_test_flag(&flags, run_dead)) {
  404. ast_chan_log(LOG_ERROR, chan, "Running ExternalIVR with 'd'ead flag on non-hungup channel isn't supported\n");
  405. goto exit;
  406. }
  407. ast_answer(chan);
  408. }
  409. if (ast_activate_generator(chan, &gen, u) < 0) {
  410. ast_chan_log(LOG_ERROR, chan, "Failed to activate generator\n");
  411. goto exit;
  412. } else {
  413. u->gen_active = 1;
  414. }
  415. }
  416. if (!strncmp(app_args[0], "ivr://", sizeof("ivr://") - 1)) {
  417. struct ast_tcptls_session_args ivr_desc = {
  418. .accept_fd = -1,
  419. .name = "IVR",
  420. };
  421. struct ast_sockaddr *addrs;
  422. int num_addrs = 0, i = 0;
  423. char *host = app_args[0] + sizeof("ivr://") - 1;
  424. /* Communicate through socket to server */
  425. ast_debug(1, "Parsing hostname/port for socket connect from \"%s\"\n", host);
  426. if (!(num_addrs = ast_sockaddr_resolve(&addrs, host, 0, AST_AF_UNSPEC))) {
  427. ast_chan_log(LOG_ERROR, chan, "Unable to locate host '%s'\n", host);
  428. goto exit;
  429. }
  430. for (i = 0; i < num_addrs; i++) {
  431. if (!ast_sockaddr_port(&addrs[i])) {
  432. /* Default port if not specified */
  433. ast_sockaddr_set_port(&addrs[i], EXTERNALIVR_PORT);
  434. }
  435. ast_sockaddr_copy(&ivr_desc.remote_address, &addrs[i]);
  436. if (!(ser = ast_tcptls_client_create(&ivr_desc)) || !(ser = ast_tcptls_client_start(ser))) {
  437. continue;
  438. }
  439. break;
  440. }
  441. ast_free(addrs);
  442. if (i == num_addrs) {
  443. ast_chan_log(LOG_ERROR, chan, "Could not connect to any host. ExternalIVR failed.\n");
  444. goto exit;
  445. }
  446. res = eivr_comm(chan, u, &ser->fd, &ser->fd, NULL, comma_delim_args, flags);
  447. } else {
  448. if (pipe(child_stdin)) {
  449. ast_chan_log(LOG_ERROR, chan, "Could not create pipe for child input: %s\n", strerror(errno));
  450. goto exit;
  451. }
  452. if (pipe(child_stdout)) {
  453. ast_chan_log(LOG_ERROR, chan, "Could not create pipe for child output: %s\n", strerror(errno));
  454. goto exit;
  455. }
  456. if (pipe(child_stderr)) {
  457. ast_chan_log(LOG_ERROR, chan, "Could not create pipe for child errors: %s\n", strerror(errno));
  458. goto exit;
  459. }
  460. pid = ast_safe_fork(0);
  461. if (pid < 0) {
  462. ast_log(LOG_ERROR, "Failed to fork(): %s\n", strerror(errno));
  463. goto exit;
  464. }
  465. if (!pid) {
  466. /* child process */
  467. if (ast_opt_high_priority)
  468. ast_set_priority(0);
  469. dup2(child_stdin[0], STDIN_FILENO);
  470. dup2(child_stdout[1], STDOUT_FILENO);
  471. dup2(child_stderr[1], STDERR_FILENO);
  472. ast_close_fds_above_n(STDERR_FILENO);
  473. execv(app_args[0], app_args);
  474. fprintf(stderr, "Failed to execute '%s': %s\n", app_args[0], strerror(errno));
  475. _exit(1);
  476. } else {
  477. /* parent process */
  478. close(child_stdin[0]);
  479. child_stdin[0] = -1;
  480. close(child_stdout[1]);
  481. child_stdout[1] = -1;
  482. close(child_stderr[1]);
  483. child_stderr[1] = -1;
  484. res = eivr_comm(chan, u, &child_stdin[1], &child_stdout[0], &child_stderr[0], comma_delim_args, flags);
  485. }
  486. }
  487. exit:
  488. if (u->gen_active) {
  489. ast_deactivate_generator(chan);
  490. }
  491. if (child_stdin[0] > -1) {
  492. close(child_stdin[0]);
  493. }
  494. if (child_stdin[1] > -1) {
  495. close(child_stdin[1]);
  496. }
  497. if (child_stdout[0] > -1) {
  498. close(child_stdout[0]);
  499. }
  500. if (child_stdout[1] > -1) {
  501. close(child_stdout[1]);
  502. }
  503. if (child_stderr[0] > -1) {
  504. close(child_stderr[0]);
  505. }
  506. if (child_stderr[1] > -1) {
  507. close(child_stderr[1]);
  508. }
  509. if (ser) {
  510. ao2_ref(ser, -1);
  511. }
  512. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  513. ast_free(entry);
  514. }
  515. return res;
  516. }
  517. static int eivr_comm(struct ast_channel *chan, struct ivr_localuser *u,
  518. int *eivr_events_fd, int *eivr_commands_fd, int *eivr_errors_fd,
  519. const struct ast_str *args, const struct ast_flags flags)
  520. {
  521. struct playlist_entry *entry;
  522. struct ast_frame *f;
  523. int ms;
  524. int exception;
  525. int ready_fd;
  526. int waitfds[2] = { *eivr_commands_fd, (eivr_errors_fd) ? *eivr_errors_fd : -1 };
  527. struct ast_channel *rchan;
  528. int res = -1;
  529. int test_available_fd = -1;
  530. int hangup_info_sent = 0;
  531. FILE *eivr_commands = NULL;
  532. FILE *eivr_errors = NULL;
  533. FILE *eivr_events = NULL;
  534. if (!(eivr_events = fdopen(*eivr_events_fd, "w"))) {
  535. ast_chan_log(LOG_ERROR, chan, "Could not open stream to send events\n");
  536. goto exit;
  537. }
  538. if (!(eivr_commands = fdopen(*eivr_commands_fd, "r"))) {
  539. ast_chan_log(LOG_ERROR, chan, "Could not open stream to receive commands\n");
  540. goto exit;
  541. }
  542. if (eivr_errors_fd) { /* if opening a socket connection, error stream will not be used */
  543. if (!(eivr_errors = fdopen(*eivr_errors_fd, "r"))) {
  544. ast_chan_log(LOG_ERROR, chan, "Could not open stream to receive errors\n");
  545. goto exit;
  546. }
  547. }
  548. test_available_fd = open("/dev/null", O_RDONLY);
  549. setvbuf(eivr_events, NULL, _IONBF, 0);
  550. setvbuf(eivr_commands, NULL, _IONBF, 0);
  551. if (eivr_errors) {
  552. setvbuf(eivr_errors, NULL, _IONBF, 0);
  553. }
  554. while (1) {
  555. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
  556. ast_chan_log(LOG_ERROR, chan, "Is a zombie\n");
  557. break;
  558. }
  559. if (!hangup_info_sent && !(ast_test_flag(&flags, run_dead)) && ast_check_hangup(chan)) {
  560. if (ast_test_flag(&flags, ignore_hangup)) {
  561. ast_verb(3, "Got check_hangup, but ignore_hangup set so sending 'I' command\n");
  562. send_eivr_event(eivr_events, 'I', "HANGUP", chan);
  563. hangup_info_sent = 1;
  564. } else {
  565. ast_verb(3, "Got check_hangup\n");
  566. send_eivr_event(eivr_events, 'H', NULL, chan);
  567. break;
  568. }
  569. }
  570. ready_fd = 0;
  571. ms = 100;
  572. errno = 0;
  573. exception = 0;
  574. rchan = ast_waitfor_nandfds(&chan, 1, waitfds, (eivr_errors_fd) ? 2 : 1, &exception, &ready_fd, &ms);
  575. if (ast_channel_state(chan) == AST_STATE_UP && !AST_LIST_EMPTY(&u->finishlist)) {
  576. AST_LIST_LOCK(&u->finishlist);
  577. while ((entry = AST_LIST_REMOVE_HEAD(&u->finishlist, list))) {
  578. send_eivr_event(eivr_events, 'F', entry->filename, chan);
  579. ast_free(entry);
  580. }
  581. AST_LIST_UNLOCK(&u->finishlist);
  582. }
  583. if (ast_channel_state(chan) == AST_STATE_UP && !(ast_check_hangup(chan)) && rchan) {
  584. /* the channel has something */
  585. f = ast_read(chan);
  586. if (!f) {
  587. ast_verb(3, "Returned no frame\n");
  588. send_eivr_event(eivr_events, 'H', NULL, chan);
  589. break;
  590. }
  591. if (f->frametype == AST_FRAME_DTMF) {
  592. send_eivr_event(eivr_events, f->subclass.integer, NULL, chan);
  593. if (u->option_autoclear) {
  594. AST_LIST_LOCK(&u->playlist);
  595. if (!u->abort_current_sound && !u->playing_silence) {
  596. /* send interrupted file as T data */
  597. if ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  598. send_eivr_event(eivr_events, 'T', entry->filename, chan);
  599. ast_free(entry);
  600. }
  601. }
  602. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  603. send_eivr_event(eivr_events, 'D', entry->filename, chan);
  604. ast_free(entry);
  605. }
  606. if (!u->playing_silence)
  607. u->abort_current_sound = 1;
  608. AST_LIST_UNLOCK(&u->playlist);
  609. }
  610. } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass.integer == AST_CONTROL_HANGUP)) {
  611. ast_verb(3, "Got AST_CONTROL_HANGUP\n");
  612. send_eivr_event(eivr_events, 'H', NULL, chan);
  613. if (f->data.uint32) {
  614. ast_channel_hangupcause_set(chan, f->data.uint32);
  615. }
  616. ast_frfree(f);
  617. break;
  618. }
  619. ast_frfree(f);
  620. } else if (ready_fd == *eivr_commands_fd) {
  621. char input[1024];
  622. if (exception || (dup2(*eivr_commands_fd, test_available_fd) == -1) || feof(eivr_commands)) {
  623. ast_chan_log(LOG_ERROR, chan, "Child process went away\n");
  624. break;
  625. }
  626. if (!fgets(input, sizeof(input), eivr_commands)) {
  627. continue;
  628. }
  629. ast_strip(input);
  630. ast_verb(4, "got command '%s'\n", input);
  631. if (strlen(input) < 3) {
  632. continue;
  633. }
  634. if (input[0] == EIVR_CMD_PARM) {
  635. struct ast_str *tmp = (struct ast_str *) args;
  636. send_eivr_event(eivr_events, 'P', ast_str_buffer(tmp), chan);
  637. } else if (input[0] == EIVR_CMD_DTMF) {
  638. ast_verb(4, "Sending DTMF: %s\n", &input[2]);
  639. ast_eivr_senddtmf(chan, &input[2]);
  640. } else if (input[0] == EIVR_CMD_ANS) {
  641. ast_verb(3, "Answering channel if needed and starting generator\n");
  642. if (ast_channel_state(chan) != AST_STATE_UP) {
  643. if (ast_test_flag(&flags, run_dead)) {
  644. ast_chan_log(LOG_WARNING, chan, "Running ExternalIVR with 'd'ead flag on non-hungup channel isn't supported\n");
  645. send_eivr_event(eivr_events, 'Z', "ANSWER_FAILURE", chan);
  646. continue;
  647. }
  648. if (ast_answer(chan)) {
  649. ast_chan_log(LOG_WARNING, chan, "Failed to answer channel\n");
  650. send_eivr_event(eivr_events, 'Z', "ANSWER_FAILURE", chan);
  651. continue;
  652. }
  653. }
  654. if (!(u->gen_active)) {
  655. if (ast_activate_generator(chan, &gen, u) < 0) {
  656. ast_chan_log(LOG_WARNING, chan, "Failed to activate generator\n");
  657. send_eivr_event(eivr_events, 'Z', "GENERATOR_FAILURE", chan);
  658. } else {
  659. u->gen_active = 1;
  660. }
  661. }
  662. } else if (input[0] == EIVR_CMD_IRPT) {
  663. if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
  664. ast_chan_log(LOG_WARNING, chan, "Queue 'I'nterrupt called on unanswered channel\n");
  665. send_eivr_event(eivr_events, 'Z', NULL, chan);
  666. continue;
  667. }
  668. AST_LIST_LOCK(&u->playlist);
  669. if (!u->abort_current_sound && !u->playing_silence) {
  670. /* send interrupted file as T data */
  671. if ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  672. send_eivr_event(eivr_events, 'T', entry->filename, chan);
  673. ast_free(entry);
  674. }
  675. }
  676. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  677. send_eivr_event(eivr_events, 'D', entry->filename, chan);
  678. ast_free(entry);
  679. }
  680. if (!u->playing_silence) {
  681. u->abort_current_sound = 1;
  682. }
  683. AST_LIST_UNLOCK(&u->playlist);
  684. } else if (input[0] == EIVR_CMD_SQUE) {
  685. if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
  686. ast_chan_log(LOG_WARNING, chan, "Queue re'S'et called on unanswered channel\n");
  687. send_eivr_event(eivr_events, 'Z', NULL, chan);
  688. continue;
  689. }
  690. if (!ast_fileexists(&input[2], NULL, ast_channel_language(u->chan))) {
  691. ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
  692. send_eivr_event(eivr_events, 'Z', &input[2], chan);
  693. } else {
  694. AST_LIST_LOCK(&u->playlist);
  695. if (!u->abort_current_sound && !u->playing_silence) {
  696. /* send interrupted file as T data */
  697. if ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  698. send_eivr_event(eivr_events, 'T', entry->filename, chan);
  699. ast_free(entry);
  700. }
  701. }
  702. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  703. send_eivr_event(eivr_events, 'D', entry->filename, chan);
  704. ast_free(entry);
  705. }
  706. if (!u->playing_silence) {
  707. u->abort_current_sound = 1;
  708. }
  709. entry = make_entry(&input[2]);
  710. if (entry) {
  711. AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
  712. }
  713. AST_LIST_UNLOCK(&u->playlist);
  714. }
  715. } else if (input[0] == EIVR_CMD_APND) {
  716. if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
  717. ast_chan_log(LOG_WARNING, chan, "Queue 'A'ppend called on unanswered channel\n");
  718. send_eivr_event(eivr_events, 'Z', NULL, chan);
  719. continue;
  720. }
  721. if (!ast_fileexists(&input[2], NULL, ast_channel_language(u->chan))) {
  722. ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
  723. send_eivr_event(eivr_events, 'Z', &input[2], chan);
  724. } else {
  725. entry = make_entry(&input[2]);
  726. if (entry) {
  727. AST_LIST_LOCK(&u->playlist);
  728. AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
  729. AST_LIST_UNLOCK(&u->playlist);
  730. }
  731. }
  732. } else if (input[0] == EIVR_CMD_GET) {
  733. char response[2048];
  734. ast_verb(4, "Retriving Variables from channel: %s\n", &input[2]);
  735. ast_eivr_getvariable(chan, &input[2], response, sizeof(response));
  736. send_eivr_event(eivr_events, 'G', response, chan);
  737. } else if (input[0] == EIVR_CMD_SVAR) {
  738. ast_verb(4, "Setting Variables in channel: %s\n", &input[2]);
  739. ast_eivr_setvariable(chan, &input[2]);
  740. } else if (input[0] == EIVR_CMD_LOG) {
  741. ast_chan_log(LOG_NOTICE, chan, "Log message from EIVR: %s\n", &input[2]);
  742. } else if (input[0] == EIVR_CMD_XIT) {
  743. ast_chan_log(LOG_NOTICE, chan, "Exiting: %s\n", &input[2]);
  744. ast_chan_log(LOG_WARNING, chan, "e'X'it command is depricated, use 'E'xit instead\n");
  745. res = 0;
  746. break;
  747. } else if (input[0] == EIVR_CMD_EXIT) {
  748. ast_chan_log(LOG_NOTICE, chan, "Exiting: %s\n", &input[2]);
  749. send_eivr_event(eivr_events, 'E', NULL, chan);
  750. res = 0;
  751. break;
  752. } else if (input[0] == EIVR_CMD_HGUP) {
  753. ast_chan_log(LOG_NOTICE, chan, "Hanging up: %s\n", &input[2]);
  754. send_eivr_event(eivr_events, 'H', NULL, chan);
  755. break;
  756. } else if (input[0] == EIVR_CMD_OPT) {
  757. if (ast_channel_state(chan) != AST_STATE_UP || ast_check_hangup(chan)) {
  758. ast_chan_log(LOG_WARNING, chan, "Option called on unanswered channel\n");
  759. send_eivr_event(eivr_events, 'Z', NULL, chan);
  760. continue;
  761. }
  762. if (!strcasecmp(&input[2], "autoclear"))
  763. u->option_autoclear = 1;
  764. else if (!strcasecmp(&input[2], "noautoclear"))
  765. u->option_autoclear = 0;
  766. else
  767. ast_chan_log(LOG_WARNING, chan, "Unknown option requested: %s\n", &input[2]);
  768. }
  769. } else if (eivr_errors_fd && (ready_fd == *eivr_errors_fd)) {
  770. char input[1024];
  771. if (exception || feof(eivr_errors)) {
  772. ast_chan_log(LOG_ERROR, chan, "Child process went away\n");
  773. break;
  774. }
  775. if (fgets(input, sizeof(input), eivr_errors)) {
  776. ast_chan_log(LOG_NOTICE, chan, "stderr: %s\n", ast_strip(input));
  777. }
  778. } else if ((ready_fd < 0) && ms) {
  779. if (errno == 0 || errno == EINTR)
  780. continue;
  781. ast_chan_log(LOG_ERROR, chan, "Wait failed (%s)\n", strerror(errno));
  782. break;
  783. }
  784. }
  785. exit:
  786. if (test_available_fd > -1) {
  787. close(test_available_fd);
  788. }
  789. if (eivr_events) {
  790. fclose(eivr_events);
  791. *eivr_events_fd = -1;
  792. }
  793. if (eivr_commands) {
  794. fclose(eivr_commands);
  795. *eivr_commands_fd = -1;
  796. }
  797. if (eivr_errors) {
  798. fclose(eivr_errors);
  799. *eivr_errors_fd = -1;
  800. }
  801. return res;
  802. }
  803. static int unload_module(void)
  804. {
  805. return ast_unregister_application(app);
  806. }
  807. static int load_module(void)
  808. {
  809. return ast_register_application_xml(app, app_exec);
  810. }
  811. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "External IVR Interface Application");