| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "asterisk.h" |
| 13 |
|
| 14 |
#define AUDIO_FILENO (STDERR_FILENO + 1) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
static int read_environment(void) |
| 26 |
{ |
| 27 |
char buf[256]; |
| 28 |
char *val; |
| 29 |
|
| 30 |
for(;;) { |
| 31 |
if (!fgets(buf, sizeof(buf), stdin)) { |
| 32 |
return -1; |
| 33 |
} |
| 34 |
if (feof(stdin)) |
| 35 |
return -1; |
| 36 |
buf[strlen(buf) - 1] = '\0'; |
| 37 |
|
| 38 |
if (!strlen(buf)) |
| 39 |
return 0; |
| 40 |
val = strchr(buf, ':'); |
| 41 |
if (!val) { |
| 42 |
fprintf(stderr, "Invalid environment: '%s'\n", buf); |
| 43 |
return -1; |
| 44 |
} |
| 45 |
*val = '\0'; |
| 46 |
val++; |
| 47 |
val++; |
| 48 |
|
| 49 |
fprintf(stderr, "Environment: '%s' is '%s'\n", buf, val); |
| 50 |
|
| 51 |
|
| 52 |
setenv(buf, val, 1); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static char *wait_result(void) |
| 60 |
{ |
| 61 |
fd_set fds; |
| 62 |
int res; |
| 63 |
int bytes = 0; |
| 64 |
static char astresp[256]; |
| 65 |
char audiobuf[4096]; |
| 66 |
for (;;) { |
| 67 |
FD_ZERO(&fds); |
| 68 |
FD_SET(STDIN_FILENO, &fds); |
| 69 |
FD_SET(AUDIO_FILENO, &fds); |
| 70 |
|
| 71 |
res = select(AUDIO_FILENO + 1, &fds, NULL, NULL, NULL); |
| 72 |
if (res < 0) { |
| 73 |
fprintf(stderr, "Error in select: %s\n", strerror(errno)); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
if (FD_ISSET(STDIN_FILENO, &fds)) { |
| 77 |
if (!fgets(astresp, sizeof(astresp), stdin)) { |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
if (feof(stdin)) { |
| 81 |
fprintf(stderr, "Got hungup on apparently\n"); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
astresp[strlen(astresp) - 1] = '\0'; |
| 85 |
fprintf(stderr, "Ooh, got a response from Asterisk: '%s'\n", astresp); |
| 86 |
return astresp; |
| 87 |
} |
| 88 |
if (FD_ISSET(AUDIO_FILENO, &fds)) { |
| 89 |
res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf)); |
| 90 |
if (res > 0) { |
| 91 |
|
| 92 |
#if 0 |
| 93 |
fprintf(stderr, "Got %d/%d bytes of audio\n", res, bytes); |
| 94 |
#endif |
| 95 |
bytes += res; |
| 96 |
|
| 97 |
if (bytes > 16000 * 3) { |
| 98 |
return "Sample Message"; |
| 99 |
bytes = 0; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
static char *run_command(char *command) |
| 108 |
{ |
| 109 |
fprintf(stdout, "%s\n", command); |
| 110 |
return wait_result(); |
| 111 |
} |
| 112 |
|
| 113 |
static int run_script(void) |
| 114 |
{ |
| 115 |
char *res; |
| 116 |
res = run_command("STREAM FILE demo-enterkeywords 0123456789*#"); |
| 117 |
if (!res) { |
| 118 |
fprintf(stderr, "Failed to execute command\n"); |
| 119 |
return -1; |
| 120 |
} |
| 121 |
fprintf(stderr, "1. Result is '%s'\n", res); |
| 122 |
res = run_command("STREAM FILE demo-nomatch 0123456789*#"); |
| 123 |
if (!res) { |
| 124 |
fprintf(stderr, "Failed to execute command\n"); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
fprintf(stderr, "2. Result is '%s'\n", res); |
| 128 |
res = run_command("SAY NUMBER 23452345 0123456789*#"); |
| 129 |
if (!res) { |
| 130 |
fprintf(stderr, "Failed to execute command\n"); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
fprintf(stderr, "3. Result is '%s'\n", res); |
| 134 |
res = run_command("GET DATA demo-enterkeywords"); |
| 135 |
if (!res) { |
| 136 |
fprintf(stderr, "Failed to execute command\n"); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
fprintf(stderr, "4. Result is '%s'\n", res); |
| 140 |
res = run_command("STREAM FILE auth-thankyou \"\""); |
| 141 |
if (!res) { |
| 142 |
fprintf(stderr, "Failed to execute command\n"); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
fprintf(stderr, "5. Result is '%s'\n", res); |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
int main(int argc, char *argv[]) |
| 150 |
{ |
| 151 |
char *tmp; |
| 152 |
int ver = 0; |
| 153 |
int subver = 0; |
| 154 |
|
| 155 |
setlinebuf(stdin); |
| 156 |
setlinebuf(stdout); |
| 157 |
if (read_environment()) { |
| 158 |
fprintf(stderr, "Failed to read environment: %s\n", strerror(errno)); |
| 159 |
exit(1); |
| 160 |
} |
| 161 |
tmp = getenv("agi_enhanced"); |
| 162 |
if (tmp) { |
| 163 |
if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2) |
| 164 |
ver = 0; |
| 165 |
} |
| 166 |
if (ver < 1) { |
| 167 |
fprintf(stderr, "No enhanced AGI services available. Use EAGI, not AGI\n"); |
| 168 |
exit(1); |
| 169 |
} |
| 170 |
if (run_script()) |
| 171 |
return -1; |
| 172 |
exit(0); |
| 173 |
} |