Merged revisions 72806 via svnmerge from
[asterisk/asterisk.git] / pbx / pbx_realtime.c
old mode 100755 (executable)
new mode 100644 (file)
index 8574a66..9ee770c
@@ -1,14 +1,38 @@
 /*
- * Realtime PBX Module
+ * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2004 - 2005, Digium Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
- * Written by Mark Spencer <markster@digium.com>
+ * Mark Spencer <markster@digium.com>
  *
- * This program is Free Software distributed under the terms of
- * of the GNU General Public License.
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Realtime PBX Module
+ *
+ * \arg See also: \ref AstARA
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/channel.h"
 #include "asterisk/crypto.h"
 #include "asterisk/astdb.h"
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-
 #define MODE_MATCH             0
 #define MODE_MATCHMORE         1
 #define MODE_CANMATCH  2
 
 #define EXT_DATA_SIZE 256
 
-static char *tdesc = "Realtime Switch";
 
 /* Realtime switch looks up extensions in the supplied realtime table.
 
@@ -58,35 +76,6 @@ static char *tdesc = "Realtime Switch";
 */
 
 
-#define REALTIME_COMMON(mode) \
-       char *buf; \
-       char *opts; \
-       const char *cxt; \
-       char *table; \
-       int res=-1; \
-       struct ast_variable *var=NULL; \
-       buf = ast_strdupa(data); \
-       if (buf) { \
-               opts = strchr(buf, '/'); \
-               if (opts) { \
-                       *opts='\0'; \
-                       opts++; \
-               } else \
-                       opts=""; \
-               table = strchr(buf, '@'); \
-               if (table) { \
-                       *table = '\0'; \
-                       table++;\
-                       cxt = buf; \
-               } else cxt = NULL; \
-               if (!cxt || ast_strlen_zero(cxt)) \
-                       cxt = context;\
-               if (!table || ast_strlen_zero(table)) \
-                       table = "extensions"; \
-               var = realtime_switch_common(table, cxt, exten, priority, mode); \
-       } else \
-               res = -1; 
-
 static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode)
 {
        struct ast_variable *var;
@@ -108,7 +97,7 @@ static struct ast_variable *realtime_switch_common(const char *table, const char
        case MODE_MATCH:
        default:
                ematch = "exten";
-               strncpy(rexten, exten, sizeof(rexten) - 1);
+               ast_copy_string(rexten, exten, sizeof(rexten));
        }
        var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, NULL);
        if (!var) {
@@ -140,56 +129,81 @@ static struct ast_variable *realtime_switch_common(const char *table, const char
        return var;
 }
 
+static struct ast_variable *realtime_common(const char *context, const char *exten, int priority, const char *data, int mode)
+{
+       const char *ctx = NULL;
+       char *table;
+       struct ast_variable *var=NULL;
+       char *buf = ast_strdupa(data);
+       if (buf) {
+               char *opts = strchr(buf, '/');
+               if (opts)
+                       *opts++ = '\0';
+               table = strchr(buf, '@');
+               if (table) {
+                       *table++ = '\0';
+                       ctx = buf;
+               }
+               ctx = S_OR(ctx, context);
+               table = S_OR(table, "extensions");
+               var = realtime_switch_common(table, ctx, exten, priority, mode);
+       }
+       return var;
+}
+
 static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 {
-       REALTIME_COMMON(MODE_MATCH);
-       if (var) ast_variables_destroy(var);
-       if (var)
-               res = 1;
-       return res > 0 ? res : 0;
+       struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
+       if (var) {
+               ast_variables_destroy(var);
+               return 1;
+       }
+       return 0;
 }
 
 static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 {
-       REALTIME_COMMON(MODE_CANMATCH);
-       if (var) ast_variables_destroy(var);
-       if (var)
-               res = 1;
-       return res > 0 ? res : 0;
+       struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_CANMATCH);
+       if (var) {
+               ast_variables_destroy(var);
+               return 1;
+       }
+       return 0;
 }
 
-static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
+static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 {
-       char app[256];
-       char appdata[512]="";
-       char *tmp="";
-    char tmp1[80];
-    char tmp2[80];
-    char tmp3[EXT_DATA_SIZE];
-       struct ast_app *a;
-       struct ast_variable *v;
-       REALTIME_COMMON(MODE_MATCH);
+       int res = -1;
+       struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
+
        if (var) {
-               v = var;
-               while(v) {
+               char *tmp="";
+               char app[256];
+               struct ast_variable *v;
+
+               for (v = var; v ; v = v->next) {
                        if (!strcasecmp(v->name, "app"))
-                               strncpy(app, v->value, sizeof(app) -1 );
+                               ast_copy_string(app, v->value, sizeof(app));
                        else if (!strcasecmp(v->name, "appdata"))
                                tmp = ast_strdupa(v->value);
-                       v = v->next;
                }
                ast_variables_destroy(var);
                if (!ast_strlen_zero(app)) {
-                       a = pbx_findapp(app);
+                       struct ast_app *a = pbx_findapp(app);
                        if (a) {
+                               char appdata[512]="";
+                               char tmp1[80];
+                               char tmp2[80];
+                               char tmp3[EXT_DATA_SIZE];
+
                                if(!ast_strlen_zero(tmp))
-                                  pbx_substitute_variables_helper(chan, tmp, appdata, sizeof(appdata) - 1);
-                if (option_verbose > 2)
+                                       pbx_substitute_variables_helper(chan, tmp, appdata, sizeof(appdata) - 1);
+                               if (option_verbose > 2)
                                        ast_verbose( VERBOSE_PREFIX_3 "Executing %s(\"%s\", \"%s\")\n",
-                                                                term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
-                                                                term_color(tmp2, chan->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
-                                                                term_color(tmp3, (!ast_strlen_zero(appdata) ? (char *)appdata : ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
-                manager_event(EVENT_FLAG_CALL, "Newexten",
+                                                term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
+                                                term_color(tmp2, chan->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
+                                                term_color(tmp3, S_OR(appdata, ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
+                               manager_event(EVENT_FLAG_CALL, "Newexten",
                                                          "Channel: %s\r\n"
                                                          "Context: %s\r\n"
                                                          "Extension: %s\r\n"
@@ -199,7 +213,7 @@ static int realtime_exec(struct ast_channel *chan, const char *context, const ch
                                                          "Uniqueid: %s\r\n",
                                                          chan->name, chan->context, chan->exten, chan->priority, app, appdata ? appdata : "(NULL)", chan->uniqueid);
                                
-                               res = pbx_exec(chan, a, appdata, newstack);
+                               res = pbx_exec(chan, a, appdata);
                        } else
                                ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
                }
@@ -209,47 +223,34 @@ static int realtime_exec(struct ast_channel *chan, const char *context, const ch
 
 static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 {
-       REALTIME_COMMON(MODE_MATCHMORE);
-       if (var) ast_variables_destroy(var);
-       if (var)
-               res = 1;
-       return res > 0 ? res : 0;
+       struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCHMORE);
+       if (var) {
+               ast_variables_destroy(var);
+               return 1;
+       }
+       return 0;
 }
 
 static struct ast_switch realtime_switch =
 {
         name:                   "Realtime",
-        description:                   "Realtime Dialplan Switch",
+        description:                   "Realtime Dialplan Switch",
         exists:                 realtime_exists,
         canmatch:               realtime_canmatch,
         exec:                   realtime_exec,
         matchmore:              realtime_matchmore,
 };
 
-char *description(void)
-{
-       return tdesc;
-}
-
-int usecount(void)
-{
-       return 1;
-}
-
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
-
-int unload_module(void)
+static int unload_module(void)
 {
        ast_unregister_switch(&realtime_switch);
        return 0;
 }
 
-int load_module(void)
+static int load_module(void)
 {
        ast_register_switch(&realtime_switch);
        return 0;
 }
 
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Switch");