func_export.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2021-2022, Naveen Albert
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Set variables and functions on other channels
  19. *
  20. * \author Naveen Albert <asterisk@phreaknet.org>
  21. *
  22. * \ingroup functions
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/module.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/app.h"
  33. #include "asterisk/stringfields.h"
  34. /*** DOCUMENTATION
  35. <function name="EXPORT" language="en_US">
  36. <synopsis>
  37. Set variables or dialplan functions on any arbitrary channel that exists.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="channel" required="true">
  41. <para>The complete channel name: <literal>SIP/12-abcd1234</literal>.</para>
  42. </parameter>
  43. <parameter name="var" required="true">
  44. <para>Variable name</para>
  45. </parameter>
  46. </syntax>
  47. <description>
  48. <para>Allows setting variables or functions on any existing channel if it exists.</para>
  49. </description>
  50. <see-also>
  51. <ref type="function">IMPORT</ref>
  52. <ref type="function">MASTER_CHANNEL</ref>
  53. <ref type="function">SHARED</ref>
  54. </see-also>
  55. </function>
  56. ***/
  57. static int func_export_write(struct ast_channel *chan, const char *function, char *data, const char *value)
  58. {
  59. struct ast_channel *ochan;
  60. AST_DECLARE_APP_ARGS(args,
  61. AST_APP_ARG(channel);
  62. AST_APP_ARG(var);
  63. );
  64. AST_STANDARD_APP_ARGS(args, data);
  65. if (!args.channel) {
  66. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  67. return -1;
  68. }
  69. if (!args.var) {
  70. ast_log(LOG_WARNING, "No variable name was provided to %s function.\n", function);
  71. return -1;
  72. }
  73. ochan = ast_channel_get_by_name(args.channel);
  74. if (!ochan) {
  75. ast_log(LOG_WARNING, "Channel '%s' not found! '%s' not set.\n", args.channel, args.var);
  76. return -1;
  77. }
  78. pbx_builtin_setvar_helper(ochan, args.var, value);
  79. ast_channel_unref(ochan);
  80. return 0;
  81. }
  82. static struct ast_custom_function export_function = {
  83. .name = "EXPORT",
  84. .write = func_export_write,
  85. };
  86. static int unload_module(void)
  87. {
  88. return ast_custom_function_unregister(&export_function);
  89. }
  90. static int load_module(void)
  91. {
  92. return ast_custom_function_register(&export_function);
  93. }
  94. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Set variables and functions on other channels");