Sat Aug 6 00:39:29 2011

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 - 2006, 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  * \author Anthony Minessale II
00023  */
00024 
00025 #include "asterisk.h"
00026 
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 168546 $")
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <sys/types.h>
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/channel.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/logger.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/app.h"
00040 
00041 static int isnull(struct ast_channel *chan, char *cmd, char *data,
00042         char *buf, size_t len)
00043 {
00044    strcpy(buf, data && *data ? "0" : "1");
00045 
00046    return 0;
00047 }
00048 
00049 static int exists(struct ast_channel *chan, char *cmd, char *data, char *buf,
00050         size_t len)
00051 {
00052    strcpy(buf, data && *data ? "1" : "0");
00053 
00054    return 0;
00055 }
00056 
00057 static int iftime(struct ast_channel *chan, char *cmd, char *data, char *buf,
00058         size_t len)
00059 {
00060    struct ast_timing timing;
00061    char *expr;
00062    char *iftrue;
00063    char *iffalse;
00064 
00065    data = ast_strip_quoted(data, "\"", "\"");
00066    expr = strsep(&data, "?");
00067    iftrue = strsep(&data, ":");
00068    iffalse = data;
00069 
00070    if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
00071       ast_log(LOG_WARNING,
00072             "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
00073       return -1;
00074    }
00075 
00076    if (!ast_build_timing(&timing, expr)) {
00077       ast_log(LOG_WARNING, "Invalid Time Spec.\n");
00078       return -1;
00079    }
00080 
00081    if (iftrue)
00082       iftrue = ast_strip_quoted(iftrue, "\"", "\"");
00083    if (iffalse)
00084       iffalse = ast_strip_quoted(iffalse, "\"", "\"");
00085 
00086    ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
00087 
00088    return 0;
00089 }
00090 
00091 static int acf_if(struct ast_channel *chan, char *cmd, char *data, char *buf,
00092         size_t len)
00093 {
00094    AST_DECLARE_APP_ARGS(args1,
00095       AST_APP_ARG(expr);
00096       AST_APP_ARG(remainder);
00097    );
00098    AST_DECLARE_APP_ARGS(args2,
00099       AST_APP_ARG(iftrue);
00100       AST_APP_ARG(iffalse);
00101    );
00102    args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
00103                                  then args1.remainder will be NULL, not a pointer to a null string, and
00104                                  then any garbage in args2.iffalse will not be cleared, and you'll crash.
00105                                   -- and if you mod the ast_app_separate_args func instead, you'll really
00106                                  mess things up badly, because the rest of everything depends on null args
00107                                  for non-specified stuff. */
00108    
00109    AST_NONSTANDARD_APP_ARGS(args1, data, '?');
00110    AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
00111 
00112    if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
00113       ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>])  (expr must be non-null, and either <true> or <false> must be non-null)\n");
00114       ast_log(LOG_WARNING, "      In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
00115       return -1;
00116    }
00117 
00118    args1.expr = ast_strip(args1.expr);
00119    if (args2.iftrue)
00120       args2.iftrue = ast_strip(args2.iftrue);
00121    if (args2.iffalse)
00122       args2.iffalse = ast_strip(args2.iffalse);
00123 
00124    ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
00125 
00126    return 0;
00127 }
00128 
00129 static int set(struct ast_channel *chan, char *cmd, char *data, char *buf,
00130           size_t len)
00131 {
00132    char *varname;
00133    char *val;
00134 
00135    varname = strsep(&data, "=");
00136    val = data;
00137 
00138    if (ast_strlen_zero(varname) || !val) {
00139       ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
00140       return -1;
00141    }
00142 
00143    varname = ast_strip(varname);
00144    val = ast_strip(val);
00145    pbx_builtin_setvar_helper(chan, varname, val);
00146    ast_copy_string(buf, val, len);
00147 
00148    return 0;
00149 }
00150 
00151 static struct ast_custom_function isnull_function = {
00152    .name = "ISNULL",
00153    .synopsis = "NULL Test: Returns 1 if NULL or 0 otherwise",
00154    .syntax = "ISNULL(<data>)",
00155    .read = isnull,
00156 };
00157 
00158 static struct ast_custom_function set_function = {
00159    .name = "SET",
00160    .synopsis = "SET assigns a value to a channel variable",
00161    .syntax = "SET(<varname>=[<value>])",
00162    .read = set,
00163 };
00164 
00165 static struct ast_custom_function exists_function = {
00166    .name = "EXISTS",
00167    .synopsis = "Existence Test: Returns 1 if exists, 0 otherwise",
00168    .syntax = "EXISTS(<data>)",
00169    .read = exists,
00170 };
00171 
00172 static struct ast_custom_function if_function = {
00173    .name = "IF",
00174    .synopsis =
00175       "Conditional: Returns the data following '?' if true else the data following ':'",
00176    .syntax = "IF(<expr>?[<true>][:<false>])",
00177    .read = acf_if,
00178 };
00179 
00180 static struct ast_custom_function if_time_function = {
00181    .name = "IFTIME",
00182    .synopsis =
00183       "Temporal Conditional: Returns the data following '?' if true else the data following ':'",
00184    .syntax = "IFTIME(<timespec>?[<true>][:<false>])",
00185    .read = iftime,
00186 };
00187 
00188 static int unload_module(void)
00189 {
00190    int res = 0;
00191 
00192    res |= ast_custom_function_unregister(&isnull_function);
00193    res |= ast_custom_function_unregister(&set_function);
00194    res |= ast_custom_function_unregister(&exists_function);
00195    res |= ast_custom_function_unregister(&if_function);
00196    res |= ast_custom_function_unregister(&if_time_function);
00197 
00198    return res;
00199 }
00200 
00201 static int load_module(void)
00202 {
00203    int res = 0;
00204 
00205    res |= ast_custom_function_register(&isnull_function);
00206    res |= ast_custom_function_register(&set_function);
00207    res |= ast_custom_function_register(&exists_function);
00208    res |= ast_custom_function_register(&if_function);
00209    res |= ast_custom_function_register(&if_time_function);
00210 
00211    return res;
00212 }
00213 
00214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7