Last active
December 19, 2015 04:39
-
-
Save seven1240/5898728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
<extension name="socket"> | |
<condition field="destination_number" expression="^socket$"> | |
<action application="socket" data="127.0.0.1 8084 async full"/> | |
</condition> | |
</extension> | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <esl.h> | |
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr) | |
{ | |
esl_handle_t handle = {{0}}; | |
int done = 0; | |
esl_status_t status; | |
time_t exp = 0; | |
char digits[5] = {0}; | |
int i = 0; | |
esl_attach_handle(&handle, client_sock, addr); | |
esl_log(ESL_LOG_INFO, "Connected! %d\n", handle.sock); | |
esl_filter(&handle, "unique-id", esl_event_get_header(handle.info_event, "caller-unique-id")); | |
esl_events(&handle, ESL_EVENT_TYPE_PLAIN, "SESSION_HEARTBEAT CHANNEL_ANSWER CHANNEL_ORIGINATE CHANNEL_PROGRESS CHANNEL_HANGUP DTMF" | |
"CHANNEL_BRIDGE CHANNEL_UNBRIDGE CHANNEL_OUTGOING CHANNEL_EXECUTE CHANNEL_EXECUTE_COMPLETE DTMF CUSTOM conference::maintenance"); | |
esl_send_recv(&handle, "linger"); | |
esl_execute(&handle, "answer", NULL, NULL); | |
esl_execute(&handle, "playback", "/tmp/please-input-a-number.wav", NULL); | |
while((status = esl_recv_timed(&handle, 1000)) != ESL_FAIL) { | |
if (done) { | |
if (time(NULL) >= exp) { | |
break; | |
} | |
} else if (status == ESL_SUCCESS) { | |
const char *type = esl_event_get_header(handle.last_event, "content-type"); | |
if (type && !strcasecmp(type, "text/disconnect-notice")) { | |
const char *dispo = esl_event_get_header(handle.last_event, "content-disposition"); | |
esl_log(ESL_LOG_INFO, "Got a disconnection notice dispostion: [%s]\n", dispo ? dispo : ""); | |
if (dispo && !strcmp(dispo, "linger")) { | |
done = 1; | |
esl_log(ESL_LOG_INFO, "Waiting 5 seconds for any remaining events.\n"); | |
exp = time(NULL) + 5; | |
} | |
} else if (type) { | |
const char *event_name = esl_event_get_header(handle.last_ievent, "Event-Name"); | |
const char *dtmf = esl_event_get_header(handle.last_ievent, "DTMF-Digit"); | |
esl_log(ESL_LOG_INFO, "recv event_name: %s\n", event_name); | |
if (!strcasecmp(event_name, "DTMF")) { | |
digits[i++] = dtmf[0]; | |
if (i == 4) { | |
esl_execute(&handle, "transfer", digits, NULL); | |
} | |
} | |
} | |
} else { | |
// const char *type = esl_event_get_header(handle.last_event, "content-type"); | |
// esl_log(ESL_LOG_INFO, "blaaaaaaaaaaaaaaa %d ESL_BREAK\n", status); | |
// if (type) { | |
// esl_log(ESL_LOG_INFO, "recv type: %s\n", type); | |
// } | |
} | |
} | |
esl_log(ESL_LOG_INFO, "Disconnected! %d\n", handle.sock); | |
esl_disconnect(&handle); | |
} | |
int main(void) | |
{ | |
esl_global_set_default_logger(7); | |
esl_listen_threaded("localhost", 8084, mycallback, 100000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment