00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <unistd.h>
00010 #include <stdlib.h>
00011 #include <errno.h>
00012 #include <string.h>
00013 #include <sys/select.h>
00014
00015 #include "asterisk.h"
00016
00017 #include "asterisk/compat.h"
00018
00019 #define AUDIO_FILENO (STDERR_FILENO + 1)
00020
00021 static int read_environment(void)
00022 {
00023 char buf[256];
00024 char *val;
00025
00026 for(;;) {
00027 if (!fgets(buf, sizeof(buf), stdin)) {
00028 return -1;
00029 }
00030 if (feof(stdin))
00031 return -1;
00032 buf[strlen(buf) - 1] = '\0';
00033
00034 if (!strlen(buf))
00035 return 0;
00036 val = strchr(buf, ':');
00037 if (!val) {
00038 fprintf(stderr, "Invalid environment: '%s'\n", buf);
00039 return -1;
00040 }
00041 *val = '\0';
00042 val++;
00043 val++;
00044
00045 fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val);
00046
00047
00048 setenv(buf, val, 1);
00049
00050 }
00051
00052 return 0;
00053 }
00054
00055 static char *wait_result(void)
00056 {
00057 fd_set fds;
00058 int res;
00059 int bytes = 0;
00060 static char astresp[256];
00061 char audiobuf[4096];
00062 for (;;) {
00063 FD_ZERO(&fds);
00064 FD_SET(STDIN_FILENO, &fds);
00065 FD_SET(AUDIO_FILENO, &fds);
00066
00067 res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL);
00068 if (res < 0) {
00069 fprintf(stderr, "Error in select: %s\n", strerror(errno));
00070 return NULL;
00071 }
00072 if (FD_ISSET(STDIN_FILENO, &fds)) {
00073 if (!fgets(astresp, sizeof(astresp), stdin)) {
00074 return NULL;
00075 }
00076 if (feof(stdin)) {
00077 fprintf(stderr, "Got hungup on apparently\n");
00078 return NULL;
00079 }
00080 astresp[strlen(astresp) - 1] = '\0';
00081 fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp);
00082 return astresp;
00083 }
00084 if (FD_ISSET(AUDIO_FILENO, &fds)) {
00085 res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
00086 if (res > 0) {
00087
00088 #if 0
00089 fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes);
00090 #endif
00091 bytes += res;
00092
00093 if (bytes > 16000 * 3) {
00094 return "Sample Message";
00095 bytes = 0;
00096 }
00097 }
00098 }
00099 }
00100
00101 }
00102
00103 static char *run_command(char *command)
00104 {
00105 fprintf(stdout, "%s\n", command);
00106 return wait_result();
00107 }
00108
00109 static int run_script(void)
00110 {
00111 char *res;
00112 res = run_command("STREAM FILE demo-enterkeywords 0123456789*#");
00113 if (!res) {
00114 fprintf(stderr, "Failed to execute command\n");
00115 return -1;
00116 }
00117 fprintf(stderr, "1. Result is '%s'\n", res);
00118 res = run_command("STREAM FILE demo-nomatch 0123456789*#");
00119 if (!res) {
00120 fprintf(stderr, "Failed to execute command\n");
00121 return -1;
00122 }
00123 fprintf(stderr, "2. Result is '%s'\n", res);
00124 res = run_command("SAY NUMBER 23452345 0123456789*#");
00125 if (!res) {
00126 fprintf(stderr, "Failed to execute command\n");
00127 return -1;
00128 }
00129 fprintf(stderr, "3. Result is '%s'\n", res);
00130 res = run_command("GET DATA demo-enterkeywords");
00131 if (!res) {
00132 fprintf(stderr, "Failed to execute command\n");
00133 return -1;
00134 }
00135 fprintf(stderr, "4. Result is '%s'\n", res);
00136 res = run_command("STREAM FILE auth-thankyou \"\"");
00137 if (!res) {
00138 fprintf(stderr, "Failed to execute command\n");
00139 return -1;
00140 }
00141 fprintf(stderr, "5. Result is '%s'\n", res);
00142 return 0;
00143 }
00144
00145 int main(int argc, char *argv[])
00146 {
00147 char *tmp;
00148 int ver = 0;
00149 int subver = 0;
00150
00151 setlinebuf(stdin);
00152 setlinebuf(stdout);
00153 if (read_environment()) {
00154 fprintf(stderr, "Failed to read environment: %s\n", strerror(errno));
00155 exit(1);
00156 }
00157 tmp = getenv("agi_enhanced");
00158 if (tmp) {
00159 if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
00160 ver = 0;
00161 }
00162 if (ver < 1) {
00163 fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n");
00164 exit(1);
00165 }
00166 if (run_script())
00167 return -1;
00168 exit(0);
00169 }