make app_queue 1.2 jump compliant (issue #5580)
[asterisk/asterisk.git] / apps / app_externalivr.c
index 1789469..ff5147d 100755 (executable)
@@ -1,23 +1,35 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * External IVR application interface
- * 
- * Copyright (C) 2005, Digium, Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Kevin P. Fleming <kpfleming@digium.com>
  *
  * Portions taken from the file-based music-on-hold work
  * created by Anthony Minessale II in res_musiconhold.c
  *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief External IVR application interface
+ * 
+ * \ingroup applications
  */
 
 #include <stdlib.h>
-#include <unistd.h>
+#include <stdio.h>
 #include <string.h>
-#include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
 
 #include "asterisk.h"
@@ -32,13 +44,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/linkedlists.h"
 
-static char *tdesc = "External IVR Interface Application";
+static const char *tdesc = "External IVR Interface Application";
 
-static char *app = "ExternalIVR";
+static const char *app = "ExternalIVR";
 
-static char *synopsis = "Interfaces with an external IVR application";
+static const char *synopsis = "Interfaces with an external IVR application";
 
-static char *descrip = 
+static const char *descrip = 
 "  ExternalIVR(command[|arg[|arg...]]): Forks an process to run the supplied command,\n"
 "and starts a generator on the channel. The generator's play list is\n"
 "controlled by the external application, which can add and clear entries\n"
@@ -48,6 +60,9 @@ static char *descrip =
 "when the channel is hung up.\n"
 "See doc/README.externalivr for a protocol specification.\n";
 
+/* XXX the parser in gcc 2.95 gets confused if you don't put a space between 'name' and the comma */
+#define ast_chan_log(level, channel, format, ...) ast_log(level, "%s: " format, channel->name , ## __VA_ARGS__)
+
 struct playlist_entry {
        AST_LIST_ENTRY(playlist_entry) list;
        char filename[1];
@@ -57,7 +72,10 @@ struct localuser {
        struct ast_channel *chan;
        struct localuser *next;
        AST_LIST_HEAD(playlist, playlist_entry) playlist;
-       int list_cleared;
+       AST_LIST_HEAD(finishlist, playlist_entry) finishlist;
+       int abort_current_sound;
+       int playing_silence;
+       int option_autoclear;
 };
 
 LOCAL_USER_DECL;
@@ -65,10 +83,25 @@ LOCAL_USER_DECL;
 struct gen_state {
        struct localuser *u;
        struct ast_filestream *stream;
+       struct playlist_entry *current;
        int sample_queue;
-       int playing_silence;
 };
 
+static void send_child_event(FILE *handle, const char event, const char *data,
+                            const struct ast_channel *chan)
+{
+       char tmp[256];
+
+       if (!data) {
+               snprintf(tmp, sizeof(tmp), "%c,%10ld", event, time(NULL));
+       } else {
+               snprintf(tmp, sizeof(tmp), "%c,%10ld,%s", event, time(NULL), data);
+       }
+
+       fprintf(handle, "%s\n", tmp);
+       ast_chan_log(LOG_DEBUG, chan, "sent '%s'\n", tmp);
+}
+
 static void *gen_alloc(struct ast_channel *chan, void *params)
 {
        struct localuser *u = params;
@@ -105,34 +138,29 @@ static void gen_release(struct ast_channel *chan, void *data)
 /* caller has the playlist locked */
 static int gen_nextfile(struct gen_state *state)
 {
-       struct playlist_entry *entry;
        struct localuser *u = state->u;
        char *file_to_stream;
        
-       state->u->list_cleared = 0;
-       state->playing_silence = 0;
+       u->abort_current_sound = 0;
+       u->playing_silence = 0;
        gen_closestream(state);
 
        while (!state->stream) {
-               if (AST_LIST_FIRST(&u->playlist))
-                       entry = AST_LIST_REMOVE_HEAD(&u->playlist, list);
-               else
-                       entry = NULL;
-
-               if (entry) {
-                       file_to_stream = ast_strdupa(entry->filename);
-                       free(entry);
+               state->current = AST_LIST_REMOVE_HEAD(&u->playlist, list);
+               if (state->current) {
+                       file_to_stream = state->current->filename;
                } else {
                        file_to_stream = "silence-10";
-                       state->playing_silence = 1;
+                       u->playing_silence = 1;
                }
 
                if (!(state->stream = ast_openstream_full(u->chan, file_to_stream, u->chan->language, 1))) {
-                       ast_log(LOG_WARNING, "File '%s' could not be opened for channel '%s': %s\n", file_to_stream, u->chan->name, strerror(errno));
-                       if (!state->playing_silence)
+                       ast_chan_log(LOG_WARNING, u->chan, "File '%s' could not be opened: %s\n", file_to_stream, strerror(errno));
+                       if (!u->playing_silence) {
                                continue;
-                       else
+                       } else { 
                                break;
+                       }
                }
        }
 
@@ -142,16 +170,23 @@ static int gen_nextfile(struct gen_state *state)
 static struct ast_frame *gen_readframe(struct gen_state *state)
 {
        struct ast_frame *f = NULL;
+       struct localuser *u = state->u;
        
-       if (state->u->list_cleared ||
-           (state->playing_silence && AST_LIST_FIRST(&state->u->playlist))) {
+       if (u->abort_current_sound ||
+           (u->playing_silence && AST_LIST_FIRST(&u->playlist))) {
                gen_closestream(state);
-               AST_LIST_LOCK(&state->u->playlist);
+               AST_LIST_LOCK(&u->playlist);
                gen_nextfile(state);
-               AST_LIST_UNLOCK(&state->u->playlist);
+               AST_LIST_UNLOCK(&u->playlist);
        }
 
        if (!(state->stream && (f = ast_readframe(state->stream)))) {
+               if (state->current) {
+                       AST_LIST_LOCK(&u->finishlist);
+                       AST_LIST_INSERT_TAIL(&u->finishlist, state->current, list);
+                       AST_LIST_UNLOCK(&u->finishlist);
+                       state->current = NULL;
+               }
                if (!gen_nextfile(state))
                        f = ast_readframe(state->stream);
        }
@@ -174,7 +209,7 @@ static int gen_generate(struct ast_channel *chan, void *data, int len, int sampl
                res = ast_write(chan, f);
                ast_frfree(f);
                if (res < 0) {
-                       ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
+                       ast_chan_log(LOG_WARNING, chan, "Failed to write frame: %s\n", strerror(errno));
                        return -1;
                }
                state->sample_queue -= f->samples;
@@ -194,7 +229,7 @@ static struct playlist_entry *make_entry(const char *filename)
 {
        struct playlist_entry *entry;
 
-       entry = calloc(1, sizeof(*entry) + strlen(filename));
+       entry = calloc(1, sizeof(*entry) + strlen(filename) + 10);
 
        if (!entry)
                return NULL;
@@ -223,43 +258,45 @@ static int app_exec(struct ast_channel *chan, void *data)
        FILE *child_errors = NULL;
        FILE *child_events = NULL;
 
-       if (!args || ast_strlen_zero(args)) {
+       LOCAL_USER_ADD(u);
+       
+       AST_LIST_HEAD_INIT(&u->playlist);
+       AST_LIST_HEAD_INIT(&u->finishlist);
+       u->abort_current_sound = 0;
+       
+       if (ast_strlen_zero(args)) {
                ast_log(LOG_WARNING, "ExternalIVR requires a command to execute\n");
                goto exit;
        }
 
        buf = ast_strdupa(data);
        command = strsep(&buf, "|");
+       memset(argv, 0, sizeof(argv) / sizeof(argv[0]));
        argv[0] = command;
        while ((argc < 31) && (argv[argc++] = strsep(&buf, "|")));
        argv[argc] = NULL;
 
-       LOCAL_USER_ADD(u);
-
        if (pipe(child_stdin)) {
-               ast_log(LOG_WARNING, "Could not create pipe for child input on channel '%s': %s\n", chan->name, strerror(errno));
+               ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child input: %s\n", strerror(errno));
                goto exit;
        }
 
        if (pipe(child_stdout)) {
-               ast_log(LOG_WARNING, "Could not create pipe for child output on channel '%s': %s\n", chan->name, strerror(errno));
+               ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child output: %s\n", strerror(errno));
                goto exit;
        }
 
        if (pipe(child_stderr)) {
-               ast_log(LOG_WARNING, "Could not create pipe for child errors on channel '%s': %s\n", chan->name, strerror(errno));
+               ast_chan_log(LOG_WARNING, chan, "Could not create pipe for child errors: %s\n", strerror(errno));
                goto exit;
        }
 
-       u->list_cleared = 0;
-       AST_LIST_HEAD_INIT(&u->playlist);
-
        if (chan->_state != AST_STATE_UP) {
                ast_answer(chan);
        }
 
        if (ast_activate_generator(chan, &gen, u) < 0) {
-               ast_log(LOG_WARNING,"Failed to activate generator on '%s'\n", chan->name);
+               ast_chan_log(LOG_WARNING, chan, "Failed to activate generator\n");
                goto exit;
        } else
                gen_active = 1;
@@ -277,9 +314,6 @@ static int app_exec(struct ast_channel *chan, void *data)
                dup2(child_stdin[0], STDIN_FILENO);
                dup2(child_stdout[1], STDOUT_FILENO);
                dup2(child_stderr[1], STDERR_FILENO);
-               close(child_stdin[1]);
-               close(child_stdout[0]);
-               close(child_stderr[0]);
                for (i = STDERR_FILENO + 1; i < 1024; i++)
                        close(i);
                execv(command, argv);
@@ -298,23 +332,26 @@ static int app_exec(struct ast_channel *chan, void *data)
                struct ast_channel *rchan;
 
                close(child_stdin[0]);
+               child_stdin[0] = 0;
                close(child_stdout[1]);
+               child_stdout[1] = 0;
                close(child_stderr[1]);
+               child_stderr[1] = 0;
 
                if (!(child_events = fdopen(child_events_fd, "w"))) {
-                       ast_log(LOG_WARNING, "Could not open stream for child events for channel '%s'\n", chan->name);
+                       ast_chan_log(LOG_WARNING, chan, "Could not open stream for child events\n");
                        goto exit;
                }
 
                setvbuf(child_events, NULL, _IONBF, 0);
 
                if (!(child_commands = fdopen(child_commands_fd, "r"))) {
-                       ast_log(LOG_WARNING, "Could not open stream for child commands for channel '%s'\n", chan->name);
+                       ast_chan_log(LOG_WARNING, chan, "Could not open stream for child commands\n");
                        goto exit;
                }
 
                if (!(child_errors = fdopen(child_errors_fd, "r"))) {
-                       ast_log(LOG_WARNING, "Could not open stream for child errors for channel '%s'\n", chan->name);
+                       ast_chan_log(LOG_WARNING, chan, "Could not open stream for child errors\n");
                        goto exit;
                }
 
@@ -322,40 +359,61 @@ static int app_exec(struct ast_channel *chan, void *data)
 
                while (1) {
                        if (ast_test_flag(chan, AST_FLAG_ZOMBIE)) {
-                               ast_log(LOG_NOTICE, "Channel '%s' is a zombie\n", chan->name);
+                               ast_chan_log(LOG_NOTICE, chan, "Is a zombie\n");
                                res = -1;
                                break;
                        }
 
                        if (ast_check_hangup(chan)) {
-                               ast_log(LOG_NOTICE, "Channel '%s' got check_hangup\n", chan->name);
-                               fprintf(child_events, "H,%10ld\n", time(NULL));
+                               ast_chan_log(LOG_NOTICE, chan, "Got check_hangup\n");
+                               send_child_event(child_events, 'H', NULL, chan);
                                res = -1;
                                break;
                        }
 
                        ready_fd = 0;
-                       ms = 1000;
+                       ms = 100;
                        errno = 0;
                        exception = 0;
 
                        rchan = ast_waitfor_nandfds(&chan, 1, waitfds, 2, &exception, &ready_fd, &ms);
 
+                       if (!AST_LIST_EMPTY(&u->finishlist)) {
+                               AST_LIST_LOCK(&u->finishlist);
+                               while ((entry = AST_LIST_REMOVE_HEAD(&u->finishlist, list))) {
+                                       send_child_event(child_events, 'F', entry->filename, chan);
+                                       free(entry);
+                               }
+                               AST_LIST_UNLOCK(&u->finishlist);
+                       }
+
                        if (rchan) {
                                /* the channel has something */
                                f = ast_read(chan);
                                if (!f) {
-                                       fprintf(child_events, "H,%10ld\n", time(NULL));
-                                       ast_log(LOG_NOTICE, "Channel '%s' returned no frame\n", chan->name);
+                                       ast_chan_log(LOG_NOTICE, chan, "Returned no frame\n");
+                                       send_child_event(child_events, 'H', NULL, chan);
                                        res = -1;
                                        break;
                                }
 
                                if (f->frametype == AST_FRAME_DTMF) {
-                                       fprintf(child_events, "%c,%10ld\n", f->subclass, time(NULL));
+                                       send_child_event(child_events, f->subclass, NULL, chan);
+                                       if (u->option_autoclear) {
+                                               if (!u->abort_current_sound && !u->playing_silence)
+                                                       send_child_event(child_events, 'T', NULL, chan);
+                                               AST_LIST_LOCK(&u->playlist);
+                                               while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
+                                                       send_child_event(child_events, 'D', entry->filename, chan);
+                                                       free(entry);
+                                               }
+                                               if (!u->playing_silence)
+                                                       u->abort_current_sound = 1;
+                                               AST_LIST_UNLOCK(&u->playlist);
+                                       }
                                } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
-                                       ast_log(LOG_NOTICE, "Channel '%s' got AST_CONTROL_HANGUP\n", chan->name);
-                                       fprintf(child_events, "H,%10ld\n", time(NULL));
+                                       ast_chan_log(LOG_NOTICE, chan, "Got AST_CONTROL_HANGUP\n");
+                                       send_child_event(child_events, 'H', NULL, chan);
                                        ast_frfree(f);
                                        res = -1;
                                        break;
@@ -365,7 +423,7 @@ static int app_exec(struct ast_channel *chan, void *data)
                                char input[1024];
 
                                if (exception || feof(child_commands)) {
-                                       ast_log(LOG_WARNING, "Child process went away for channel '%s'\n", chan->name);
+                                       ast_chan_log(LOG_WARNING, chan, "Child process went away\n");
                                        res = -1;
                                        break;
                                }
@@ -375,27 +433,34 @@ static int app_exec(struct ast_channel *chan, void *data)
 
                                command = ast_strip(input);
 
+                               ast_chan_log(LOG_DEBUG, chan, "got command '%s'\n", input);
+
                                if (strlen(input) < 4)
                                        continue;
 
                                if (input[0] == 'S') {
                                        if (ast_fileexists(&input[2], NULL, NULL) == -1) {
-                                               fprintf(child_events, "Z,%10ld\n", time(NULL));
-                                               ast_log(LOG_WARNING, "Unknown file requested '%s' for channel '%s'\n", &input[2], chan->name);
+                                               ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
+                                               send_child_event(child_events, 'Z', NULL, chan);
                                                strcpy(&input[2], "exception");
                                        }
+                                       if (!u->abort_current_sound && !u->playing_silence)
+                                               send_child_event(child_events, 'T', NULL, chan);
                                        AST_LIST_LOCK(&u->playlist);
-                                       while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list)))
+                                       while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list))) {
+                                               send_child_event(child_events, 'D', entry->filename, chan);
                                                free(entry);
-                                       u->list_cleared = 1;
+                                       }
+                                       if (!u->playing_silence)
+                                               u->abort_current_sound = 1;
                                        entry = make_entry(&input[2]);
                                        if (entry)
                                                AST_LIST_INSERT_TAIL(&u->playlist, entry, list);
                                        AST_LIST_UNLOCK(&u->playlist);
                                } else if (input[0] == 'A') {
                                        if (ast_fileexists(&input[2], NULL, NULL) == -1) {
-                                               fprintf(child_events, "Z,%10ld\n", time(NULL));
-                                               ast_log(LOG_WARNING, "Unknown file requested '%s' for channel '%s'\n", &input[2], chan->name);
+                                               ast_chan_log(LOG_WARNING, chan, "Unknown file requested '%s'\n", &input[2]);
+                                               send_child_event(child_events, 'Z', NULL, chan);
                                                strcpy(&input[2], "exception");
                                        }
                                        entry = make_entry(&input[2]);
@@ -405,28 +470,35 @@ static int app_exec(struct ast_channel *chan, void *data)
                                                AST_LIST_UNLOCK(&u->playlist);
                                        }
                                } else if (input[0] == 'H') {
-                                       ast_log(LOG_NOTICE, "Hanging up: %s\n", &input[2]);
-                                       fprintf(child_events, "H,%10ld\n", time(NULL));
+                                       ast_chan_log(LOG_NOTICE, chan, "Hanging up: %s\n", &input[2]);
+                                       send_child_event(child_events, 'H', NULL, chan);
                                        break;
+                               } else if (input[0] == 'O') {
+                                       if (!strcasecmp(&input[2], "autoclear"))
+                                               u->option_autoclear = 1;
+                                       else if (!strcasecmp(&input[2], "noautoclear"))
+                                               u->option_autoclear = 0;
+                                       else
+                                               ast_chan_log(LOG_WARNING, chan, "Unknown option requested '%s'\n", &input[2]);
                                }
                        } else if (ready_fd == child_errors_fd) {
                                char input[1024];
 
                                if (exception || feof(child_errors)) {
-                                       ast_log(LOG_WARNING, "Child process went away for channel '%s'\n", chan->name);
+                                       ast_chan_log(LOG_WARNING, chan, "Child process went away\n");
                                        res = -1;
                                        break;
                                }
 
                                if (fgets(input, sizeof(input), child_errors)) {
                                        command = ast_strip(input);
-                                       ast_log(LOG_NOTICE, "%s\n", command);
+                                       ast_chan_log(LOG_NOTICE, chan, "stderr: %s\n", command);
                                }
                        } else if ((ready_fd < 0) && ms) { 
                                if (errno == 0 || errno == EINTR)
                                        continue;
 
-                               ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
+                               ast_chan_log(LOG_WARNING, chan, "Wait failed (%s)\n", strerror(errno));
                                break;
                        }
                }
@@ -445,36 +517,41 @@ static int app_exec(struct ast_channel *chan, void *data)
        if (child_errors)
                fclose(child_errors);
 
-       if (child_stdin[0]) {
+       if (child_stdin[0])
                close(child_stdin[0]);
+
+       if (child_stdin[1])
                close(child_stdin[1]);
-       }
 
-       if (child_stdout[0]) {
+       if (child_stdout[0])
                close(child_stdout[0]);
+
+       if (child_stdout[1])
                close(child_stdout[1]);
-       }
 
-       if (child_stderr[0]) {
+       if (child_stderr[0])
                close(child_stderr[0]);
+
+       if (child_stderr[1])
                close(child_stderr[1]);
-       }
 
-       if (u) {
-               while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list)))
-                       free(entry);
+       while ((entry = AST_LIST_REMOVE_HEAD(&u->playlist, list)))
+               free(entry);
 
-               LOCAL_USER_REMOVE(u);
-       }
+       LOCAL_USER_REMOVE(u);
 
        return res;
 }
 
 int unload_module(void)
 {
+       int res;
+
+       res = ast_unregister_application(app);
+
        STANDARD_HANGUP_LOCALUSERS;
 
-       return ast_unregister_application(app);
+       return res;
 }
 
 int load_module(void)
@@ -484,7 +561,7 @@ int load_module(void)
 
 char *description(void)
 {
-       return tdesc;
+       return (char *) tdesc;
 }
 
 int usecount(void)