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>
38 #define MODE_MATCHMORE 1
39 #define MODE_CANMATCH 2
41 static char *tdesc = "Realtime Switch";
43 /* Realtime switch looks up extensions in the supplied realtime table.
45 [context@][realtimetable][/options]
47 If the realtimetable is omitted it is assumed to be "extensions". If no context is
48 specified the context is assumed to be whatever is the container.
50 The realtime table should have entries for context,exten,priority,app,args
52 The realtime table currently does not support patterns or callerid fields.
57 #define REALTIME_COMMON(mode) \
63 struct ast_variable *var=NULL; \
64 buf = ast_strdupa(data); \
66 opts = strchr(buf, '/'); \
72 table = strchr(buf, '@'); \
78 if (!cxt || ast_strlen_zero(cxt)) \
80 if (!table || ast_strlen_zero(table)) \
81 table = "extensions"; \
82 var = realtime_switch_common(table, cxt, exten, priority, mode); \
86 static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode)
88 struct ast_variable *var;
90 snprintf(pri, sizeof(pri), "%d", priority);
91 printf("%s/%s/%s/%s exists\n", table, context, exten, pri);
92 var = ast_load_realtime(table, "context", context, "exten", exten, "priority", pri, NULL);
96 static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
98 REALTIME_COMMON(MODE_MATCH);
99 if (var) ast_destroy_realtime(var);
102 return res > 0 ? res : 0;
105 static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
107 REALTIME_COMMON(MODE_CANMATCH);
108 if (var) ast_destroy_realtime(var);
111 return res > 0 ? res : 0;
114 static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
119 struct ast_variable *v;
120 REALTIME_COMMON(MODE_MATCH);
124 if (!strcasecmp(v->name, "app"))
125 strncpy(app, v->value, sizeof(app) -1 );
126 else if (!strcasecmp(v->name, "appdata"))
127 appdata = ast_strdupa(v->value);
130 ast_destroy_realtime(var);
131 if (!ast_strlen_zero(app)) {
132 a = pbx_findapp(app);
134 res = pbx_exec(chan, a, appdata, newstack);
136 ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
142 static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
144 REALTIME_COMMON(MODE_MATCHMORE);
145 if (var) ast_destroy_realtime(var);
146 return res > 0 ? res : 0;
149 static struct ast_switch realtime_switch =
152 description: "Realtime Dialplan Switch",
153 exists: realtime_exists,
154 canmatch: realtime_canmatch,
156 matchmore: realtime_matchmore,
159 char *description(void)
171 return ASTERISK_GPL_KEY;
174 int unload_module(void)
176 ast_unregister_switch(&realtime_switch);
180 int load_module(void)
182 ast_register_switch(&realtime_switch);