ari/resource_channels: Add channel variables earlier in the creation process
[asterisk/asterisk.git] / res / ari / resource_channels.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@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  *
21  * \brief Implementation for ARI stubs.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend type="module">res_stasis_app_playback</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/file.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/callerid.h"
39 #include "asterisk/stasis_app.h"
40 #include "asterisk/stasis_app_playback.h"
41 #include "asterisk/stasis_app_recording.h"
42 #include "asterisk/stasis_app_snoop.h"
43 #include "asterisk/stasis_channels.h"
44 #include "asterisk/causes.h"
45 #include "resource_channels.h"
46
47 #include <limits.h>
48
49 /*!
50  * \brief Finds the control object for a channel, filling the response with an
51  * error, if appropriate.
52  * \param[out] response Response to fill with an error if control is not found.
53  * \param channel_id ID of the channel to lookup.
54  * \return Channel control object.
55  * \return \c NULL if control object does not exist.
56  */
57 static struct stasis_app_control *find_control(
58         struct ast_ari_response *response,
59         const char *channel_id)
60 {
61         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
62
63         ast_assert(response != NULL);
64
65         control = stasis_app_control_find_by_channel_id(channel_id);
66         if (control == NULL) {
67                 /* Distinguish between 404 and 409 errors */
68                 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
69                 chan = ast_channel_get_by_name(channel_id);
70                 if (chan == NULL) {
71                         ast_ari_response_error(response, 404, "Not Found",
72                                    "Channel not found");
73                         return NULL;
74                 }
75
76                 ast_ari_response_error(response, 409, "Conflict",
77                            "Channel not in Stasis application");
78                 return NULL;
79         }
80
81         ao2_ref(control, +1);
82         return control;
83 }
84
85 void ast_ari_channels_continue_in_dialplan(
86         struct ast_variable *headers,
87         struct ast_ari_channels_continue_in_dialplan_args *args,
88         struct ast_ari_response *response)
89 {
90         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
91
92         ast_assert(response != NULL);
93
94         control = find_control(response, args->channel_id);
95         if (control == NULL) {
96                 return;
97         }
98
99         if (stasis_app_control_continue(control, args->context, args->extension, args->priority)) {
100                 ast_ari_response_alloc_failed(response);
101                 return;
102         }
103
104         ast_ari_response_no_content(response);
105 }
106
107 void ast_ari_channels_answer(struct ast_variable *headers,
108         struct ast_ari_channels_answer_args *args,
109         struct ast_ari_response *response)
110 {
111         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
112
113         control = find_control(response, args->channel_id);
114         if (control == NULL) {
115                 return;
116         }
117
118         if (stasis_app_control_answer(control) != 0) {
119                 ast_ari_response_error(
120                         response, 500, "Internal Server Error",
121                         "Failed to answer channel");
122                 return;
123         }
124
125         ast_ari_response_no_content(response);
126 }
127
128 void ast_ari_channels_ring(struct ast_variable *headers,
129         struct ast_ari_channels_ring_args *args,
130         struct ast_ari_response *response)
131 {
132         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
133
134         control = find_control(response, args->channel_id);
135         if (control == NULL) {
136                 return;
137         }
138
139         stasis_app_control_ring(control);
140
141         ast_ari_response_no_content(response);
142 }
143
144 void ast_ari_channels_ring_stop(struct ast_variable *headers,
145         struct ast_ari_channels_ring_stop_args *args,
146         struct ast_ari_response *response)
147 {
148         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
149
150         control = find_control(response, args->channel_id);
151         if (control == NULL) {
152                 return;
153         }
154
155         stasis_app_control_ring_stop(control);
156
157         ast_ari_response_no_content(response);
158 }
159
160 void ast_ari_channels_mute(struct ast_variable *headers,
161         struct ast_ari_channels_mute_args *args,
162         struct ast_ari_response *response)
163 {
164         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
165         unsigned int direction = 0;
166         enum ast_frame_type frametype = AST_FRAME_VOICE;
167
168         control = find_control(response, args->channel_id);
169         if (control == NULL) {
170                 return;
171         }
172
173         if (ast_strlen_zero(args->direction)) {
174                 ast_ari_response_error(
175                         response, 400, "Bad Request",
176                         "Direction is required");
177                 return;
178         }
179
180         if (!strcmp(args->direction, "in")) {
181                 direction = AST_MUTE_DIRECTION_READ;
182         } else if (!strcmp(args->direction, "out")) {
183                 direction = AST_MUTE_DIRECTION_WRITE;
184         } else if (!strcmp(args->direction, "both")) {
185                 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
186         } else {
187                 ast_ari_response_error(
188                         response, 400, "Bad Request",
189                         "Invalid direction specified");
190                 return;
191         }
192
193         stasis_app_control_mute(control, direction, frametype);
194
195         ast_ari_response_no_content(response);
196 }
197
198 void ast_ari_channels_unmute(struct ast_variable *headers,
199         struct ast_ari_channels_unmute_args *args,
200         struct ast_ari_response *response)
201 {
202         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
203         unsigned int direction = 0;
204         enum ast_frame_type frametype = AST_FRAME_VOICE;
205
206         control = find_control(response, args->channel_id);
207         if (control == NULL) {
208                 return;
209         }
210
211         if (ast_strlen_zero(args->direction)) {
212                 ast_ari_response_error(
213                         response, 400, "Bad Request",
214                         "Direction is required");
215                 return;
216         }
217
218         if (!strcmp(args->direction, "in")) {
219                 direction = AST_MUTE_DIRECTION_READ;
220         } else if (!strcmp(args->direction, "out")) {
221                 direction = AST_MUTE_DIRECTION_WRITE;
222         } else if (!strcmp(args->direction, "both")) {
223                 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
224         } else {
225                 ast_ari_response_error(
226                         response, 400, "Bad Request",
227                         "Invalid direction specified");
228                 return;
229         }
230
231         stasis_app_control_unmute(control, direction, frametype);
232
233         ast_ari_response_no_content(response);
234 }
235
236 void ast_ari_channels_send_dtmf(struct ast_variable *headers,
237         struct ast_ari_channels_send_dtmf_args *args,
238         struct ast_ari_response *response)
239 {
240         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
241
242         control = find_control(response, args->channel_id);
243         if (control == NULL) {
244                 return;
245         }
246
247         if (ast_strlen_zero(args->dtmf)) {
248                 ast_ari_response_error(
249                         response, 400, "Bad Request",
250                         "DTMF is required");
251                 return;
252         }
253
254         stasis_app_control_dtmf(control, args->dtmf, args->before, args->between, args->duration, args->after);
255
256         ast_ari_response_no_content(response);
257 }
258
259 void ast_ari_channels_hold(struct ast_variable *headers,
260         struct ast_ari_channels_hold_args *args,
261         struct ast_ari_response *response)
262 {
263         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
264
265         control = find_control(response, args->channel_id);
266         if (control == NULL) {
267                 /* Response filled in by find_control */
268                 return;
269         }
270
271         stasis_app_control_hold(control);
272
273         ast_ari_response_no_content(response);
274 }
275
276 void ast_ari_channels_unhold(struct ast_variable *headers,
277         struct ast_ari_channels_unhold_args *args,
278         struct ast_ari_response *response)
279 {
280         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
281
282         control = find_control(response, args->channel_id);
283         if (control == NULL) {
284                 /* Response filled in by find_control */
285                 return;
286         }
287
288         stasis_app_control_unhold(control);
289
290         ast_ari_response_no_content(response);
291 }
292
293 void ast_ari_channels_start_moh(struct ast_variable *headers,
294         struct ast_ari_channels_start_moh_args *args,
295         struct ast_ari_response *response)
296 {
297         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
298
299         control = find_control(response, args->channel_id);
300         if (control == NULL) {
301                 /* Response filled in by find_control */
302                 return;
303         }
304
305         stasis_app_control_moh_start(control, args->moh_class);
306         ast_ari_response_no_content(response);
307 }
308
309 void ast_ari_channels_stop_moh(struct ast_variable *headers,
310         struct ast_ari_channels_stop_moh_args *args,
311         struct ast_ari_response *response)
312 {
313         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
314
315         control = find_control(response, args->channel_id);
316         if (control == NULL) {
317                 /* Response filled in by find_control */
318                 return;
319         }
320
321         stasis_app_control_moh_stop(control);
322         ast_ari_response_no_content(response);
323 }
324
325 void ast_ari_channels_start_silence(struct ast_variable *headers,
326         struct ast_ari_channels_start_silence_args *args,
327         struct ast_ari_response *response)
328 {
329         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
330
331         control = find_control(response, args->channel_id);
332         if (control == NULL) {
333                 /* Response filled in by find_control */
334                 return;
335         }
336
337         stasis_app_control_silence_start(control);
338         ast_ari_response_no_content(response);
339 }
340
341 void ast_ari_channels_stop_silence(struct ast_variable *headers,
342         struct ast_ari_channels_stop_silence_args *args,
343         struct ast_ari_response *response)
344 {
345         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
346
347         control = find_control(response, args->channel_id);
348         if (control == NULL) {
349                 /* Response filled in by find_control */
350                 return;
351         }
352
353         stasis_app_control_silence_stop(control);
354         ast_ari_response_no_content(response);
355 }
356
357 void ast_ari_channels_play(struct ast_variable *headers,
358         struct ast_ari_channels_play_args *args,
359         struct ast_ari_response *response)
360 {
361         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
362         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
363         RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
364         RAII_VAR(char *, playback_url, NULL, ast_free);
365         struct ast_json *json;
366         const char *language;
367
368         ast_assert(response != NULL);
369
370         control = find_control(response, args->channel_id);
371         if (control == NULL) {
372                 /* Response filled in by find_control */
373                 return;
374         }
375
376         snapshot = stasis_app_control_get_snapshot(control);
377         if (!snapshot) {
378                 ast_ari_response_error(
379                         response, 404, "Not Found",
380                         "Channel not found");
381                 return;
382         }
383
384         if (args->skipms < 0) {
385                 ast_ari_response_error(
386                         response, 400, "Bad Request",
387                         "skipms cannot be negative");
388                 return;
389         }
390
391         if (args->offsetms < 0) {
392                 ast_ari_response_error(
393                         response, 400, "Bad Request",
394                         "offsetms cannot be negative");
395                 return;
396         }
397
398         language = S_OR(args->lang, snapshot->language);
399
400         playback = stasis_app_control_play_uri(control, args->media, language,
401                 args->channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args->skipms, args->offsetms);
402         if (!playback) {
403                 ast_ari_response_error(
404                         response, 500, "Internal Server Error",
405                         "Failed to queue media for playback");
406                 return;
407         }
408
409         ast_asprintf(&playback_url, "/playback/%s",
410                 stasis_app_playback_get_id(playback));
411         if (!playback_url) {
412                 ast_ari_response_error(
413                         response, 500, "Internal Server Error",
414                         "Out of memory");
415                 return;
416         }
417
418         json = stasis_app_playback_to_json(playback);
419         if (!json) {
420                 ast_ari_response_error(
421                         response, 500, "Internal Server Error",
422                         "Out of memory");
423                 return;
424         }
425
426         ast_ari_response_created(response, playback_url, json);
427 }
428
429 void ast_ari_channels_record(struct ast_variable *headers,
430         struct ast_ari_channels_record_args *args,
431         struct ast_ari_response *response)
432 {
433         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
434         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
435         RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
436         RAII_VAR(char *, recording_url, NULL, ast_free);
437         struct ast_json *json;
438         RAII_VAR(struct stasis_app_recording_options *, options, NULL,
439                 ao2_cleanup);
440         RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
441         size_t uri_name_maxlen;
442
443         ast_assert(response != NULL);
444
445         if (args->max_duration_seconds < 0) {
446                 ast_ari_response_error(
447                         response, 400, "Bad Request",
448                         "max_duration_seconds cannot be negative");
449                 return;
450         }
451
452         if (args->max_silence_seconds < 0) {
453                 ast_ari_response_error(
454                         response, 400, "Bad Request",
455                         "max_silence_seconds cannot be negative");
456                 return;
457         }
458
459         control = find_control(response, args->channel_id);
460         if (control == NULL) {
461                 /* Response filled in by find_control */
462                 return;
463         }
464
465         options = stasis_app_recording_options_create(args->name, args->format);
466         if (options == NULL) {
467                 ast_ari_response_error(
468                         response, 500, "Internal Server Error",
469                         "Out of memory");
470         }
471         options->max_silence_seconds = args->max_silence_seconds;
472         options->max_duration_seconds = args->max_duration_seconds;
473         options->terminate_on =
474                 stasis_app_recording_termination_parse(args->terminate_on);
475         options->if_exists =
476                 stasis_app_recording_if_exists_parse(args->if_exists);
477         options->beep = args->beep;
478
479         if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
480                 ast_ari_response_error(
481                         response, 400, "Bad Request",
482                         "terminateOn invalid");
483                 return;
484         }
485
486         if (options->if_exists == -1) {
487                 ast_ari_response_error(
488                         response, 400, "Bad Request",
489                         "ifExists invalid");
490                 return;
491         }
492
493         if (!ast_get_format_for_file_ext(options->format)) {
494                 ast_ari_response_error(
495                         response, 422, "Unprocessable Entity",
496                         "specified format is unknown on this system");
497                 return;
498         }
499
500         recording = stasis_app_control_record(control, options);
501         if (recording == NULL) {
502                 switch(errno) {
503                 case EINVAL:
504                         /* While the arguments are invalid, we should have
505                          * caught them prior to calling record.
506                          */
507                         ast_ari_response_error(
508                                 response, 500, "Internal Server Error",
509                                 "Error parsing request");
510                         break;
511                 case EEXIST:
512                         ast_ari_response_error(response, 409, "Conflict",
513                                 "Recording '%s' already exists and can not be overwritten",
514                                 args->name);
515                         break;
516                 case ENOMEM:
517                         ast_ari_response_error(
518                                 response, 500, "Internal Server Error",
519                                 "Out of memory");
520                         break;
521                 case EPERM:
522                         ast_ari_response_error(
523                                 response, 400, "Bad Request",
524                                 "Recording name invalid");
525                         break;
526                 default:
527                         ast_log(LOG_WARNING,
528                                 "Unrecognized recording error: %s\n",
529                                 strerror(errno));
530                         ast_ari_response_error(
531                                 response, 500, "Internal Server Error",
532                                 "Internal Server Error");
533                         break;
534                 }
535                 return;
536         }
537
538         uri_name_maxlen = strlen(args->name) * 3;
539         uri_encoded_name = ast_malloc(uri_name_maxlen);
540         if (!uri_encoded_name) {
541                 ast_ari_response_error(
542                         response, 500, "Internal Server Error",
543                         "Out of memory");
544                 return;
545         }
546         ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
547                 ast_uri_http);
548
549         ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
550         if (!recording_url) {
551                 ast_ari_response_error(
552                         response, 500, "Internal Server Error",
553                         "Out of memory");
554                 return;
555         }
556
557         json = stasis_app_recording_to_json(recording);
558         if (!json) {
559                 ast_ari_response_error(
560                         response, 500, "Internal Server Error",
561                         "Out of memory");
562                 return;
563         }
564
565         ast_ari_response_created(response, recording_url, json);
566 }
567
568 void ast_ari_channels_get(struct ast_variable *headers,
569         struct ast_ari_channels_get_args *args,
570         struct ast_ari_response *response)
571 {
572         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
573         struct stasis_cache *cache;
574         struct ast_channel_snapshot *snapshot;
575
576         cache = ast_channel_cache();
577         if (!cache) {
578                 ast_ari_response_error(
579                         response, 500, "Internal Server Error",
580                         "Message bus not initialized");
581                 return;
582         }
583
584         msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
585                                args->channel_id);
586         if (!msg) {
587                 ast_ari_response_error(
588                         response, 404, "Not Found",
589                         "Channel not found");
590                 return;
591         }
592
593         snapshot = stasis_message_data(msg);
594         ast_assert(snapshot != NULL);
595
596         ast_ari_response_ok(response,
597                                 ast_channel_snapshot_to_json(snapshot, NULL));
598 }
599
600 void ast_ari_channels_hangup(struct ast_variable *headers,
601         struct ast_ari_channels_hangup_args *args,
602         struct ast_ari_response *response)
603 {
604         RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
605         int cause;
606
607         chan = ast_channel_get_by_name(args->channel_id);
608         if (chan == NULL) {
609                 ast_ari_response_error(
610                         response, 404, "Not Found",
611                         "Channel not found");
612                 return;
613         }
614
615         if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
616                 cause = AST_CAUSE_NORMAL;
617         } else if (!strcmp(args->reason, "busy")) {
618                 cause = AST_CAUSE_BUSY;
619         } else if (!strcmp(args->reason, "congestion")) {
620                 cause = AST_CAUSE_CONGESTION;
621         } else {
622                 ast_ari_response_error(
623                         response, 400, "Invalid Reason",
624                         "Invalid reason for hangup provided");
625                 return;
626         }
627
628         ast_channel_hangupcause_set(chan, cause);
629         ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
630
631         ast_ari_response_no_content(response);
632 }
633
634 void ast_ari_channels_list(struct ast_variable *headers,
635         struct ast_ari_channels_list_args *args,
636         struct ast_ari_response *response)
637 {
638         RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
639         RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
640         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
641         struct ao2_iterator i;
642         void *obj;
643         struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
644
645         cache = ast_channel_cache();
646         if (!cache) {
647                 ast_ari_response_error(
648                         response, 500, "Internal Server Error",
649                         "Message bus not initialized");
650                 return;
651         }
652         ao2_ref(cache, +1);
653
654         snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
655         if (!snapshots) {
656                 ast_ari_response_alloc_failed(response);
657                 return;
658         }
659
660         json = ast_json_array_create();
661         if (!json) {
662                 ast_ari_response_alloc_failed(response);
663                 return;
664         }
665
666         i = ao2_iterator_init(snapshots, 0);
667         while ((obj = ao2_iterator_next(&i))) {
668                 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
669                 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
670                 int r;
671
672                 if (sanitize && sanitize->channel_snapshot
673                         && sanitize->channel_snapshot(snapshot)) {
674                         continue;
675                 }
676
677                 r = ast_json_array_append(
678                         json, ast_channel_snapshot_to_json(snapshot, NULL));
679                 if (r != 0) {
680                         ast_ari_response_alloc_failed(response);
681                         ao2_iterator_destroy(&i);
682                         return;
683                 }
684         }
685         ao2_iterator_destroy(&i);
686
687         ast_ari_response_ok(response, ast_json_ref(json));
688 }
689
690 static int json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
691 {
692         struct ast_variable *current = NULL;
693         struct ast_json_iter *it_json_var;
694
695         for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
696                  it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
697                 struct ast_variable *new_var;
698
699                 new_var = ast_variable_new(ast_json_object_iter_key(it_json_var),
700                                            ast_json_string_get(ast_json_object_iter_value(it_json_var)),
701                                            "");
702                 if (!new_var) {
703                         ast_variables_destroy(*variables);
704                         *variables = NULL;
705                         return 1;
706                 }
707
708                 if (!current) {
709                         *variables = new_var;
710                         current = *variables;
711                 } else {
712                         current->next = new_var;
713                         current = new_var;
714                 }
715         }
716
717         return 0;
718 }
719
720 void ast_ari_channels_originate(struct ast_variable *headers,
721         struct ast_ari_channels_originate_args *args,
722         struct ast_ari_response *response)
723 {
724         char *dialtech;
725         char dialdevice[AST_CHANNEL_NAME];
726         char *caller_id = NULL;
727         char *cid_num = NULL;
728         char *cid_name = NULL;
729         int timeout = 30000;
730         RAII_VAR(struct ast_format_cap *, cap,
731                 ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
732         struct ast_format tmp_fmt;
733         RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
734         char *stuff;
735         struct ast_channel *chan;
736         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
737
738         if (!cap) {
739                 ast_ari_response_alloc_failed(response);
740                 return;
741         }
742         ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
743
744         /* Parse any query parameters out of the body parameter */
745         if (args->variables) {
746                 struct ast_json *json_variables;
747
748                 ast_ari_channels_originate_parse_body(args->variables, args);
749                 json_variables = ast_json_object_get(args->variables, "variables");
750                 if (json_variables) {
751                         if (json_to_ast_variables(json_variables, &variables)) {
752                                 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
753                                 ast_ari_response_alloc_failed(response);
754                                 return;
755                         }
756                 }
757         }
758
759         if (ast_strlen_zero(args->endpoint)) {
760                 ast_ari_response_error(response, 400, "Bad Request",
761                         "Endpoint must be specified");
762                 return;
763         }
764
765         dialtech = ast_strdupa(args->endpoint);
766         if ((stuff = strchr(dialtech, '/'))) {
767                 *stuff++ = '\0';
768                 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
769         }
770
771         if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
772                 ast_ari_response_error(response, 400, "Bad Request",
773                         "Invalid endpoint specified");
774                 return;
775         }
776
777         if (args->timeout > 0) {
778                 timeout = args->timeout * 1000;
779         } else if (args->timeout == -1) {
780                 timeout = -1;
781         }
782
783         if (!ast_strlen_zero(args->caller_id)) {
784                 caller_id = ast_strdupa(args->caller_id);
785                 ast_callerid_parse(caller_id, &cid_name, &cid_num);
786
787                 if (ast_is_shrinkable_phonenumber(cid_num)) {
788                         ast_shrink_phone_number(cid_num);
789                 }
790         }
791
792         if (!ast_strlen_zero(args->app)) {
793                 const char *app = "Stasis";
794
795                 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
796
797                 if (!appdata) {
798                         ast_ari_response_alloc_failed(response);
799                         return;
800                 }
801
802                 ast_str_set(&appdata, 0, "%s", args->app);
803                 if (!ast_strlen_zero(args->app_args)) {
804                         ast_str_append(&appdata, 0, ",%s", args->app_args);
805                 }
806
807                 /* originate a channel, putting it into an application */
808                 if (ast_pbx_outgoing_app(dialtech, cap, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, variables, NULL, &chan)) {
809                         ast_ari_response_alloc_failed(response);
810                         return;
811                 }
812         } else if (!ast_strlen_zero(args->extension)) {
813                 /* originate a channel, sending it to an extension */
814                 if (ast_pbx_outgoing_exten(dialtech, cap, dialdevice, timeout, S_OR(args->context, "default"), args->extension, args->priority ? args->priority : 1, NULL, 0, cid_num, cid_name, variables, NULL, &chan, 0)) {
815                         ast_ari_response_alloc_failed(response);
816                         return;
817                 }
818         } else {
819                 ast_ari_response_error(response, 400, "Bad Request",
820                         "Application or extension must be specified");
821                 return;
822         }
823
824         snapshot = ast_channel_snapshot_create(chan);
825         ast_channel_unlock(chan);
826
827         if (!ast_strlen_zero(args->app)) {
828                 /* channel: + channel ID + null terminator */
829                 char uri[9 + strlen(ast_channel_uniqueid(chan))];
830                 const char *uris[1] = { uri, };
831
832                 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
833                 stasis_app_subscribe(args->app, uris, 1, NULL);
834         }
835
836         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
837         ast_channel_unref(chan);
838 }
839
840 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
841         struct ast_ari_channels_get_channel_var_args *args,
842         struct ast_ari_response *response)
843 {
844         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
845         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
846         RAII_VAR(char *, value, NULL, ast_free);
847
848         ast_assert(response != NULL);
849
850         if (ast_strlen_zero(args->variable)) {
851                 ast_ari_response_error(
852                         response, 400, "Bad Request",
853                         "Variable name is required");
854                 return;
855         }
856
857         control = find_control(response, args->channel_id);
858         if (control == NULL) {
859                 /* response filled in by find_control */
860                 return;
861         }
862
863         value = stasis_app_control_get_channel_var(control, args->variable);
864
865         if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
866                 ast_ari_response_alloc_failed(response);
867                 return;
868         }
869
870         ast_ari_response_ok(response, ast_json_ref(json));
871 }
872
873 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
874         struct ast_ari_channels_set_channel_var_args *args,
875         struct ast_ari_response *response)
876 {
877         RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
878
879         ast_assert(response != NULL);
880
881         if (ast_strlen_zero(args->variable)) {
882                 ast_ari_response_error(
883                         response, 400, "Bad Request",
884                         "Variable name is required");
885                 return;
886         }
887
888         control = find_control(response, args->channel_id);
889         if (control == NULL) {
890                 /* response filled in by find_control */
891                 return;
892         }
893
894         if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
895                 ast_ari_response_error(
896                         response, 400, "Bad Request",
897                         "Failed to execute function");
898                 return;
899         }
900
901         ast_ari_response_no_content(response);
902 }
903
904 void ast_ari_channels_snoop_channel(struct ast_variable *headers, struct ast_ari_channels_snoop_channel_args *args, struct ast_ari_response *response)
905 {
906         enum stasis_app_snoop_direction spy, whisper;
907         RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
908         RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
909         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
910
911         ast_assert(response != NULL);
912
913         if (ast_strlen_zero(args->spy) || !strcmp(args->spy, "none")) {
914                 spy = STASIS_SNOOP_DIRECTION_NONE;
915         } else if (!strcmp(args->spy, "both")) {
916                 spy = STASIS_SNOOP_DIRECTION_BOTH;
917         } else if (!strcmp(args->spy, "out")) {
918                 spy = STASIS_SNOOP_DIRECTION_OUT;
919         } else if (!strcmp(args->spy, "in")) {
920                 spy = STASIS_SNOOP_DIRECTION_IN;
921         } else {
922                 ast_ari_response_error(
923                         response, 400, "Bad Request",
924                         "Invalid direction specified for spy");
925                 return;
926         }
927
928         if (ast_strlen_zero(args->whisper) || !strcmp(args->whisper, "none")) {
929                 whisper = STASIS_SNOOP_DIRECTION_NONE;
930         } else if (!strcmp(args->whisper, "both")) {
931                 whisper = STASIS_SNOOP_DIRECTION_BOTH;
932         } else if (!strcmp(args->whisper, "out")) {
933                 whisper = STASIS_SNOOP_DIRECTION_OUT;
934         } else if (!strcmp(args->whisper, "in")) {
935                 whisper = STASIS_SNOOP_DIRECTION_IN;
936         } else {
937                 ast_ari_response_error(
938                         response, 400, "Bad Request",
939                         "Invalid direction specified for whisper");
940                 return;
941         }
942
943         if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
944                 ast_ari_response_error(
945                         response, 400, "Bad Request",
946                         "Direction must be specified for at least spy or whisper");
947                 return;
948         } else if (ast_strlen_zero(args->app)) {
949                 ast_ari_response_error(
950                         response, 400, "Bad Request",
951                         "Application name is required");
952                 return;
953         }
954
955         chan = ast_channel_get_by_name(args->channel_id);
956         if (chan == NULL) {
957                 ast_ari_response_error(
958                         response, 404, "Channel Not Found",
959                         "Provided channel was not found");
960                 return;
961         }
962
963         snoop = stasis_app_control_snoop(chan, spy, whisper, args->app, args->app_args);
964         if (snoop == NULL) {
965                 ast_ari_response_error(
966                         response, 500, "Internal error",
967                         "Snoop channel could not be created");
968                 return;
969         }
970
971         snapshot = ast_channel_snapshot_create(snoop);
972         ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
973 }