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