set standard properties on all non-binary files
[asterisk/asterisk.git] / res / res_clioriginate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, 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  *
22  * \brief Originate calls via the CLI
23  * 
24  */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
33
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/module.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/frame.h"
41
42 /*! The timeout for originated calls, in seconds */
43 #define TIMEOUT 30
44
45 STANDARD_USECOUNT_DECL;
46
47 static char *tdesc = "Call origination from the CLI";
48
49 static char orig_help[] = 
50 "  There are two ways to use this command. A call can be originated between a\n"
51 "channel and a specific application, or between a channel and an extension in\n"
52 "the dialplan. This is similar to call files or the manager originate action.\n"
53 "Calls originated with this command are given a timeout of 30 seconds.\n\n"
54
55 "Usage1: originate <tech/data> application <appname> [appdata]\n"
56 "  This will originate a call between the specified channel tech/data and the\n"
57 "given application. Arguments to the application are optional. If the given\n"
58 "arguments to the application include spaces, all of the arguments to the\n"
59 "application need to be placed in quotation marks.\n\n"
60
61 "Usage2: originate <tech/data> extension [exten@][context]\n"
62 "  This will originate a call between the specified channel tech/data and the\n"
63 "given extension. If no context is specified, the 'default' context will be\n"
64 "used. If no extension is given, the 's' extension will be used.\n";
65
66 static int handle_orig(int fd, int argc, char *argv[]);
67 static char *complete_orig(const char *line, const char *word, int pos, int state);
68
69 struct ast_cli_entry cli_orig = { { "originate", NULL }, handle_orig, "Originate a call", orig_help, complete_orig };
70
71 static int orig_app(const char *chan, const char *app, const char *appdata)
72 {
73         char *chantech;
74         char *chandata;
75         int reason = 0;
76         
77         if (ast_strlen_zero(app))
78                 return RESULT_SHOWUSAGE;
79
80         chandata = ast_strdupa(chan);
81         if (!chandata) {
82                 ast_log(LOG_ERROR, "Out of Memory!\n");
83                 return RESULT_FAILURE;
84         }
85         chantech = strsep(&chandata, "/");
86         if (!chandata) {
87                 ast_log(LOG_ERROR, "No dial string.\n");
88                 return RESULT_SHOWUSAGE;
89         }
90
91         ast_pbx_outgoing_app(chantech, AST_FORMAT_SLINEAR, chandata, TIMEOUT * 1000, app, appdata, &reason, 1, NULL, NULL, NULL, NULL);
92
93         return RESULT_SUCCESS;
94 }
95
96 static int orig_exten(const char *chan, const char *data)
97 {
98         char *chantech;
99         char *chandata;
100         char *exten = NULL;
101         char *context = NULL;
102         int reason = 0;
103
104         chandata = ast_strdupa(chan);
105         if (!chandata) {
106                 ast_log(LOG_ERROR, "Out of Memory!\n");
107                 return RESULT_FAILURE;
108         }
109         chantech = strsep(&chandata, "/");
110
111         if (!ast_strlen_zero(data)) {
112                 context = ast_strdupa(data);
113                 if (!context) {
114                         ast_log(LOG_ERROR, "Out of Memory!\n");
115                         return RESULT_FAILURE;
116                 }
117                 exten = strsep(&context, "@");
118         }
119
120         if (ast_strlen_zero(exten))
121                 exten = "s";
122         if (ast_strlen_zero(context))
123                 context = "default";
124         
125         ast_pbx_outgoing_exten(chantech, AST_FORMAT_SLINEAR, chandata, TIMEOUT * 1000, context, exten, 1, &reason, 1, NULL, NULL, NULL, NULL);
126
127         return RESULT_SUCCESS;
128 }
129
130 static int handle_orig(int fd, int argc, char *argv[])
131 {
132         int res;
133
134         if (ast_strlen_zero(argv[1]) || ast_strlen_zero(argv[2]))
135                 return RESULT_SHOWUSAGE;
136
137         STANDARD_INCREMENT_USECOUNT;
138
139         if (!strcasecmp("application", argv[2])) {
140                 res = orig_app(argv[1], argv[3], argv[4]);      
141         } else if (!strcasecmp("extension", argv[2])) {
142                 res = orig_exten(argv[1], argv[3]);
143         } else
144                 res = RESULT_SHOWUSAGE;
145
146         STANDARD_DECREMENT_USECOUNT;
147
148         return res;
149 }
150
151 static char *complete_orig(const char *line, const char *word, int pos, int state)
152 {
153         int wordlen;
154         char *app = "application";
155         char *exten = "extension";
156         char *ret = NULL;
157
158         if (pos != 2 || state)
159                 return NULL;
160
161         STANDARD_INCREMENT_USECOUNT;
162
163         wordlen = strlen(word);
164
165         if (ast_strlen_zero(word)) {
166                 /* show the options in alphabetical order */
167                 if (!state)
168                         ret = strdup(app);
169                 else
170                         ret = strdup(exten);
171         } else if (!strncasecmp(word, app, wordlen)) {
172                 ret = strdup(app);
173         } else if (!strncasecmp(word, exten, wordlen)) {
174                 ret = strdup(exten);
175         }
176
177         STANDARD_DECREMENT_USECOUNT;
178
179         return ret;
180 }
181
182 int unload_module(void)
183 {
184         return ast_cli_unregister(&cli_orig);
185 }
186
187 int load_module(void)
188 {
189         return ast_cli_register(&cli_orig);
190 }
191
192 char *description(void)
193 {
194         return tdesc;
195 }
196
197 int usecount(void)
198 {
199         return 0;
200 }
201
202 char *key()
203 {
204         return ASTERISK_GPL_KEY;
205 }
206