Merged revisions 50266 via svnmerge from
[asterisk/asterisk.git] / res / res_clioriginate.c
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 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/module.h"
39 #include "asterisk/cli.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/frame.h"
42
43 /*! The timeout for originated calls, in seconds */
44 #define TIMEOUT 30
45
46 static char orig_help[] = 
47 "  There are two ways to use this command. A call can be originated between a\n"
48 "channel and a specific application, or between a channel and an extension in\n"
49 "the dialplan. This is similar to call files or the manager originate action.\n"
50 "Calls originated with this command are given a timeout of 30 seconds.\n\n"
51
52 "Usage1: originate <tech/data> application <appname> [appdata]\n"
53 "  This will originate a call between the specified channel tech/data and the\n"
54 "given application. Arguments to the application are optional. If the given\n"
55 "arguments to the application include spaces, all of the arguments to the\n"
56 "application need to be placed in quotation marks.\n\n"
57
58 "Usage2: originate <tech/data> extension [exten@][context]\n"
59 "  This will originate a call between the specified channel tech/data and the\n"
60 "given extension. If no context is specified, the 'default' context will be\n"
61 "used. If no extension is given, the 's' extension will be used.\n";
62
63 static int handle_orig(int fd, int argc, char *argv[]);
64 static char *complete_orig(const char *line, const char *word, int pos, int state);
65
66 struct ast_cli_entry cli_cliorig[] = {
67         { { "originate", NULL },
68         handle_orig, "Originate a call",
69         orig_help, complete_orig },
70 };
71
72 static int orig_app(int fd, const char *chan, const char *app, const char *appdata)
73 {
74         char *chantech;
75         char *chandata;
76         int reason = 0;
77         
78         if (ast_strlen_zero(app))
79                 return RESULT_SHOWUSAGE;
80
81         chandata = ast_strdupa(chan);
82         
83         chantech = strsep(&chandata, "/");
84         if (!chandata) {
85                 ast_cli(fd, "*** No data provided after channel type! ***\n");
86                 return RESULT_SHOWUSAGE;
87         }
88
89         ast_pbx_outgoing_app(chantech, AST_FORMAT_SLINEAR, chandata, TIMEOUT * 1000, app, appdata, &reason, 1, NULL, NULL, NULL, NULL, NULL);
90
91         return RESULT_SUCCESS;
92 }
93
94 static int orig_exten(int fd, const char *chan, const char *data)
95 {
96         char *chantech;
97         char *chandata;
98         char *exten = NULL;
99         char *context = NULL;
100         int reason = 0;
101
102         chandata = ast_strdupa(chan);
103         
104         chantech = strsep(&chandata, "/");
105         if (!chandata) {
106                 ast_cli(fd, "*** No data provided after channel type! ***\n");
107                 return RESULT_SHOWUSAGE;
108         }
109
110         if (!ast_strlen_zero(data)) {
111                 context = ast_strdupa(data);
112                 exten = strsep(&context, "@");
113         }
114
115         if (ast_strlen_zero(exten))
116                 exten = "s";
117         if (ast_strlen_zero(context))
118                 context = "default";
119         
120         ast_pbx_outgoing_exten(chantech, AST_FORMAT_SLINEAR, chandata, TIMEOUT * 1000, context, exten, 1, &reason, 1, NULL, NULL, NULL, NULL, NULL);
121
122         return RESULT_SUCCESS;
123 }
124
125 static int handle_orig(int fd, int argc, char *argv[])
126 {
127         int res;
128
129         if (ast_strlen_zero(argv[1]) || ast_strlen_zero(argv[2]))
130                 return RESULT_SHOWUSAGE;
131
132         /* ugly, can be removed when CLI entries have ast_module pointers */
133         ast_module_ref(ast_module_info->self);
134
135         if (!strcasecmp("application", argv[2])) {
136                 res = orig_app(fd, argv[1], argv[3], argv[4]);  
137         } else if (!strcasecmp("extension", argv[2])) {
138                 res = orig_exten(fd, argv[1], argv[3]);
139         } else
140                 res = RESULT_SHOWUSAGE;
141
142         ast_module_unref(ast_module_info->self);
143
144         return res;
145 }
146
147 static char *complete_orig(const char *line, const char *word, int pos, int state)
148 {
149         static char *choices[] = { "application", "extension", NULL };
150         char *ret;
151
152         if (pos != 2)
153                 return NULL;
154
155         /* ugly, can be removed when CLI entries have ast_module pointers */
156         ast_module_ref(ast_module_info->self);
157         ret = ast_cli_complete(word, choices, state);
158         ast_module_unref(ast_module_info->self);
159
160         return ret;
161 }
162
163 static int unload_module(void)
164 {
165         ast_cli_unregister_multiple(cli_cliorig, sizeof(cli_cliorig) / sizeof(struct ast_cli_entry));
166         return 0;
167 }
168
169 static int load_module(void)
170 {
171         ast_cli_register_multiple(cli_cliorig, sizeof(cli_cliorig) / sizeof(struct ast_cli_entry));
172         return 0;
173 }
174
175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call origination from the CLI");