res_clioriginate.c 7.5 KB

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