func_logic.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. * Portions Copyright (C) 2005, Anthony Minessale II
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief Conditional logic dialplan functions
  20. *
  21. * \author Anthony Minessale II
  22. *
  23. * \ingroup functions
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/module.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/app.h"
  35. /*** DOCUMENTATION
  36. <function name="ISNULL" language="en_US">
  37. <synopsis>
  38. Check if a value is NULL.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="data" required="true" />
  42. </syntax>
  43. <description>
  44. <para>Returns <literal>1</literal> if NULL or <literal>0</literal> otherwise.</para>
  45. </description>
  46. </function>
  47. <function name="SET" language="en_US">
  48. <synopsis>
  49. SET assigns a value to a channel variable.
  50. </synopsis>
  51. <syntax argsep="=">
  52. <parameter name="varname" required="true" />
  53. <parameter name="value" />
  54. </syntax>
  55. <description>
  56. </description>
  57. </function>
  58. <function name="EXISTS" language="en_US">
  59. <synopsis>
  60. Test the existence of a value.
  61. </synopsis>
  62. <syntax>
  63. <parameter name="data" required="true" />
  64. </syntax>
  65. <description>
  66. <para>Returns <literal>1</literal> if exists, <literal>0</literal> otherwise.</para>
  67. </description>
  68. </function>
  69. <function name="IF" language="en_US">
  70. <synopsis>
  71. Check for an expresion.
  72. </synopsis>
  73. <syntax argsep="?">
  74. <parameter name="expresion" required="true" />
  75. <parameter name="retvalue" argsep=":" required="true">
  76. <argument name="true" />
  77. <argument name="false" />
  78. </parameter>
  79. </syntax>
  80. <description>
  81. <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
  82. </description>
  83. </function>
  84. <function name="IFTIME" language="en_US">
  85. <synopsis>
  86. Temporal Conditional.
  87. </synopsis>
  88. <syntax argsep="?">
  89. <parameter name="timespec" required="true" />
  90. <parameter name="retvalue" required="true" argsep=":">
  91. <argument name="true" />
  92. <argument name="false" />
  93. </parameter>
  94. </syntax>
  95. <description>
  96. <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
  97. </description>
  98. </function>
  99. <function name="IMPORT" language="en_US">
  100. <synopsis>
  101. Retrieve the value of a variable from another channel.
  102. </synopsis>
  103. <syntax>
  104. <parameter name="channel" required="true" />
  105. <parameter name="variable" required="true" />
  106. </syntax>
  107. <description>
  108. </description>
  109. </function>
  110. ***/
  111. static int isnull(struct ast_channel *chan, const char *cmd, char *data,
  112. char *buf, size_t len)
  113. {
  114. strcpy(buf, data && *data ? "0" : "1");
  115. return 0;
  116. }
  117. static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  118. size_t len)
  119. {
  120. strcpy(buf, data && *data ? "1" : "0");
  121. return 0;
  122. }
  123. static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  124. size_t len)
  125. {
  126. struct ast_timing timing;
  127. char *expr;
  128. char *iftrue;
  129. char *iffalse;
  130. data = ast_strip_quoted(data, "\"", "\"");
  131. expr = strsep(&data, "?");
  132. iftrue = strsep(&data, ":");
  133. iffalse = data;
  134. if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
  135. ast_log(LOG_WARNING,
  136. "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
  137. return -1;
  138. }
  139. if (!ast_build_timing(&timing, expr)) {
  140. ast_log(LOG_WARNING, "Invalid Time Spec.\n");
  141. ast_destroy_timing(&timing);
  142. return -1;
  143. }
  144. if (iftrue)
  145. iftrue = ast_strip_quoted(iftrue, "\"", "\"");
  146. if (iffalse)
  147. iffalse = ast_strip_quoted(iffalse, "\"", "\"");
  148. ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
  149. ast_destroy_timing(&timing);
  150. return 0;
  151. }
  152. static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  153. size_t len)
  154. {
  155. AST_DECLARE_APP_ARGS(args1,
  156. AST_APP_ARG(expr);
  157. AST_APP_ARG(remainder);
  158. );
  159. AST_DECLARE_APP_ARGS(args2,
  160. AST_APP_ARG(iftrue);
  161. AST_APP_ARG(iffalse);
  162. );
  163. args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
  164. then args1.remainder will be NULL, not a pointer to a null string, and
  165. then any garbage in args2.iffalse will not be cleared, and you'll crash.
  166. -- and if you mod the ast_app_separate_args func instead, you'll really
  167. mess things up badly, because the rest of everything depends on null args
  168. for non-specified stuff. */
  169. AST_NONSTANDARD_APP_ARGS(args1, data, '?');
  170. AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
  171. if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
  172. ast_log(LOG_WARNING, "Syntax IF(<expr>?[<true>][:<false>]) (expr must be non-null, and either <true> or <false> must be non-null)\n");
  173. ast_log(LOG_WARNING, " In this case, <expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
  174. return -1;
  175. }
  176. args1.expr = ast_strip(args1.expr);
  177. if (args2.iftrue)
  178. args2.iftrue = ast_strip(args2.iftrue);
  179. if (args2.iffalse)
  180. args2.iffalse = ast_strip(args2.iffalse);
  181. ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
  182. return 0;
  183. }
  184. static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  185. size_t len)
  186. {
  187. char *varname;
  188. char *val;
  189. varname = strsep(&data, "=");
  190. val = data;
  191. if (ast_strlen_zero(varname) || !val) {
  192. ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
  193. return -1;
  194. }
  195. varname = ast_strip(varname);
  196. val = ast_strip(val);
  197. pbx_builtin_setvar_helper(chan, varname, val);
  198. ast_copy_string(buf, val, len);
  199. return 0;
  200. }
  201. static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
  202. {
  203. if (len > -1) {
  204. ast_str_make_space(str, len == 0 ? strlen(data) : len);
  205. }
  206. return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
  207. }
  208. static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
  209. {
  210. AST_DECLARE_APP_ARGS(args,
  211. AST_APP_ARG(channel);
  212. AST_APP_ARG(varname);
  213. );
  214. AST_STANDARD_APP_ARGS(args, data);
  215. if (buf) {
  216. *buf = '\0';
  217. }
  218. if (!ast_strlen_zero(args.varname)) {
  219. struct ast_channel *chan2;
  220. if ((chan2 = ast_channel_get_by_name(args.channel))) {
  221. char *s = ast_alloca(strlen(args.varname) + 4);
  222. sprintf(s, "${%s}", args.varname);
  223. ast_channel_lock(chan2);
  224. if (buf) {
  225. pbx_substitute_variables_helper(chan2, s, buf, len);
  226. } else {
  227. ast_str_substitute_variables(str, len, chan2, s);
  228. }
  229. ast_channel_unlock(chan2);
  230. chan2 = ast_channel_unref(chan2);
  231. }
  232. }
  233. return 0;
  234. }
  235. static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  236. {
  237. return import_helper(chan, cmd, data, buf, NULL, len);
  238. }
  239. static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
  240. {
  241. return import_helper(chan, cmd, data, NULL, str, len);
  242. }
  243. static struct ast_custom_function isnull_function = {
  244. .name = "ISNULL",
  245. .read = isnull,
  246. .read_max = 2,
  247. };
  248. static struct ast_custom_function set_function = {
  249. .name = "SET",
  250. .read = set,
  251. .read2 = set2,
  252. };
  253. static struct ast_custom_function exists_function = {
  254. .name = "EXISTS",
  255. .read = exists,
  256. .read_max = 2,
  257. };
  258. static struct ast_custom_function if_function = {
  259. .name = "IF",
  260. .read = acf_if,
  261. };
  262. static struct ast_custom_function if_time_function = {
  263. .name = "IFTIME",
  264. .read = iftime,
  265. };
  266. static struct ast_custom_function import_function = {
  267. .name = "IMPORT",
  268. .read = import_read,
  269. .read2 = import_read2,
  270. };
  271. static int unload_module(void)
  272. {
  273. int res = 0;
  274. res |= ast_custom_function_unregister(&isnull_function);
  275. res |= ast_custom_function_unregister(&set_function);
  276. res |= ast_custom_function_unregister(&exists_function);
  277. res |= ast_custom_function_unregister(&if_function);
  278. res |= ast_custom_function_unregister(&if_time_function);
  279. res |= ast_custom_function_unregister(&import_function);
  280. return res;
  281. }
  282. static int load_module(void)
  283. {
  284. int res = 0;
  285. res |= ast_custom_function_register(&isnull_function);
  286. res |= ast_custom_function_register(&set_function);
  287. res |= ast_custom_function_register(&exists_function);
  288. res |= ast_custom_function_register(&if_function);
  289. res |= ast_custom_function_register(&if_time_function);
  290. res |= ast_custom_function_register(&import_function);
  291. return res;
  292. }
  293. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");