func_logic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #include "asterisk/module.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/app.h"
  34. /*** DOCUMENTATION
  35. <function name="ISNULL" language="en_US">
  36. <synopsis>
  37. Check if a value is NULL.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="data" required="true" />
  41. </syntax>
  42. <description>
  43. <para>Returns <literal>1</literal> if NULL or <literal>0</literal> otherwise.</para>
  44. </description>
  45. </function>
  46. <function name="SET" language="en_US">
  47. <synopsis>
  48. SET assigns a value to a channel variable.
  49. </synopsis>
  50. <syntax argsep="=">
  51. <parameter name="varname" required="true" />
  52. <parameter name="value" />
  53. </syntax>
  54. <description>
  55. </description>
  56. </function>
  57. <function name="EXISTS" language="en_US">
  58. <synopsis>
  59. Test the existence of a value.
  60. </synopsis>
  61. <syntax>
  62. <parameter name="data" required="true" />
  63. </syntax>
  64. <description>
  65. <para>Returns <literal>1</literal> if exists, <literal>0</literal> otherwise.</para>
  66. </description>
  67. </function>
  68. <function name="IF" language="en_US">
  69. <synopsis>
  70. Check for an expression.
  71. </synopsis>
  72. <syntax argsep="?">
  73. <parameter name="expression" required="true" />
  74. <parameter name="retvalue" argsep=":" required="true">
  75. <argument name="true" />
  76. <argument name="false" />
  77. </parameter>
  78. </syntax>
  79. <description>
  80. <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
  81. </description>
  82. </function>
  83. <function name="IFTIME" language="en_US">
  84. <synopsis>
  85. Temporal Conditional.
  86. </synopsis>
  87. <syntax argsep="?">
  88. <parameter name="timespec" required="true" />
  89. <parameter name="retvalue" required="true" argsep=":">
  90. <argument name="true" />
  91. <argument name="false" />
  92. </parameter>
  93. </syntax>
  94. <description>
  95. <para>Returns the data following <literal>?</literal> if true, else the data following <literal>:</literal></para>
  96. </description>
  97. </function>
  98. <function name="IMPORT" language="en_US">
  99. <synopsis>
  100. Retrieve the value of a variable from another channel.
  101. </synopsis>
  102. <syntax>
  103. <parameter name="channel" required="true" />
  104. <parameter name="variable" required="true" />
  105. </syntax>
  106. <description>
  107. </description>
  108. </function>
  109. <function name="DELETE" language="en_US">
  110. <synopsis>
  111. Deletes a specified channel variable.
  112. </synopsis>
  113. <syntax>
  114. <parameter name="varname" required="true">
  115. <para>Channel variable name</para>
  116. </parameter>
  117. </syntax>
  118. <description>
  119. <para>Delete the channel variable specified in <replaceable>varname</replaceable>.
  120. Will succeed if the channel variable exists or not.</para>
  121. </description>
  122. <see-also>
  123. <ref type="function">GLOBAL_DELETE</ref>
  124. </see-also>
  125. </function>
  126. <function name="VARIABLE_EXISTS" language="en_US">
  127. <synopsis>
  128. Check if a dialplan variable exists or not.
  129. </synopsis>
  130. <syntax>
  131. <parameter name="varname" required="true">
  132. <para>Channel variable name</para>
  133. </parameter>
  134. </syntax>
  135. <description>
  136. <para>Returns <literal>1</literal> if channel variable exists or <literal>0</literal> otherwise.</para>
  137. </description>
  138. <see-also>
  139. <ref type="function">GLOBAL_EXISTS</ref>
  140. </see-also>
  141. </function>
  142. ***/
  143. static int isnull(struct ast_channel *chan, const char *cmd, char *data,
  144. char *buf, size_t len)
  145. {
  146. strcpy(buf, data && *data ? "0" : "1");
  147. return 0;
  148. }
  149. static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  150. size_t len)
  151. {
  152. strcpy(buf, data && *data ? "1" : "0");
  153. return 0;
  154. }
  155. static int iftime(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  156. size_t len)
  157. {
  158. struct ast_timing timing;
  159. char *expr;
  160. char *iftrue;
  161. char *iffalse;
  162. data = ast_strip_quoted(data, "\"", "\"");
  163. expr = strsep(&data, "?");
  164. iftrue = strsep(&data, ":");
  165. iffalse = data;
  166. if (ast_strlen_zero(expr) || !(iftrue || iffalse)) {
  167. ast_log(LOG_WARNING,
  168. "Syntax IFTIME(<timespec>?[<true>][:<false>])\n");
  169. return -1;
  170. }
  171. if (!ast_build_timing(&timing, expr)) {
  172. ast_log(LOG_WARNING, "Invalid Time Spec.\n");
  173. ast_destroy_timing(&timing);
  174. return -1;
  175. }
  176. if (iftrue)
  177. iftrue = ast_strip_quoted(iftrue, "\"", "\"");
  178. if (iffalse)
  179. iffalse = ast_strip_quoted(iffalse, "\"", "\"");
  180. ast_copy_string(buf, ast_check_timing(&timing) ? S_OR(iftrue, "") : S_OR(iffalse, ""), len);
  181. ast_destroy_timing(&timing);
  182. return 0;
  183. }
  184. static int acf_if(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  185. size_t len)
  186. {
  187. AST_DECLARE_APP_ARGS(args1,
  188. AST_APP_ARG(expr);
  189. AST_APP_ARG(remainder);
  190. );
  191. AST_DECLARE_APP_ARGS(args2,
  192. AST_APP_ARG(iftrue);
  193. AST_APP_ARG(iffalse);
  194. );
  195. args2.iftrue = args2.iffalse = NULL; /* you have to set these, because if there is nothing after the '?',
  196. then args1.remainder will be NULL, not a pointer to a null string, and
  197. then any garbage in args2.iffalse will not be cleared, and you'll crash.
  198. -- and if you mod the ast_app_separate_args func instead, you'll really
  199. mess things up badly, because the rest of everything depends on null args
  200. for non-specified stuff. */
  201. AST_NONSTANDARD_APP_ARGS(args1, data, '?');
  202. AST_NONSTANDARD_APP_ARGS(args2, args1.remainder, ':');
  203. if (ast_strlen_zero(args1.expr) || !(args2.iftrue || args2.iffalse)) {
  204. ast_debug(1, "<expr>='%s', <true>='%s', and <false>='%s'\n", args1.expr, args2.iftrue, args2.iffalse);
  205. return -1;
  206. }
  207. args1.expr = ast_strip(args1.expr);
  208. if (args2.iftrue)
  209. args2.iftrue = ast_strip(args2.iftrue);
  210. if (args2.iffalse)
  211. args2.iffalse = ast_strip(args2.iffalse);
  212. ast_copy_string(buf, pbx_checkcondition(args1.expr) ? (S_OR(args2.iftrue, "")) : (S_OR(args2.iffalse, "")), len);
  213. return 0;
  214. }
  215. static int set(struct ast_channel *chan, const char *cmd, char *data, char *buf,
  216. size_t len)
  217. {
  218. char *varname;
  219. char *val;
  220. varname = strsep(&data, "=");
  221. val = data;
  222. if (ast_strlen_zero(varname) || !val) {
  223. ast_log(LOG_WARNING, "Syntax SET(<varname>=[<value>])\n");
  224. return -1;
  225. }
  226. varname = ast_strip(varname);
  227. val = ast_strip(val);
  228. pbx_builtin_setvar_helper(chan, varname, val);
  229. ast_copy_string(buf, val, len);
  230. return 0;
  231. }
  232. static int set2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
  233. {
  234. if (len > -1) {
  235. ast_str_make_space(str, len == 0 ? strlen(data) : len);
  236. }
  237. return set(chan, cmd, data, ast_str_buffer(*str), ast_str_size(*str));
  238. }
  239. static int import_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len)
  240. {
  241. AST_DECLARE_APP_ARGS(args,
  242. AST_APP_ARG(channel);
  243. AST_APP_ARG(varname);
  244. );
  245. AST_STANDARD_APP_ARGS(args, data);
  246. if (buf) {
  247. *buf = '\0';
  248. }
  249. if (!ast_strlen_zero(args.varname)) {
  250. struct ast_channel *chan2;
  251. if ((chan2 = ast_channel_get_by_name(args.channel))) {
  252. char *s = ast_alloca(strlen(args.varname) + 4);
  253. sprintf(s, "${%s}", args.varname);
  254. ast_channel_lock(chan2);
  255. if (buf) {
  256. pbx_substitute_variables_helper(chan2, s, buf, len);
  257. } else {
  258. ast_str_substitute_variables(str, len, chan2, s);
  259. }
  260. ast_channel_unlock(chan2);
  261. chan2 = ast_channel_unref(chan2);
  262. }
  263. }
  264. return 0;
  265. }
  266. static int import_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  267. {
  268. return import_helper(chan, cmd, data, buf, NULL, len);
  269. }
  270. static int import_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
  271. {
  272. return import_helper(chan, cmd, data, NULL, str, len);
  273. }
  274. static int delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  275. {
  276. pbx_builtin_setvar_helper(chan, data, NULL);
  277. return 0;
  278. }
  279. static int variable_exists_read(struct ast_channel *chan, const char *cmd, char *data,
  280. char *buf, size_t len)
  281. {
  282. const char *var = pbx_builtin_getvar_helper(chan, data);
  283. strcpy(buf, var ? "1" : "0");
  284. return 0;
  285. }
  286. static struct ast_custom_function isnull_function = {
  287. .name = "ISNULL",
  288. .read = isnull,
  289. .read_max = 2,
  290. };
  291. static struct ast_custom_function set_function = {
  292. .name = "SET",
  293. .read = set,
  294. .read2 = set2,
  295. };
  296. static struct ast_custom_function exists_function = {
  297. .name = "EXISTS",
  298. .read = exists,
  299. .read_max = 2,
  300. };
  301. static struct ast_custom_function if_function = {
  302. .name = "IF",
  303. .read = acf_if,
  304. };
  305. static struct ast_custom_function if_time_function = {
  306. .name = "IFTIME",
  307. .read = iftime,
  308. };
  309. static struct ast_custom_function import_function = {
  310. .name = "IMPORT",
  311. .read = import_read,
  312. .read2 = import_read2,
  313. };
  314. static struct ast_custom_function delete_function = {
  315. .name = "DELETE",
  316. .write = delete_write,
  317. };
  318. static struct ast_custom_function variable_exists_function = {
  319. .name = "VARIABLE_EXISTS",
  320. .read = variable_exists_read,
  321. };
  322. static int unload_module(void)
  323. {
  324. int res = 0;
  325. res |= ast_custom_function_unregister(&isnull_function);
  326. res |= ast_custom_function_unregister(&set_function);
  327. res |= ast_custom_function_unregister(&exists_function);
  328. res |= ast_custom_function_unregister(&if_function);
  329. res |= ast_custom_function_unregister(&if_time_function);
  330. res |= ast_custom_function_unregister(&import_function);
  331. res |= ast_custom_function_unregister(&delete_function);
  332. res |= ast_custom_function_unregister(&variable_exists_function);
  333. return res;
  334. }
  335. static int load_module(void)
  336. {
  337. int res = 0;
  338. res |= ast_custom_function_register(&isnull_function);
  339. res |= ast_custom_function_register(&set_function);
  340. res |= ast_custom_function_register(&exists_function);
  341. res |= ast_custom_function_register(&if_function);
  342. res |= ast_custom_function_register(&if_time_function);
  343. res |= ast_custom_function_register(&import_function);
  344. res |= ast_custom_function_register(&delete_function);
  345. res |= ast_custom_function_register(&variable_exists_function);
  346. return res;
  347. }
  348. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logical dialplan functions");