res_clioriginate.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005 - 2006, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.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. /*!
  19. * \file
  20. * \author Russell Bryant <russell@digium.com>
  21. *
  22. * \brief Originate calls via the CLI
  23. *
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  30. #include "asterisk/channel.h"
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/cli.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/frame.h"
  36. #include "asterisk/format_cache.h"
  37. /*! The timeout for originated calls, in seconds */
  38. #define TIMEOUT 30
  39. /*!
  40. * \brief orginate a call from the CLI
  41. * \param fd file descriptor for cli
  42. * \param chan channel to create type/data
  43. * \param app application you want to run
  44. * \param appdata data for application
  45. * \retval CLI_SUCCESS on success.
  46. * \retval CLI_SHOWUSAGE on failure.
  47. */
  48. static char *orig_app(int fd, const char *chan, const char *app, const char *appdata)
  49. {
  50. char *chantech;
  51. char *chandata;
  52. int reason = 0;
  53. struct ast_format_cap *cap;
  54. if (ast_strlen_zero(app))
  55. return CLI_SHOWUSAGE;
  56. chandata = ast_strdupa(chan);
  57. chantech = strsep(&chandata, "/");
  58. if (!chandata) {
  59. ast_cli(fd, "*** No data provided after channel type! ***\n");
  60. return CLI_SHOWUSAGE;
  61. }
  62. if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  63. return CLI_FAILURE;
  64. }
  65. ast_format_cap_append(cap, ast_format_slin, 0);
  66. ast_pbx_outgoing_app(chantech, cap, chandata, TIMEOUT * 1000, app, appdata, &reason, 0, NULL, NULL, NULL, NULL, NULL, NULL);
  67. ao2_ref(cap, -1);
  68. return CLI_SUCCESS;
  69. }
  70. /*!
  71. * \brief orginate from extension
  72. * \param fd file descriptor for cli
  73. * \param chan channel to create type/data
  74. * \param data contains exten\@context
  75. * \retval CLI_SUCCESS on success.
  76. * \retval CLI_SHOWUSAGE on failure.
  77. */
  78. static char *orig_exten(int fd, const char *chan, const char *data)
  79. {
  80. char *chantech;
  81. char *chandata;
  82. char *exten = NULL;
  83. char *context = NULL;
  84. int reason = 0;
  85. struct ast_format_cap *cap;
  86. chandata = ast_strdupa(chan);
  87. chantech = strsep(&chandata, "/");
  88. if (!chandata) {
  89. ast_cli(fd, "*** No data provided after channel type! ***\n");
  90. return CLI_SHOWUSAGE;
  91. }
  92. if (!ast_strlen_zero(data)) {
  93. context = ast_strdupa(data);
  94. exten = strsep(&context, "@");
  95. }
  96. if (ast_strlen_zero(exten))
  97. exten = "s";
  98. if (ast_strlen_zero(context))
  99. context = "default";
  100. if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  101. return CLI_FAILURE;
  102. }
  103. ast_format_cap_append(cap, ast_format_slin, 0);
  104. ast_pbx_outgoing_exten(chantech, cap, chandata, TIMEOUT * 1000, context, exten, 1, &reason, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL);
  105. ao2_ref(cap, -1);
  106. return CLI_SUCCESS;
  107. }
  108. /*!
  109. * \brief handle for orgination app or exten.
  110. * \param e pointer to the CLI structure to initialize
  111. * \param cmd operation to execute
  112. * \param a structure that contains either application or extension arguments
  113. * \retval CLI_SUCCESS on success.
  114. * \retval CLI_SHOWUSAGE on failure.*/
  115. static char *handle_orig(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  116. {
  117. static const char * const choices[] = { "application", "extension", NULL };
  118. char *res = NULL;
  119. switch (cmd) {
  120. case CLI_INIT:
  121. e->command = "channel originate";
  122. e->usage =
  123. " There are two ways to use this command. A call can be originated between a\n"
  124. "channel and a specific application, or between a channel and an extension in\n"
  125. "the dialplan. This is similar to call files or the manager originate action.\n"
  126. "Calls originated with this command are given a timeout of 30 seconds.\n\n"
  127. "Usage1: channel originate <tech/data> application <appname> [appdata]\n"
  128. " This will originate a call between the specified channel tech/data and the\n"
  129. "given application. Arguments to the application are optional. If the given\n"
  130. "arguments to the application include spaces, all of the arguments to the\n"
  131. "application need to be placed in quotation marks.\n\n"
  132. "Usage2: channel originate <tech/data> extension [exten@][context]\n"
  133. " This will originate a call between the specified channel tech/data and the\n"
  134. "given extension. If no context is specified, the 'default' context will be\n"
  135. "used. If no extension is given, the 's' extension will be used.\n";
  136. return NULL;
  137. case CLI_GENERATE:
  138. /* ugly, can be removed when CLI entries have ast_module pointers */
  139. ast_module_ref(ast_module_info->self);
  140. if (a->pos == 3) {
  141. res = ast_cli_complete(a->word, choices, a->n);
  142. } else if (a->pos == 4) {
  143. if (!strcasecmp("application", a->argv[3])) {
  144. res = ast_complete_applications(a->line, a->word, a->n);
  145. }
  146. }
  147. ast_module_unref(ast_module_info->self);
  148. return res;
  149. }
  150. if (ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3]))
  151. return CLI_SHOWUSAGE;
  152. /* ugly, can be removed when CLI entries have ast_module pointers */
  153. ast_module_ref(ast_module_info->self);
  154. if (!strcasecmp("application", a->argv[3])) {
  155. res = orig_app(a->fd, a->argv[2], a->argv[4], a->argv[5]);
  156. } else if (!strcasecmp("extension", a->argv[3])) {
  157. res = orig_exten(a->fd, a->argv[2], a->argv[4]);
  158. } else {
  159. res = CLI_SHOWUSAGE;
  160. }
  161. ast_module_unref(ast_module_info->self);
  162. return res;
  163. }
  164. static char *handle_redirect(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  165. {
  166. const char *name, *dest;
  167. struct ast_channel *chan;
  168. int res;
  169. switch (cmd) {
  170. case CLI_INIT:
  171. e->command = "channel redirect";
  172. e->usage = ""
  173. "Usage: channel redirect <channel> <[[context,]exten,]priority>\n"
  174. " Redirect an active channel to a specified extension.\n";
  175. /*! \todo It would be nice to be able to redirect 2 channels at the same
  176. * time like you can with AMI redirect. However, it is not possible to acquire
  177. * two channels without the potential for a deadlock with how ast_channel structs
  178. * are managed today. Once ast_channel is a refcounted object, this command
  179. * will be able to support that. */
  180. return NULL;
  181. case CLI_GENERATE:
  182. return ast_complete_channels(a->line, a->word, a->pos, a->n, 2);
  183. }
  184. if (a->argc != e->args + 2) {
  185. return CLI_SHOWUSAGE;
  186. }
  187. name = a->argv[2];
  188. dest = a->argv[3];
  189. if (!(chan = ast_channel_get_by_name(name))) {
  190. ast_cli(a->fd, "Channel '%s' not found\n", name);
  191. return CLI_FAILURE;
  192. }
  193. res = ast_async_parseable_goto(chan, dest);
  194. chan = ast_channel_unref(chan);
  195. if (!res) {
  196. ast_cli(a->fd, "Channel '%s' successfully redirected to %s\n", name, dest);
  197. } else {
  198. ast_cli(a->fd, "Channel '%s' failed to be redirected to %s\n", name, dest);
  199. }
  200. return res ? CLI_FAILURE : CLI_SUCCESS;
  201. }
  202. static struct ast_cli_entry cli_cliorig[] = {
  203. AST_CLI_DEFINE(handle_orig, "Originate a call"),
  204. AST_CLI_DEFINE(handle_redirect, "Redirect a call"),
  205. };
  206. static int unload_module(void)
  207. {
  208. return ast_cli_unregister_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
  209. }
  210. static int load_module(void)
  211. {
  212. int res;
  213. res = ast_cli_register_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
  214. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  215. }
  216. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call origination and redirection from the CLI");