00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "asterisk.h"
00026
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 44808 $")
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) ? iftrue : 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 char *expr;
00095 char *iftrue;
00096 char *iffalse;
00097
00098 data = ast_strip_quoted(data, "\"", "\"");
00099 expr = strsep(&data, "?");
00100 iftrue = strsep(&data, ":");
00101 iffalse = data;
00102
00103 if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
00104 ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>])\n");
00105 return -1;
00106 }
00107
00108 expr = ast_strip(expr);
00109 if (iftrue)
00110 iftrue = ast_strip_quoted(iftrue, "\"", "\"");
00111 if (iffalse)
00112 iffalse = ast_strip_quoted(iffalse, "\"", "\"");
00113
00114 ast_copy_string(buf, pbx_checkcondition(expr) ? (S_OR(iftrue, "")) : (S_OR(iffalse, "")), len);
00115
00116 return 0;
00117 }
00118
00119 static int set(struct ast_channel *chan, char *cmd, char *data, char *buf,
00120 size_t len)
00121 {
00122 char *varname;
00123 char *val;
00124
00125 varname = strsep(&data, "=");
00126 val = data;
00127
00128 if (ast_strlen_zero(varname) || !val) {
00129 ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
00130 return -1;
00131 }
00132
00133 varname = ast_strip(varname);
00134 val = ast_strip(val);
00135 pbx_builtin_setvar_helper(chan, varname, val);
00136 ast_copy_string(buf, val, len);
00137
00138 return 0;
00139 }
00140
00141 static struct ast_custom_function isnull_function = {
00142 .name = "ISNULL",
00143 .synopsis = "NULL Test: Returns 1 if NULL or 0 otherwise",
00144 .syntax = "ISNULL(<data>)",
00145 .read = isnull,
00146 };
00147
00148 static struct ast_custom_function set_function = {
00149 .name = "SET",
00150 .synopsis = "SET assigns a value to a channel variable",
00151 .syntax = "SET(<varname>=[<value>])",
00152 .read = set,
00153 };
00154
00155 static struct ast_custom_function exists_function = {
00156 .name = "EXISTS",
00157 .synopsis = "Existence Test: Returns 1 if exists, 0 otherwise",
00158 .syntax = "EXISTS(<data>)",
00159 .read = exists,
00160 };
00161
00162 static struct ast_custom_function if_function = {
00163 .name = "IF",
00164 .synopsis =
00165 "Conditional: Returns the data following '?' if true else the data following ':'",
00166 .syntax = "IF(<expr>?[<true>][:<false>])",
00167 .read = acf_if,
00168 };
00169
00170 static struct ast_custom_function if_time_function = {
00171 .name = "IFTIME",
00172 .synopsis =
00173 "Temporal Conditional: Returns the data following '?' if true else the data following ':'",
00174 .syntax = "IFTIME(<timespec>?[<true>][:<false>])",
00175 .read = iftime,
00176 };
00177
00178 static int unload_module(void)
00179 {
00180 int res = 0;
00181
00182 res |= ast_custom_function_unregister(&isnull_function);
00183 res |= ast_custom_function_unregister(&set_function);
00184 res |= ast_custom_function_unregister(&exists_function);
00185 res |= ast_custom_function_unregister(&if_function);
00186 res |= ast_custom_function_unregister(&if_time_function);
00187
00188 return res;
00189 }
00190
00191 static int load_module(void)
00192 {
00193 int res = 0;
00194
00195 res |= ast_custom_function_register(&isnull_function);
00196 res |= ast_custom_function_register(&set_function);
00197 res |= ast_custom_function_register(&exists_function);
00198 res |= ast_custom_function_register(&if_function);
00199 res |= ast_custom_function_register(&if_time_function);
00200
00201 return res;
00202 }
00203
00204 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");