app_externalivr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. * \ingroup applications
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/lock.h"
  35. #include "asterisk/file.h"
  36. #include "asterisk/logger.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/linkedlists.h"
  41. static const char *tdesc = "External IVR Interface Application";
  42. static const char *app = "ExternalIVR";
  43. static const char *synopsis = "Interfaces with an external IVR application";
  44. static const char *descrip =
  45. " ExternalIVR(command[|arg[|arg...]]): Forks an process to run the supplied command,\n"
  46. "and starts a generator on the channel. The generator's play list is\n"
  47. "controlled by the external application, which can add and clear entries\n"
  48. "via simple commands issued over its stdout. The external application\n"
  49. "will receive all DTMF events received on the channel, and notification\n"
  50. "if the channel is hung up. The application will not be forcibly terminated\n"
  51. "when the channel is hung up.\n"
  52. "See doc/README.externalivr for a protocol specification.\n";
  53. /* XXX the parser in gcc 2.95 gets confused if you don't put a space between 'name' and the comma */
  54. #define ast_chan_log(level, channel, format, ...) ast_log(level, "%s: " format, channel->name , ## __VA_ARGS__)
  55. struct playlist_entry {
  56. AST_LIST_ENTRY(playlist_entry) list;
  57. char filename[1];
  58. };
  59. struct localuser {
  60. struct ast_channel *chan;
  61. struct localuser *next;
  62. AST_LIST_HEAD(playlist, playlist_entry) playlist;
  63. AST_LIST_HEAD(finishlist, playlist_entry) finishlist;
  64. int abort_current_sound;
  65. int playing_silence;
  66. int option_autoclear;
  67. };
  68. LOCAL_USER_DECL;
  69. struct gen_state {
  70. struct localuser *u;
  71. struct ast_filestream *stream;
  72. struct playlist_entry *current;
  73. int sample_queue;
  74. };
  75. static void send_child_event(FILE *handle, const char event, const char *data,
  76. const struct ast_channel *chan)
  77. {
  78. char tmp[256];
  79. if (!data) {
  80. snprintf(tmp, sizeof(tmp), "%c,%10ld", event, time(NULL));
  81. } else {
  82. snprintf(tmp, sizeof(tmp), "%c,%10ld,%s", event, time(NULL), data);
  83. }
  84. fprintf(handle, "%s\n", tmp);
  85. ast_chan_log(LOG_DEBUG, chan, "sent '%s'\n", tmp);
  86. }
  87. static void *gen_alloc(struct ast_channel *chan, void *params)
  88. {
  89. struct localuser *u = params;
  90. struct gen_state *state;
  91. state = calloc(1, sizeof(*state));
  92. if (!state)
  93. return NULL;
  94. state->u = u;
  95. return state;
  96. }
  97. static void gen_closestream(struct gen_state *state)
  98. {
  99. if (!state->stream)
  100. return;
  101. ast_closestream(state->stream);
  102. state->u->chan->stream = NULL;
  103. state->stream = NULL;
  104. }
  105. static void gen_release(struct ast_channel *chan, void *data)
  106. {
  107. struct gen_state *state = data;
  108. gen_closestream(state);
  109. free(data);
  110. }
  111. /* caller has the playlist locked */
  112. static int gen_nextfile(struct gen_state *state)
  113. {
  114. struct localuser *u = state->u;
  115. char *file_to_stream;
  116. u->abort_current_sound = 0;
  117. u->playing_silence = 0;
  118. gen_closestream(state);
  119. while (!state->stream) {
  120. state->current = AST_LIST_REMOVE_HEAD(&u->playlist, list);
  121. if (state->current) {
  122. file_to_stream = state->current->filename;
  123. } else {
  124. file_to_stream = "silence-10";
  125. u->playing_silence = 1;
  126. }
  127. if (!(state->stream = ast_openstream_full(u->chan, file_to_stream, u->chan->language, 1))) {
  128. ast_chan_log(LOG_WARNING, u->chan, "File '%s' could not be opened: %s\n", file_to_stream, strerror(errno));
  129. if (!u->playing_silence) {
  130. continue;
  131. } else {
  132. break;
  133. }
  134. }
  135. }
  136. return (!state->stream);
  137. }
  138. static struct ast_frame *gen_readframe(struct gen_state *state)
  139. {
  140. struct ast_frame *f = NULL;
  141. struct localuser *u = state->u;
  142. if (u->abort_current_sound ||
  143. (u->playing_silence && AST_LIST_FIRST(&u->playlist))) {
  144. gen_closestream(state);
  145. AST_LIST_LOCK(&u->playlist);
  146. gen_nextfile(state);
  147. AST_LIST_UNLOCK(&u->playlist);
  148. }
  149. if (!(state->stream && (f = ast_readframe(state->stream)))) {
  150. if (state->current) {
  151. AST_LIST_LOCK(&u->finishlist);
  152. AST_LIST_INSERT_TAIL(&u->finishlist, state->current, list);
  153. AST_LIST_UNLOCK(&u->finishlist);
  154. state->current = NULL;
  155. }
  156. if (!gen_nextfile(state))
  157. f = ast_readframe(state->stream);
  158. }
  159. return f;
  160. }
  161. static int gen_generate(struct ast_channel *chan, void *data, int len, int samples)
  162. {
  163. struct gen_state *state = data;
  164. struct ast_frame *f = NULL;
  165. int res = 0;
  166. state->sample_queue += samples;
  167. while (state->sample_queue > 0) {
  168. if (!(f = gen_readframe(state)))
  169. return -1;
  170. res = ast_write(chan, f);
  171. ast_frfree(f);
  172. if (res < 0) {
  173. ast_chan_log(LOG_WARNING, chan, "Failed to write frame: %s\n", strerror(errno));
  174. return -1;
  175. }
  176. state->sample_queue -= f->samples;
  177. }
  178. return res;
  179. }
  180. static struct ast_generator gen =
  181. {
  182. alloc: gen_alloc,
  183. release: gen_release,
  184. generate: gen_generate,
  185. };
  186. static struct playlist_entry *make_entry(const char *filename)
  187. {
  188. struct playlist_entry *entry;
  189. entry = calloc(1, sizeof(*entry) + strlen(filename) + 10);
  190. if (!entry)
  191. return NULL;
  192. strcpy(entry->filename, filename);
  193. return entry;
  194. }
  195. static int app_exec(struct ast_channel *chan, void *data)
  196. {
  197. struct localuser *u = NULL;
  198. struct playlist_entry *entry;
  199. const char *args = data;
  200. int child_stdin[2] = { 0,0 };
  201. int child_stdout[2] = { 0,0 };
  202. int child_stderr[2] = { 0,0 };
  203. int res = -1;
  204. int gen_active = 0;
  205. int pid;
  206. char *command;
  207. char *argv[32];
  208. int argc = 1;
  209. char *buf;
  210. FILE *child_commands = NULL;
  211. FILE *child_errors = NULL;
  212. FILE *child_events = NULL;
  213. LOCAL_USER_ADD(u);
  214. AST_LIST_HEAD_INIT(&u->playlist);
  215. AST_LIST_HEAD_INIT(&u->finishlist);
  216. u->abort_current_sound = 0;
  217. if (ast_strlen_zero(args)) {
  218. ast_log(LOG_WARNING, "ExternalIVR requires a command to execute\n");
  219. goto exit;
  220. }
  221. buf = ast_strdupa(data);
  222. command = strsep(&buf, "|");
  223. memset(argv, 0, sizeof(argv) / sizeof(argv[0]));
  224. argv[0] = command;
  225. while ((argc < 31) && (argv[argc++] = strsep(&buf, "|")));
  226. argv[argc] = NULL;
  227. if (pipe(child_stdin)) {
  228. ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child input: %s\n", strerror(errno));
  229. goto exit;
  230. }
  231. if (pipe(child_stdout)) {
  232. ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child output: %s\n", strerror(errno));
  233. goto exit;
  234. }
  235. if (pipe(child_stderr)) {
  236. ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child errors: %s\n", strerror(errno));
  237. goto exit;
  238. }
  239. if (chan->_state != AST_STATE_UP) {
  240. ast_answer(chan);
  241. }
  242. if (ast_activate_generator(chan, &gen, u) < 0) {
  243. ast_chan_log(LOG_WARNING, chan, "Failed to activate generator\n");
  244. goto exit;
  245. } else
  246. gen_active = 1;
  247. pid = fork();
  248. if (pid < 0) {
  249. ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
  250. goto exit;
  251. }
  252. if (!pid) {
  253. /* child process */
  254. int i;
  255. dup2(child_stdin[0], STDIN_FILENO);
  256. dup2(child_stdout[1], STDOUT_FILENO);
  257. dup2(child_stderr[1], STDERR_FILENO);
  258. for (i = STDERR_FILENO + 1; i < 1024; i++)
  259. close(i);
  260. execv(command, argv);
  261. fprintf(stderr, "Failed to execute '%s': %s\n", command, strerror(errno));
  262. exit(1);
  263. } else {
  264. /* parent process */
  265. int child_events_fd = child_stdin[1];
  266. int child_commands_fd = child_stdout[0];
  267. int child_errors_fd = child_stderr[0];
  268. struct ast_frame *f;
  269. int ms;
  270. int exception;
  271. int ready_fd;
  272. int waitfds[2] = { child_errors_fd, child_commands_fd };
  273. struct ast_channel *rchan;
  274. close(child_stdin[0]);
  275. child_stdin[0] = 0;
  276. close(child_stdout[1]);
  277. child_stdout[1] = 0;
  278. close(child_stderr[1]);
  279. child_stderr[1] = 0;
  280. if (!(child_events = fdopen(child_events_fd, "w"))) {
  281. ast_chan_log(LOG_WARNING, chan, "Could not open stream for child events\n");
  282. goto exit;
  283. }
  284. setvbuf(child_events, NULL, _IONBF, 0);
  285. if (!(child_commands = fdopen(child_commands_fd, "r"))) {
  286. ast_chan_log(LOG_WARNING, chan, "Could not open stream for child commands\n");
  287. goto exit;
  288. }
  289. if (!(child_errors = fdopen(child_errors_fd, "r"))) {
  290. ast_chan_log(LOG_WARNING, chan, "Could not open stream for child errors\n");
  291. goto exit;
  292. }
  293. res = 0;
  294. while (1) {
  295. if (ast_test_flag(chan, AST_FLAG_ZOMBIE)) {
  296. ast_chan_log(LOG_NOTICE, chan, "Is a zombie\n");
  297. res = -1;
  298. break;
  299. }
  300. if (ast_check_hangup(chan)) {
  301. ast_chan_log(LOG_NOTICE, chan, "Got check_hangup\n");
  302. send_child_event(child_events, 'H', NULL, chan);
  303. res = -1;
  304. break;
  305. }
  306. ready_fd = 0;
  307. ms = 100;
  308. errno = 0;
  309. exception = 0;
  310. rchan = ast_waitfor_nandfds(&chan, 1, waitfds, 2, &exception, &ready_fd, &ms);
  311. if (!AST_LIST_EMPTY(&u->finishlist)) {
  312. AST_LIST_LOCK(&u->finishlist);
  313. while ((entry = AST_LIST_REMOVE_HEAD(&u->finishlist, list))) {
  314. send_child_event(child_events, 'F', entry->filename, chan);
  315. free(entry);
  316. }
  317. AST_LIST_UNLOCK(&u->finishlist);
  318. }
  319. if (rchan) {
  320. /* the channel has something */
  321. f = ast_read(chan);
  322. if (!f) {
  323. ast_chan_log(LOG_NOTICE, chan, "Returned no frame\n");
  324. send_child_event(child_events, 'H', NULL, chan);
  325. res = -1;
  326. break;
  327. }
  328. if (f->frametype == AST_FRAME_DTMF) {
  329. send_child_event(child_events, f->subclass, NULL, chan);
  330. if (u->option_autoclear) {
  331. if (!u->abort_current_sound && !u->playing_silence)
  332. send_child_event(child_events, 'T', NULL, chan);
  333. AST_LIST_LOCK(&u->playlist);
  334. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  335. send_child_event(child_events, 'D', entry->filename, chan);
  336. free(entry);
  337. }
  338. if (!u->playing_silence)
  339. u->abort_current_sound = 1;
  340. AST_LIST_UNLOCK(&u->playlist);
  341. }
  342. } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
  343. ast_chan_log(LOG_NOTICE, chan, "Got AST_CONTROL_HANGUP\n");
  344. send_child_event(child_events, 'H', NULL, chan);
  345. ast_frfree(f);
  346. res = -1;
  347. break;
  348. }
  349. ast_frfree(f);
  350. } else if (ready_fd == child_commands_fd) {
  351. char input[1024];
  352. if (exception || feof(child_commands)) {
  353. ast_chan_log(LOG_WARNING, chan, "Child process went away\n");
  354. res = -1;
  355. break;
  356. }
  357. if (!fgets(input, sizeof(input), child_commands))
  358. continue;
  359. command = ast_strip(input);
  360. ast_chan_log(LOG_DEBUG, chan, "got command '%s'\n", input);
  361. if (strlen(input) < 4)
  362. continue;
  363. if (input[0] == 'S') {
  364. if (ast_fileexists(&input[2], NULL, NULL) == -1) {
  365. ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
  366. send_child_event(child_events, 'Z', NULL, chan);
  367. strcpy(&input[2], "exception");
  368. }
  369. if (!u->abort_current_sound && !u->playing_silence)
  370. send_child_event(child_events, 'T', NULL, chan);
  371. AST_LIST_LOCK(&u->playlist);
  372. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
  373. send_child_event(child_events, 'D', entry->filename, chan);
  374. free(entry);
  375. }
  376. if (!u->playing_silence)
  377. u->abort_current_sound = 1;
  378. entry = make_entry(&input[2]);
  379. if (entry)
  380. AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
  381. AST_LIST_UNLOCK(&u->playlist);
  382. } else if (input[0] == 'A') {
  383. if (ast_fileexists(&input[2], NULL, NULL) == -1) {
  384. ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
  385. send_child_event(child_events, 'Z', NULL, chan);
  386. strcpy(&input[2], "exception");
  387. }
  388. entry = make_entry(&input[2]);
  389. if (entry) {
  390. AST_LIST_LOCK(&u->playlist);
  391. AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
  392. AST_LIST_UNLOCK(&u->playlist);
  393. }
  394. } else if (input[0] == 'H') {
  395. ast_chan_log(LOG_NOTICE, chan, "Hanging up: %s\n", &input[2]);
  396. send_child_event(child_events, 'H', NULL, chan);
  397. break;
  398. } else if (input[0] == 'O') {
  399. if (!strcasecmp(&input[2], "autoclear"))
  400. u->option_autoclear = 1;
  401. else if (!strcasecmp(&input[2], "noautoclear"))
  402. u->option_autoclear = 0;
  403. else
  404. ast_chan_log(LOG_WARNING, chan, "Unknown option requested '%s'\n", &input[2]);
  405. }
  406. } else if (ready_fd == child_errors_fd) {
  407. char input[1024];
  408. if (exception || feof(child_errors)) {
  409. ast_chan_log(LOG_WARNING, chan, "Child process went away\n");
  410. res = -1;
  411. break;
  412. }
  413. if (fgets(input, sizeof(input), child_errors)) {
  414. command = ast_strip(input);
  415. ast_chan_log(LOG_NOTICE, chan, "stderr: %s\n", command);
  416. }
  417. } else if ((ready_fd < 0) && ms) {
  418. if (errno == 0 || errno == EINTR)
  419. continue;
  420. ast_chan_log(LOG_WARNING, chan, "Wait failed (%s)\n", strerror(errno));
  421. break;
  422. }
  423. }
  424. }
  425. exit:
  426. if (gen_active)
  427. ast_deactivate_generator(chan);
  428. if (child_events)
  429. fclose(child_events);
  430. if (child_commands)
  431. fclose(child_commands);
  432. if (child_errors)
  433. fclose(child_errors);
  434. if (child_stdin[0])
  435. close(child_stdin[0]);
  436. if (child_stdin[1])
  437. close(child_stdin[1]);
  438. if (child_stdout[0])
  439. close(child_stdout[0]);
  440. if (child_stdout[1])
  441. close(child_stdout[1]);
  442. if (child_stderr[0])
  443. close(child_stderr[0]);
  444. if (child_stderr[1])
  445. close(child_stderr[1]);
  446. while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list)))
  447. free(entry);
  448. LOCAL_USER_REMOVE(u);
  449. return res;
  450. }
  451. int unload_module(void)
  452. {
  453. int res;
  454. res = ast_unregister_application(app);
  455. STANDARD_HANGUP_LOCALUSERS;
  456. return res;
  457. }
  458. int load_module(void)
  459. {
  460. return ast_register_application(app, app_exec, synopsis, descrip);
  461. }
  462. char *description(void)
  463. {
  464. return (char *) tdesc;
  465. }
  466. int usecount(void)
  467. {
  468. int res;
  469. STANDARD_USECOUNT(res);
  470. return res;
  471. }
  472. char *key()
  473. {
  474. return ASTERISK_GPL_KEY;
  475. }