00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 341074 $")
00033
00034 #include <signal.h>
00035
00036 #include "asterisk/file.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/config.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/module.h"
00042 #include "asterisk/frame.h"
00043 #include "asterisk/term.h"
00044 #include "asterisk/manager.h"
00045 #include "asterisk/cli.h"
00046 #include "asterisk/lock.h"
00047 #include "asterisk/linkedlists.h"
00048 #include "asterisk/chanvars.h"
00049 #include "asterisk/sched.h"
00050 #include "asterisk/io.h"
00051 #include "asterisk/utils.h"
00052 #include "asterisk/astdb.h"
00053 #include "asterisk/app.h"
00054 #include "asterisk/astobj2.h"
00055
00056 #define MODE_MATCH 0
00057 #define MODE_MATCHMORE 1
00058 #define MODE_CANMATCH 2
00059
00060 #define EXT_DATA_SIZE 256
00061
00062 enum option_flags {
00063 OPTION_PATTERNS_DISABLED = (1 << 0),
00064 };
00065
00066 AST_APP_OPTIONS(switch_opts, {
00067 AST_APP_OPTION('p', OPTION_PATTERNS_DISABLED),
00068 });
00069
00070 struct cache_entry {
00071 struct timeval when;
00072 struct ast_variable *var;
00073 int priority;
00074 char *context;
00075 char exten[2];
00076 };
00077
00078 struct ao2_container *cache;
00079 pthread_t cleanup_thread = 0;
00080
00081 static int cache_hash(const void *obj, const int flags)
00082 {
00083 const struct cache_entry *e = obj;
00084 return ast_str_case_hash(e->exten) + e->priority;
00085 }
00086
00087 static int cache_cmp(void *obj, void *arg, int flags)
00088 {
00089 struct cache_entry *e = obj, *f = arg;
00090 return e->priority != f->priority ? 0 :
00091 strcmp(e->exten, f->exten) ? 0 :
00092 strcmp(e->context, f->context) ? 0 :
00093 CMP_MATCH;
00094 }
00095
00096 static struct ast_variable *dup_vars(struct ast_variable *v)
00097 {
00098 struct ast_variable *new, *list = NULL;
00099 for (; v; v = v->next) {
00100 if (!(new = ast_variable_new(v->name, v->value, v->file))) {
00101 ast_variables_destroy(list);
00102 return NULL;
00103 }
00104
00105
00106 new->next = list;
00107 list = new;
00108 }
00109 return list;
00110 }
00111
00112 static void free_entry(void *obj)
00113 {
00114 struct cache_entry *e = obj;
00115 ast_variables_destroy(e->var);
00116 }
00117
00118 static int purge_old_fn(void *obj, void *arg, int flags)
00119 {
00120 struct cache_entry *e = obj;
00121 struct timeval *now = arg;
00122 return ast_tvdiff_ms(*now, e->when) >= 1000 ? CMP_MATCH : 0;
00123 }
00124
00125 static void *cleanup(void *unused)
00126 {
00127 struct timespec forever = { 999999999, 0 }, one_second = { 1, 0 };
00128 struct timeval now;
00129
00130 for (;;) {
00131 pthread_testcancel();
00132 if (ao2_container_count(cache) == 0) {
00133 nanosleep(&forever, NULL);
00134 }
00135 pthread_testcancel();
00136 now = ast_tvnow();
00137 ao2_callback(cache, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, purge_old_fn, &now);
00138 pthread_testcancel();
00139 nanosleep(&one_second, NULL);
00140 }
00141
00142 return NULL;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode, struct ast_flags flags)
00161 {
00162 struct ast_variable *var;
00163 struct ast_config *cfg;
00164 char pri[20];
00165 char *ematch;
00166 char rexten[AST_MAX_EXTENSION + 20]="";
00167 int match;
00168
00169
00170
00171 if (priority < 0) {
00172 return NULL;
00173 }
00174 snprintf(pri, sizeof(pri), "%d", priority);
00175 switch(mode) {
00176 case MODE_MATCHMORE:
00177 ematch = "exten LIKE";
00178 snprintf(rexten, sizeof(rexten), "%s_%%", exten);
00179 break;
00180 case MODE_CANMATCH:
00181 ematch = "exten LIKE";
00182 snprintf(rexten, sizeof(rexten), "%s%%", exten);
00183 break;
00184 case MODE_MATCH:
00185 default:
00186 ematch = "exten";
00187 ast_copy_string(rexten, exten, sizeof(rexten));
00188 }
00189 var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, SENTINEL);
00190 if (!var && !ast_test_flag(&flags, OPTION_PATTERNS_DISABLED)) {
00191 cfg = ast_load_realtime_multientry(table, "exten LIKE", "\\_%", "context", context, "priority", pri, SENTINEL);
00192 if (cfg) {
00193 char *cat = ast_category_browse(cfg, NULL);
00194
00195 while(cat) {
00196 switch(mode) {
00197 case MODE_MATCHMORE:
00198 match = ast_extension_close(cat, exten, 1);
00199 break;
00200 case MODE_CANMATCH:
00201 match = ast_extension_close(cat, exten, 0);
00202 break;
00203 case MODE_MATCH:
00204 default:
00205 match = ast_extension_match(cat, exten);
00206 }
00207 if (match) {
00208 var = ast_category_detach_variables(ast_category_get(cfg, cat));
00209 break;
00210 }
00211 cat = ast_category_browse(cfg, cat);
00212 }
00213 ast_config_destroy(cfg);
00214 }
00215 }
00216 return var;
00217 }
00218
00219 static struct ast_variable *realtime_common(const char *context, const char *exten, int priority, const char *data, int mode)
00220 {
00221 const char *ctx = NULL;
00222 char *table;
00223 struct ast_variable *var=NULL;
00224 struct ast_flags flags = { 0, };
00225 struct cache_entry *ce;
00226 struct {
00227 struct cache_entry ce;
00228 char exten[AST_MAX_EXTENSION];
00229 } cache_search = { { .priority = priority, .context = (char *) context }, };
00230 char *buf = ast_strdupa(data);
00231 if (buf) {
00232
00233
00234 char *opts = strchr(buf, '/');
00235 if (opts)
00236 *opts++ = '\0';
00237 table = strchr(buf, '@');
00238 if (table) {
00239 *table++ = '\0';
00240 ctx = buf;
00241 }
00242 ctx = S_OR(ctx, context);
00243 table = S_OR(table, "extensions");
00244 if (!ast_strlen_zero(opts)) {
00245 ast_app_parse_options(switch_opts, &flags, NULL, opts);
00246 }
00247 ast_copy_string(cache_search.exten, exten, sizeof(cache_search.exten));
00248 if (mode == MODE_MATCH && (ce = ao2_find(cache, &cache_search, OBJ_POINTER))) {
00249 var = dup_vars(ce->var);
00250 ao2_ref(ce, -1);
00251 } else {
00252 var = realtime_switch_common(table, ctx, exten, priority, mode, flags);
00253 do {
00254 struct ast_variable *new;
00255
00256 if (mode != MODE_MATCH) {
00257 break;
00258 }
00259 if (!(new = dup_vars(var))) {
00260 break;
00261 }
00262 if (!(ce = ao2_alloc(sizeof(*ce) + strlen(exten) + strlen(context), free_entry))) {
00263 ast_variables_destroy(new);
00264 break;
00265 }
00266 ce->context = ce->exten + strlen(exten) + 1;
00267 strcpy(ce->exten, exten);
00268 strcpy(ce->context, context);
00269 ce->priority = priority;
00270 ce->var = new;
00271 ce->when = ast_tvnow();
00272 ao2_link(cache, ce);
00273 pthread_kill(cleanup_thread, SIGURG);
00274 ao2_ref(ce, -1);
00275 } while (0);
00276 }
00277 }
00278 return var;
00279 }
00280
00281 static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
00282 {
00283 struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
00284 if (var) {
00285 ast_variables_destroy(var);
00286 return 1;
00287 }
00288 return 0;
00289 }
00290
00291 static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
00292 {
00293 struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_CANMATCH);
00294 if (var) {
00295 ast_variables_destroy(var);
00296 return 1;
00297 }
00298 return 0;
00299 }
00300
00301 static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
00302 {
00303 int res = -1;
00304 struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
00305
00306 if (var) {
00307 char *tmp="";
00308 char *app = NULL;
00309 struct ast_variable *v;
00310
00311 for (v = var; v ; v = v->next) {
00312 if (!strcasecmp(v->name, "app"))
00313 app = ast_strdupa(v->value);
00314 else if (!strcasecmp(v->name, "appdata")) {
00315 if (ast_compat_pbx_realtime) {
00316 char *ptr;
00317 int in = 0;
00318 tmp = alloca(strlen(v->value) * 2 + 1);
00319 for (ptr = tmp; *v->value; v->value++) {
00320 if (*v->value == ',') {
00321 *ptr++ = '\\';
00322 *ptr++ = ',';
00323 } else if (*v->value == '|' && !in) {
00324 *ptr++ = ',';
00325 } else {
00326 *ptr++ = *v->value;
00327 }
00328
00329
00330 if (v->value[0] == '[' && v->value[-1] == '$') {
00331 in++;
00332 } else if (v->value[0] == ']' && in) {
00333 in--;
00334 }
00335 }
00336 *ptr = '\0';
00337 } else {
00338 tmp = ast_strdupa(v->value);
00339 }
00340 }
00341 }
00342 ast_variables_destroy(var);
00343 if (!ast_strlen_zero(app)) {
00344 struct ast_app *a = pbx_findapp(app);
00345 if (a) {
00346 char appdata[512];
00347 char tmp1[80];
00348 char tmp2[80];
00349 char tmp3[EXT_DATA_SIZE];
00350
00351 appdata[0] = 0;
00352 if(!ast_strlen_zero(tmp))
00353 pbx_substitute_variables_helper(chan, tmp, appdata, sizeof(appdata) - 1);
00354 ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n",
00355 chan->exten, chan->context, chan->priority,
00356 term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
00357 term_color(tmp2, chan->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
00358 term_color(tmp3, S_OR(appdata, ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
00359 manager_event(EVENT_FLAG_DIALPLAN, "Newexten",
00360 "Channel: %s\r\n"
00361 "Context: %s\r\n"
00362 "Extension: %s\r\n"
00363 "Priority: %d\r\n"
00364 "Application: %s\r\n"
00365 "AppData: %s\r\n"
00366 "Uniqueid: %s\r\n",
00367 chan->name, chan->context, chan->exten, chan->priority, app, !ast_strlen_zero(appdata) ? appdata : "(NULL)", chan->uniqueid);
00368
00369 res = pbx_exec(chan, a, appdata);
00370 } else
00371 ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
00372 } else {
00373 ast_log(LOG_WARNING, "No application specified for realtime extension '%s' in context '%s'\n", exten, context);
00374 }
00375 }
00376 return res;
00377 }
00378
00379 static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
00380 {
00381 struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCHMORE);
00382 if (var) {
00383 ast_variables_destroy(var);
00384 return 1;
00385 }
00386 return 0;
00387 }
00388
00389 static struct ast_switch realtime_switch =
00390 {
00391 name: "Realtime",
00392 description: "Realtime Dialplan Switch",
00393 exists: realtime_exists,
00394 canmatch: realtime_canmatch,
00395 exec: realtime_exec,
00396 matchmore: realtime_matchmore,
00397 };
00398
00399 static int unload_module(void)
00400 {
00401 ast_unregister_switch(&realtime_switch);
00402 pthread_cancel(cleanup_thread);
00403 pthread_kill(cleanup_thread, SIGURG);
00404 pthread_join(cleanup_thread, NULL);
00405
00406 ao2_ref(cache, -1);
00407 return 0;
00408 }
00409
00410 static int load_module(void)
00411 {
00412 if (!(cache = ao2_container_alloc(573, cache_hash, cache_cmp))) {
00413 return AST_MODULE_LOAD_FAILURE;
00414 }
00415
00416 if (ast_pthread_create(&cleanup_thread, NULL, cleanup, NULL)) {
00417 return AST_MODULE_LOAD_FAILURE;
00418 }
00419
00420 if (ast_register_switch(&realtime_switch))
00421 return AST_MODULE_LOAD_FAILURE;
00422 return AST_MODULE_LOAD_SUCCESS;
00423 }
00424
00425 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Switch");