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;
91 char rexten[AST_MAX_EXTENSION + 20]="";
92 snprintf(pri, sizeof(pri), "%d", priority);
95 ematch = "exten LIKE";
96 snprintf(rexten, sizeof(rexten), "%s_%%", exten);
99 ematch = "exten LIKE";
100 snprintf(rexten, sizeof(rexten), "%s%%", exten);
105 strncpy(rexten, exten, sizeof(rexten) - 1);
107 var = ast_load_realtime(table, "context", context, ematch, rexten, "priority", pri, NULL);
111 static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
113 REALTIME_COMMON(MODE_MATCH);
114 if (var) ast_destroy_realtime(var);
117 return res > 0 ? res : 0;
120 static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
122 REALTIME_COMMON(MODE_CANMATCH);
123 if (var) ast_destroy_realtime(var);
126 return res > 0 ? res : 0;
129 static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
134 struct ast_variable *v;
135 REALTIME_COMMON(MODE_MATCH);
139 if (!strcasecmp(v->name, "app"))
140 strncpy(app, v->value, sizeof(app) -1 );
141 else if (!strcasecmp(v->name, "appdata"))
142 appdata = ast_strdupa(v->value);
145 ast_destroy_realtime(var);
146 if (!ast_strlen_zero(app)) {
147 a = pbx_findapp(app);
149 res = pbx_exec(chan, a, appdata, newstack);
151 ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
157 static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
159 REALTIME_COMMON(MODE_MATCHMORE);
160 if (var) ast_destroy_realtime(var);
163 return res > 0 ? res : 0;
166 static struct ast_switch realtime_switch =
169 description: "Realtime Dialplan Switch",
170 exists: realtime_exists,
171 canmatch: realtime_canmatch,
173 matchmore: realtime_matchmore,
176 char *description(void)
188 return ASTERISK_GPL_KEY;
191 int unload_module(void)
193 ast_unregister_switch(&realtime_switch);
197 int load_module(void)
199 ast_register_switch(&realtime_switch);