func_global.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006, Tilghman Lesher
  5. *
  6. * Tilghman Lesher <func_global__200605@the-tilghman.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Global variable dialplan functions
  21. *
  22. * \author Tilghman Lesher <func_global__200605@the-tilghman.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <sys/stat.h>
  32. #include "asterisk/module.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/app.h"
  36. #include "asterisk/stasis_channels.h"
  37. /*** DOCUMENTATION
  38. <function name="GLOBAL" language="en_US">
  39. <synopsis>
  40. Gets or sets the global variable specified.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="varname" required="true">
  44. <para>Global variable name</para>
  45. </parameter>
  46. </syntax>
  47. <description>
  48. <para>Set or get the value of a global variable specified in <replaceable>varname</replaceable></para>
  49. </description>
  50. </function>
  51. <function name="SHARED" language="en_US">
  52. <synopsis>
  53. Gets or sets the shared variable specified.
  54. </synopsis>
  55. <syntax>
  56. <parameter name="varname" required="true">
  57. <para>Variable name</para>
  58. </parameter>
  59. <parameter name="channel">
  60. <para>If not specified will default to current channel. It is the complete
  61. channel name: <literal>SIP/12-abcd1234</literal> or the prefix only <literal>SIP/12</literal>.</para>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>Implements a shared variable area, in which you may share variables between
  66. channels.</para>
  67. <para>The variables used in this space are separate from the general namespace of
  68. the channel and thus <variable>SHARED(foo)</variable> and <variable>foo</variable>
  69. represent two completely different variables, despite sharing the same name.</para>
  70. <para>Finally, realize that there is an inherent race between channels operating
  71. at the same time, fiddling with each others' internal variables, which is why
  72. this special variable namespace exists; it is to remind you that variables in
  73. the SHARED namespace may change at any time, without warning. You should
  74. therefore take special care to ensure that when using the SHARED namespace,
  75. you retrieve the variable and store it in a regular channel variable before
  76. using it in a set of calculations (or you might be surprised by the result).</para>
  77. </description>
  78. </function>
  79. <managerEvent language="en_US" name="VarSet">
  80. <managerEventInstance class="EVENT_FLAG_DIALPLAN">
  81. <synopsis>Raised when a variable is shared between channels.</synopsis>
  82. <syntax>
  83. <channel_snapshot/>
  84. <parameter name="Variable">
  85. <para>The SHARED variable being set.</para>
  86. <note><para>The variable name will always be enclosed with
  87. <literal>SHARED()</literal></para></note>
  88. </parameter>
  89. <parameter name="Value">
  90. <para>The new value of the variable.</para>
  91. </parameter>
  92. </syntax>
  93. <see-also>
  94. <ref type="function">SHARED</ref>
  95. </see-also>
  96. </managerEventInstance>
  97. </managerEvent>
  98. ***/
  99. static void shared_variable_free(void *data);
  100. static const struct ast_datastore_info shared_variable_info = {
  101. .type = "SHARED_VARIABLES",
  102. .destroy = shared_variable_free,
  103. };
  104. static void shared_variable_free(void *data)
  105. {
  106. struct varshead *varshead = data;
  107. struct ast_var_t *var;
  108. while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
  109. ast_var_delete(var);
  110. }
  111. ast_free(varshead);
  112. }
  113. static int global_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  114. {
  115. const char *var = pbx_builtin_getvar_helper(NULL, data);
  116. *buf = '\0';
  117. if (var)
  118. ast_copy_string(buf, var, len);
  119. return 0;
  120. }
  121. static int global_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  122. {
  123. pbx_builtin_setvar_helper(NULL, data, value);
  124. return 0;
  125. }
  126. static struct ast_custom_function global_function = {
  127. .name = "GLOBAL",
  128. .read = global_read,
  129. .write = global_write,
  130. };
  131. static int shared_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  132. {
  133. struct ast_datastore *varstore;
  134. struct varshead *varshead;
  135. struct ast_var_t *var;
  136. AST_DECLARE_APP_ARGS(args,
  137. AST_APP_ARG(var);
  138. AST_APP_ARG(chan);
  139. );
  140. struct ast_channel *c_ref = NULL;
  141. if (ast_strlen_zero(data)) {
  142. ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
  143. return -1;
  144. }
  145. AST_STANDARD_APP_ARGS(args, data);
  146. if (!ast_strlen_zero(args.chan)) {
  147. char *prefix = ast_alloca(strlen(args.chan) + 2);
  148. sprintf(prefix, "%s-", args.chan);
  149. if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
  150. ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
  151. return -1;
  152. }
  153. chan = c_ref;
  154. } else if (!chan) {
  155. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  156. return -1;
  157. }
  158. ast_channel_lock(chan);
  159. if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
  160. ast_channel_unlock(chan);
  161. if (c_ref) {
  162. c_ref = ast_channel_unref(c_ref);
  163. }
  164. return -1;
  165. }
  166. varshead = varstore->data;
  167. *buf = '\0';
  168. /* Protected by the channel lock */
  169. AST_LIST_TRAVERSE(varshead, var, entries) {
  170. if (!strcmp(args.var, ast_var_name(var))) {
  171. ast_copy_string(buf, ast_var_value(var), len);
  172. break;
  173. }
  174. }
  175. ast_channel_unlock(chan);
  176. if (c_ref) {
  177. c_ref = ast_channel_unref(c_ref);
  178. }
  179. return 0;
  180. }
  181. static int shared_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  182. {
  183. struct ast_datastore *varstore;
  184. struct varshead *varshead;
  185. struct ast_var_t *var;
  186. AST_DECLARE_APP_ARGS(args,
  187. AST_APP_ARG(var);
  188. AST_APP_ARG(chan);
  189. );
  190. struct ast_channel *c_ref = NULL;
  191. int len;
  192. RAII_VAR(char *, shared_buffer, NULL, ast_free);
  193. if (ast_strlen_zero(data)) {
  194. ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
  195. return -1;
  196. }
  197. AST_STANDARD_APP_ARGS(args, data);
  198. if (!ast_strlen_zero(args.chan)) {
  199. char *prefix = ast_alloca(strlen(args.chan) + 2);
  200. sprintf(prefix, "%s-", args.chan);
  201. if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
  202. ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
  203. return -1;
  204. }
  205. chan = c_ref;
  206. } else if (!chan) {
  207. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  208. return -1;
  209. }
  210. len = 9 + strlen(args.var); /* SHARED() + var */
  211. shared_buffer = ast_malloc(len);
  212. if (!shared_buffer) {
  213. if (c_ref) {
  214. ast_channel_unref(c_ref);
  215. }
  216. return -1;
  217. }
  218. ast_channel_lock(chan);
  219. if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
  220. if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
  221. ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
  222. ast_channel_unlock(chan);
  223. if (c_ref) {
  224. c_ref = ast_channel_unref(c_ref);
  225. }
  226. return -1;
  227. }
  228. if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
  229. ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
  230. ast_datastore_free(varstore);
  231. ast_channel_unlock(chan);
  232. if (c_ref) {
  233. c_ref = ast_channel_unref(c_ref);
  234. }
  235. return -1;
  236. }
  237. varstore->data = varshead;
  238. ast_channel_datastore_add(chan, varstore);
  239. }
  240. varshead = varstore->data;
  241. /* Protected by the channel lock */
  242. AST_LIST_TRAVERSE_SAFE_BEGIN(varshead, var, entries) {
  243. /* If there's a previous value, remove it */
  244. if (!strcmp(args.var, ast_var_name(var))) {
  245. AST_LIST_REMOVE_CURRENT(entries);
  246. ast_var_delete(var);
  247. break;
  248. }
  249. }
  250. AST_LIST_TRAVERSE_SAFE_END;
  251. if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
  252. AST_LIST_INSERT_HEAD(varshead, var, entries);
  253. sprintf(shared_buffer, "SHARED(%s)", args.var);
  254. ast_channel_publish_varset(chan, shared_buffer, value);
  255. }
  256. ast_channel_unlock(chan);
  257. if (c_ref) {
  258. c_ref = ast_channel_unref(c_ref);
  259. }
  260. return 0;
  261. }
  262. static struct ast_custom_function shared_function = {
  263. .name = "SHARED",
  264. .read = shared_read,
  265. .write = shared_write,
  266. };
  267. static int unload_module(void)
  268. {
  269. int res = 0;
  270. res |= ast_custom_function_unregister(&global_function);
  271. res |= ast_custom_function_unregister(&shared_function);
  272. return res;
  273. }
  274. static int load_module(void)
  275. {
  276. int res = 0;
  277. res |= ast_custom_function_register(&global_function);
  278. res |= ast_custom_function_register(&shared_function);
  279. return res;
  280. }
  281. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Variable dialplan functions");