2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Realtime PBX Module
23 * \arg See also: \ref AstARA
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/config.h"
40 #include "asterisk/options.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/term.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/file.h"
47 #include "asterisk/cli.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/md5.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/chanvars.h"
52 #include "asterisk/sched.h"
53 #include "asterisk/io.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/crypto.h"
56 #include "asterisk/astdb.h"
59 #define MODE_MATCHMORE 1
60 #define MODE_CANMATCH 2
62 #define EXT_DATA_SIZE 256
65 /* Realtime switch looks up extensions in the supplied realtime table.
67 [context@][realtimetable][/options]
69 If the realtimetable is omitted it is assumed to be "extensions". If no context is
70 specified the context is assumed to be whatever is the container.
72 The realtime table should have entries for context,exten,priority,app,args
74 The realtime table currently does not support callerid fields.
79 static struct ast_variable
*realtime_switch_common(const char *table
, const char *context
, const char *exten
, int priority
, int mode
)
81 struct ast_variable
*var
;
82 struct ast_config
*cfg
;
85 char rexten
[AST_MAX_EXTENSION
+ 20]="";
87 snprintf(pri
, sizeof(pri
), "%d", priority
);
90 ematch
= "exten LIKE";
91 snprintf(rexten
, sizeof(rexten
), "%s_%%", exten
);
94 ematch
= "exten LIKE";
95 snprintf(rexten
, sizeof(rexten
), "%s%%", exten
);
100 ast_copy_string(rexten
, exten
, sizeof(rexten
));
102 var
= ast_load_realtime(table
, ematch
, rexten
, "context", context
, "priority", pri
, NULL
);
104 cfg
= ast_load_realtime_multientry(table
, "exten LIKE", "\\_%", "context", context
, "priority", pri
, NULL
);
106 char *cat
= ast_category_browse(cfg
, NULL
);
111 match
= ast_extension_close(cat
, exten
, 1);
114 match
= ast_extension_close(cat
, exten
, 0);
118 match
= ast_extension_match(cat
, exten
);
121 var
= ast_category_detach_variables(ast_category_get(cfg
, cat
));
124 cat
= ast_category_browse(cfg
, cat
);
126 ast_config_destroy(cfg
);
132 static struct ast_variable
*realtime_common(const char *context
, const char *exten
, int priority
, const char *data
, int mode
)
134 const char *ctx
= NULL
;
136 struct ast_variable
*var
=NULL
;
137 char *buf
= ast_strdupa(data
);
139 char *opts
= strchr(buf
, '/');
142 table
= strchr(buf
, '@');
147 ctx
= S_OR(ctx
, context
);
148 table
= S_OR(table
, "extensions");
149 var
= realtime_switch_common(table
, ctx
, exten
, priority
, mode
);
154 static int realtime_exists(struct ast_channel
*chan
, const char *context
, const char *exten
, int priority
, const char *callerid
, const char *data
)
156 struct ast_variable
*var
= realtime_common(context
, exten
, priority
, data
, MODE_MATCH
);
158 ast_variables_destroy(var
);
164 static int realtime_canmatch(struct ast_channel
*chan
, const char *context
, const char *exten
, int priority
, const char *callerid
, const char *data
)
166 struct ast_variable
*var
= realtime_common(context
, exten
, priority
, data
, MODE_CANMATCH
);
168 ast_variables_destroy(var
);
174 static int realtime_exec(struct ast_channel
*chan
, const char *context
, const char *exten
, int priority
, const char *callerid
, const char *data
)
177 struct ast_variable
*var
= realtime_common(context
, exten
, priority
, data
, MODE_MATCH
);
182 struct ast_variable
*v
;
184 for (v
= var
; v
; v
= v
->next
) {
185 if (!strcasecmp(v
->name
, "app"))
186 app
= ast_strdupa(v
->value
);
187 else if (!strcasecmp(v
->name
, "appdata"))
188 tmp
= ast_strdupa(v
->value
);
190 ast_variables_destroy(var
);
191 if (!ast_strlen_zero(app
)) {
192 struct ast_app
*a
= pbx_findapp(app
);
194 char appdata
[512]="";
197 char tmp3
[EXT_DATA_SIZE
];
199 if(!ast_strlen_zero(tmp
))
200 pbx_substitute_variables_helper(chan
, tmp
, appdata
, sizeof(appdata
) - 1);
201 if (option_verbose
> 2)
202 ast_verbose( VERBOSE_PREFIX_3
"Executing %s(\"%s\", \"%s\")\n",
203 term_color(tmp1
, app
, COLOR_BRCYAN
, 0, sizeof(tmp1
)),
204 term_color(tmp2
, chan
->name
, COLOR_BRMAGENTA
, 0, sizeof(tmp2
)),
205 term_color(tmp3
, S_OR(appdata
, ""), COLOR_BRMAGENTA
, 0, sizeof(tmp3
)));
206 manager_event(EVENT_FLAG_CALL
, "Newexten",
211 "Application: %s\r\n"
214 chan
->name
, chan
->context
, chan
->exten
, chan
->priority
, app
, !ast_strlen_zero(appdata
) ? appdata
: "(NULL)", chan
->uniqueid
);
216 res
= pbx_exec(chan
, a
, appdata
);
218 ast_log(LOG_NOTICE
, "No such application '%s' for extension '%s' in context '%s'\n", app
, exten
, context
);
220 ast_log(LOG_WARNING
, "No application specified for realtime extension '%s' in context '%s'\n", exten
, context
);
226 static int realtime_matchmore(struct ast_channel
*chan
, const char *context
, const char *exten
, int priority
, const char *callerid
, const char *data
)
228 struct ast_variable
*var
= realtime_common(context
, exten
, priority
, data
, MODE_MATCHMORE
);
230 ast_variables_destroy(var
);
236 static struct ast_switch realtime_switch
=
239 description
: "Realtime Dialplan Switch",
240 exists
: realtime_exists
,
241 canmatch
: realtime_canmatch
,
243 matchmore
: realtime_matchmore
,
246 static int unload_module(void)
248 ast_unregister_switch(&realtime_switch
);
252 static int load_module(void)
254 ast_register_switch(&realtime_switch
);
258 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Realtime Switch");