#include "jitterbuf.h"
#endif
-typedef long time_in_ms_t;
+typedef int64_t time_in_ms_t;
#include "iax-client.h"
#include "md5.h"
event_handlers/mod_xmpp_event
formats/mod_sndfile
timers/mod_softtimer
+dialplans/mod_pcre
char buf[1024];
/*! current line number in file */
int lineno;
+ /*! current category number in file */
+ int catno;
};
/*!
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-
+#include <pcre.h>
static const char modname[] = "mod_pcre";
+#define cleanre() if (re) {\
+ pcre_free(re);\
+ re = NULL;\
+ }
switch_caller_extension *dialplan_hunt(switch_core_session *session)
{
switch_caller_profile *caller_profile;
switch_caller_extension *extension = NULL;
switch_channel *channel;
- char *cf = "extensions.conf";
+ char *cf = "regextensions.conf";
switch_config cfg;
char *var, *val;
- char app[1024];
+ char app[1024] = "";
+ int catno = -1;
+ char *regex = NULL;
+ char *exten_name = NULL;
+ pcre *re = NULL;
channel = switch_core_session_get_channel(session);
caller_profile = switch_channel_get_caller_profile(channel);
- //switch_channel_set_variable(channel, "pleasework", "yay");
- switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Hello %s You Dialed %s!\n", caller_profile->caller_id_name, caller_profile->destination_number);
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Processing %s->%s!\n", caller_profile->caller_id_name, caller_profile->destination_number);
if (!switch_config_open_file(&cfg, cf)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "open of %s failed\n", cf);
}
while (switch_config_next_pair(&cfg, &var, &val)) {
- if (!strcasecmp(cfg.category, "extensions")) {
- if (!strcmp(var, caller_profile->destination_number) && val) {
+ if (cfg.catno != catno) { /* new category */
+ regex = NULL;
+ catno = cfg.catno;
+ exten_name = cfg.category;
+ }
+
+
+
+ if (!strcasecmp(var, "regex")) {
+ const char *error = NULL;
+ int erroffset = 0;
+
+ cleanre();
+ re = pcre_compile(
+ val, /* the pattern */
+ 0, /* default options */
+ &error, /* for error message */
+ &erroffset, /* for error offset */
+ NULL); /* use default character tables */
+ if (error) {
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "ERROR: %d %s\n", erroffset, error);
+ cleanre();
+ return NULL;
+ }
+ } else if (!strcasecmp(var, "match")) {
+ int rc;
+ int ovector[30];
+
+ rc = pcre_exec(
+ re, /* result of pcre_compile() */
+ NULL, /* we didn't study the pattern */
+ caller_profile->destination_number, /* the subject string */
+ strlen(caller_profile->destination_number), /* the length of the subject string */
+ 0, /* start at offset 0 in the subject */
+ 0, /* default options */
+ ovector, /* vector of integers for substring information */
+ sizeof(ovector) / sizeof(ovector[0])); /* number of elements (NOT size in bytes) */
+
+
+ if (rc > 0) {
+ char newval[1024] = "";
+ char index[10] = "";
+ char replace[128] = "";
+ int x, y=0, z=0, num = 0;
char *data;
+
+ for (x = 0; x < sizeof(newval) && x < strlen(val);) {
+ if (val[x] == '$') {
+ x++;
+
+ while(val[x] > 47 && val[x] < 58) {
+ index[z++] = val[x];
+ x++;
+ }
+ index[z++] = '\0';
+ z = 0;
+ num = atoi(index);
+
+ if (pcre_copy_substring(caller_profile->destination_number, ovector, rc, num, replace, sizeof(replace)) > 0) {
+ int r;
+ for(r = 0; r < strlen(replace); r++) {
+ newval[y++] = replace[r];
+ }
+ }
+ } else {
+ newval[y++] = val[x];
+ x++;
+ }
+ }
+ newval[y++] = '\0';
+
memset(app, 0, sizeof(app));
- strncpy(app, val, sizeof(app));
+ switch_copy_string(app, newval, sizeof(app));
if ((data = strchr(app, ' '))) {
*data = '\0';
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "invalid extension on line %d\n", cfg.lineno);
continue;
}
+
+
if (!extension) {
- if (!(extension = switch_caller_extension_new(session, caller_profile->destination_number, caller_profile->destination_number))) {
+ if (!(extension = switch_caller_extension_new(session, exten_name, caller_profile->destination_number))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "memory error!\n");
break;
}
}
switch_caller_extension_add_application(session, extension, app, data);
- }
+ }
+
}
+
}
switch_config_close_file(&cfg);
switch_channel_hangup(channel);
}
+ cleanre();
return extension;
}
static const switch_dialplan_interface dialplan_interface = {
- /*.interface_name =*/ "demo",
+ /*.interface_name =*/ "pcre",
/*.hunt_function = */ dialplan_hunt
/*.next = NULL */
};
unsigned int codecs;
unsigned short samprate;
switch_mutex_t *mutex;
- switch_thread_cond_t *cond;
+ //switch_thread_cond_t *cond;
};
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
switch_set_flag(tech_pvt, TFLAG_IO);
switch_mutex_init(&tech_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
- switch_thread_cond_create(&tech_pvt->cond, switch_core_session_get_pool(session));
- switch_mutex_lock(tech_pvt->mutex);
+ //switch_thread_cond_create(&tech_pvt->cond, switch_core_session_get_pool(session));
+ //switch_mutex_lock(tech_pvt->mutex);
/* Move Channel's State Machine to RING */
switch_channel_set_state(channel, CS_RING);
assert(tech_pvt != NULL);
switch_clear_flag(tech_pvt, TFLAG_IO);
- switch_thread_cond_signal(tech_pvt->cond);
+ switch_clear_flag(tech_pvt, TFLAG_VOICE);
+ //switch_thread_cond_signal(tech_pvt->cond);
if (tech_pvt->read_codec.implementation) {
switch_core_codec_destroy(&tech_pvt->read_codec);
switch_clear_flag(tech_pvt, TFLAG_IO);
switch_clear_flag(tech_pvt, TFLAG_VOICE);
switch_channel_hangup(channel);
- switch_thread_cond_signal(tech_pvt->cond);
+ //switch_thread_cond_signal(tech_pvt->cond);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s CHANNEL KILL\n", switch_channel_get_name(channel));
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
- for(;;) {
- if (switch_test_flag(tech_pvt, TFLAG_IO)) {
- switch_thread_cond_wait(tech_pvt->cond, tech_pvt->mutex);
- if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
- return SWITCH_STATUS_FALSE;
- }
-
- if (switch_test_flag(tech_pvt, TFLAG_IO)) {
- switch_clear_flag(tech_pvt, TFLAG_VOICE);
- if(!tech_pvt->read_frame.datalen) {
- continue;
- }
- *frame = &tech_pvt->read_frame;
- return SWITCH_STATUS_SUCCESS;
+ while (switch_test_flag(tech_pvt, TFLAG_IO)) {
+ //switch_thread_cond_wait(tech_pvt->cond, tech_pvt->mutex);
+ if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
+ return SWITCH_STATUS_FALSE;
+ }
+
+ if (switch_test_flag(tech_pvt, TFLAG_IO) && switch_test_flag(tech_pvt, TFLAG_VOICE)) {
+ switch_clear_flag(tech_pvt, TFLAG_VOICE);
+ if(!tech_pvt->read_frame.datalen) {
+ continue;
}
+
+ *frame = &tech_pvt->read_frame;
+ return SWITCH_STATUS_SUCCESS;
}
- break;
+ switch_yield(1000);
}
+
+
return SWITCH_STATUS_FALSE;
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Hangup %s\n", switch_channel_get_name(channel));
switch_set_flag(tech_pvt, TFLAG_HANGUP);
switch_channel_hangup(channel);
- switch_thread_cond_signal(tech_pvt->cond);
+ //switch_thread_cond_signal(tech_pvt->cond);
iaxevent->session = NULL;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "No Session? %s\n", switch_test_flag(tech_pvt, TFLAG_VOICE) ? "yes" : "no");
break;
case IAX_EVENT_VOICE:
if (tech_pvt && (tech_pvt->read_frame.datalen = iaxevent->datalen)) {
- int bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
- int frames = (int)(tech_pvt->read_frame.datalen / bytes);
- tech_pvt->read_frame.samples = frames * tech_pvt->read_codec.implementation->samples_per_frame;
- memcpy(tech_pvt->read_frame.data, iaxevent->data, iaxevent->datalen);
- /* wake up the i/o thread*/
- switch_set_flag(tech_pvt, TFLAG_VOICE);
- switch_thread_cond_signal(tech_pvt->cond);
+ switch_channel *channel;
+ if ((channel = switch_core_session_get_channel(tech_pvt->session)) && switch_channel_get_state(channel) <= CS_HANGUP) {
+ int bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
+ int frames = (int)(tech_pvt->read_frame.datalen / bytes);
+ tech_pvt->read_frame.samples = frames * tech_pvt->read_codec.implementation->samples_per_frame;
+ memcpy(tech_pvt->read_frame.data, iaxevent->data, iaxevent->datalen);
+ /* wake up the i/o thread*/
+ switch_set_flag(tech_pvt, TFLAG_VOICE);
+ //switch_thread_cond_signal(tech_pvt->cond);
+ }
}
break;
case IAX_EVENT_TRANSFER:
*end = '\0';
(*var)++;
switch_copy_string(cfg->category, *var, sizeof(cfg->category));
+ cfg->catno++;
continue;
}