Wed Oct 28 15:47:54 2009

Asterisk developer's documentation


func_logic.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  * Portions Copyright (C) 2005, Anthony Minessale II
00006  *
00007  * See http://www.asterisk.org for more information about
00008  * the Asterisk project. Please do not directly contact
00009  * any of the maintainers of this project for assistance;
00010  * the project provides a web site, mailing lists and IRC
00011  * channels for your use.
00012  *
00013  * This program is free software, distributed under the terms of
00014  * the GNU General Public License Version 2. See the LICENSE file
00015  * at the top of the source tree.
00016  */
00017 
00018 /*! \file
00019  * 
00020  * \brief Conditional logic dialplan functions
00021  * 
00022  */
00023 
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <sys/types.h>
00027 
00028 #include "asterisk.h"
00029 
00030 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 27051 $") */
00031 
00032 #include "asterisk/channel.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/logger.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/app.h"
00037 #include "asterisk/config.h"     /* for ast_true */
00038 
00039 static char *builtin_function_isnull(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00040 {
00041    return data && *data ? "0" : "1";
00042 }
00043 
00044 static char *builtin_function_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00045 {
00046    return data && *data ? "1" : "0";
00047 }
00048 
00049 static char *builtin_function_iftime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00050 {
00051    struct ast_timing timing;
00052    char *ret;
00053    char *expr;
00054    char *iftrue;
00055    char *iffalse;
00056 
00057    if (!(data = ast_strdupa(data))) {
00058       ast_log(LOG_WARNING, "Memory Error!\n");
00059       return NULL;
00060    }
00061 
00062    data = ast_strip_quoted(data, "\"", "\"");
00063    expr = strsep(&data, "?");
00064    iftrue = strsep(&data, ":");
00065    iffalse = data;
00066 
00067    if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
00068       ast_log(LOG_WARNING, "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
00069       return NULL;
00070    }
00071 
00072    if (!ast_build_timing(&timing, expr)) {
00073       ast_log(LOG_WARNING, "Invalid Time Spec.\n");
00074       return NULL;
00075    }
00076 
00077    if (iftrue)
00078       iftrue = ast_strip_quoted(iftrue, "\"", "\"");
00079    if (iffalse)
00080       iffalse = ast_strip_quoted(iffalse, "\"", "\"");
00081 
00082    if ((ret = ast_check_timing(&timing) ? iftrue : iffalse)) {
00083       ast_copy_string(buf, ret, len);
00084       ret = buf;
00085    } 
00086    
00087    return ret;
00088 }
00089 
00090 static char *builtin_function_if(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00091 {
00092    char *ret;
00093    char *expr;
00094    char *iftrue;
00095    char *iffalse;
00096 
00097    if (!(data = ast_strdupa(data))) {
00098       ast_log(LOG_WARNING, "Memory Error!\n");
00099       return NULL;
00100    }
00101 
00102    data = ast_strip_quoted(data, "\"", "\"");
00103    expr = strsep(&data, "?");
00104    iftrue = strsep(&data, ":");
00105    iffalse = data;
00106 
00107    if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
00108       ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>])\n");
00109       return NULL;
00110    }
00111 
00112    expr = ast_strip(expr);
00113    if (iftrue)
00114       iftrue = ast_strip_quoted(iftrue, "\"", "\"");
00115    if (iffalse)
00116       iffalse = ast_strip_quoted(iffalse, "\"", "\"");
00117 
00118    if ((ret = pbx_checkcondition(expr) ? iftrue : iffalse)) {
00119       ast_copy_string(buf, ret, len);
00120       ret = buf;
00121    } 
00122    
00123    return ret;
00124 }
00125 
00126 static char *builtin_function_set(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00127 {
00128    char *varname;
00129    char *val;
00130 
00131    if (!(data = ast_strdupa(data))) {
00132       ast_log(LOG_WARNING, "Memory Error!\n");
00133       return NULL;
00134    }
00135 
00136    varname = strsep(&data, "=");
00137    val = data;
00138 
00139    if (ast_strlen_zero(varname) || !val) {
00140       ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
00141       return NULL;
00142    }
00143 
00144    varname = ast_strip(varname);
00145    val = ast_strip(val);
00146    pbx_builtin_setvar_helper(chan, varname, val);
00147    ast_copy_string(buf, val, len);
00148 
00149    return buf;
00150 }
00151 
00152 #ifndef BUILTIN_FUNC
00153 static
00154 #endif
00155 struct ast_custom_function isnull_function = {
00156    .name = "ISNULL",
00157    .synopsis = "NULL Test: Returns 1 if NULL or 0 otherwise",
00158    .syntax = "ISNULL(<data>)",
00159    .read = builtin_function_isnull,
00160 };
00161 
00162 #ifndef BUILTIN_FUNC
00163 static
00164 #endif
00165 struct ast_custom_function set_function = {
00166    .name = "SET",
00167    .synopsis = "SET assigns a value to a channel variable",
00168    .syntax = "SET(<varname>=[<value>])",
00169    .read = builtin_function_set,
00170 };
00171 
00172 #ifndef BUILTIN_FUNC
00173 static
00174 #endif
00175 struct ast_custom_function exists_function = {
00176    .name = "EXISTS",
00177    .synopsis = "Existence Test: Returns 1 if exists, 0 otherwise",
00178    .syntax = "EXISTS(<data>)",
00179    .read = builtin_function_exists,
00180 };
00181 
00182 #ifndef BUILTIN_FUNC
00183 static
00184 #endif
00185 struct ast_custom_function if_function = {
00186    .name = "IF",
00187    .synopsis = "Conditional: Returns the data following '?' if true else the data following ':'",
00188    .syntax = "IF(<expr>?[<true>][:<false>])",
00189    .read = builtin_function_if,
00190 };
00191 
00192 
00193 #ifndef BUILTIN_FUNC
00194 static
00195 #endif
00196 struct ast_custom_function if_time_function = {
00197    .name = "IFTIME",
00198    .synopsis = "Temporal Conditional: Returns the data following '?' if true else the data following ':'",
00199    .syntax = "IFTIME(<timespec>?[<true>][:<false>])",
00200    .read = builtin_function_iftime,
00201 };

Generated on Wed Oct 28 15:47:54 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6