Asterisk - The Open Source Telephony Project  21.4.1
res_clioriginate.c
Go to the documentation of this file.
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 /*!
20  * \file
21  * \author Russell Bryant <russell@digium.com>
22  *
23  * \brief Originate calls via the CLI
24  *
25  */
26 
27 /*** MODULEINFO
28  <support_level>core</support_level>
29  ***/
30 
31 #include "asterisk.h"
32 
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/module.h"
36 #include "asterisk/cli.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/frame.h"
39 #include "asterisk/format_cache.h"
40 
41 /*! The timeout for originated calls, in seconds */
42 #define TIMEOUT 30
43 
44 /*!
45  * \brief orginate a call from the CLI
46  * \param fd file descriptor for cli
47  * \param chan channel to create type/data
48  * \param app application you want to run
49  * \param appdata data for application
50  * \retval CLI_SUCCESS on success.
51  * \retval CLI_SHOWUSAGE on failure.
52 */
53 static char *orig_app(int fd, const char *chan, const char *app, const char *appdata)
54 {
55  char *chantech;
56  char *chandata;
57  int reason = 0;
58  struct ast_format_cap *cap;
59 
60  if (ast_strlen_zero(app))
61  return CLI_SHOWUSAGE;
62 
63  chandata = ast_strdupa(chan);
64 
65  chantech = strsep(&chandata, "/");
66  if (!chandata) {
67  ast_cli(fd, "*** No data provided after channel type! ***\n");
68  return CLI_SHOWUSAGE;
69  }
70 
72  return CLI_FAILURE;
73  }
75  ast_pbx_outgoing_app(chantech, cap, chandata, TIMEOUT * 1000, app, appdata,
76  &reason, AST_OUTGOING_NO_WAIT, NULL, NULL, NULL, NULL,
77  NULL, NULL);
78  ao2_ref(cap, -1);
79 
80  return CLI_SUCCESS;
81 }
82 
83 /*!
84  * \brief orginate from extension
85  * \param fd file descriptor for cli
86  * \param chan channel to create type/data
87  * \param data contains exten\@context
88  * \retval CLI_SUCCESS on success.
89  * \retval CLI_SHOWUSAGE on failure.
90 */
91 static char *orig_exten(int fd, const char *chan, const char *data)
92 {
93  char *chantech;
94  char *chandata;
95  char *exten = NULL;
96  char *context = NULL;
97  int reason = 0;
98  struct ast_format_cap *cap;
99 
100  chandata = ast_strdupa(chan);
101 
102  chantech = strsep(&chandata, "/");
103  if (!chandata) {
104  ast_cli(fd, "*** No data provided after channel type! ***\n");
105  return CLI_SHOWUSAGE;
106  }
107 
108  if (!ast_strlen_zero(data)) {
109  context = ast_strdupa(data);
110  exten = strsep(&context, "@");
111  }
112 
113  if (ast_strlen_zero(exten))
114  exten = "s";
115  if (ast_strlen_zero(context))
116  context = "default";
118  return CLI_FAILURE;
119  }
121  ast_pbx_outgoing_exten(chantech, cap, chandata, TIMEOUT * 1000, context,
122  exten, 1, &reason, AST_OUTGOING_NO_WAIT, NULL, NULL,
123  NULL, NULL, NULL, 0, NULL);
124  ao2_ref(cap, -1);
125 
126  return CLI_SUCCESS;
127 }
128 
129 /*!
130  * \brief handle for orgination app or exten.
131  * \param e pointer to the CLI structure to initialize
132  * \param cmd operation to execute
133  * \param a structure that contains either application or extension arguments
134  * \retval CLI_SUCCESS on success.
135  * \retval CLI_SHOWUSAGE on failure.*/
136 static char *handle_orig(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
137 {
138  static const char * const choices[] = { "application", "extension", NULL };
139  char *res = NULL;
140  switch (cmd) {
141  case CLI_INIT:
142  e->command = "channel originate";
143  e->usage =
144  " There are two ways to use this command. A call can be originated between a\n"
145  "channel and a specific application, or between a channel and an extension in\n"
146  "the dialplan. This is similar to call files or the manager originate action.\n"
147  "Calls originated with this command are given a timeout of 30 seconds.\n\n"
148 
149  "Usage1: channel originate <tech/data> application <appname> [appdata]\n"
150  " This will originate a call between the specified channel tech/data and the\n"
151  "given application. Arguments to the application are optional. If the given\n"
152  "arguments to the application include spaces, all of the arguments to the\n"
153  "application need to be placed in quotation marks.\n\n"
154 
155  "Usage2: channel originate <tech/data> extension [exten@][context]\n"
156  " This will originate a call between the specified channel tech/data and the\n"
157  "given extension. If no context is specified, the 'default' context will be\n"
158  "used. If no extension is given, the 's' extension will be used.\n";
159  return NULL;
160  case CLI_GENERATE:
161  if (a->pos == 3) {
162  res = ast_cli_complete(a->word, choices, a->n);
163  } else if (a->pos == 4) {
164  if (!strcasecmp("application", a->argv[3])) {
165  res = ast_complete_applications(a->line, a->word, a->n);
166  }
167  }
168  return res;
169  }
170 
171  if (ast_strlen_zero(a->argv[2]) || ast_strlen_zero(a->argv[3]))
172  return CLI_SHOWUSAGE;
173 
174  if (!strcasecmp("application", a->argv[3])) {
175  res = orig_app(a->fd, a->argv[2], a->argv[4], a->argv[5]);
176  } else if (!strcasecmp("extension", a->argv[3])) {
177  res = orig_exten(a->fd, a->argv[2], a->argv[4]);
178  } else {
179  res = CLI_SHOWUSAGE;
180  }
181 
182  return res;
183 }
184 
185 static char *handle_redirect(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
186 {
187  const char *name, *dest;
188  struct ast_channel *chan;
189  int res;
190 
191  switch (cmd) {
192  case CLI_INIT:
193  e->command = "channel redirect";
194  e->usage = ""
195  "Usage: channel redirect <channel> <[[context,]exten,]priority>\n"
196  " Redirect an active channel to a specified extension.\n";
197  /*! \todo It would be nice to be able to redirect 2 channels at the same
198  * time like you can with AMI redirect. However, it is not possible to acquire
199  * two channels without the potential for a deadlock with how ast_channel structs
200  * are managed today. Once ast_channel is a refcounted object, this command
201  * will be able to support that. */
202  return NULL;
203  case CLI_GENERATE:
204  return ast_complete_channels(a->line, a->word, a->pos, a->n, 2);
205  }
206 
207  if (a->argc != e->args + 2) {
208  return CLI_SHOWUSAGE;
209  }
210 
211  name = a->argv[2];
212  dest = a->argv[3];
213 
214  if (!(chan = ast_channel_get_by_name(name))) {
215  ast_cli(a->fd, "Channel '%s' not found\n", name);
216  return CLI_FAILURE;
217  }
218 
219  res = ast_async_parseable_goto(chan, dest);
220 
221  chan = ast_channel_unref(chan);
222 
223  if (!res) {
224  ast_cli(a->fd, "Channel '%s' successfully redirected to %s\n", name, dest);
225  } else {
226  ast_cli(a->fd, "Channel '%s' failed to be redirected to %s\n", name, dest);
227  }
228 
229  return res ? CLI_FAILURE : CLI_SUCCESS;
230 }
231 
232 static struct ast_cli_entry cli_cliorig[] = {
233  AST_CLI_DEFINE(handle_orig, "Originate a call"),
234  AST_CLI_DEFINE(handle_redirect, "Redirect a call"),
235 };
236 
237 static int unload_module(void)
238 {
239  return ast_cli_unregister_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
240 }
241 
242 static int load_module(void)
243 {
244  int res;
245  res = ast_cli_register_multiple(cli_cliorig, ARRAY_LEN(cli_cliorig));
247 }
248 
249 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call origination and redirection from the CLI");
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
descriptor for a cli entry.
Definition: cli.h:171
char * ast_complete_applications(const char *line, const char *word, int state)
Command completion for the list of installed applications.
Definition: pbx_app.c:429
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define TIMEOUT
static char * orig_app(int fd, const char *chan, const char *app, const char *appdata)
orginate a call from the CLI
int ast_pbx_outgoing_exten(const char *type, struct ast_format_cap *cap, const char *addr, int timeout, const char *context, const char *exten, int priority, int *reason, int synchronous, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel, int early_media, const struct ast_assigned_ids *assignedids)
Synchronously or asynchronously make an outbound call and send it to a particular extension...
Definition: pbx.c:7916
Utility functions.
int args
This gets set in ast_cli_register()
Definition: cli.h:185
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: main/cli.c:1846
General Asterisk PBX channel definitions.
static char * handle_orig(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
handle for orgination app or exten.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Asterisk internal frame definitions.
static char * orig_exten(int fd, const char *chan, const char *data)
orginate from extension
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
Core PBX routines and definitions.
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
int ast_pbx_outgoing_app(const char *type, struct ast_format_cap *cap, const char *addr, int timeout, const char *app, const char *appdata, int *reason, int synchronous, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel, const struct ast_assigned_ids *assignedids)
Synchronously or asynchronously make an outbound call and execute an application on the channel...
Definition: pbx.c:7980
char * command
Definition: cli.h:186
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
const char * usage
Definition: cli.h:177
int ast_async_parseable_goto(struct ast_channel *chan, const char *goto_string)
Definition: pbx.c:8871
Standard Command Line Interface.
static char * handle_redirect(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
char * ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos)
Command completion for the list of active channels.
Definition: main/cli.c:1865
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1454
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
Asterisk module definitions.
Media Format Cache API.