4 * Copyright (C) 2004, Digium Inc.
6 * Written by Mark Spencer <markster@digium.com>
8 * This program is Free Software distributed under the terms of
9 * of the GNU General Public License.
12 #include <asterisk/file.h>
13 #include <asterisk/logger.h>
14 #include <asterisk/channel.h>
15 #include <asterisk/config.h>
16 #include <asterisk/options.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/frame.h>
20 #include <asterisk/file.h>
21 #include <asterisk/cli.h>
22 #include <asterisk/lock.h>
23 #include <asterisk/md5.h>
24 #include <asterisk/linkedlists.h>
25 #include <asterisk/chanvars.h>
26 #include <asterisk/sched.h>
27 #include <asterisk/io.h>
28 #include <asterisk/utils.h>
29 #include <asterisk/crypto.h>
30 #include <asterisk/astdb.h>
37 static char *tdesc = "Realtime Switch";
39 /* Realtime switch looks up extensions in the supplied realtime table.
41 [context@][realtimetable][/options]
43 If the realtimetable is omitted it is assumed to be "extensions". If no context is
44 specified the context is assumed to be whatever is the container.
46 The realtime table should have entries for context,exten,priority,app,args
48 The realtime table currently does not support patterns or callerid fields.
53 #define REALTIME_COMMON(mode) \
59 struct ast_variable *var=NULL; \
60 buf = ast_strdupa(data); \
62 opts = strchr(buf, '/'); \
68 table = strchr(buf, '@'); \
74 if (!cxt || ast_strlen_zero(cxt)) \
76 if (!table || ast_strlen_zero(table)) \
77 table = "extensions"; \
78 var = realtime_switch_common(table, cxt, exten, priority, mode); \
82 static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority)
84 struct ast_variable *var;
86 snprintf(pri, sizeof(pri), "%d", priority);
87 printf("%s/%s/%s/%s exists\n", table, context, exten, pri);
88 var = ast_load_realtime(table, "context", context, "exten", exten, "priority", pri, NULL);
92 static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
95 if (var) ast_destroy_realtime(var);
98 return res > 0 ? res : 0;
101 static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
104 if (var) ast_destroy_realtime(var);
107 return res > 0 ? res : 0;
110 static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
115 struct ast_variable *v;
120 if (!strcasecmp(v->name, "app"))
121 strncpy(app, v->value, sizeof(app) -1 );
122 else if (!strcasecmp(v->name, "appdata"))
123 appdata = ast_strdupa(v->value);
126 ast_destroy_realtime(var);
127 if (!ast_strlen_zero(app)) {
128 a = pbx_findapp(app);
130 res = pbx_exec(chan, a, appdata, newstack);
132 ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
138 static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
141 if (var) ast_destroy_realtime(var);
142 return res > 0 ? res : 0;
145 static struct ast_switch realtime_switch =
148 description: "Realtime Dialplan Switch",
149 exists: realtime_exists,
150 canmatch: realtime_canmatch,
152 matchmore: realtime_matchmore,
155 char *description(void)
167 return ASTERISK_GPL_KEY;
170 int unload_module(void)
172 ast_unregister_switch(&realtime_switch);
176 int load_module(void)
178 ast_register_switch(&realtime_switch);