Fri Jul 3 20:35:32 2009

Asterisk developer's documentation


app_macro.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Dial plan macro Implementation
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00031 
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 #include <sys/types.h>
00037 
00038 #include "asterisk/file.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/config.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/lock.h"
00047 
00048 #define MAX_ARGS 80
00049 
00050 /* special result value used to force macro exit */
00051 #define MACRO_EXIT_RESULT 1024
00052 
00053 static char *descrip =
00054 "  Macro(macroname|arg1|arg2...): Executes a macro using the context\n"
00055 "'macro-<macroname>', jumping to the 's' extension of that context and\n"
00056 "executing each step, then returning when the steps end. \n"
00057 "The calling extension, context, and priority are stored in ${MACRO_EXTEN}, \n"
00058 "${MACRO_CONTEXT} and ${MACRO_PRIORITY} respectively.  Arguments become\n"
00059 "${ARG1}, ${ARG2}, etc in the macro context.\n"
00060 "If you Goto out of the Macro context, the Macro will terminate and control\n"
00061 "will be returned at the location of the Goto.\n"
00062 "If ${MACRO_OFFSET} is set at termination, Macro will attempt to continue\n"
00063 "at priority MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.\n"
00064 "Extensions: While a macro is being executed, it becomes the current context.\n"
00065 "            This means that if a hangup occurs, for instance, that the macro\n"
00066 "            will be searched for an 'h' extension, NOT the context from which\n"
00067 "            the macro was called. So, make sure to define all appropriate\n"
00068 "            extensions in your macro! (you can use 'catch' in AEL) \n"
00069 "WARNING: Because of the way Macro is implemented (it executes the priorities\n"
00070 "         contained within it via sub-engine), and a fixed per-thread\n"
00071 "         memory stack allowance, macros are limited to 7 levels\n"
00072 "         of nesting (macro calling macro calling macro, etc.); It\n"
00073 "         may be possible that stack-intensive applications in deeply nested macros\n"
00074 "         could cause asterisk to crash earlier than this limit. It is advised that\n"
00075 "         if you need to deeply nest macro calls, that you use the Gosub application\n"
00076 "         (now allows arguments like a Macro) with explict Return() calls instead.\n";
00077 
00078 static char *if_descrip =
00079 "  MacroIf(<expr>?macroname_a[|arg1][:macroname_b[|arg1]])\n"
00080 "Executes macro defined in <macroname_a> if <expr> is true\n"
00081 "(otherwise <macroname_b> if provided)\n"
00082 "Arguments and return values as in application macro()\n";
00083 
00084 static char *exclusive_descrip =
00085 "  MacroExclusive(macroname|arg1|arg2...):\n"
00086 "Executes macro defined in the context 'macro-macroname'\n"
00087 "Only one call at a time may run the macro.\n"
00088 "(we'll wait if another call is busy executing in the Macro)\n"
00089 "Arguments and return values as in application Macro()\n";
00090 
00091 static char *exit_descrip =
00092 "  MacroExit():\n"
00093 "Causes the currently running macro to exit as if it had\n"
00094 "ended normally by running out of priorities to execute.\n"
00095 "If used outside a macro, will likely cause unexpected\n"
00096 "behavior.\n";
00097 
00098 static char *app = "Macro";
00099 static char *if_app = "MacroIf";
00100 static char *exclusive_app = "MacroExclusive";
00101 static char *exit_app = "MacroExit";
00102 
00103 static char *synopsis = "Macro Implementation";
00104 static char *if_synopsis = "Conditional Macro Implementation";
00105 static char *exclusive_synopsis = "Exclusive Macro Implementation";
00106 static char *exit_synopsis = "Exit From Macro";
00107 
00108 static void macro_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00109 
00110 struct ast_datastore_info macro_ds_info = {
00111    .type = "MACRO",
00112    .chan_fixup = macro_fixup,
00113 };
00114 
00115 static void macro_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
00116 {
00117    int i;
00118    char varname[10];
00119    pbx_builtin_setvar_helper(new_chan, "MACRO_DEPTH", "0");
00120    pbx_builtin_setvar_helper(new_chan, "MACRO_CONTEXT", NULL);
00121    pbx_builtin_setvar_helper(new_chan, "MACRO_EXTEN", NULL);
00122    pbx_builtin_setvar_helper(new_chan, "MACRO_PRIORITY", NULL);
00123    pbx_builtin_setvar_helper(new_chan, "MACRO_OFFSET", NULL);
00124    for (i = 1; i < 100; i++) {
00125       snprintf(varname, sizeof(varname), "ARG%d", i);
00126       while (pbx_builtin_getvar_helper(new_chan, varname)) {
00127          /* Kill all levels of arguments */
00128          pbx_builtin_setvar_helper(new_chan, varname, NULL);
00129       }
00130    }
00131 }
00132 
00133 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
00134 {
00135    struct ast_exten *e;
00136    struct ast_include *i;
00137    struct ast_context *c2;
00138 
00139    for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
00140       if (ast_extension_match(ast_get_extension_name(e), exten)) {
00141          int needmatch = ast_get_extension_matchcid(e);
00142          if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
00143             (!needmatch)) {
00144             /* This is the matching extension we want */
00145             struct ast_exten *p;
00146             for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
00147                if (priority != ast_get_extension_priority(p))
00148                   continue;
00149                return p;
00150             }
00151          }
00152       }
00153    }
00154 
00155    /* No match; run through includes */
00156    for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
00157       for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
00158          if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
00159             e = find_matching_priority(c2, exten, priority, callerid);
00160             if (e)
00161                return e;
00162          }
00163       }
00164    }
00165    return NULL;
00166 }
00167 
00168 static int _macro_exec(struct ast_channel *chan, void *data, int exclusive)
00169 {
00170    const char *s;
00171    char *tmp;
00172    char *cur, *rest;
00173    char *macro;
00174    char fullmacro[80];
00175    char varname[80];
00176    char runningapp[80], runningdata[1024];
00177    char *oldargs[MAX_ARGS + 1] = { NULL, };
00178    int argc, x;
00179    int res=0;
00180    char oldexten[256]="";
00181    int oldpriority, gosub_level = 0;
00182    char pc[80], depthc[12];
00183    char oldcontext[AST_MAX_CONTEXT] = "";
00184    const char *inhangupc;
00185    int offset, depth = 0, maxdepth = 7;
00186    int setmacrocontext=0;
00187    int autoloopflag, inhangup = 0;
00188   
00189    char *save_macro_exten;
00190    char *save_macro_context;
00191    char *save_macro_priority;
00192    char *save_macro_offset;
00193    struct ast_module_user *u;
00194    struct ast_datastore *macro_store = ast_channel_datastore_find(chan, &macro_ds_info, NULL);
00195  
00196    if (ast_strlen_zero(data)) {
00197       ast_log(LOG_WARNING, "Macro() requires arguments. See \"show application macro\" for help.\n");
00198       return -1;
00199    }
00200 
00201    u = ast_module_user_add(chan);
00202 
00203    do {
00204       if (macro_store) {
00205          break;
00206       }
00207       if (!(macro_store = ast_channel_datastore_alloc(&macro_ds_info, NULL))) {
00208          ast_log(LOG_WARNING, "Unable to allocate new datastore.\n");
00209          break;
00210       }
00211       /* Just the existence of this datastore is enough. */
00212       macro_store->inheritance = DATASTORE_INHERIT_FOREVER;
00213       ast_channel_datastore_add(chan, macro_store);
00214    } while (0);
00215 
00216    /* does the user want a deeper rabbit hole? */
00217    s = pbx_builtin_getvar_helper(chan, "MACRO_RECURSION");
00218    if (s)
00219       sscanf(s, "%d", &maxdepth);
00220 
00221    /* Count how many levels deep the rabbit hole goes */
00222    s = pbx_builtin_getvar_helper(chan, "MACRO_DEPTH");
00223    if (s)
00224       sscanf(s, "%d", &depth);
00225    /* Used for detecting whether to return when a Macro is called from another Macro after hangup */
00226    if (strcmp(chan->exten, "h") == 0)
00227       pbx_builtin_setvar_helper(chan, "MACRO_IN_HANGUP", "1");
00228    inhangupc = pbx_builtin_getvar_helper(chan, "MACRO_IN_HANGUP");
00229    if (!ast_strlen_zero(inhangupc))
00230       sscanf(inhangupc, "%d", &inhangup);
00231 
00232    if (depth >= maxdepth) {
00233       ast_log(LOG_ERROR, "Macro():  possible infinite loop detected.  Returning early.\n");
00234       ast_module_user_remove(u);
00235       return 0;
00236    }
00237    snprintf(depthc, sizeof(depthc), "%d", depth + 1);
00238    pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
00239 
00240    tmp = ast_strdupa(data);
00241    rest = tmp;
00242    macro = strsep(&rest, "|");
00243    if (ast_strlen_zero(macro)) {
00244       ast_log(LOG_WARNING, "Invalid macro name specified\n");
00245       ast_module_user_remove(u);
00246       return 0;
00247    }
00248 
00249    snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
00250    if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->cid.cid_num)) {
00251       if (!ast_context_find(fullmacro)) 
00252          ast_log(LOG_WARNING, "No such context '%s' for macro '%s'\n", fullmacro, macro);
00253       else
00254          ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro);
00255       ast_module_user_remove(u);
00256       return 0;
00257    }
00258 
00259    /* If we are to run the macro exclusively, take the mutex */
00260    if (exclusive) {
00261       ast_log(LOG_DEBUG, "Locking macrolock for '%s'\n", fullmacro);
00262       ast_autoservice_start(chan);
00263       if (ast_context_lockmacro(fullmacro)) {
00264          ast_log(LOG_WARNING, "Failed to lock macro '%s' as in-use\n", fullmacro);
00265          ast_autoservice_stop(chan);
00266          ast_module_user_remove(u);
00267 
00268          return 0;
00269       }
00270       ast_autoservice_stop(chan);
00271    }
00272    
00273    /* Save old info */
00274    oldpriority = chan->priority;
00275    ast_copy_string(oldexten, chan->exten, sizeof(oldexten));
00276    ast_copy_string(oldcontext, chan->context, sizeof(oldcontext));
00277    if (ast_strlen_zero(chan->macrocontext)) {
00278       ast_copy_string(chan->macrocontext, chan->context, sizeof(chan->macrocontext));
00279       ast_copy_string(chan->macroexten, chan->exten, sizeof(chan->macroexten));
00280       chan->macropriority = chan->priority;
00281       setmacrocontext=1;
00282    }
00283    argc = 1;
00284    /* Save old macro variables */
00285    save_macro_exten = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_EXTEN"));
00286    pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
00287 
00288    save_macro_context = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT"));
00289    pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
00290 
00291    save_macro_priority = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY"));
00292    snprintf(pc, sizeof(pc), "%d", oldpriority);
00293    pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
00294   
00295    save_macro_offset = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"));
00296    pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
00297 
00298    /* Setup environment for new run */
00299    chan->exten[0] = 's';
00300    chan->exten[1] = '\0';
00301    ast_copy_string(chan->context, fullmacro, sizeof(chan->context));
00302    chan->priority = 1;
00303 
00304    while((cur = strsep(&rest, "|")) && (argc < MAX_ARGS)) {
00305       const char *s;
00306       /* Save copy of old arguments if we're overwriting some, otherwise
00307          let them pass through to the other macro */
00308       snprintf(varname, sizeof(varname), "ARG%d", argc);
00309       s = pbx_builtin_getvar_helper(chan, varname);
00310       if (s)
00311          oldargs[argc] = ast_strdup(s);
00312       pbx_builtin_setvar_helper(chan, varname, cur);
00313       argc++;
00314    }
00315    autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
00316    ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
00317    while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
00318       struct ast_context *c;
00319       struct ast_exten *e;
00320       runningapp[0] = '\0';
00321       runningdata[0] = '\0';
00322 
00323       /* What application will execute? */
00324       if (ast_rdlock_contexts()) {
00325          ast_log(LOG_WARNING, "Failed to lock contexts list\n");
00326       } else {
00327          for (c = ast_walk_contexts(NULL), e = NULL; c; c = ast_walk_contexts(c)) {
00328             if (!strcmp(ast_get_context_name(c), chan->context)) {
00329                if (ast_lock_context(c)) {
00330                   ast_log(LOG_WARNING, "Unable to lock context?\n");
00331                } else {
00332                   e = find_matching_priority(c, chan->exten, chan->priority, chan->cid.cid_num);
00333                   if (e) { /* This will only be undefined for pbx_realtime, which is majorly broken. */
00334                      ast_copy_string(runningapp, ast_get_extension_app(e), sizeof(runningapp));
00335                      ast_copy_string(runningdata, ast_get_extension_app_data(e), sizeof(runningdata));
00336                   }
00337                   ast_unlock_context(c);
00338                }
00339                break;
00340             }
00341          }
00342       }
00343       ast_unlock_contexts();
00344 
00345       /* Reset the macro depth, if it was changed in the last iteration */
00346       pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
00347 
00348       if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num))) {
00349          /* Something bad happened, or a hangup has been requested. */
00350          if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
00351             (res == '*') || (res == '#')) {
00352             /* Just return result as to the previous application as if it had been dialed */
00353             ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
00354             break;
00355          }
00356          switch(res) {
00357          case MACRO_EXIT_RESULT:
00358             res = 0;
00359             goto out;
00360          case AST_PBX_KEEPALIVE:
00361             if (option_debug)
00362                ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE in macro %s on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
00363             else if (option_verbose > 1)
00364                ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE in macro '%s' on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
00365             goto out;
00366          default:
00367             if (option_debug)
00368                ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
00369             else if (option_verbose > 1)
00370                ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
00371             goto out;
00372          }
00373       }
00374 
00375       ast_log(LOG_DEBUG, "Executed application: %s\n", runningapp);
00376 
00377       if (!strcasecmp(runningapp, "GOSUB")) {
00378          gosub_level++;
00379          ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
00380       } else if (!strcasecmp(runningapp, "GOSUBIF")) {
00381          char tmp2[1024] = "", *cond, *app, *app2 = tmp2;
00382          pbx_substitute_variables_helper(chan, runningdata, tmp2, sizeof(tmp2) - 1);
00383          cond = strsep(&app2, "?");
00384          app = strsep(&app2, ":");
00385          if (pbx_checkcondition(cond)) {
00386             if (!ast_strlen_zero(app)) {
00387                gosub_level++;
00388                ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
00389             }
00390          } else {
00391             if (!ast_strlen_zero(app2)) {
00392                gosub_level++;
00393                ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
00394             }
00395          }
00396       } else if (!strcasecmp(runningapp, "RETURN")) {
00397          gosub_level--;
00398          ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
00399       } else if (!strcasecmp(runningapp, "STACKPOP")) {
00400          gosub_level--;
00401          ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
00402       } else if (!strncasecmp(runningapp, "EXEC", 4)) {
00403          /* Must evaluate args to find actual app */
00404          char tmp2[1024] = "", *tmp3 = NULL;
00405          pbx_substitute_variables_helper(chan, runningdata, tmp2, sizeof(tmp2) - 1);
00406          if (!strcasecmp(runningapp, "EXECIF")) {
00407             tmp3 = strchr(tmp2, '|');
00408             if (tmp3)
00409                *tmp3++ = '\0';
00410             if (!pbx_checkcondition(tmp2))
00411                tmp3 = NULL;
00412          } else
00413             tmp3 = tmp2;
00414 
00415          if (tmp3)
00416             ast_log(LOG_DEBUG, "Last app: %s\n", tmp3);
00417 
00418          if (tmp3 && !strncasecmp(tmp3, "GOSUB", 5)) {
00419             gosub_level++;
00420             ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
00421          } else if (tmp3 && !strncasecmp(tmp3, "RETURN", 6)) {
00422             gosub_level--;
00423             ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
00424          } else if (tmp3 && !strncasecmp(tmp3, "STACKPOP", 8)) {
00425             gosub_level--;
00426             ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
00427          }
00428       }
00429 
00430       if (gosub_level == 0 && strcasecmp(chan->context, fullmacro)) {
00431          if (option_verbose > 1)
00432             ast_verbose(VERBOSE_PREFIX_2 "Channel '%s' jumping out of macro '%s'\n", chan->name, macro);
00433          break;
00434       }
00435 
00436       /* don't stop executing extensions when we're in "h" */
00437       if (chan->_softhangup && !inhangup) {
00438          ast_log(LOG_DEBUG, "Extension %s, macroexten %s, priority %d returned normally even though call was hung up\n",
00439             chan->exten, chan->macroexten, chan->priority);
00440          goto out;
00441       }
00442       chan->priority++;
00443    }
00444    out:
00445 
00446    /* Don't let the channel change now. */
00447    ast_channel_lock(chan);
00448 
00449    /* Reset the depth back to what it was when the routine was entered (like if we called Macro recursively) */
00450    snprintf(depthc, sizeof(depthc), "%d", depth);
00451    pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
00452    ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
00453 
00454    for (x = 1; x < argc; x++) {
00455       /* Restore old arguments and delete ours */
00456       snprintf(varname, sizeof(varname), "ARG%d", x);
00457       if (oldargs[x]) {
00458          pbx_builtin_setvar_helper(chan, varname, oldargs[x]);
00459          free(oldargs[x]);
00460       } else {
00461          pbx_builtin_setvar_helper(chan, varname, NULL);
00462       }
00463    }
00464 
00465    /* Restore macro variables */
00466    pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten);
00467    pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context);
00468    pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority);
00469    if (save_macro_exten)
00470       free(save_macro_exten);
00471    if (save_macro_context)
00472       free(save_macro_context);
00473    if (save_macro_priority)
00474       free(save_macro_priority);
00475 
00476    if (setmacrocontext) {
00477       chan->macrocontext[0] = '\0';
00478       chan->macroexten[0] = '\0';
00479       chan->macropriority = 0;
00480    }
00481 
00482    if (!strcasecmp(chan->context, fullmacro)) {
00483       /* If we're leaving the macro normally, restore original information */
00484       chan->priority = oldpriority;
00485       ast_copy_string(chan->context, oldcontext, sizeof(chan->context));
00486       if (!(chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO)) {
00487          /* Copy the extension, so long as we're not in softhangup, where we could be given an asyncgoto */
00488          const char *offsets;
00489          ast_copy_string(chan->exten, oldexten, sizeof(chan->exten));
00490          if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) {
00491             /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
00492                normally if there is any problem */
00493             if (sscanf(offsets, "%d", &offset) == 1) {
00494                if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->cid.cid_num)) {
00495                   chan->priority += offset;
00496                }
00497             }
00498          }
00499       }
00500    }
00501 
00502    pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset);
00503    if (save_macro_offset)
00504       free(save_macro_offset);
00505 
00506    /* Unlock the macro */
00507    if (exclusive) {
00508       ast_log(LOG_DEBUG, "Unlocking macrolock for '%s'\n", fullmacro);
00509       if (ast_context_unlockmacro(fullmacro)) {
00510          ast_log(LOG_ERROR, "Failed to unlock macro '%s' - that isn't good\n", fullmacro);
00511          res = 0;
00512       }
00513    }
00514    ast_channel_unlock(chan);
00515    
00516    ast_module_user_remove(u);
00517 
00518    return res;
00519 }
00520 
00521 static int macro_exec(struct ast_channel *chan, void *data)
00522 {
00523    return _macro_exec(chan, data, 0);
00524 }
00525 
00526 static int macroexclusive_exec(struct ast_channel *chan, void *data)
00527 {
00528    return _macro_exec(chan, data, 1);
00529 }
00530 
00531 static int macroif_exec(struct ast_channel *chan, void *data) 
00532 {
00533    char *expr = NULL, *label_a = NULL, *label_b = NULL;
00534    int res = 0;
00535    struct ast_module_user *u;
00536 
00537    u = ast_module_user_add(chan);
00538 
00539    if (!(expr = ast_strdupa(data))) {
00540       ast_module_user_remove(u);
00541       return -1;
00542    }
00543 
00544    if ((label_a = strchr(expr, '?'))) {
00545       *label_a = '\0';
00546       label_a++;
00547       if ((label_b = strchr(label_a, ':'))) {
00548          *label_b = '\0';
00549          label_b++;
00550       }
00551       if (pbx_checkcondition(expr))
00552          res = macro_exec(chan, label_a);
00553       else if (label_b) 
00554          res = macro_exec(chan, label_b);
00555    } else
00556       ast_log(LOG_WARNING, "Invalid Syntax.\n");
00557 
00558    ast_module_user_remove(u);
00559 
00560    return res;
00561 }
00562          
00563 static int macro_exit_exec(struct ast_channel *chan, void *data)
00564 {
00565    return MACRO_EXIT_RESULT;
00566 }
00567 
00568 static int unload_module(void)
00569 {
00570    int res;
00571 
00572    res = ast_unregister_application(if_app);
00573    res |= ast_unregister_application(exit_app);
00574    res |= ast_unregister_application(app);
00575    res |= ast_unregister_application(exclusive_app);
00576 
00577    ast_module_user_hangup_all();
00578 
00579    return res;
00580 }
00581 
00582 static int load_module(void)
00583 {
00584    int res;
00585 
00586    res = ast_register_application(exit_app, macro_exit_exec, exit_synopsis, exit_descrip);
00587    res |= ast_register_application(if_app, macroif_exec, if_synopsis, if_descrip);
00588    res |= ast_register_application(exclusive_app, macroexclusive_exec, exclusive_synopsis, exclusive_descrip);
00589    res |= ast_register_application(app, macro_exec, synopsis, descrip);
00590 
00591    return res;
00592 }
00593 
00594 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Extension Macros");

Generated on Fri Jul 3 20:35:32 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9