/* * Asterisk -- A telephony toolkit for Linux. * * OnResultGoto application * * Copyright (c) 2003 Tilghman Lesher. All rights reserved. * * Tilghman Lesher * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include static char *tdesc = "Use result codes to branch"; static char *app_onresultgoto = "OnResultGoto"; static char *onresultgoto_synopsis = "Branch to a new priority/extension based upon the prior application's result code."; static char *onresultgoto_descrip = "OnResultGoto(res-code:[[context]|extension]|priority[&res-code:...])\n" " (res-code:+priority[&res-code:...])\n" " You may mix the two types of Gotos (i.e. absolute or relative) in the\n" "same command. Returns the same result code as the prior application (which\n" "allows processing of result codes to occur over multiple lines).\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; static int onresultgoto_exec(struct ast_channel *chan, void *data) { struct localuser *u; char *arg, *nextarg, *thisarg; if (!data) { ast_log(LOG_WARNING, "OnResultGoto requires an argument (rescode:+priority[&rescode:...])\n"); return chan->res; } LOCAL_USER_ADD(u); arg = strdup(data); nextarg = arg; while((thisarg = strsep(&nextarg,"&"))) { char *arg2, *gotopath, *resultcode; arg2 = strdup(thisarg); gotopath = arg2; resultcode = strsep(&gotopath,":"); if ((resultcode != NULL) && (atoi(resultcode) == chan->res)) { if ((gotopath[0] == '+') || (gotopath[0] == '-')) { /* Relative goto */ int pri = atoi(gotopath); if (chan->priority + pri - 1 >= 0) chan->priority += pri - 1; if (option_verbose > 2) ast_verbose( VERBOSE_PREFIX_3 "OnResultGoto (%s,%s,%d) resultcode=%i\n", chan->context,chan->exten, chan->priority+1, chan->res); /* Short-circuit */ nextarg = NULL; } else { /* Absolute goto */ char *context, *exten, *pri; context = strsep(&gotopath, "|"); exten = strsep(&gotopath, "|"); if (!exten) { /* Only a priority */ pri = context; exten = NULL; context = NULL; } else { pri = strsep(&gotopath, "|"); if (!pri) { pri = exten; exten = context; context = NULL; } } if (atoi(pri) < 0) { ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", pri); } else { /* At this point we have a priority and */ /* maybe an extension and a context */ chan->priority = atoi(pri) - 1; if (exten && strcasecmp(exten, "BYEXTENSION")) strncpy(chan->exten, exten, sizeof(chan->exten)-1); if (context) strncpy(chan->context, context, sizeof(chan->context)-1); if (option_verbose > 2) ast_verbose( VERBOSE_PREFIX_3 "OnResultGoto (%s,%s,%d) resultcode=%d\n", chan->context,chan->exten, chan->priority+1, chan->res); /* Short-circuit */ nextarg = NULL; } } } free(arg2); } free(arg); LOCAL_USER_REMOVE(u); return chan->res; } int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; return ast_unregister_application(app_onresultgoto); } int load_module(void) { return ast_register_application(app_onresultgoto, onresultgoto_exec, onresultgoto_synopsis, onresultgoto_descrip); } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; } char *key() { return ASTERISK_GPL_KEY; }