-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_originate.c
338 lines (294 loc) · 11.2 KB
/
app_originate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2008, Roberto Casas.
* Copyright (C) 2008, Digium, Inc.
*
* Roberto Casas <roberto.casas@diaple.com>
* Russell Bryant <russell@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief Originate application
*
* \author Roberto Casas <roberto.casas@diaple.com>
* \author Russell Bryant <russell@digium.com>
*
* \ingroup applications
*
* \todo Make a way to be able to set variables (and functions) on the outbound
* channel, similar to the Variable headers for the AMI Originate, and the
* Set options for call files.
*/
/* Modified by Matthew M. Gamble <mgamble@primustel.ca> to allow passing of CLID since we don't want the calls to be from anonymous */
/* Based on a previous modification for Asterisk 1.8 (https://github.com/cmendes0101/asterisk-originate-callerid/blob/master/app_originate.c) */
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/format_cache.h"
static const char app_originate[] = "Originate";
/*** DOCUMENTATION
<application name="Originate" language="en_US">
<synopsis>
Originate a call.
</synopsis>
<syntax>
<parameter name="tech_data" required="true">
<para>Channel technology and data for creating the outbound channel.
For example, SIP/1234.</para>
</parameter>
<parameter name="type" required="true">
<para>This should be <literal>app</literal> or <literal>exten</literal>, depending on whether the outbound channel should be connected to an application or extension.</para>
</parameter>
<parameter name="arg1" required="true">
<para>If the type is <literal>app</literal>, then this is the application name. If the type is <literal>exten</literal>, then this is the context that the channel will be sent to.</para>
</parameter>
<parameter name="arg2" required="false">
<para>If the type is <literal>app</literal>, then this is the data passed as arguments to the application. If the type is <literal>exten</literal>, then this is the extension that the channel will be sent to.</para>
</parameter>
<parameter name="arg3" required="false">
<para>If the type is <literal>exten</literal>, then this is the priority that the channel is sent to. If the type is <literal>app</literal>, then this parameter is ignored.</para>
</parameter>
<parameter name="timeout" required="false">
<para>Timeout in seconds. Default is 30 seconds.</para>
</parameter>
<parameter name="cid_num" required="false">
<para>This is the <literal>CallerID Number</literal> to use for the call</para>
</parameter>
<parameter name="cid_name" required="false">
<para>This is the <literal>CallerID Name</literal> to use for the call</para>
</parameter>
<parameter name="vars" required="false">
<para>If you need to pass any <literal>variables</literal>, this is the place to do it.</para>
</parameter>
</syntax>
<description>
<para>This application originates an outbound call and connects it to a specified extension or application. This application will block until the outgoing call fails or gets answered. At that point, this application will exit with the status variable set and dialplan processing will continue.</para>
<para>This application sets the following channel variable before exiting:</para>
<variablelist>
<variable name="ORIGINATE_STATUS">
<para>This indicates the result of the call origination.</para>
<value name="FAILED"/>
<value name="SUCCESS"/>
<value name="BUSY"/>
<value name="CONGESTION"/>
<value name="HANGUP"/>
<value name="RINGING"/>
<value name="UNKNOWN">
In practice, you should never see this value. Please report it to the issue tracker if you ever see it.
</value>
</variable>
</variablelist>
</description>
</application>
***/
/*!
* \internal
* \brief Process one "Variable:" header value string.
*
* \param head Current list of AMI variables to get new values added.
* \param hdr_val Header value string to process.
*
* \return New variable list head.
*/
static struct ast_variable *man_do_variable_value(struct ast_variable *head, const char *hdr_val)
{
char *parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(vars)[64];
);
hdr_val = ast_skip_blanks(hdr_val); /* ignore leading spaces in the value */
parse = ast_strdupa(hdr_val);
/* Break the header value string into name=val pair items. */
AST_STANDARD_APP_ARGS(args, parse);
if (args.argc) {
int y;
/* Process each name=val pair item. */
for (y = 0; y < args.argc; y++) {
struct ast_variable *cur;
char *var;
char *val;
if (!args.vars[y]) {
continue;
}
var = val = args.vars[y];
strsep(&val, "=");
ast_log(LOG_DEBUG, "Processing variable var='%s', value='%s'\n", var, val);
/* XXX We may wish to trim whitespace from the strings. */
if (!val || ast_strlen_zero(var)) {
continue;
}
/* Create new variable list node and prepend it to the list. */
cur = ast_variable_new(var, val, "");
if (cur) {
cur->next = head;
head = cur;
}
}
}
return head;
}
static int originate_exec(struct ast_channel *chan, const char *data)
{
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(tech_data);
AST_APP_ARG(type);
AST_APP_ARG(arg1);
AST_APP_ARG(arg2);
AST_APP_ARG(arg3);
AST_APP_ARG(timeout);
AST_APP_ARG(cid_num);
AST_APP_ARG(cid_name);
AST_APP_ARG(vars);
);
char *parse;
char *chantech, *chandata;
char *cid_num, *cid_name;
struct ast_variable *vars = NULL;
int res = -1;
int outgoing_status = 0;
unsigned int timeout = 30;
static const char default_exten[] = "s";
struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
ast_autoservice_start(chan);
if (!cap_slin) {
goto return_cleanup;
}
ast_format_cap_append(cap_slin, ast_format_slin, 0);
ast_format_cap_append(cap_slin, ast_format_slin12, 0);
ast_format_cap_append(cap_slin, ast_format_slin16, 0);
ast_format_cap_append(cap_slin, ast_format_slin24, 0);
ast_format_cap_append(cap_slin, ast_format_slin32, 0);
ast_format_cap_append(cap_slin, ast_format_slin44, 0);
ast_format_cap_append(cap_slin, ast_format_slin48, 0);
ast_format_cap_append(cap_slin, ast_format_slin96, 0);
ast_format_cap_append(cap_slin, ast_format_slin192, 0);
if (ast_strlen_zero(data)) {
ast_log(LOG_ERROR, "Originate() requires arguments\n");
goto return_cleanup;
}
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (args.argc < 3) {
ast_log(LOG_ERROR, "Incorrect number of arguments\n");
goto return_cleanup;
}
if (!ast_strlen_zero(args.timeout)) {
if(sscanf(args.timeout, "%u", &timeout) != 1) {
ast_log(LOG_NOTICE, "Invalid timeout: '%s'. Setting timeout to 30 seconds\n", args.timeout);
timeout = 30;
}
}
if (!ast_strlen_zero(args.vars)) {
ast_log(LOG_NOTICE, "Originate - passed var string: '%s'\n", args.vars);
/* User passed variables - parse them so we can include them*/
args.vars = ast_strip(ast_strip_quoted(args.vars, "\"", "\""));
vars = man_do_variable_value(vars, args.vars);
}
/* Check the CLID name / num */
if (ast_strlen_zero(args.cid_num)) {
ast_log(LOG_NOTICE, "No Caller ID Number passed - setting to default value\n");
cid_num = "";
} else {
cid_num = ast_strdupa(args.cid_num);
}
if (ast_strlen_zero(args.cid_name)) {
ast_log(LOG_NOTICE, "No Caller ID Name passed - setting to default value\n");
cid_name = "";
} else {
cid_name = ast_strdupa(args.cid_name);
}
chandata = ast_strdupa(args.tech_data);
chantech = strsep(&chandata, "/");
if (ast_strlen_zero(chandata) || ast_strlen_zero(chantech)) {
ast_log(LOG_ERROR, "Channel Tech/Data invalid: '%s'\n", args.tech_data);
goto return_cleanup;
}
if (!strcasecmp(args.type, "exten")) {
int priority = 1; /* Initialized in case priority not specified */
const char *exten = args.arg2;
if (args.argc >= 5) {
/* Context/Exten/Priority all specified */
if (sscanf(args.arg3, "%30d", &priority) != 1) {
ast_log(LOG_ERROR, "Invalid priority: '%s'\n", args.arg3);
goto return_cleanup;
}
} else if (args.argc == 3) {
/* Exten not specified */
exten = default_exten;
}
ast_debug(1, "Originating call to '%s/%s' and connecting them to extension %s,%s,%d\n",
chantech, chandata, args.arg1, exten, priority);
ast_pbx_outgoing_exten(chantech, cap_slin, chandata,
timeout * 1000, args.arg1, exten, priority, &outgoing_status, 0,cid_num,
cid_name, vars, NULL, NULL, 0, NULL);
} else if (!strcasecmp(args.type, "app")) {
ast_debug(1, "Originating call to '%s/%s' and connecting them to %s(%s)\n",
chantech, chandata, args.arg1, S_OR(args.arg2, ""));
ast_pbx_outgoing_app(chantech, cap_slin, chandata,
timeout * 1000, args.arg1, args.arg2, &outgoing_status, 0, cid_num ,
cid_name, vars, NULL, NULL,NULL);
} else {
ast_log(LOG_ERROR, "Incorrect type, it should be 'exten' or 'app': %s\n",
args.type);
goto return_cleanup;
}
res = 0;
return_cleanup:
if (res) {
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "FAILED");
} else {
switch (outgoing_status) {
case 0:
case AST_CONTROL_ANSWER:
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "SUCCESS");
break;
case AST_CONTROL_BUSY:
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "BUSY");
break;
case AST_CONTROL_CONGESTION:
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "CONGESTION");
break;
case AST_CONTROL_HANGUP:
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "HANGUP");
break;
case AST_CONTROL_RINGING:
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "RINGING");
break;
default:
ast_log(LOG_WARNING, "Unknown originate status result of '%d'\n",
outgoing_status);
pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "UNKNOWN");
break;
}
}
ast_autoservice_stop(chan);
return res;
}
static int unload_module(void)
{
return ast_unregister_application(app_originate);
}
static int load_module(void)
{
int res;
res = ast_register_application_xml(app_originate, originate_exec);
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Originate call");